Skip to content

internal: Replace Span with SyntaxContextId in MacroCallLoc #16844

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 3 commits into from
Mar 15, 2024
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
4 changes: 2 additions & 2 deletions crates/hir-def/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ impl<'a> AssocItemCollector<'a> {
}
AssocItem::MacroCall(call) => {
let file_id = self.expander.current_file_id();
let MacroCall { ast_id, expand_to, call_site, ref path } = item_tree[call];
let MacroCall { ast_id, expand_to, ctxt, ref path } = item_tree[call];
let module = self.expander.module.local_id;

let resolver = |path| {
Expand All @@ -734,7 +734,7 @@ impl<'a> AssocItemCollector<'a> {
match macro_call_as_call_id(
self.db.upcast(),
&AstIdWithPath::new(file_id, ast_id, Clone::clone(path)),
call_site,
ctxt,
expand_to,
self.expander.module.krate(),
resolver,
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-def/src/item_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use intern::Interned;
use la_arena::{Arena, Idx, IdxRange, RawIdx};
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use span::{AstIdNode, FileAstId, Span};
use span::{AstIdNode, FileAstId, SyntaxContextId};
use stdx::never;
use syntax::{ast, match_ast, SyntaxKind};
use triomphe::Arc;
Expand Down Expand Up @@ -790,7 +790,7 @@ pub struct MacroCall {
pub path: Interned<ModPath>,
pub ast_id: FileAstId<ast::MacroCall>,
pub expand_to: ExpandTo,
pub call_site: Span,
pub ctxt: SyntaxContextId,
}

#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ impl<'a> Ctx<'a> {
})?);
let ast_id = self.source_ast_id_map.ast_id(m);
let expand_to = hir_expand::ExpandTo::from_call_site(m);
let res = MacroCall { path, ast_id, expand_to, call_site: span_map.span_for_range(range) };
let res = MacroCall { path, ast_id, expand_to, ctxt: span_map.span_for_range(range).ctx };
Some(id(self.data().macro_calls.alloc(res)))
}

Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/item_tree/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,12 @@ impl Printer<'_> {
}
}
ModItem::MacroCall(it) => {
let MacroCall { path, ast_id, expand_to, call_site } = &self.tree[it];
let MacroCall { path, ast_id, expand_to, ctxt } = &self.tree[it];
let _ = writeln!(
self,
"// AstId: {:?}, Span: {}, ExpandTo: {:?}",
"// AstId: {:?}, SyntaxContext: {}, ExpandTo: {:?}",
ast_id.erase().into_raw(),
call_site,
ctxt,
expand_to
);
wln!(self, "{}!(...);", path.display(self.db.upcast()));
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 @@ -278,7 +278,7 @@ m!();
// AstId: 2
pub macro m2 { ... }

// AstId: 3, Span: 0:[email protected]#0, ExpandTo: Items
// AstId: 3, SyntaxContext: 0, ExpandTo: Items
m!(...);
"#]],
);
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ use hir_expand::{
use item_tree::ExternBlock;
use la_arena::Idx;
use nameres::DefMap;
use span::{AstIdNode, FileAstId, FileId, Span};
use span::{AstIdNode, FileAstId, FileId, SyntaxContextId};
use stdx::impl_from;
use syntax::{ast, AstNode};

Expand Down Expand Up @@ -1357,7 +1357,7 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
macro_call_as_call_id_with_eager(
db,
&AstIdWithPath::new(ast_id.file_id, ast_id.value, path),
call_site,
call_site.ctx,
expands_to,
krate,
resolver,
Expand All @@ -1382,7 +1382,7 @@ impl<T: AstIdNode> AstIdWithPath<T> {
fn macro_call_as_call_id(
db: &dyn ExpandDatabase,
call: &AstIdWithPath<ast::MacroCall>,
call_site: Span,
call_site: SyntaxContextId,
expand_to: ExpandTo,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<MacroDefId> + Copy,
Expand All @@ -1394,7 +1394,7 @@ fn macro_call_as_call_id(
fn macro_call_as_call_id_with_eager(
db: &dyn ExpandDatabase,
call: &AstIdWithPath<ast::MacroCall>,
call_site: Span,
call_site: SyntaxContextId,
expand_to: ExpandTo,
krate: CrateId,
resolver: impl FnOnce(path::ModPath) -> Option<MacroDefId>,
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-def/src/macro_expansion_tests/mbe/matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ m!(&k");
"#,
expect![[r#"
macro_rules! m { ($i:literal) => {}; }
/* error: mismatched delimiters */"#]],
/* error: expected literal */"#]],
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ macro_rules! m1 { ($x:ident) => { ($x } }
macro_rules! m2 { ($x:ident) => {} }

/* error: macro definition has parse errors */
/* error: mismatched delimiters */
/* error: expected ident */
"#]],
)
}
Expand Down
8 changes: 4 additions & 4 deletions crates/hir-def/src/nameres/attr_resolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use hir_expand::{
attrs::{Attr, AttrId, AttrInput},
MacroCallId, MacroCallKind, MacroDefId,
};
use span::Span;
use span::SyntaxContextId;
use syntax::{ast, SmolStr};
use triomphe::Arc;

Expand Down Expand Up @@ -109,7 +109,7 @@ pub(super) fn attr_macro_as_call_id(
let arg = match macro_attr.input.as_deref() {
Some(AttrInput::TokenTree(tt)) => {
let mut tt = tt.as_ref().clone();
tt.delimiter = tt::Delimiter::invisible_spanned(macro_attr.span);
tt.delimiter.kind = tt::DelimiterKind::Invisible;
Some(tt)
}

Expand All @@ -124,7 +124,7 @@ pub(super) fn attr_macro_as_call_id(
attr_args: arg.map(Arc::new),
invoc_attr_index: macro_attr.id,
},
macro_attr.span,
macro_attr.ctxt,
)
}

Expand All @@ -133,7 +133,7 @@ pub(super) fn derive_macro_as_call_id(
item_attr: &AstIdWithPath<ast::Adt>,
derive_attr_index: AttrId,
derive_pos: u32,
call_site: Span,
call_site: SyntaxContextId,
krate: CrateId,
resolver: impl Fn(path::ModPath) -> Option<(MacroId, MacroDefId)>,
) -> Result<(MacroId, MacroDefId, MacroCallId), UnresolvedMacro> {
Expand Down
20 changes: 10 additions & 10 deletions crates/hir-def/src/nameres/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ enum MacroDirectiveKind {
FnLike {
ast_id: AstIdWithPath<ast::MacroCall>,
expand_to: ExpandTo,
call_site: Span,
ctxt: SyntaxContextId,
},
Derive {
ast_id: AstIdWithPath<ast::Adt>,
derive_attr: AttrId,
derive_pos: usize,
call_site: Span,
ctxt: SyntaxContextId,
},
Attr {
ast_id: AstIdWithPath<ast::Item>,
Expand Down Expand Up @@ -1126,7 +1126,7 @@ impl DefCollector<'_> {
let resolver_def_id = |path| resolver(path).map(|(_, it)| it);

match &directive.kind {
MacroDirectiveKind::FnLike { ast_id, expand_to, call_site } => {
MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt: call_site } => {
let call_id = macro_call_as_call_id(
self.db.upcast(),
ast_id,
Expand All @@ -1146,7 +1146,7 @@ impl DefCollector<'_> {
return Resolved::Yes;
}
}
MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, call_site } => {
MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, ctxt: call_site } => {
let id = derive_macro_as_call_id(
self.db,
ast_id,
Expand Down Expand Up @@ -1266,7 +1266,7 @@ impl DefCollector<'_> {
ast_id,
derive_attr: attr.id,
derive_pos: idx,
call_site,
ctxt: call_site.ctx,
},
container: directive.container,
});
Expand Down Expand Up @@ -1428,7 +1428,7 @@ impl DefCollector<'_> {

for directive in &self.unresolved_macros {
match &directive.kind {
MacroDirectiveKind::FnLike { ast_id, expand_to, call_site } => {
MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt: call_site } => {
// FIXME: we shouldn't need to re-resolve the macro here just to get the unresolved error!
let macro_call_as_call_id = macro_call_as_call_id(
self.db.upcast(),
Expand Down Expand Up @@ -1460,7 +1460,7 @@ impl DefCollector<'_> {
));
}
}
MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, call_site: _ } => {
MacroDirectiveKind::Derive { ast_id, derive_attr, derive_pos, ctxt: _ } => {
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
directive.module_id,
MacroCallKind::Derive {
Expand Down Expand Up @@ -2289,7 +2289,7 @@ impl ModCollector<'_, '_> {

fn collect_macro_call(
&mut self,
&MacroCall { ref path, ast_id, expand_to, call_site }: &MacroCall,
&MacroCall { ref path, ast_id, expand_to, ctxt }: &MacroCall,
container: ItemContainerId,
) {
let ast_id = AstIdWithPath::new(self.file_id(), ast_id, ModPath::clone(path));
Expand All @@ -2303,7 +2303,7 @@ impl ModCollector<'_, '_> {
if let Ok(res) = macro_call_as_call_id_with_eager(
db.upcast(),
&ast_id,
call_site,
ctxt,
expand_to,
self.def_collector.def_map.krate,
|path| {
Expand Down Expand Up @@ -2361,7 +2361,7 @@ impl ModCollector<'_, '_> {
self.def_collector.unresolved_macros.push(MacroDirective {
module_id: self.module_id,
depth: self.macro_depth + 1,
kind: MacroDirectiveKind::FnLike { ast_id, expand_to, call_site },
kind: MacroDirectiveKind::FnLike { ast_id, expand_to, ctxt },
container,
});
}
Expand Down
12 changes: 6 additions & 6 deletions crates/hir-expand/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use either::Either;
use intern::Interned;
use mbe::{syntax_node_to_token_tree, DelimiterKind, Punct};
use smallvec::{smallvec, SmallVec};
use span::Span;
use span::{Span, SyntaxContextId};
use syntax::{ast, match_ast, AstNode, AstToken, SmolStr, SyntaxNode};
use triomphe::Arc;

Expand Down Expand Up @@ -53,7 +53,7 @@ impl RawAttrs {
id,
input: Some(Interned::new(AttrInput::Literal(SmolStr::new(doc)))),
path: Interned::new(ModPath::from(crate::name!(doc))),
span: span_map.span_for_range(comment.syntax().text_range()),
ctxt: span_map.span_for_range(comment.syntax().text_range()).ctx,
}),
});
let entries: Arc<[Attr]> = Arc::from_iter(entries);
Expand Down Expand Up @@ -173,7 +173,7 @@ pub struct Attr {
pub id: AttrId,
pub path: Interned<ModPath>,
pub input: Option<Interned<AttrInput>>,
pub span: Span,
pub ctxt: SyntaxContextId,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -219,11 +219,11 @@ impl Attr {
} else {
None
};
Some(Attr { id, path, input, span })
Some(Attr { id, path, input, ctxt: span.ctx })
}

fn from_tt(db: &dyn ExpandDatabase, tt: &[tt::TokenTree], id: AttrId) -> Option<Attr> {
let span = tt.first()?.first_span();
let ctxt = tt.first()?.first_span().ctx;
let path_end = tt
.iter()
.position(|tt| {
Expand Down Expand Up @@ -255,7 +255,7 @@ impl Attr {
}
_ => None,
};
Some(Attr { id, path, input, span })
Some(Attr { id, path, input, ctxt })
}

pub fn path(&self) -> &ModPath {
Expand Down
14 changes: 7 additions & 7 deletions crates/hir-expand/src/builtin_attr_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ macro_rules! register_builtin {
}

impl BuiltinAttrExpander {
pub fn expander(&self) -> fn (&dyn ExpandDatabase, MacroCallId, &tt::Subtree) -> ExpandResult<tt::Subtree> {
pub fn expander(&self) -> fn (&dyn ExpandDatabase, MacroCallId, &tt::Subtree, Span) -> ExpandResult<tt::Subtree> {
match *self {
$( BuiltinAttrExpander::$variant => $expand, )*
}
Expand All @@ -34,8 +34,9 @@ impl BuiltinAttrExpander {
db: &dyn ExpandDatabase,
id: MacroCallId,
tt: &tt::Subtree,
span: Span,
) -> ExpandResult<tt::Subtree> {
self.expander()(db, id, tt)
self.expander()(db, id, tt, span)
}

pub fn is_derive(self) -> bool {
Expand Down Expand Up @@ -71,6 +72,7 @@ fn dummy_attr_expand(
_db: &dyn ExpandDatabase,
_id: MacroCallId,
tt: &tt::Subtree,
_span: Span,
) -> ExpandResult<tt::Subtree> {
ExpandResult::ok(tt.clone())
}
Expand Down Expand Up @@ -100,20 +102,18 @@ fn derive_expand(
db: &dyn ExpandDatabase,
id: MacroCallId,
tt: &tt::Subtree,
span: Span,
) -> ExpandResult<tt::Subtree> {
let loc = db.lookup_intern_macro_call(id);
let derives = match &loc.kind {
MacroCallKind::Attr { attr_args: Some(attr_args), .. } if loc.def.is_attribute_derive() => {
attr_args
}
_ => {
return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan {
open: loc.call_site,
close: loc.call_site,
}))
return ExpandResult::ok(tt::Subtree::empty(tt::DelimSpan { open: span, close: span }))
}
};
pseudo_derive_attr_expansion(tt, derives, loc.call_site)
pseudo_derive_attr_expansion(tt, derives, span)
}

pub fn pseudo_derive_attr_expansion(
Expand Down
2 changes: 1 addition & 1 deletion crates/hir-expand/src/builtin_derive_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ impl BuiltinDeriveExpander {
db: &dyn ExpandDatabase,
id: MacroCallId,
tt: &tt::Subtree,
span: Span,
) -> ExpandResult<tt::Subtree> {
let span = db.lookup_intern_macro_call(id).call_site;
let span = span_with_def_site_ctxt(db, span, id);
self.expander()(span, tt)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-expand/src/builtin_fn_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ impl BuiltinFnLikeExpander {
db: &dyn ExpandDatabase,
id: MacroCallId,
tt: &tt::Subtree,
span: Span,
) -> ExpandResult<tt::Subtree> {
let span = db.lookup_intern_macro_call(id).call_site;
let span = span_with_def_site_ctxt(db, span, id);
self.expander()(db, id, tt, span)
}
Expand All @@ -75,8 +75,8 @@ impl EagerExpander {
db: &dyn ExpandDatabase,
id: MacroCallId,
tt: &tt::Subtree,
span: Span,
) -> ExpandResult<tt::Subtree> {
let span = db.lookup_intern_macro_call(id).call_site;
let span = span_with_def_site_ctxt(db, span, id);
self.expander()(db, id, tt, span)
}
Expand Down
Loading