Skip to content

fix: fix "X is not a valid punct" panic with floats in macros #12231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions crates/hir-def/src/macro_expansion_tests/proc_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,40 @@ fn foo() {
}"##]],
);
}

#[test]
fn float_parsing_panic() {
// Regression test for https://github.com/rust-lang/rust-analyzer/issues/12211
check(
r#"
//- proc_macros: identity
macro_rules! id {
($($t:tt)*) => {
$($t)*
};
}

id! {
#[proc_macros::identity]
impl Foo for WrapBj {
async fn foo(&self) {
self.0. id().await;
}
}
}
"#,
expect![[r##"
macro_rules! id {
($($t:tt)*) => {
$($t)*
};
}

#[proc_macros::identity] impl Foo for WrapBj {
async fn foo(&self ) {
self .0.id().await ;
}
}
"##]],
);
}
2 changes: 2 additions & 0 deletions crates/mbe/src/syntax_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ fn convert_tokens<C: TokenConvertor>(conv: &mut C) -> tt::Subtree {
let char = match token.to_char(conv) {
Some(c) => c,
None => {
// FIXME: this isn't really correct, `to_char` yields the *first* char of the token,
// and this is relevant when eg. creating 2 `tt::Punct` from a single `::` token
panic!("Token from lexer must be single char: token = {:#?}", token);
}
};
Expand Down
4 changes: 3 additions & 1 deletion crates/parser/src/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ fn name_ref_or_index(p: &mut Parser) {
);
let m = p.start();
if p.at(FLOAT_NUMBER_PART) || p.at_ts(FLOAT_LITERAL_FIRST) {
p.bump_remap(INT_NUMBER);
// Ideally we'd remap this to `INT_NUMBER` instead, but that causes the MBE conversion to
// lose track of what's a float and what isn't, causing panics.
p.bump_remap(FLOAT_NUMBER_PART);
} else {
p.bump_any();
}
Expand Down
6 changes: 3 additions & 3 deletions crates/parser/test_data/parser/inline/ok/0011_field_expr.rast
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ SOURCE_FILE
IDENT "x"
DOT "."
NAME_REF
INT_NUMBER "0"
FLOAT_NUMBER_PART "0"
DOT "."
WHITESPACE " "
NAME_REF
Expand All @@ -67,10 +67,10 @@ SOURCE_FILE
IDENT "x"
DOT "."
NAME_REF
INT_NUMBER "0"
FLOAT_NUMBER_PART "0"
DOT "."
NAME_REF
INT_NUMBER "1"
FLOAT_NUMBER_PART "1"
SEMICOLON ";"
WHITESPACE "\n "
EXPR_STMT
Expand Down