Skip to content

Commit 5dbe3fe

Browse files
committed
Auto merge of #17065 - Veykril:edition-parse-mac, r=Veykril
internal: Thread edition through to parsing/tt-to-syntax-tree routines for macros Follow up to #16450, cc #16324
2 parents 74cef6d + a483d3b commit 5dbe3fe

39 files changed

+187
-145
lines changed

crates/base-db/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ fn toolchain_channel(db: &dyn SourceDatabase, krate: CrateId) -> Option<ReleaseC
8383
fn parse(db: &dyn SourceDatabase, file_id: FileId) -> Parse<ast::SourceFile> {
8484
let _p = tracing::span!(tracing::Level::INFO, "parse_query", ?file_id).entered();
8585
let text = db.file_text(file_id);
86-
SourceFile::parse(&text)
86+
// FIXME: Edition based parsing
87+
SourceFile::parse(&text, span::Edition::CURRENT)
8788
}
8889

8990
/// We don't want to give HIR knowledge of source roots, hence we extract these

crates/cfg/src/tests.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
use arbitrary::{Arbitrary, Unstructured};
22
use expect_test::{expect, Expect};
33
use mbe::{syntax_node_to_token_tree, DummyTestSpanMap, DUMMY};
4-
use syntax::{ast, AstNode};
4+
use syntax::{ast, AstNode, Edition};
55

66
use crate::{CfgAtom, CfgExpr, CfgOptions, DnfExpr};
77

88
fn assert_parse_result(input: &str, expected: CfgExpr) {
9-
let source_file = ast::SourceFile::parse(input).ok().unwrap();
9+
let source_file = ast::SourceFile::parse(input, Edition::CURRENT).ok().unwrap();
1010
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
1111
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
1212
let cfg = CfgExpr::parse(&tt);
1313
assert_eq!(cfg, expected);
1414
}
1515

