Skip to content

Commit 814da15

Browse files
committed
Auto merge of rust-lang#18146 - ChayimFriedman2:allow-comment, r=Veykril
fix: Remove check that text of `parse_expr_from_str()` matches the produced parsed tree This check is incorrect when we have comments and whitespace in the text. We can strip comments, but then we still have whitespace, which we cannot strip without changing meaning for the parser. So instead I opt to remove the check, and wrap the expression in parentheses (asserting what produced is a parenthesized expression) to strengthen verification. Fixes rust-lang#18144.
2 parents 4ed7f4b + 65f83e4 commit 814da15

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_case.rs

+13
Original file line numberDiff line numberDiff line change
@@ -996,4 +996,17 @@ fn BAR() {
996996
"#,
997997
);
998998
}
999+
1000+
#[test]
1001+
fn allow_with_comment() {
1002+
check_diagnostics(
1003+
r#"
1004+
#[allow(
1005+
// Yo, sup
1006+
non_snake_case
1007+
)]
1008+
fn foo(_HelloWorld: ()) {}
1009+
"#,
1010+
);
1011+
}
9991012
}

src/tools/rust-analyzer/crates/syntax/src/hacks.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ use crate::{ast, AstNode};
88

99
pub fn parse_expr_from_str(s: &str, edition: Edition) -> Option<ast::Expr> {
1010
let s = s.trim();
11-
let file = ast::SourceFile::parse(&format!("const _: () = {s};"), edition);
12-
let expr = file.syntax_node().descendants().find_map(ast::Expr::cast)?;
13-
if expr.syntax().text() != s {
14-
return None;
15-
}
16-
Some(expr)
11+
12+
let file = ast::SourceFile::parse(
13+
// Need a newline because the text may contain line comments.
14+
&format!("const _: () = ({s}\n);"),
15+
edition,
16+
);
17+
let expr = file.syntax_node().descendants().find_map(ast::ParenExpr::cast)?;
18+
// Can't check the text because the original text may contain whitespace and comments.
19+
// Wrap in parentheses to better allow for verification. Of course, the real fix is
20+
// to get rid of this hack.
21+
expr.expr()
1722
}

0 commit comments

Comments
 (0)