Skip to content

Commit 5919f6d

Browse files
authored
Merge pull request #19970 from ChayimFriedman2/proc-macro-srv-minus
fix: Fix proc macro server handling of strings with minuses
2 parents 9c3476d + 8ca5ad6 commit 5919f6d

File tree

3 files changed

+26
-31
lines changed

3 files changed

+26
-31
lines changed

crates/proc-macro-srv/proc-macro-test/imp/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub fn fn_like_mk_literals(_args: TokenStream) -> TokenStream {
3131
TokenTree::from(Literal::byte_string(b"byte_string")),
3232
TokenTree::from(Literal::character('c')),
3333
TokenTree::from(Literal::string("string")),
34+
TokenTree::from(Literal::string("-string")),
3435
TokenTree::from(Literal::c_string(c"cstring")),
3536
// as of 2022-07-21, there's no method on `Literal` to build a raw
3637
// string or a raw byte string

crates/proc-macro-srv/src/server_impl.rs

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -199,37 +199,29 @@ pub(super) fn from_token_tree<Span: Copy>(
199199
}
200200

201201
bridge::TokenTree::Literal(literal) => {
202-
let token_trees =
203-
if let Some((_minus, symbol)) = literal.symbol.as_str().split_once('-') {
204-
let punct = tt::Punct {
205-
spacing: tt::Spacing::Alone,
206-
span: literal.span,
207-
char: '-' as char,
208-
};
209-
let leaf: tt::Leaf<Span> = tt::Leaf::from(punct);
210-
let minus_tree = tt::TokenTree::from(leaf);
211-
212-
let literal = tt::Literal {
213-
symbol: Symbol::intern(symbol),
214-
suffix: literal.suffix,
215-
span: literal.span,
216-
kind: literal_kind_to_internal(literal.kind),
217-
};
218-
let leaf: tt::Leaf<Span> = tt::Leaf::from(literal);
219-
let tree = tt::TokenTree::from(leaf);
220-
vec![minus_tree, tree]
221-
} else {
222-
let literal = tt::Literal {
223-
symbol: literal.symbol,
224-
suffix: literal.suffix,
225-
span: literal.span,
226-
kind: literal_kind_to_internal(literal.kind),
227-
};
228-
229-
let leaf: tt::Leaf<Span> = tt::Leaf::from(literal);
230-
let tree = tt::TokenTree::from(leaf);
231-
vec![tree]
232-
};
202+
let mut token_trees = Vec::new();
203+
let mut symbol = literal.symbol;
204+
if matches!(
205+
literal.kind,
206+
proc_macro::bridge::LitKind::Integer | proc_macro::bridge::LitKind::Float
207+
) && symbol.as_str().starts_with('-')
208+
{
209+
token_trees.push(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct {
210+
spacing: tt::Spacing::Alone,
211+
span: literal.span,
212+
char: '-' as char,
213+
})));
214+
symbol = Symbol::intern(&symbol.as_str()[1..]);
215+
}
216+
let literal = tt::Literal {
217+
symbol,
218+
suffix: literal.suffix,
219+
span: literal.span,
220+
kind: literal_kind_to_internal(literal.kind),
221+
};
222+
let leaf: tt::Leaf<Span> = tt::Leaf::from(literal);
223+
let tree = tt::TokenTree::from(leaf);
224+
token_trees.push(tree);
233225
TokenStream { token_trees }
234226
}
235227

crates/proc-macro-srv/src/tests/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ fn test_fn_like_mk_literals() {
244244
LITERAL ByteStr byte_string 1
245245
LITERAL Char c 1
246246
LITERAL Str string 1
247+
LITERAL Str -string 1
247248
LITERAL CStr cstring 1
248249
LITERAL Float 3.14f64 1
249250
PUNCH - [alone] 1
@@ -266,6 +267,7 @@ fn test_fn_like_mk_literals() {
266267
LITERAL ByteStr byte_string 42:[email protected]#ROOT2024
267268
LITERAL Char c 42:[email protected]#ROOT2024
268269
LITERAL Str string 42:[email protected]#ROOT2024
270+
LITERAL Str -string 42:[email protected]#ROOT2024
269271
LITERAL CStr cstring 42:[email protected]#ROOT2024
270272
LITERAL Float 3.14f64 42:[email protected]#ROOT2024
271273
PUNCH - [alone] 42:[email protected]#ROOT2024

0 commit comments

Comments
 (0)