Fix proc macro server handling of strings with minuses

It used to decompose them thinking they were numbers.
This commit is contained in:
Chayim Refael Friedman 2025-06-11 01:03:35 +03:00
parent 9c3476d225
commit 8ca5ad6bdd
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::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

View file

@ -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 }
}

View file

@ -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: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 CStr cstring 42:2@0..100#ROOT2024
LITERAL Float 3.14f64 42:2@0..100#ROOT2024
PUNCH - [alone] 42:2@0..100#ROOT2024