1616
fn check_dnf(input: &str, expect: Expect) {
17-
let source_file = ast::SourceFile::parse(input).ok().unwrap();
17+
let source_file = ast::SourceFile::parse(input, Edition::CURRENT).ok().unwrap();
1818
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
1919
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
2020
let cfg = CfgExpr::parse(&tt);
@@ -23,7 +23,7 @@ fn check_dnf(input: &str, expect: Expect) {
2323
}
2424

2525
fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
26-
let source_file = ast::SourceFile::parse(input).ok().unwrap();
26+
let source_file = ast::SourceFile::parse(input, Edition::CURRENT).ok().unwrap();
2727
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
2828
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
2929
let cfg = CfgExpr::parse(&tt);
@@ -34,7 +34,7 @@ fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
3434

3535
#[track_caller]
3636
fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) {
37-
let source_file = ast::SourceFile::parse(input).ok().unwrap();
37+
let source_file = ast::SourceFile::parse(input, Edition::CURRENT).ok().unwrap();
3838
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
3939
let tt = syntax_node_to_token_tree(tt.syntax(), DummyTestSpanMap, DUMMY);
4040
let cfg = CfgExpr::parse(&tt);

crates/hir-def/src/attr/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use syntax::{ast, AstNode, TextRange};
1111
use crate::attr::{DocAtom, DocExpr};
1212

1313
fn assert_parse_result(input: &str, expected: DocExpr) {
14-
let source_file = ast::SourceFile::parse(input).ok().unwrap();
14+
let source_file = ast::SourceFile::parse(input, span::Edition::CURRENT).ok().unwrap();
1515
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
1616
let map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(FileId::from_raw(0))));
1717
let tt = syntax_node_to_token_tree(

crates/hir-def/src/find_path.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,8 @@ mod tests {
610610
) {
611611
let (db, pos) = TestDB::with_position(ra_fixture);
612612
let module = db.module_at_position(pos);
613-
let parsed_path_file = syntax::SourceFile::parse(&format!("use {path};"));
613+
let parsed_path_file =
614+
syntax::SourceFile::parse(&format!("use {path};"), span::Edition::CURRENT);
614615
let ast_path =
615616
parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap();
616617
let mod_path = ModPath::from_src(&db, ast_path, &mut |range| {

crates/hir-expand/src/builtin_fn_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ fn assert_expand(
219219
span: Span,
220220
) -> ExpandResult<tt::Subtree> {
221221
let call_site_span = span_with_call_site_ctxt(db, span, id);
222-
let args = parse_exprs_with_sep(tt, ',', call_site_span);
222+
let args = parse_exprs_with_sep(tt, ',', call_site_span, Edition::CURRENT);
223223
let dollar_crate = dollar_crate(span);
224224
let expanded = match &*args {
225225
[cond, panic_args @ ..] => {

crates/hir-expand/src/cfg_process.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ mod tests {
327327
use crate::cfg_process::parse_from_attr_meta;
328328

329329
fn check_dnf_from_syntax(input: &str, expect: Expect) {
330-
let parse = SourceFile::parse(input);
330+
let parse = SourceFile::parse(input, span::Edition::CURRENT);
331331
let node = match parse.tree().syntax().descendants().find_map(Attr::cast) {
332332
Some(it) => it,
333333
None => {

crates/hir-expand/src/db.rs

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -225,43 +225,45 @@ pub fn expand_speculative(
225225

226226
// Do the actual expansion, we need to directly expand the proc macro due to the attribute args
227227
// Otherwise the expand query will fetch the non speculative attribute args and pass those instead.
228-
let mut speculative_expansion = match loc.def.kind {
229-
MacroDefKind::ProcMacro(expander, _, ast) => {
230-
let span = db.proc_macro_span(ast);
231-
tt.delimiter = tt::Delimiter::invisible_spanned(span);
232-
expander.expand(
233-
db,
234-
loc.def.krate,
235-
loc.krate,
236-
&tt,
237-
attr_arg.as_ref(),
238-
span_with_def_site_ctxt(db, span, actual_macro_call),
239-
span_with_call_site_ctxt(db, span, actual_macro_call),
240-
span_with_mixed_site_ctxt(db, span, actual_macro_call),
241-
)
242-
}
243-
MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => {
244-
pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?, span)
245-
}
246-
MacroDefKind::Declarative(it) => {
247-
db.decl_macro_expander(loc.krate, it).expand_unhygienic(db, tt, loc.def.krate, span)
248-
}
249-
MacroDefKind::BuiltIn(it, _) => {
250-
it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
251-
}
252-
MacroDefKind::BuiltInDerive(it, ..) => {
253-
it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
254-
}
255-
MacroDefKind::BuiltInEager(it, _) => {
256-
it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
257-
}
258-
MacroDefKind::BuiltInAttr(it, _) => it.expand(db, actual_macro_call, &tt, span),
259-
};
228+
let mut speculative_expansion =
229+
match loc.def.kind {
230+
MacroDefKind::ProcMacro(expander, _, ast) => {
231+
let span = db.proc_macro_span(ast);
232+
tt.delimiter = tt::Delimiter::invisible_spanned(span);
233+
expander.expand(
234+
db,
235+
loc.def.krate,
236+
loc.krate,
237+
&tt,
238+
attr_arg.as_ref(),
239+
span_with_def_site_ctxt(db, span, actual_macro_call),
240+
span_with_call_site_ctxt(db, span, actual_macro_call),
241+
span_with_mixed_site_ctxt(db, span, actual_macro_call),
242+
)
243+
}
244+
MacroDefKind::BuiltInAttr(BuiltinAttrExpander::Derive, _) => {
245+
pseudo_derive_attr_expansion(&tt, attr_arg.as_ref()?, span)
246+
}
247+
MacroDefKind::Declarative(it) => db
248+
.decl_macro_expander(loc.krate, it)
249+
.expand_unhygienic(db, tt, loc.def.krate, span, loc.def.edition),
250+
MacroDefKind::BuiltIn(it, _) => {
251+
it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
252+
}
253+
MacroDefKind::BuiltInDerive(it, ..) => {
254+
it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
255+
}
256+
MacroDefKind::BuiltInEager(it, _) => {
257+
it.expand(db, actual_macro_call, &tt, span).map_err(Into::into)
258+
}
259+
MacroDefKind::BuiltInAttr(it, _) => it.expand(db, actual_macro_call, &tt, span),
260+
};
260261

261262
let expand_to = loc.expand_to();
262263

263264
fixup::reverse_fixups(&mut speculative_expansion.value, &undo_info);
264-
let (node, rev_tmap) = token_tree_to_syntax_node(&speculative_expansion.value, expand_to);
265+
let (node, rev_tmap) =
266+
token_tree_to_syntax_node(&speculative_expansion.value, expand_to, loc.def.edition);
265267

266268
let syntax_node = node.syntax_node();
267269
let token = rev_tmap
@@ -309,6 +311,7 @@ fn parse_macro_expansion(
309311
) -> ExpandResult<(Parse<SyntaxNode>, Arc<ExpansionSpanMap>)> {
310312
let _p = tracing::span!(tracing::Level::INFO, "parse_macro_expansion").entered();
311313
let loc = db.lookup_intern_macro_call(macro_file.macro_call_id);
314+
let edition = loc.def.edition;
312315
let expand_to = loc.expand_to();
313316
let mbe::ValueResult { value: tt, err } = macro_expand(db, macro_file.macro_call_id, loc);
314317

@@ -318,6 +321,7 @@ fn parse_macro_expansion(
318321
CowArc::Owned(it) => it,
319322
},
320323
expand_to,
324+
edition,
321325
);
322326

323327
ExpandResult { value: (parse, Arc::new(rev_token_map)), err }
@@ -668,6 +672,7 @@ fn expand_proc_macro(db: &dyn ExpandDatabase, id: MacroCallId) -> ExpandResult<A
668672
fn token_tree_to_syntax_node(
669673
tt: &tt::Subtree,
670674
expand_to: ExpandTo,
675+
edition: parser::Edition,
671676
) -> (Parse<SyntaxNode>, ExpansionSpanMap) {
672677
let entry_point = match expand_to {
673678
ExpandTo::Statements => mbe::TopEntryPoint::MacroStmts,
@@ -676,7 +681,7 @@ fn token_tree_to_syntax_node(
676681
ExpandTo::Type => mbe::TopEntryPoint::Type,
677682
ExpandTo::Expr => mbe::TopEntryPoint::Expr,
678683
};
679-
mbe::token_tree_to_syntax_node(tt, entry_point, parser::Edition::CURRENT)
684+
mbe::token_tree_to_syntax_node(tt, entry_point, edition)
680685
}
681686

682687
fn check_tt_count(tt: &tt::Subtree) -> Result<(), ExpandResult<()>> {

crates/hir-expand/src/declarative.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
use std::sync::OnceLock;
33

44
use base_db::{CrateId, VersionReq};
5-
use span::{MacroCallId, Span, SyntaxContextId};
5+
use span::{Edition, MacroCallId, Span, SyntaxContextId};
66
use syntax::{ast, AstNode};
77
use triomphe::Arc;
88

@@ -56,6 +56,7 @@ impl DeclarativeMacroExpander {
5656
|s| s.ctx = apply_mark(db, s.ctx, call_id, self.transparency),
5757
new_meta_vars,
5858
span,
59+
loc.def.edition,
5960
)
6061
.map_err(Into::into),
6162
}
@@ -67,6 +68,7 @@ impl DeclarativeMacroExpander {
6768
tt: tt::Subtree,
6869
krate: CrateId,
6970
call_site: Span,
71+
def_site_edition: Edition,
7072
) -> ExpandResult<tt::Subtree> {
7173
let toolchain = db.toolchain(krate);
7274
let new_meta_vars = toolchain.as_ref().map_or(false, |version| {
@@ -85,7 +87,10 @@ impl DeclarativeMacroExpander {
8587
tt::Subtree::empty(tt::DelimSpan { open: call_site, close: call_site }),
8688
ExpandError::MacroDefinition,
8789
),
88-
None => self.mac.expand(&tt, |_| (), new_meta_vars, call_site).map_err(Into::into),
90+
None => self
91+
.mac
92+
.expand(&tt, |_| (), new_meta_vars, call_site, def_site_edition)
93+
.map_err(Into::into),
8994
}
9095
}
9196

crates/hir-expand/src/fixup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ mod tests {
396396

397397
#[track_caller]
398398
fn check(ra_fixture: &str, mut expect: Expect) {
399-
let parsed = syntax::SourceFile::parse(ra_fixture);
399+
let parsed = syntax::SourceFile::parse(ra_fixture, span::Edition::CURRENT);
400400
let span_map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(FileId::from_raw(0))));
401401
let fixups = super::fixup_syntax(
402402
span_map.as_ref(),

crates/ide-completion/src/context.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use ide_db::{
1717
};
1818
use syntax::{
1919
ast::{self, AttrKind, NameOrNameRef},
20-
AstNode, SmolStr,
20+
AstNode, Edition, SmolStr,
2121
SyntaxKind::{self, *},
2222
SyntaxToken, TextRange, TextSize, T,
2323
};
@@ -667,7 +667,8 @@ impl<'a> CompletionContext<'a> {
667667
let file_with_fake_ident = {
668668
let parse = db.parse(file_id);
669669
let edit = Indel::insert(offset, COMPLETION_MARKER.to_owned());
670-
parse.reparse(&edit).tree()
670+
// FIXME: Edition
671+
parse.reparse(&edit, Edition::CURRENT).tree()
671672
};
672673

673674
// always pick the token to the immediate left of the cursor, as that is what we are actually

crates/ide-completion/src/snippet.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ fn validate_snippet(
200200
) -> Option<(Box<[GreenNode]>, String, Option<Box<str>>)> {
201201
let mut imports = Vec::with_capacity(requires.len());
202202
for path in requires.iter() {
203-
let use_path = ast::SourceFile::parse(&format!("use {path};"))
203+
let use_path = ast::SourceFile::parse(&format!("use {path};"), syntax::Edition::CURRENT)
204204
.syntax_node()
205205
.descendants()
206206
.find_map(ast::Path::cast)?;

crates/ide-db/src/imports/insert_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ pub fn insert_use(scope: &ImportScope, path: ast::Path, cfg: &InsertUseConfig) {
176176

177177
pub fn insert_use_as_alias(scope: &ImportScope, path: ast::Path, cfg: &InsertUseConfig) {
178178
let text: &str = "use foo as _";
179-
let parse = syntax::SourceFile::parse(text);
179+
let parse = syntax::SourceFile::parse(text, span::Edition::CURRENT);
180180
let node = parse
181181
.tree()
182182
.syntax()

crates/ide-db/src/imports/insert_use/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ fn check_with_config(
12431243
.and_then(|it| ImportScope::find_insert_use_container(&it, sema))
12441244
.or_else(|| ImportScope::from(syntax))
12451245
.unwrap();
1246-
let path = ast::SourceFile::parse(&format!("use {path};"))
1246+
let path = ast::SourceFile::parse(&format!("use {path};"), span::Edition::CURRENT)
12471247
.tree()
12481248
.syntax()
12491249
.descendants()
@@ -1292,14 +1292,14 @@ fn check_one(path: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
12921292
}
12931293

12941294
fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior) {
1295-
let use0 = ast::SourceFile::parse(ra_fixture0)
1295+
let use0 = ast::SourceFile::parse(ra_fixture0, span::Edition::CURRENT)
12961296
.tree()
12971297
.syntax()
12981298
.descendants()
12991299
.find_map(ast::Use::cast)
13001300
.unwrap();
13011301

1302-
let use1 = ast::SourceFile::parse(ra_fixture1)
1302+
let use1 = ast::SourceFile::parse(ra_fixture1, span::Edition::CURRENT)
13031303
.tree()
13041304
.syntax()
13051305
.descendants()
@@ -1311,7 +1311,7 @@ fn check_merge_only_fail(ra_fixture0: &str, ra_fixture1: &str, mb: MergeBehavior
13111311
}
13121312

13131313
fn check_guess(ra_fixture: &str, expected: ImportGranularityGuess) {
1314-
let syntax = ast::SourceFile::parse(ra_fixture).tree().syntax().clone();
1314+
let syntax = ast::SourceFile::parse(ra_fixture, span::Edition::CURRENT).tree().syntax().clone();
13151315
let file = ImportScope::from(syntax).unwrap();
13161316
assert_eq!(super::guess_granularity_from_scope(&file), expected);
13171317
}

crates/ide-ssr/src/fragments.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub(crate) fn expr(s: &str) -> Result<SyntaxNode, ()> {
2727
pub(crate) fn stmt(s: &str) -> Result<SyntaxNode, ()> {
2828
let template = "const _: () = { {}; };";
2929
let input = template.replace("{}", s);
30-
let parse = syntax::SourceFile::parse(&input);
30+
let parse = syntax::SourceFile::parse(&input, syntax::Edition::CURRENT);
3131
if !parse.errors().is_empty() {
3232
return Err(());
3333
}
@@ -48,7 +48,7 @@ pub(crate) fn stmt(s: &str) -> Result<SyntaxNode, ()> {
4848
fn fragment<T: AstNode>(template: &str, s: &str) -> Result<SyntaxNode, ()> {
4949
let s = s.trim();
5050
let input = template.replace("{}", s);
51-
let parse = syntax::SourceFile::parse(&input);
51+
let parse = syntax::SourceFile::parse(&input, syntax::Edition::CURRENT);
5252
if !parse.errors().is_empty() {
5353
return Err(());
5454
}

crates/ide/src/file_structure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ mod tests {
220220
use super::*;
221221

222222
fn check(ra_fixture: &str, expect: Expect) {
223-
let file = SourceFile::parse(ra_fixture).ok().unwrap();
223+
let file = SourceFile::parse(ra_fixture, span::Edition::CURRENT).ok().unwrap();
224224
let structure = file_structure(&file);
225225
expect.assert_debug_eq(&structure)
226226
}

crates/ide/src/folding_ranges.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ mod tests {
289289
fn check(ra_fixture: &str) {
290290
let (ranges, text) = extract_tags(ra_fixture, "fold");
291291

292-
let parse = SourceFile::parse(&text);
292+
let parse = SourceFile::parse(&text, span::Edition::CURRENT);
293293
let mut folds = folding_ranges(&parse.tree());
294294
folds.sort_by_key(|fold| (fold.range.start(), fold.range.end()));
295295

crates/ide/src/join_lines.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ mod tests {
316316
};
317317

318318
let (before_cursor_pos, before) = extract_offset(ra_fixture_before);
319-
let file = SourceFile::parse(&before).ok().unwrap();
319+
let file = SourceFile::parse(&before, span::Edition::CURRENT).ok().unwrap();
320320

321321
let range = TextRange::empty(before_cursor_pos);
322322
let result = join_lines(&config, &file, range);
@@ -342,7 +342,7 @@ mod tests {
342342
};
343343

344344
let (sel, before) = extract_range(ra_fixture_before);
345-
let parse = SourceFile::parse(&before);
345+
let parse = SourceFile::parse(&before, span::Edition::CURRENT);
346346
let result = join_lines(&config, &parse.tree(), sel);
347347
let actual = {
348348
let mut actual = before;

crates/ide/src/matching_brace.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ mod tests {
5050
fn test_matching_brace() {
5151
fn do_check(before: &str, after: &str) {
5252
let (pos, before) = extract_offset(before);
53-
let parse = SourceFile::parse(&before);
53+
let parse = SourceFile::parse(&before, span::Edition::CURRENT);
5454
let new_pos = match matching_brace(&parse.tree(), pos) {
5555
None => pos,
5656
Some(pos) => pos,

crates/ide/src/syntax_tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option<St
8888
// Remove custom markers
8989
.replace("$0", "");
9090

91-
let parsed = SourceFile::parse(&text);
91+
let parsed = SourceFile::parse(&text, span::Edition::CURRENT);
9292

9393
// If the "file" parsed without errors,
9494
// return its syntax

0 commit comments

Comments
 (0)