Skip to content

Commit f6e8974

Browse files
authored
Merge pull request #18861 from ChayimFriedman2/await-edition
fix: Make edition per-token, not per-file
2 parents f4ecc34 + 89a72c7 commit f6e8974

File tree

34 files changed

+480
-316
lines changed

34 files changed

+480
-316
lines changed

src/tools/rust-analyzer/crates/edition/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use std::fmt;
55
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
66
#[repr(u8)]
77
pub enum Edition {
8-
Edition2015,
8+
// The syntax context stuff needs the discriminants to start from 0 and be consecutive.
9+
Edition2015 = 0,
910
Edition2018,
1011
Edition2021,
1112
Edition2024,

src/tools/rust-analyzer/crates/hir-def/src/body.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use hir_expand::{name::Name, ExpandError, InFile};
1515
use la_arena::{Arena, ArenaMap, Idx, RawIdx};
1616
use rustc_hash::FxHashMap;
1717
use smallvec::SmallVec;
18-
use span::{Edition, MacroFileId};
18+
use span::{Edition, MacroFileId, SyntaxContextData};
1919
use syntax::{ast, AstPtr, SyntaxNodePtr};
2020
use triomphe::Arc;
2121
use tt::TextRange;
@@ -37,15 +37,22 @@ use crate::{
3737

3838
/// A wrapper around [`span::SyntaxContextId`] that is intended only for comparisons.
3939
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
40-
pub struct HygieneId(pub(crate) span::SyntaxContextId);
40+
pub struct HygieneId(span::SyntaxContextId);
4141

4242
impl HygieneId {
43-
pub const ROOT: Self = Self(span::SyntaxContextId::ROOT);
43+
// The edition doesn't matter here, we only use this for comparisons and to lookup the macro.
44+
pub const ROOT: Self = Self(span::SyntaxContextId::root(Edition::Edition2015));
4445

45-
pub fn new(ctx: span::SyntaxContextId) -> Self {
46+
pub fn new(mut ctx: span::SyntaxContextId) -> Self {
47+
// See `Name` for why we're doing that.
48+
ctx.remove_root_edition();
4649
Self(ctx)
4750
}
4851

52+
pub(crate) fn lookup(self, db: &dyn DefDatabase) -> SyntaxContextData {
53+
db.lookup_intern_syntax_context(self.0)
54+
}
55+
4956
pub(crate) fn is_root(self) -> bool {
5057
self.0.is_root()
5158
}

src/tools/rust-analyzer/crates/hir-def/src/body/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2460,7 +2460,7 @@ impl ExprCollector<'_> {
24602460
None => HygieneId::ROOT,
24612461
Some(span_map) => {
24622462
let ctx = span_map.span_at(span_start).ctx;
2463-
HygieneId(self.db.lookup_intern_syntax_context(ctx).opaque_and_semitransparent)
2463+
HygieneId::new(self.db.lookup_intern_syntax_context(ctx).opaque_and_semitransparent)
24642464
}
24652465
}
24662466
}

src/tools/rust-analyzer/crates/hir-def/src/expander.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use hir_expand::{
1010
ExpandResult, HirFileId, InFile, Lookup, MacroCallId,
1111
};
1212
use limit::Limit;
13-
use span::SyntaxContextId;
13+
use span::{Edition, SyntaxContextId};
1414
use syntax::{ast, Parse};
1515
use triomphe::Arc;
1616

@@ -60,7 +60,7 @@ impl Expander {
6060

6161
pub fn syntax_context(&self) -> SyntaxContextId {
6262
// FIXME:
63-
SyntaxContextId::ROOT
63+
SyntaxContextId::root(Edition::CURRENT)
6464
}
6565

6666
pub fn enter_expand<T: ast::AstNode>(

src/tools/rust-analyzer/crates/hir-def/src/item_tree/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ m!();
270270
// AstId: 2
271271
pub macro m2 { ... }
272272
273-
// AstId: 3, SyntaxContext: 0, ExpandTo: Items
273+
// AstId: 3, SyntaxContext: 2, ExpandTo: Items
274274
m!(...);
275275
"#]],
276276
);

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mbe.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ macro_rules! f {
3535
};
3636
}
3737
38-
struct#0:[email protected]#1# MyTraitMap2#0:[email protected]#0# {#0:[email protected]#1#
39-
40-
38+
struct#0:[email protected]#4# MyTraitMap2#0:[email protected]#2# {#0:[email protected]#4#
39+
40+
4141
"#]],
4242
);
4343
}
@@ -75,12 +75,12 @@ macro_rules! f {
7575
};
7676
}
7777
78-
79-
80-
81-
82-
83-
78+
79+
80+
81+
82+
83+
8484
8585
8686
"#]],
@@ -171,7 +171,7 @@ fn main(foo: ()) {
171171
}
172172
173173
fn main(foo: ()) {
174-
/* error: unresolved macro unresolved */"helloworld!"#0:[email protected]#0#;
174+
/* error: unresolved macro unresolved */"helloworld!"#0:[email protected]#2#;
175175
}
176176
}
177177
@@ -197,7 +197,7 @@ macro_rules! mk_struct {
197197
#[macro_use]
198198
mod foo;
199199
200-
200+
201201
"#]],
202202
);
203203
}
@@ -423,10 +423,10 @@ m! { foo, bar }
423423
macro_rules! m {
424424
($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
425425
}
426-
impl#\1# Bar#\1# {#\1#
427-
fn#\1# foo#\0#(#\1#)#\1# {#\1#}#\1#
428-
fn#\1# bar#\0#(#\1#)#\1# {#\1#}#\1#
429-
}#\1#
426+
impl#\4# Bar#\4# {#\4#
427+
fn#\4# foo#\2#(#\4#)#\4# {#\4#}#\4#
428+
fn#\4# bar#\2#(#\4#)#\4# {#\4#}#\4#
429+
}#\4#
430430
"#]],
431431
);
432432
}

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
358358
let (parse, _) = syntax_bridge::token_tree_to_syntax_node(
359359
subtree,
360360
syntax_bridge::TopEntryPoint::MacroItems,
361+
&mut |_| span::Edition::CURRENT,
361362
span::Edition::CURRENT,
362363
);
363364
if parse.errors().is_empty() {

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/proc_macros.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ fn foo(&self) {
181181
self.0. 1;
182182
}
183183
184-
185-
186-
}#0:[email protected]#0#"#]],
184+
185+
186+
}#0:[email protected]#2#"#]],
187187
);
188188
}
189189

src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI
7474

7575
let proc_macros = if krate.is_proc_macro {
7676
db.proc_macros()
77-
.for_crate(def_map.krate, db.syntax_context(tree_id.file_id()))
77+
.for_crate(def_map.krate, db.syntax_context(tree_id.file_id(), krate.edition))
7878
.unwrap_or_default()
7979
} else {
8080
Default::default()

src/tools/rust-analyzer/crates/hir-def/src/resolver.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl Resolver {
324324

325325
if n_segments <= 1 {
326326
let mut hygiene_info = if !hygiene_id.is_root() {
327-
let ctx = db.lookup_intern_syntax_context(hygiene_id.0);
327+
let ctx = hygiene_id.lookup(db);
328328
ctx.outer_expn.map(|expansion| {
329329
let expansion = db.lookup_intern_macro_call(expansion);
330330
(ctx.parent, expansion.def)

0 commit comments

Comments
 (0)