Merge pull request #19970 from ChayimFriedman2/proc-macro-srv-minus

fix: Fix proc macro server handling of strings with minuses
This commit is contained in:
Lukas Wirth 2025-06-11 06:08:54 +00:00 committed by GitHub
commit 5919f6db3a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 31 deletions

View File

@ -31,6 +31,7 @@ pub fn fn_like_mk_literals(_args: TokenStream) -> TokenStream {
TokenTree::from(Literal::byte_string(b"byte_string")), TokenTree::from(Literal::byte_string(b"byte_string")),
TokenTree::from(Literal::character('c')), TokenTree::from(Literal::character('c')),
TokenTree::from(Literal::string("string")), TokenTree::from(Literal::string("string")),
TokenTree::from(Literal::string("-string")),
TokenTree::from(Literal::c_string(c"cstring")), TokenTree::from(Literal::c_string(c"cstring")),
// as of 2022-07-21, there's no method on `Literal` to build a raw // as of 2022-07-21, there's no method on `Literal` to build a raw
// string or a raw byte string // string or a raw byte string

View File

@ -199,37 +199,29 @@ pub(super) fn from_token_tree<Span: Copy>(
} }
bridge::TokenTree::Literal(literal) => { bridge::TokenTree::Literal(literal) => {
let token_trees = let mut token_trees = Vec::new();
if let Some((_minus, symbol)) = literal.symbol.as_str().split_once('-') { let mut symbol = literal.symbol;
let punct = tt::Punct { if matches!(
spacing: tt::Spacing::Alone, literal.kind,
span: literal.span, proc_macro::bridge::LitKind::Integer | proc_macro::bridge::LitKind::Float
char: '-' as char, ) && symbol.as_str().starts_with('-')
}; {
let leaf: tt::Leaf<Span> = tt::Leaf::from(punct); token_trees.push(tt::TokenTree::Leaf(tt::Leaf::Punct(tt::Punct {
let minus_tree = tt::TokenTree::from(leaf); spacing: tt::Spacing::Alone,
span: literal.span,
let literal = tt::Literal { char: '-' as char,
symbol: Symbol::intern(symbol), })));
suffix: literal.suffix, symbol = Symbol::intern(&symbol.as_str()[1..]);
span: literal.span, }
kind: literal_kind_to_internal(literal.kind), let literal = tt::Literal {
}; symbol,
let leaf: tt::Leaf<Span> = tt::Leaf::from(literal); suffix: literal.suffix,
let tree = tt::TokenTree::from(leaf); span: literal.span,
vec![minus_tree, tree] kind: literal_kind_to_internal(literal.kind),
} else { };
let literal = tt::Literal { let leaf: tt::Leaf<Span> = tt::Leaf::from(literal);
symbol: literal.symbol, let tree = tt::TokenTree::from(leaf);
suffix: literal.suffix, token_trees.push(tree);
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]
};
TokenStream { token_trees } TokenStream { token_trees }
} }

View File

@ -244,6 +244,7 @@ fn test_fn_like_mk_literals() {
LITERAL ByteStr byte_string 1 LITERAL ByteStr byte_string 1
LITERAL Char c 1 LITERAL Char c 1
LITERAL Str string 1 LITERAL Str string 1
LITERAL Str -string 1
LITERAL CStr cstring 1 LITERAL CStr cstring 1
LITERAL Float 3.14f64 1 LITERAL Float 3.14f64 1
PUNCH - [alone] 1 PUNCH - [alone] 1
@ -266,6 +267,7 @@ fn test_fn_like_mk_literals() {
LITERAL ByteStr byte_string 42:2@0..100#ROOT2024 LITERAL ByteStr byte_string 42:2@0..100#ROOT2024
LITERAL Char c 42:2@0..100#ROOT2024 LITERAL Char c 42:2@0..100#ROOT2024
LITERAL Str string 42:2@0..100#ROOT2024 LITERAL Str string 42:2@0..100#ROOT2024
LITERAL Str -string 42:2@0..100#ROOT2024
LITERAL CStr cstring 42:2@0..100#ROOT2024 LITERAL CStr cstring 42:2@0..100#ROOT2024
LITERAL Float 3.14f64 42:2@0..100#ROOT2024 LITERAL Float 3.14f64 42:2@0..100#ROOT2024
PUNCH - [alone] 42:2@0..100#ROOT2024 PUNCH - [alone] 42:2@0..100#ROOT2024