Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/syntax/src/hacks.rs')
| -rw-r--r-- | crates/syntax/src/hacks.rs | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/crates/syntax/src/hacks.rs b/crates/syntax/src/hacks.rs index 9e63448ce9..2184359f1d 100644 --- a/crates/syntax/src/hacks.rs +++ b/crates/syntax/src/hacks.rs @@ -8,10 +8,15 @@ use crate::{ast, AstNode}; pub fn parse_expr_from_str(s: &str, edition: Edition) -> Option<ast::Expr> { let s = s.trim(); - let file = ast::SourceFile::parse(&format!("const _: () = {s};"), edition); - let expr = file.syntax_node().descendants().find_map(ast::Expr::cast)?; - if expr.syntax().text() != s { - return None; - } - Some(expr) + + let file = ast::SourceFile::parse( + // Need a newline because the text may contain line comments. + &format!("const _: () = ({s}\n);"), + edition, + ); + let expr = file.syntax_node().descendants().find_map(ast::ParenExpr::cast)?; + // Can't check the text because the original text may contain whitespace and comments. + // Wrap in parentheses to better allow for verification. Of course, the real fix is + // to get rid of this hack. + expr.expr() } |