Skip to content

Commit ddbb28d

Browse files
Modify hacks::parse_expr_from_str() to take an edition too
This will be needed as we parse unknown identifiers and want to insert them into source code.
1 parent e6d59e6 commit ddbb28d

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

crates/ide-assists/src/handlers/remove_dbg.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use itertools::Itertools;
22
use syntax::{
33
ast::{self, make, AstNode, AstToken},
4-
match_ast, ted, NodeOrToken, SyntaxElement, TextRange, TextSize, T,
4+
match_ast, ted, Edition, NodeOrToken, SyntaxElement, TextRange, TextSize, T,
55
};
66

77
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -77,7 +77,7 @@ fn compute_dbg_replacement(macro_expr: ast::MacroExpr) -> Option<(TextRange, Opt
7777
let input_expressions = input_expressions
7878
.into_iter()
7979
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
80-
.map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join("")))
80+
.map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join(""), Edition::CURRENT))
8181
.collect::<Option<Vec<ast::Expr>>>()?;
8282

8383
let parent = macro_expr.syntax().parent()?;

crates/ide-completion/src/completions/attribute.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use ide_db::{
1414
use itertools::Itertools;
1515
use syntax::{
1616
ast::{self, AttrKind},
17-
AstNode, SyntaxKind, T,
17+
AstNode, Edition, SyntaxKind, T,
1818
};
1919

2020
use crate::{
@@ -373,7 +373,9 @@ fn parse_comma_sep_expr(input: ast::TokenTree) -> Option<Vec<ast::Expr>> {
373373
input_expressions
374374
.into_iter()
375375
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
376-
.filter_map(|mut tokens| syntax::hacks::parse_expr_from_str(&tokens.join("")))
376+
.filter_map(|mut tokens| {
377+
syntax::hacks::parse_expr_from_str(&tokens.join(""), Edition::CURRENT)
378+
})
377379
.collect::<Vec<ast::Expr>>(),
378380
)
379381
}

crates/ide-db/src/syntax_helpers/node_ext.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -477,10 +477,12 @@ pub fn parse_tt_as_comma_sep_paths(
477477
.into_iter()
478478
.filter_map(|(is_sep, group)| (!is_sep).then_some(group))
479479
.filter_map(|mut tokens| {
480-
syntax::hacks::parse_expr_from_str(&tokens.join("")).and_then(|expr| match expr {
481-
ast::Expr::PathExpr(it) => it.path(),
482-
_ => None,
483-
})
480+
syntax::hacks::parse_expr_from_str(&tokens.join(""), Edition::CURRENT).and_then(
481+
|expr| match expr {
482+
ast::Expr::PathExpr(it) => it.path(),
483+
_ => None,
484+
},
485+
)
484486
})
485487
.collect();
486488
Some(paths)

crates/syntax/src/hacks.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ use parser::Edition;
66

77
use crate::{ast, AstNode};
88

9-
pub fn parse_expr_from_str(s: &str) -> Option<ast::Expr> {
9+
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::CURRENT);
11+
let file = ast::SourceFile::parse(&format!("const _: () = {s};"), edition);
1212
let expr = file.syntax_node().descendants().find_map(ast::Expr::cast)?;
1313
if expr.syntax().text() != s {
1414
return None;

0 commit comments

Comments
 (0)