Skip to content

Commit

Permalink
Make edition per-token, not per-file
Browse files Browse the repository at this point in the history
More correctly, *also* per-token. Because as it turns out, while the top-level edition affects parsing (I think), the per-token edition affects escaping of identifiers/keywords.
  • Loading branch information
ChayimFriedman2 committed Jan 9, 2025
1 parent 3f2bbe9 commit 97afb7b
Show file tree
Hide file tree
Showing 34 changed files with 480 additions and 316 deletions.
3 changes: 2 additions & 1 deletion crates/edition/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum Edition {
Edition2015,
// The syntax context stuff needs the discriminants to start from 0 and be consecutive.
Edition2015 = 0,
Edition2018,
Edition2021,
Edition2024,
Expand Down
15 changes: 11 additions & 4 deletions crates/hir-def/src/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use hir_expand::{name::Name, ExpandError, InFile};
use la_arena::{Arena, ArenaMap, Idx, RawIdx};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use span::{Edition, MacroFileId};
use span::{Edition, MacroFileId, SyntaxContextData};
use syntax::{ast, AstPtr, SyntaxNodePtr};
use triomphe::Arc;
use tt::TextRange;
Expand All @@ -37,15 +37,22 @@ use crate::{

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

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

pub fn new(ctx: span::SyntaxContextId) -> Self {
pub fn new(mut ctx: span::SyntaxContextId) -> Self {
// See `Name` for why we're doing that.
ctx.remove_root_edition();
Self(ctx)
}

pub(crate) fn lookup(self, db: &dyn DefDatabase) -> SyntaxContextData {
db.lookup_intern_syntax_context(self.0)
}

pub(crate) fn is_root(self) -> bool {
self.0.is_root()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/body/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2460,7 +2460,7 @@ impl ExprCollector<'_> {
None => HygieneId::ROOT,
Some(span_map) => {
let ctx = span_map.span_at(span_start).ctx;
HygieneId(self.db.lookup_intern_syntax_context(ctx).opaque_and_semitransparent)
HygieneId::new(self.db.lookup_intern_syntax_context(ctx).opaque_and_semitransparent)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use hir_expand::{
ExpandResult, HirFileId, InFile, Lookup, MacroCallId,
};
use limit::Limit;
use span::SyntaxContextId;
use span::{Edition, SyntaxContextId};
use syntax::{ast, Parse};
use triomphe::Arc;

Expand Down Expand Up @@ -60,7 +60,7 @@ impl Expander {

pub fn syntax_context(&self) -> SyntaxContextId {
// FIXME:
SyntaxContextId::ROOT
SyntaxContextId::root(Edition::CURRENT)
}

pub fn enter_expand<T: ast::AstNode>(
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/item_tree/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ m!();
// AstId: 2
pub macro m2 { ... }
// AstId: 3, SyntaxContext: 0, ExpandTo: Items
// AstId: 3, SyntaxContext: 2, ExpandTo: Items
m!(...);
"#]],
);
Expand Down
30 changes: 15 additions & 15 deletions crates/hir-def/src/macro_expansion_tests/mbe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ macro_rules! f {
};
}
struct#0:[email protected]#1# MyTraitMap2#0:[email protected]#0# {#0:[email protected]#1#
map#0:[email protected]#1#:#0:[email protected]#1# #0:[email protected]#1#::#0:[email protected]#1#std#0:[email protected]#1#::#0:[email protected]#1#collections#0:[email protected]#1#::#0:[email protected]#1#HashSet#0:[email protected]#1#<#0:[email protected]#1#(#0:[email protected]#1#)#0:[email protected]#1#>#0:[email protected]#1#,#0:[email protected]#1#
}#0:[email protected]#1#
struct#0:[email protected]#4# MyTraitMap2#0:[email protected]#2# {#0:[email protected]#4#
map#0:[email protected]#4#:#0:[email protected]#4# #0:[email protected]#4#::#0:[email protected]#4#std#0:[email protected]#4#::#0:[email protected]#4#collections#0:[email protected]#4#::#0:[email protected]#4#HashSet#0:[email protected]#4#<#0:[email protected]#4#(#0:[email protected]#4#)#0:[email protected]#4#>#0:[email protected]#4#,#0:[email protected]#4#
}#0:[email protected]#4#
"#]],
);
}
Expand Down Expand Up @@ -75,12 +75,12 @@ macro_rules! f {
};
}
fn#0:[email protected]#0# main#0:[email protected]#0#(#0:[email protected]#0#)#0:[email protected]#0# {#0:[email protected]#0#
1#0:[email protected]#0#;#0:[email protected]#0#
1.0#0:[email protected]#0#;#0:[email protected]#0#
(#0:[email protected]#0#(#0:[email protected]#0#1#0:[email protected]#0#,#0:[email protected]#0# )#0:[email protected]#0#,#0:[email protected]#0# )#0:[email protected]#0#.#0:[email protected]#0#0#0:[email protected]#0#.#0:[email protected]#0#0#0:[email protected]#0#;#0:[email protected]#0#
let#0:[email protected]#0# x#0:[email protected]#0# =#0:[email protected]#0# 1#0:[email protected]#0#;#0:[email protected]#0#
}#0:[email protected]#0#
fn#0:[email protected]#2# main#0:[email protected]#2#(#0:[email protected]#2#)#0:[email protected]#2# {#0:[email protected]#2#
1#0:[email protected]#2#;#0:[email protected]#2#
1.0#0:[email protected]#2#;#0:[email protected]#2#
(#0:[email protected]#2#(#0:[email protected]#2#1#0:[email protected]#2#,#0:[email protected]#2# )#0:[email protected]#2#,#0:[email protected]#2# )#0:[email protected]#2#.#0:[email protected]#2#0#0:[email protected]#2#.#0:[email protected]#2#0#0:[email protected]#2#;#0:[email protected]#2#
let#0:[email protected]#2# x#0:[email protected]#2# =#0:[email protected]#2# 1#0:[email protected]#2#;#0:[email protected]#2#
}#0:[email protected]#2#
"#]],
Expand Down Expand Up @@ -171,7 +171,7 @@ fn main(foo: ()) {
}
fn main(foo: ()) {
/* error: unresolved macro unresolved */"helloworld!"#0:[email protected]#0#;
/* error: unresolved macro unresolved */"helloworld!"#0:[email protected]#2#;
}
}
Expand All @@ -197,7 +197,7 @@ macro_rules! mk_struct {
#[macro_use]
mod foo;
struct#1:[email protected]#1# Foo#0:[email protected]#0#(#1:[email protected]#1#u32#0:[email protected]#0#)#1:[email protected]#1#;#1:[email protected]#1#
struct#1:[email protected]#4# Foo#0:[email protected]#2#(#1:[email protected]#4#u32#0:[email protected]#2#)#1:[email protected]#4#;#1:[email protected]#4#
"#]],
);
}
Expand Down Expand Up @@ -423,10 +423,10 @@ m! { foo, bar }
macro_rules! m {
($($i:ident),*) => ( impl Bar { $(fn $i() {})* } );
}
impl#\1# Bar#\1# {#\1#
fn#\1# foo#\0#(#\1#)#\1# {#\1#}#\1#
fn#\1# bar#\0#(#\1#)#\1# {#\1#}#\1#
}#\1#
impl#\4# Bar#\4# {#\4#
fn#\4# foo#\2#(#\4#)#\4# {#\4#}#\4#
fn#\4# bar#\2#(#\4#)#\4# {#\4#}#\4#
}#\4#
"#]],
);
}
Expand Down
1 change: 1 addition & 0 deletions crates/hir-def/src/macro_expansion_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ impl ProcMacroExpander for IdentityWhenValidProcMacroExpander {
let (parse, _) = syntax_bridge::token_tree_to_syntax_node(
subtree,
syntax_bridge::TopEntryPoint::MacroItems,
&mut |_| span::Edition::CURRENT,
span::Edition::CURRENT,
);
if parse.errors().is_empty() {
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/macro_expansion_tests/proc_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ fn foo(&self) {
self.0. 1;
}
fn#0:[email protected]#0# foo#0:[email protected]#0#(#0:[email protected]#0#&#0:[email protected]#0#self#0:[email protected]#0# )#0:[email protected]#0# {#0:[email protected]#0#
self#0:[email protected]#0# .#0:[email protected]#0#0#0:[email protected]#0#.#0:[email protected]#0#1#0:[email protected]#0#;#0:[email protected]#0#
}#0:[email protected]#0#"#]],
fn#0:[email protected]#2# foo#0:[email protected]#2#(#0:[email protected]#2#&#0:[email protected]#2#self#0:[email protected]#2# )#0:[email protected]#2# {#0:[email protected]#2#
self#0:[email protected]#2# .#0:[email protected]#2#0#0:[email protected]#2#.#0:[email protected]#2#1#0:[email protected]#2#;#0:[email protected]#2#
}#0:[email protected]#2#"#]],
);
}

Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub(super) fn collect_defs(db: &dyn DefDatabase, def_map: DefMap, tree_id: TreeI

let proc_macros = if krate.is_proc_macro {
db.proc_macros()
.for_crate(def_map.krate, db.syntax_context(tree_id.file_id()))
.for_crate(def_map.krate, db.syntax_context(tree_id.file_id(), krate.edition))
.unwrap_or_default()
} else {
Default::default()
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ impl Resolver {

if n_segments <= 1 {
let mut hygiene_info = if !hygiene_id.is_root() {
let ctx = db.lookup_intern_syntax_context(hygiene_id.0);
let ctx = hygiene_id.lookup(db);
ctx.outer_expn.map(|expansion| {
let expansion = db.lookup_intern_macro_call(expansion);
(ctx.parent, expansion.def)
Expand Down
Loading

0 comments on commit 97afb7b

Please sign in to comment.