syntax/hacks.rs
1//! Things which exist to solve practical issues, but which shouldn't exist.
2//!
3//! Please avoid adding new usages of the functions in this module
4
5use parser::Edition;
6
7use crate::{AstNode, ast};
8
9pub fn parse_expr_from_str(s: &str, edition: Edition) -> Option<ast::Expr> {
10 let s = s.trim();
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()
22}