Skip to content

refactor: Remove unnecessary extension trait #19714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions crates/hir-expand/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ use crate::{
cfg_process,
declarative::DeclarativeMacroExpander,
fixup::{self, SyntaxFixupUndoInfo},
hygiene::{
SyntaxContextExt as _, span_with_call_site_ctxt, span_with_def_site_ctxt,
span_with_mixed_site_ctxt,
},
hygiene::{span_with_call_site_ctxt, span_with_def_site_ctxt, span_with_mixed_site_ctxt},
proc_macro::{CrateProcMacros, CustomProcMacroExpander, ProcMacros},
span_map::{ExpansionSpanMap, RealSpanMap, SpanMap, SpanMapRef},
tt,
Expand Down
60 changes: 1 addition & 59 deletions crates/hir-expand/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
// FIXME: Move this into the span crate? Not quite possible today as that depends on `MacroCallLoc`
// which contains a bunch of unrelated things

use std::{convert::identity, iter};
use std::convert::identity;

use span::{Edition, MacroCallId, Span, SyntaxContext};

Expand Down Expand Up @@ -141,61 +141,3 @@ fn apply_mark_internal(
|_| opaque_and_semitransparent,
)
}

pub trait SyntaxContextExt {
fn normalize_to_macro_rules(self, db: &dyn ExpandDatabase) -> span::SyntaxContext;
fn normalize_to_macros_2_0(self, db: &dyn ExpandDatabase) -> span::SyntaxContext;
fn parent_ctxt(self, db: &dyn ExpandDatabase) -> span::SyntaxContext;
fn remove_mark(&mut self, db: &dyn ExpandDatabase)
-> (Option<span::MacroCallId>, Transparency);
fn outer_mark(self, db: &dyn ExpandDatabase) -> (Option<span::MacroCallId>, Transparency);
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(span::MacroCallId, Transparency)>;
fn is_opaque(self, db: &dyn ExpandDatabase) -> bool;
}

impl SyntaxContextExt for SyntaxContext {
fn normalize_to_macro_rules(self, db: &dyn ExpandDatabase) -> span::SyntaxContext {
self.opaque_and_semitransparent(db)
}
fn normalize_to_macros_2_0(self, db: &dyn ExpandDatabase) -> span::SyntaxContext {
self.opaque(db)
}
fn parent_ctxt(self, db: &dyn ExpandDatabase) -> span::SyntaxContext {
self.parent(db)
}
fn outer_mark(self, db: &dyn ExpandDatabase) -> (Option<span::MacroCallId>, Transparency) {
let data = self;
(data.outer_expn(db), data.outer_transparency(db))
}
fn remove_mark(
&mut self,
db: &dyn ExpandDatabase,
) -> (Option<span::MacroCallId>, Transparency) {
let data = *self;
*self = data.parent(db);
(data.outer_expn(db), data.outer_transparency(db))
}
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(span::MacroCallId, Transparency)> {
let mut marks = marks_rev(self, db).collect::<Vec<_>>();
marks.reverse();
marks
}
fn is_opaque(self, db: &dyn ExpandDatabase) -> bool {
!self.is_root() && self.outer_transparency(db).is_opaque()
}
}

