Unnamed repository; edit this file 'description' to name the repository.
Merge pull request #19970 from ChayimFriedman2/proc-macro-srv-minus
fix: Fix proc macro server handling of strings with minuses
Lukas Wirth 11 months ago
parent 9c3476d · parent 8ca5ad6 · commit 5919f6d
-rw-r--r--crates/proc-macro-srv/proc-macro-test/imp/src/lib.rs1
-rw-r--r--crates/proc-macro-srv/src/server_impl.rs54
-rw-r--r--crates/proc-macro-srv/src/tests/mod.rs2
3 files changed, 26 insertions, 31 deletions
diff --git a/crates/proc-macro-srv/proc-macro-test/imp/src/lib.rs b/crates/proc-macro-srv/proc-macro-test/imp/src/lib.rs
index 6820e4b335..2a72e50f91 100644
--- a/crates/proc-macro-srv/proc-macro-test/imp/src/lib.rs
+++ b/crates/proc-macro-srv/proc-macro-test/imp/src/lib.rs
@@ -31,6 +31,7 @@ pub fn fn_like_mk_literals(_args: TokenStream) -> TokenStream {
TokenTree::from(Literal::byte_string(b"byte_string")),
TokenTree::from(Literal::character('c')),
TokenTree::from(Literal::string("string")),
+ TokenTree::from(Literal::string("-string")),
TokenTree::from(Literal::c_string(c"cstring")),
// as of 2022-07-21, there's no method on `Literal` to build a raw
// string or a raw byte string
diff --git a/crates/proc-macro-srv/src/server_impl.rs b/crates/proc-macro-srv/src/server_impl.rs
index ad28599033..dd576f23ae 100644
--- a/crates/proc-macro-srv/src/server_impl.rs
+++ b/crates/proc-macro-srv/src/server_impl.rs
@@ -199,37 +199,29 @@ pub(super) fn from_token_tree<Span: Copy>(
}
bridge::TokenTree::Literal(literal) => {
- let token_trees =
- if let Some((_minus, symbol)) = literal.symbol.as_str().split_once('-') {
- let punct = tt::Punct {
- spacing: tt::Spacing::Alone,
- span: literal.span,
- char: '-' as char,
- };
- let leaf: tt::Leaf<Span> = tt::Leaf::from(punct);
- let minus_tree = tt::TokenTree::from(leaf);
-
- let literal = tt::Literal {
- symbol: Symbol::intern(symbol),
- suffix: literal.suffix,
- span: literal.span,
- kind: literal_kind_to_internal(literal.kind),
- };
- let leaf: tt::Leaf<Span> = tt::Leaf::from(literal);
- let tree = tt::TokenTree::from(leaf);
- vec![minus_tree, tree]
- } else {
- let literal = tt::Literal {
- symbol: literal.symbol,
- suffix: literal.suffix,
- span: literal.span,
- kind: literal_kind_to_internal(literal.kind),
- };
-
- let leaf: tt::Leaf<Span> = tt::Leaf::from(literal);
- let tree = tt::TokenTree::from(leaf);
- vec![tree]
- };
+ let mut token_trees = Vec::new();
+ let mut symbol = literal.symbol;
+ if matches!(
+ literal.kind,
+ proc_macro::bridge::LitKind::Integer | proc_macro::bridge::LitKind::Float
+ ) && symbol.as_str().starts_with('-')
+ {
+ token_trees.push(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct {
+ spacing: tt::Spacing::Alone,
+ span: literal.span,
+ char: '-' as char,
+ })));
+ symbol = Symbol::intern(&symbol.as_str()[1..]);
+ }
+ let literal = tt::Literal {
+ symbol,
+ suffix: literal.suffix,
+ span: literal.span,
+ kind: literal_kind_to_internal(literal.kind),
+ };
+ let leaf: tt::Leaf<Span> = tt::Leaf::from(literal);
+ let tree = tt::TokenTree::from(leaf);
+ token_trees.push(tree);
TokenStream { token_trees }
}
diff --git a/crates/proc-macro-srv/src/tests/mod.rs b/crates/proc-macro-srv/src/tests/mod.rs
index 3a6ce639d1..d1ea89d2dc 100644
--- a/crates/proc-macro-srv/src/tests/mod.rs
+++ b/crates/proc-macro-srv/src/tests/mod.rs
@@ -244,6 +244,7 @@ fn test_fn_like_mk_literals() {
LITERAL ByteStr byte_string 1
LITERAL Char c 1
LITERAL Str string 1
+ LITERAL Str -string 1
LITERAL CStr cstring 1
LITERAL Float 3.14f64 1
PUNCH - [alone] 1
@@ -266,6 +267,7 @@ fn test_fn_like_mk_literals() {
LITERAL ByteStr byte_string 42:[email protected]#ROOT2024
LITERAL Char c 42:[email protected]#ROOT2024
LITERAL Str string 42:[email protected]#ROOT2024
+ LITERAL Str -string 42:[email protected]#ROOT2024
LITERAL CStr cstring 42:[email protected]#ROOT2024
LITERAL Float 3.14f64 42:[email protected]#ROOT2024
PUNCH - [alone] 42:[email protected]#ROOT2024