Skip to content

Commit 0c881e8

Browse files
committed
Auto merge of rust-lang#12231 - jonas-schievink:fix-float-macro-panic, r=jonas-schievink
fix: fix "X is not a valid punct" panic with floats in macros Should fix rust-lang/rust-analyzer#12211
2 parents f8c0062 + cb5e8da commit 0c881e8

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

crates/hir-def/src/macro_expansion_tests/proc_macros.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,40 @@ fn foo() {
9292
}"##]],
9393
);
9494
}
95+
96+
#[test]
97+
fn float_parsing_panic() {
98+
// Regression test for https://github.com/rust-lang/rust-analyzer/issues/12211
99+
check(
100+
r#"
101+
//- proc_macros: identity
102+
macro_rules! id {
103+
($($t:tt)*) => {
104+
$($t)*
105+
};
106+
}
107+
108+
id! {
109+
#[proc_macros::identity]
110+
impl Foo for WrapBj {
111+
async fn foo(&self) {
112+
self.0. id().await;
113+
}
114+
}
115+
}
116+
"#,
117+
expect![[r##"
118+
macro_rules! id {
119+
($($t:tt)*) => {
120+
$($t)*
121+
};
122+
}
123+
124+
#[proc_macros::identity] impl Foo for WrapBj {
125+
async fn foo(&self ) {
126+
self .0.id().await ;
127+
}
128+
}
129+
"##]],
130+
);
131+
}

crates/mbe/src/syntax_bridge.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ fn convert_tokens<C: TokenConvertor>(conv: &mut C) -> tt::Subtree {
243243
let char = match token.to_char(conv) {
244244
Some(c) => c,
245245
None => {
246+
// FIXME: this isn't really correct, `to_char` yields the *first* char of the token,
247+
// and this is relevant when eg. creating 2 `tt::Punct` from a single `::` token
246248
panic!("Token from lexer must be single char: token = {:#?}", token);
247249
}
248250
};

crates/parser/src/grammar.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ fn name_ref_or_index(p: &mut Parser) {
324324
);
325325
let m = p.start();
326326
if p.at(FLOAT_NUMBER_PART) || p.at_ts(FLOAT_LITERAL_FIRST) {
327-
p.bump_remap(INT_NUMBER);
327+
// Ideally we'd remap this to `INT_NUMBER` instead, but that causes the MBE conversion to
328+
// lose track of what's a float and what isn't, causing panics.
329+
p.bump_remap(FLOAT_NUMBER_PART);
328330
} else {
329331
p.bump_any();
330332
}

crates/parser/test_data/parser/inline/ok/0011_field_expr.rast

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ SOURCE_FILE
5050
IDENT "x"
5151
DOT "."
5252
NAME_REF
53-
INT_NUMBER "0"
53+
FLOAT_NUMBER_PART "0"
5454
DOT "."
5555
WHITESPACE " "
5656
NAME_REF
@@ -67,10 +67,10 @@ SOURCE_FILE
6767
IDENT "x"
6868
DOT "."
6969
NAME_REF
70-
INT_NUMBER "0"
70+
FLOAT_NUMBER_PART "0"
7171
DOT "."
7272
NAME_REF
73-
INT_NUMBER "1"
73+
FLOAT_NUMBER_PART "1"
7474
SEMICOLON ";"
7575
WHITESPACE "\n "
7676
EXPR_STMT

0 commit comments

Comments
 (0)