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:1@58..64#1# MyTraitMap2#0:2@31..42#0# {#0:1@72..73#1#
39-
map#0:1@86..89#1#:#0:1@89..90#1# #0:1@89..90#1#::#0:1@91..93#1#std#0:1@93..96#1#::#0:1@96..98#1#collections#0:1@98..109#1#::#0:1@109..111#1#HashSet#0:1@111..118#1#<#0:1@118..119#1#(#0:1@119..120#1#)#0:1@120..121#1#>#0:1@121..122#1#,#0:1@122..123#1#
40-
}#0:1@132..133#1#
38+
struct#0:1@58..64#4# MyTraitMap2#0:2@31..42#2# {#0:1@72..73#4#
39+
map#0:1@86..89#4#:#0:1@89..90#4# #0:1@89..90#4#::#0:1@91..93#4#std#0:1@93..96#4#::#0:1@96..98#4#collections#0:1@98..109#4#::#0:1@109..111#4#HashSet#0:1@111..118#4#<#0:1@118..119#4#(#0:1@119..120#4#)#0:1@120..121#4#>#0:1@121..122#4#,#0:1@122..123#4#
40+
}#0:1@132..133#4#
4141
"#]],
4242
);
4343
}
@@ -75,12 +75,12 @@ macro_rules! f {
7575
};
7676
}
7777
78-
fn#0:2@30..32#0# main#0:2@33..37#0#(#0:2@37..38#0#)#0:2@38..39#0# {#0:2@40..41#0#
79-
1#0:2@50..51#0#;#0:2@51..52#0#
80-
1.0#0:2@61..64#0#;#0:2@64..65#0#
81-
(#0:2@74..75#0#(#0:2@75..76#0#1#0:2@76..77#0#,#0:2@77..78#0# )#0:2@78..79#0#,#0:2@79..80#0# )#0:2@80..81#0#.#0:2@81..82#0#0#0:2@82..85#0#.#0:2@82..85#0#0#0:2@82..85#0#;#0:2@85..86#0#
82-
let#0:2@95..98#0# x#0:2@99..100#0# =#0:2@101..102#0# 1#0:2@103..104#0#;#0:2@104..105#0#
83-
}#0:2@110..111#0#
78+
fn#0:2@30..32#2# main#0:2@33..37#2#(#0:2@37..38#2#)#0:2@38..39#2# {#0:2@40..41#2#
79+
1#0:2@50..51#2#;#0:2@51..52#2#
80+
1.0#0:2@61..64#2#;#0:2@64..65#2#
81+
(#0:2@74..75#2#(#0:2@75..76#2#1#0:2@76..77#2#,#0:2@77..78#2# )#0:2@78..79#2#,#0:2@79..80#2# )#0:2@80..81#2#.#0:2@81..82#2#0#0:2@82..85#2#.#0:2@82..85#2#0#0:2@82..85#2#;#0:2@85..86#2#
82+
let#0:2@95..98#2# x#0:2@99..100#2# =#0:2@101..102#2# 1#0:2@103..104#2#;#0:2@104..105#2#
83+
}#0:2@110..111#2#
8484
8585
8686
"#]],
@@ -171,7 +171,7 @@ fn main(foo: ()) {
171171
}
172172
173173
fn main(foo: ()) {
174-
/* error: unresolved macro unresolved */"helloworld!"#0:3@236..321#0#;
174+
/* error: unresolved macro unresolved */"helloworld!"#0:3@236..321#2#;
175175
}
176176
}
177177
@@ -197,7 +197,7 @@ macro_rules! mk_struct {
197197
#[macro_use]
198198
mod foo;
199199
200-
struct#1:1@59..65#1# Foo#0:2@32..35#0#(#1:1@70..71#1#u32#0:2@41..44#0#)#1:1@74..75#1#;#1:1@75..76#1#
200+
struct#1:1@59..65#4# Foo#0:2@32..35#2#(#1:1@70..71#4#u32#0:2@41..44#2#)#1:1@74..75#4#;#1:1@75..76#4#
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-
fn#0:1@45..47#0# foo#0:1@48..51#0#(#0:1@51..52#0#&#0:1@52..53#0#self#0:1@53..57#0# )#0:1@57..58#0# {#0:1@59..60#0#
185-
self#0:1@65..69#0# .#0:1@69..70#0#0#0:1@70..71#0#.#0:1@71..72#0#1#0:1@73..74#0#;#0:1@74..75#0#
186-
}#0:1@76..77#0#"#]],
184+
fn#0:1@45..47#2# foo#0:1@48..51#2#(#0:1@51..52#2#&#0:1@52..53#2#self#0:1@53..57#2# )#0:1@57..58#2# {#0:1@59..60#2#
185+
self#0:1@65..69#2# .#0:1@69..70#2#0#0:1@70..71#2#.#0:1@71..72#2#1#0:1@73..74#2#;#0:1@74..75#2#
186+
}#0:1@76..77#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)