Skip to content

Commit 9e19e97

Browse files
committed
eii_macro_for
1 parent 0942961 commit 9e19e97

File tree

31 files changed

+136
-66
lines changed

31 files changed

+136
-66
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,8 @@ pub struct MacroDef {
18811881
pub body: P<DelimArgs>,
18821882
/// `true` if macro was defined with `macro_rules`.
18831883
pub macro_rules: bool,
1884+
1885+
pub eii_macro_for: Option<Path>,
18841886
}
18851887

18861888
#[derive(Clone, Encodable, Decodable, Debug, Copy, Hash, Eq, PartialEq)]

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,10 @@ fn walk_mac<T: MutVisitor>(vis: &mut T, mac: &mut MacCall) {
746746
}
747747

748748
fn walk_macro_def<T: MutVisitor>(vis: &mut T, macro_def: &mut MacroDef) {
749-
let MacroDef { body, macro_rules: _ } = macro_def;
749+
let MacroDef { body, macro_rules: _, eii_macro_for } = macro_def;
750+
if let Some(path) = eii_macro_for {
751+
vis.visit_path(path);
752+
}
750753
visit_delim_args(vis, body);
751754
}
752755

compiler/rustc_ast/src/visit.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ pub enum BoundKind {
4646
/// Trait bounds in trait object type.
4747
/// E.g., `dyn Bound1 + Bound2 + Bound3`.
4848
TraitObject,
49-
5049
/// Super traits of a trait.
5150
/// E.g., `trait A: B`
5251
SuperTraits,
@@ -439,7 +438,12 @@ impl WalkItemKind for ItemKind {
439438
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
440439
}
441440
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
442-
ItemKind::MacroDef(ts) => try_visit!(visitor.visit_mac_def(ts, id)),
441+
ItemKind::MacroDef(ts) => {
442+
try_visit!(visitor.visit_mac_def(ts, id));
443+
if let Some(i) = &ts.eii_macro_for {
444+
try_visit!(visitor.visit_path(i, id));
445+
}
446+
}
443447
ItemKind::Delegation(box Delegation {
444448
id,
445449
qself,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use super::{
2323
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
2424
ResolverAstLoweringExt,
2525
};
26+
use crate::GenericArgsMode;
2627

2728
pub(super) struct ItemLowerer<'a, 'hir> {
2829
pub(super) tcx: TyCtxt<'hir>,
@@ -433,7 +434,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
433434
);
434435
hir::ItemKind::TraitAlias(generics, bounds)
435436
}
436-
ItemKind::MacroDef(MacroDef { body, macro_rules }) => {
437+
ItemKind::MacroDef(MacroDef { body, macro_rules, eii_macro_for }) => {
437438
let body = P(self.lower_delim_args(body));
438439
let def_id = self.local_def_id(id);
439440
let def_kind = self.tcx.def_kind(def_id);
@@ -443,8 +444,33 @@ impl<'hir> LoweringContext<'_, 'hir> {
443444
def_kind.descr(def_id.to_def_id())
444445
);
445446
};
446-
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
447-
hir::ItemKind::Macro(macro_def, macro_kind)
447+
let ast_macro_def = self.arena.alloc(ast::MacroDef {
448+
body,
449+
macro_rules: *macro_rules,
450+
eii_macro_for: None,
451+
});
452+
453+
hir::ItemKind::Macro {
454+
ast_macro_def,
455+
kind: macro_kind,
456+
eii_macro_for: eii_macro_for.as_ref().map(|path| {
457+
let lowered = self.lower_qpath(
458+
id,
459+
&None,
460+
path,
461+
ParamMode::Explicit,
462+
crate::AllowReturnTypeNotation::No,
463+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
464+
None,
465+
);
466+
467+
let QPath::Resolved(None, path) = lowered else {
468+
panic!("{lowered:?}");
469+
};
470+
471+
path.res.def_id()
472+
}),
473+
}
448474
}
449475
ItemKind::Delegation(box delegation) => {
450476
let delegation_results = self.lower_delegation(delegation, id);

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ mod define_opaque;
4444
mod derive;
4545
mod deriving;
4646
mod edition_panic;
47+
mod eii;
4748
mod env;
4849
mod errors;
4950
mod format;
@@ -122,6 +123,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
122123
global_allocator: global_allocator::expand,
123124
test: test::expand_test,
124125
test_case: test::expand_test_case,
126+
eii_macro_for: eii::eii_macro_for,
125127
}
126128

127129
register_derive! {

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,21 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
10211021
"#[rustc_force_inline] forces a free function to be inlined"
10221022
),
10231023

1024+
gated!(
1025+
eii, Normal, template!(Word),
1026+
ErrorPreceding, EncodeCrossCrate::No,
1027+
eii_internals, "internally used to implement EII"
1028+
),
1029+
gated!(
1030+
eii_impl, Normal, template!(List: "/*opt*/ default"),
1031+
ErrorPreceding, EncodeCrossCrate::No,
1032+
eii_internals, "internally used to implement EII"
1033+
),
1034+
gated!(
1035+
eii_macro_for, Normal, template!(List: "path"),
1036+
ErrorPreceding, EncodeCrossCrate::No,
1037+
eii_internals, "internally used to implement EII"
1038+
),
10241039
// ==========================================================================
10251040
// Internal attributes, Testing:
10261041
// ==========================================================================

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3947,7 +3947,7 @@ impl<'hir> Item<'hir> {
39473947
expect_fn, (&FnSig<'hir>, &'hir Generics<'hir>, BodyId),
39483948
ItemKind::Fn { sig, generics, body, .. }, (sig, generics, *body);
39493949

3950-
expect_macro, (&ast::MacroDef, MacroKind), ItemKind::Macro(def, mk), (def, *mk);
3950+
expect_macro, (&ast::MacroDef, MacroKind, Option<DefId>), ItemKind::Macro {ast_macro_def, kind, eii_macro_for}, (ast_macro_def, *kind, *eii_macro_for);
39513951

39523952
expect_mod, &'hir Mod<'hir>, ItemKind::Mod(m), m;
39533953

@@ -4115,7 +4115,7 @@ pub enum ItemKind<'hir> {
41154115
has_body: bool,
41164116
},
41174117
/// A MBE macro definition (`macro_rules!` or `macro`).
4118-
Macro(&'hir ast::MacroDef, MacroKind),
4118+
Macro { ast_macro_def: &'hir ast::MacroDef, kind: MacroKind, eii_macro_for: Option<DefId> },
41194119
/// A module.
41204120
Mod(&'hir Mod<'hir>),
41214121
/// An external module, e.g. `extern { .. }`.
@@ -4192,7 +4192,7 @@ impl ItemKind<'_> {
41924192
ItemKind::Static(..) => "static item",
41934193
ItemKind::Const(..) => "constant item",
41944194
ItemKind::Fn { .. } => "function",
4195-
ItemKind::Macro(..) => "macro",
4195+
ItemKind::Macro { .. } => "macro",
41964196
ItemKind::Mod(..) => "module",
41974197
ItemKind::ForeignMod { .. } => "extern block",
41984198
ItemKind::GlobalAsm { .. } => "global asm item",

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::
559559
item.owner_id.def_id,
560560
));
561561
}
562-
ItemKind::Macro(..) => {}
562+
ItemKind::Macro { .. } => {}
563563
ItemKind::Mod(ref module) => {
564564
try_visit!(visitor.visit_mod(module, item.span, item.hir_id()));
565565
}

compiler/rustc_hir/src/target.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl Target {
109109
ItemKind::Static { .. } => Target::Static,
110110
ItemKind::Const(..) => Target::Const,
111111
ItemKind::Fn { .. } => Target::Fn,
112-
ItemKind::Macro(..) => Target::MacroDef,
112+
ItemKind::Macro { .. } => Target::MacroDef,
113113
ItemKind::Mod(..) => Target::Mod,
114114
ItemKind::ForeignMod { .. } => Target::ForeignMod,
115115
ItemKind::GlobalAsm { .. } => Target::GlobalAsm,

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
677677
// These don't define types.
678678
hir::ItemKind::ExternCrate(_)
679679
| hir::ItemKind::Use(..)
680-
| hir::ItemKind::Macro(..)
680+
| hir::ItemKind::Macro { .. }
681681
| hir::ItemKind::Mod(_)
682682
| hir::ItemKind::GlobalAsm { .. } => {}
683683
hir::ItemKind::ForeignMod { items, .. } => {

0 commit comments

Comments
 (0)