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

Commit 8a5bb9d

Browse files
committed
Only the ROOT syntax context has None outer_expn
1 parent eb7a4f2 commit 8a5bb9d

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

crates/hir-expand/src/hygiene.rs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(super) fn apply_mark(
6262
transparency: Transparency,
6363
) -> SyntaxContextId {
6464
if transparency == Transparency::Opaque {
65-
return apply_mark_internal(db, ctxt, Some(call_id), transparency);
65+
return apply_mark_internal(db, ctxt, call_id, transparency);
6666
}
6767

6868
let call_site_ctxt = db.lookup_intern_macro_call(call_id).call_site.ctx;
@@ -73,7 +73,7 @@ pub(super) fn apply_mark(
7373
};
7474

7575
if call_site_ctxt.is_root() {
76-
return apply_mark_internal(db, ctxt, Some(call_id), transparency);
76+
return apply_mark_internal(db, ctxt, call_id, transparency);
7777
}
7878

7979
// Otherwise, `expn_id` is a macros 1.0 definition and the call site is in a
@@ -88,17 +88,19 @@ pub(super) fn apply_mark(
8888
for (call_id, transparency) in ctxt.marks(db) {
8989
call_site_ctxt = apply_mark_internal(db, call_site_ctxt, call_id, transparency);
9090
}
91-
apply_mark_internal(db, call_site_ctxt, Some(call_id), transparency)
91+
apply_mark_internal(db, call_site_ctxt, call_id, transparency)
9292
}
9393

9494
fn apply_mark_internal(
9595
db: &dyn ExpandDatabase,
9696
ctxt: SyntaxContextId,
97-
call_id: Option<MacroCallId>,
97+
call_id: MacroCallId,
9898
transparency: Transparency,
9999
) -> SyntaxContextId {
100100
use base_db::salsa;
101101

102+
let call_id = Some(call_id);
103+
102104
let syntax_context_data = db.lookup_intern_syntax_context(ctxt);
103105
let mut opaque = syntax_context_data.opaque;
104106
let mut opaque_and_semitransparent = syntax_context_data.opaque_and_semitransparent;
@@ -148,7 +150,7 @@ pub trait SyntaxContextExt {
148150
fn parent_ctxt(self, db: &dyn ExpandDatabase) -> Self;
149151
fn remove_mark(&mut self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparency);
150152
fn outer_mark(self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparency);
151-
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(Option<MacroCallId>, Transparency)>;
153+
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(MacroCallId, Transparency)>;
152154
}
153155

154156
impl SyntaxContextExt for SyntaxContextId {
@@ -170,7 +172,7 @@ impl SyntaxContextExt for SyntaxContextId {
170172
*self = data.parent;
171173
(data.outer_expn, data.outer_transparency)
172174
}
173-
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(Option<MacroCallId>, Transparency)> {
175+
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(MacroCallId, Transparency)> {
174176
let mut marks = marks_rev(self, db).collect::<Vec<_>>();
175177
marks.reverse();
176178
marks
@@ -181,11 +183,15 @@ impl SyntaxContextExt for SyntaxContextId {
181183
pub fn marks_rev(
182184
ctxt: SyntaxContextId,
183185
db: &dyn ExpandDatabase,
184-
) -> impl Iterator<Item = (Option<MacroCallId>, Transparency)> + '_ {
185-
iter::successors(Some(ctxt), move |&mark| {
186-
Some(mark.parent_ctxt(db)).filter(|&it| it != SyntaxContextId::ROOT)
187-
})
188-
.map(|ctx| ctx.outer_mark(db))
186+
) -> impl Iterator<Item = (MacroCallId, Transparency)> + '_ {
187+
iter::successors(Some(ctxt), move |&mark| Some(mark.parent_ctxt(db)))
188+
.take_while(|&it| !it.is_root())
189+
.map(|ctx| {
190+
let mark = ctx.outer_mark(db);
191+
// We stop before taking the root expansion, as such we cannot encounter a `None` outer
192+
// expansion, as only the ROOT has it.
193+
(mark.0.unwrap(), mark.1)
194+
})
189195
}
190196

191197
pub(crate) fn dump_syntax_contexts(db: &dyn ExpandDatabase) -> String {
@@ -224,7 +230,7 @@ pub(crate) fn dump_syntax_contexts(db: &dyn ExpandDatabase) -> String {
224230
}
225231
}
226232

227-
pub fn fancy_debug(
233+
fn fancy_debug(
228234
this: &SyntaxContextData,
229235
self_id: SyntaxContextId,
230236
db: &dyn ExpandDatabase,

crates/hir-expand/src/mod_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ pub fn resolve_crate_root(db: &dyn ExpandDatabase, mut ctxt: SyntaxContextId) ->
358358
result_mark = Some(mark);
359359
}
360360

361-
result_mark.flatten().map(|call| db.lookup_intern_macro_call(call).def.krate)
361+
result_mark.map(|call| db.lookup_intern_macro_call(call).def.krate)
362362
}
363363

364364
pub use crate::name as __name;

crates/span/src/hygiene.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl SyntaxContextId {
6868
/// A syntax context describes a hierarchy tracking order of macro definitions.
6969
#[derive(Copy, Clone, Hash, PartialEq, Eq)]
7070
pub struct SyntaxContextData {
71+
/// Invariant: Only [`SyntaxContextId::ROOT`] has a [`None`] outer expansion.
7172
pub outer_expn: Option<MacroCallId>,
7273
pub outer_transparency: Transparency,
7374
pub parent: SyntaxContextId,

0 commit comments

Comments
 (0)