Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5dbe3fe

Browse files
committed
Auto merge of rust-lang#17065 - Veykril:edition-parse-mac, r=Veykril
internal: Thread edition through to parsing/tt-to-syntax-tree routines for macros Follow up to rust-lang/rust-analyzer#16450, cc rust-lang/rust-analyzer#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

0 commit comments

Comments
 (0)