1
Fork 0

expand: Stop using artificial ast::Item for macros loaded from metadata

This commit is contained in:
Vadim Petrochenkov 2024-10-26 18:51:15 +03:00
parent 9260be36b2
commit b93a2dd0ef
8 changed files with 94 additions and 120 deletions

View file

@ -177,7 +177,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let loaded_macro = self.cstore().load_macro_untracked(def_id, self.tcx);
let macro_data = match loaded_macro {
LoadedMacro::MacroDef(item, edition) => self.compile_macro(&item, edition),
LoadedMacro::MacroDef { def, ident, attrs, span, edition } => {
self.compile_macro(&def, ident, &attrs, span, ast::DUMMY_NODE_ID, edition)
}
LoadedMacro::ProcMacro(ext) => MacroData::new(Lrc::new(ext)),
};

View file

@ -199,8 +199,10 @@ impl<'a, 'ra, 'tcx> visit::Visitor<'a> for DefCollector<'a, 'ra, 'tcx> {
},
ItemKind::Const(..) => DefKind::Const,
ItemKind::Fn(..) | ItemKind::Delegation(..) => DefKind::Fn,
ItemKind::MacroDef(..) => {
let macro_data = self.resolver.compile_macro(i, self.resolver.tcx.sess.edition());
ItemKind::MacroDef(def) => {
let edition = self.resolver.tcx.sess.edition();
let macro_data =
self.resolver.compile_macro(def, i.ident, &i.attrs, i.span, i.id, edition);
let macro_kind = macro_data.ext.macro_kind();
opt_macro_data = Some(macro_data);
DefKind::Macro(macro_kind)

View file

@ -1122,9 +1122,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
/// Compile the macro into a `SyntaxExtension` and its rule spans.
///
/// Possibly replace its expander to a pre-defined one for built-in macros.
pub(crate) fn compile_macro(&mut self, item: &ast::Item, edition: Edition) -> MacroData {
let (mut ext, mut rule_spans) =
compile_declarative_macro(self.tcx.sess, self.tcx.features(), item, edition);
pub(crate) fn compile_macro(
&mut self,
macro_def: &ast::MacroDef,
ident: Ident,
attrs: &[ast::Attribute],
span: Span,
node_id: NodeId,
edition: Edition,
) -> MacroData {
let (mut ext, mut rule_spans) = compile_declarative_macro(
self.tcx.sess,
self.tcx.features(),
macro_def,
ident,
attrs,
span,
node_id,
edition,
);
if let Some(builtin_name) = ext.builtin_name {
// The macro was marked with `#[rustc_builtin_macro]`.
@ -1132,28 +1148,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// The macro is a built-in, replace its expander function
// while still taking everything else from the source code.
// If we already loaded this builtin macro, give a better error message than 'no such builtin macro'.
match mem::replace(builtin_macro, BuiltinMacroState::AlreadySeen(item.span)) {
match mem::replace(builtin_macro, BuiltinMacroState::AlreadySeen(span)) {
BuiltinMacroState::NotYetSeen(builtin_ext) => {
ext.kind = builtin_ext;
rule_spans = Vec::new();
}
BuiltinMacroState::AlreadySeen(span) => {
self.dcx().emit_err(errors::AttemptToDefineBuiltinMacroTwice {
span: item.span,
note_span: span,
});
BuiltinMacroState::AlreadySeen(note_span) => {
self.dcx()
.emit_err(errors::AttemptToDefineBuiltinMacroTwice { span, note_span });
}
}
} else {
self.dcx().emit_err(errors::CannotFindBuiltinMacroWithName {
span: item.span,
ident: item.ident,
});
self.dcx().emit_err(errors::CannotFindBuiltinMacroWithName { span, ident });
}
}
let ItemKind::MacroDef(def) = &item.kind else { unreachable!() };
MacroData { ext: Lrc::new(ext), rule_spans, macro_rules: def.macro_rules }
MacroData { ext: Lrc::new(ext), rule_spans, macro_rules: macro_def.macro_rules }
}
fn path_accessible(