Skip to content

Commit 361a927

Browse files
committed
fix: Fix proc-macro API creating malformed negative literals
1 parent cb18ead commit 361a927

File tree

4 files changed

+85
-29
lines changed

4 files changed

+85
-29
lines changed

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

+32-10
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,38 @@ impl server::TokenStream for RaSpanServer {
168168
}
169169

170170
bridge::TokenTree::Literal(literal) => {
171-
let literal = tt::Literal {
172-
symbol: literal.symbol,
173-
suffix: literal.suffix,
174-
span: literal.span,
175-
kind: literal_kind_to_internal(literal.kind),
176-
};
177-
178-
let leaf: tt::Leaf = tt::Leaf::from(literal);
179-
let tree = tt::TokenTree::from(leaf);
180-
TokenStream { token_trees: vec![tree] }
171+
let token_trees =
172+
if let Some((_minus, symbol)) = literal.symbol.as_str().split_once('-') {
173+
let punct = tt::Punct {
174+
spacing: tt::Spacing::Alone,
175+
span: literal.span,
176+
char: '-' as char,
177+
};
178+
let leaf: tt::Leaf = tt::Leaf::from(punct);
179+
let minus_tree = tt::TokenTree::from(leaf);
180+
181+
let literal = tt::Literal {
182+
symbol: Symbol::intern(symbol),
183+
suffix: literal.suffix,
184+
span: literal.span,
185+
kind: literal_kind_to_internal(literal.kind),
186+
};
187+
let leaf: tt::Leaf = tt::Leaf::from(literal);
188+
let tree = tt::TokenTree::from(leaf);
189+
vec![minus_tree, tree]
190+
} else {
191+
let literal = tt::Literal {
192+
symbol: literal.symbol,
193+
suffix: literal.suffix,
194+
span: literal.span,
195+
kind: literal_kind_to_internal(literal.kind),
196+
};
197+
198+
let leaf: tt::Leaf = tt::Leaf::from(literal);
199+
let tree = tt::TokenTree::from(leaf);
200+
vec![tree]
201+
};
202+
TokenStream { token_trees }
181203
}
182204

183205
bridge::TokenTree::Punct(p) => {

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

+32-10
Original file line numberDiff line numberDiff line change
@@ -153,16 +153,38 @@ impl server::TokenStream for TokenIdServer {
153153
}
154154

155155
bridge::TokenTree::Literal(literal) => {
156-
let literal = Literal {
157-
symbol: literal.symbol,
158-
suffix: literal.suffix,
159-
span: literal.span,
160-
kind: literal_kind_to_internal(literal.kind),
161-
};
162-
163-
let leaf = tt::Leaf::from(literal);
164-
let tree = TokenTree::from(leaf);
165-
TokenStream { token_trees: vec![tree] }
156+
let token_trees =
157+
if let Some((_minus, symbol)) = literal.symbol.as_str().split_once('-') {
158+
let punct = tt::Punct {
159+
spacing: tt::Spacing::Alone,
160+
span: literal.span,
161+
char: '-' as char,
162+
};
163+
let leaf: tt::Leaf = tt::Leaf::from(punct);
164+
let minus_tree = tt::TokenTree::from(leaf);
165+
166+
let literal = Literal {
167+
symbol: Symbol::intern(symbol),
168+
suffix: literal.suffix,
169+
span: literal.span,
170+
kind: literal_kind_to_internal(literal.kind),
171+
};
172+
let leaf: tt::Leaf = tt::Leaf::from(literal);
173+
let tree = tt::TokenTree::from(leaf);
174+
vec![minus_tree, tree]
175+
} else {
176+
let literal = Literal {
177+
symbol: literal.symbol,
178+
suffix: literal.suffix,
179+
span: literal.span,
180+
kind: literal_kind_to_internal(literal.kind),
181+
};
182+
183+
let leaf: tt::Leaf = tt::Leaf::from(literal);
184+
let tree = tt::TokenTree::from(leaf);
185+
vec![tree]
186+
};
187+
TokenStream { token_trees }
166188
}
167189

168190
bridge::TokenTree::Punct(p) => {

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

+5
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ impl<S: Copy> TokenStream<S> {
6868
span: ident.span,
6969
}))
7070
}
71+
// Note, we do not have to assemble our `-` punct and literal split into a single
72+
// negative bridge literal here. As the proc-macro docs state
73+
// > Literals created from negative numbers might not survive round-trips through
74+
// > TokenStream or strings and may be broken into two tokens (- and positive
75+
// > literal).
7176
tt::TokenTree::Leaf(tt::Leaf::Literal(lit)) => {
7277
result.push(bridge::TokenTree::Literal(bridge::Literal {
7378
span: lit.span,

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

+16-9
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,17 @@ fn test_fn_like_mk_literals() {
248248
LITERAL Str string 1
249249
LITERAL CStr cstring 1
250250
LITERAL Float 3.14f64 1
251-
LITERAL Float -3.14f64 1
251+
PUNCH - [alone] 1
252+
LITERAL Float 3.14f64 1
253+
LITERAL Float 3.14 1
254+
PUNCH - [alone] 1
252255
LITERAL Float 3.14 1
253-
LITERAL Float -3.14 1
254256
LITERAL Integer 123i64 1
255-
LITERAL Integer -123i64 1
257+
PUNCH - [alone] 1
258+
LITERAL Integer 123i64 1
256259
LITERAL Integer 123 1
257-
LITERAL Integer -123 1"#]],
260+
PUNCH - [alone] 1
261+
LITERAL Integer 123 1"#]],
258262
expect![[r#"
259263
SUBTREE $$ 42:[email protected]#ROOT2024 42:[email protected]#ROOT2024
260264
@@ -266,13 +270,17 @@ fn test_fn_like_mk_literals() {
266270
LITERAL Str string 42:[email protected]#ROOT2024
267271
LITERAL CStr cstring 42:[email protected]#ROOT2024
268272
LITERAL Float 3.14f64 42:[email protected]#ROOT2024
269-
LITERAL Float -3.14f64 42:[email protected]#ROOT2024
273+
PUNCH - [alone] 42:[email protected]#ROOT2024
274+
LITERAL Float 3.14f64 42:[email protected]#ROOT2024
275+
LITERAL Float 3.14 42:[email protected]#ROOT2024
276+
PUNCH - [alone] 42:[email protected]#ROOT2024
270277
LITERAL Float 3.14 42:[email protected]#ROOT2024
271-
LITERAL Float -3.14 42:[email protected]#ROOT2024
272278
LITERAL Integer 123i64 42:[email protected]#ROOT2024
273-
LITERAL Integer -123i64 42:[email protected]#ROOT2024
279+
PUNCH - [alone] 42:[email protected]#ROOT2024
280+
LITERAL Integer 123i64 42:[email protected]#ROOT2024
274281
LITERAL Integer 123 42:[email protected]#ROOT2024
275-
LITERAL Integer -123 42:[email protected]#ROOT2024"#]],
282+
PUNCH - [alone] 42:[email protected]#ROOT2024
283+
LITERAL Integer 123 42:[email protected]#ROOT2024"#]],
276284
);
277285
}
278286

@@ -400,7 +408,6 @@ fn test_fn_like_macro_clone_literals() {
400408
);
401409
}
402410

403-
404411
#[test]
405412
fn test_fn_like_macro_negative_literals() {
406413
assert_expand(

0 commit comments

Comments
 (0)