// FIXME: Make this a SyntaxContextExt method once we have RPIT
pub fn marks_rev(
ctxt: SyntaxContext,
db: &dyn ExpandDatabase,
) -> impl Iterator<Item = (span::MacroCallId, Transparency)> + '_ {
iter::successors(Some(ctxt), move |&mark| Some(mark.parent_ctxt(db)))
.take_while(|&it| !it.is_root())
.map(|ctx| {
let mark = ctx.outer_mark(db);
// We stop before taking the root expansion, as such we cannot encounter a `None` outer
// expansion, as only the ROOT has it.
(mark.0.unwrap(), mark.1)
})
}
4 changes: 2 additions & 2 deletions crates/hir-expand/src/mod_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{

use crate::{
db::ExpandDatabase,
hygiene::{SyntaxContextExt, Transparency, marks_rev},
hygiene::Transparency,
name::{AsName, Name},
tt,
};
Expand Down Expand Up @@ -340,7 +340,7 @@ pub fn resolve_crate_root(db: &dyn ExpandDatabase, mut ctxt: SyntaxContext) -> O
// definitions actually produced by `macro` and `macro` definitions produced by
// `macro_rules!`, but at least such configurations are not stable yet.
ctxt = ctxt.normalize_to_macro_rules(db);
let mut iter = marks_rev(ctxt, db).peekable();
let mut iter = ctxt.marks_rev(db).peekable();
let mut result_mark = None;
// Find the last opaque mark from the end if it exists.
while let Some(&(mark, Transparency::Opaque)) = iter.peek() {
Expand Down
1 change: 0 additions & 1 deletion crates/hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ pub use {
HirFileRange, InFile, InFileWrapper, InMacroFile, InRealFile, MacroFilePosition,
MacroFileRange,
},
hygiene::{SyntaxContextExt, marks_rev},
inert_attr_macro::AttributeTemplate,
mod_path::{ModPath, PathKind, tool_path},
name::Name,
Expand Down
1 change: 0 additions & 1 deletion crates/hir/src/semantics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ use hir_expand::{
builtin::{BuiltinFnLikeExpander, EagerExpander},
db::ExpandDatabase,
files::{FileRangeWrapper, InRealFile},
hygiene::SyntaxContextExt as _,
inert_attr_macro::find_builtin_attr_idx,
mod_path::{ModPath, PathKind},
name::AsName,
Expand Down
56 changes: 55 additions & 1 deletion crates/span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ impl SyntaxContext {
}

#[cfg(feature = "salsa")]
impl SyntaxContext {
impl<'db> SyntaxContext {
const MAX_ID: u32 = salsa::Id::MAX_U32 - 1;

#[inline]
Expand Down Expand Up @@ -340,6 +340,60 @@ impl SyntaxContext {
// SAFETY: This comes from a Salsa ID.
unsafe { Self::from_u32(id.as_u32()) }
}

#[inline]
pub fn outer_mark(
self,
db: &'db dyn salsa::Database,
) -> (Option<crate::MacroCallId>, Transparency) {
(self.outer_expn(db), self.outer_transparency(db))
}

#[inline]
pub fn normalize_to_macros_2_0(self, db: &'db dyn salsa::Database) -> SyntaxContext {
self.opaque(db)
}

#[inline]
pub fn normalize_to_macro_rules(self, db: &'db dyn salsa::Database) -> SyntaxContext {
self.opaque_and_semitransparent(db)
}

pub fn is_opaque(self, db: &'db dyn salsa::Database) -> bool {
!self.is_root() && self.outer_transparency(db).is_opaque()
}

pub fn remove_mark(
&mut self,
db: &'db dyn salsa::Database,
) -> (Option<crate::MacroCallId>, Transparency) {
let data = *self;
*self = data.parent(db);
(data.outer_expn(db), data.outer_transparency(db))
}

pub fn marks(
self,
db: &'db dyn salsa::Database,
) -> impl Iterator<Item = (crate::MacroCallId, Transparency)> {
let mut marks = self.marks_rev(db).collect::<Vec<_>>();
marks.reverse();
marks.into_iter()
}

pub fn marks_rev(
self,
db: &'db dyn salsa::Database,
) -> impl Iterator<Item = (crate::MacroCallId, Transparency)> {
std::iter::successors(Some(self), move |&mark| Some(mark.parent(db)))
.take_while(|&it| !it.is_root())
.map(|ctx| {
let mark = ctx.outer_mark(db);
// We stop before taking the root expansion, as such we cannot encounter a `None` outer
// expansion, as only the ROOT has it.
(mark.0.unwrap(), mark.1)
})
}
}
#[cfg(not(feature = "salsa"))]
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down