Skip to content

Commit 50692d8

Browse files
committed
eii_macro_for
1 parent e88d117 commit 50692d8

File tree

37 files changed

+146
-75
lines changed

37 files changed

+146
-75
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,6 +1873,8 @@ pub struct MacroDef {
18731873
pub body: P<DelimArgs>,
18741874
/// `true` if macro was defined with `macro_rules`.
18751875
pub macro_rules: bool,
1876+
1877+
pub eii_macro_for: Option<Path>,
18761878
}
18771879

18781880
#[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
@@ -750,7 +750,10 @@ fn walk_mac<T: MutVisitor>(vis: &mut T, mac: &mut MacCall) {
750750
}
751751

752752
fn walk_macro_def<T: MutVisitor>(vis: &mut T, macro_def: &mut MacroDef) {
753-
let MacroDef { body, macro_rules: _ } = macro_def;
753+
let MacroDef { body, macro_rules: _, eii_macro_for } = macro_def;
754+
if let Some(path) = eii_macro_for {
755+
vis.visit_path(path);
756+
}
754757
visit_delim_args(vis, body);
755758
}
756759

compiler/rustc_ast/src/visit.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub enum BoundKind {
4747
/// Trait bounds in trait object type.
4848
/// E.g., `dyn Bound1 + Bound2 + Bound3`.
4949
TraitObject,
50-
5150
/// Super traits of a trait.
5251
/// E.g., `trait A: B`
5352
SuperTraits,
@@ -459,7 +458,12 @@ impl WalkItemKind for ItemKind {
459458
walk_list!(visitor, visit_param_bound, bounds, BoundKind::Bound);
460459
}
461460
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
462-
ItemKind::MacroDef(ts) => try_visit!(visitor.visit_mac_def(ts, id)),
461+
ItemKind::MacroDef(ts) => {
462+
try_visit!(visitor.visit_mac_def(ts, id));
463+
if let Some(i) = &ts.eii_macro_for {
464+
try_visit!(visitor.visit_path(i, id));
465+
}
466+
}
463467
ItemKind::Delegation(box Delegation {
464468
id,
465469
qself,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use super::{
2222
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
2323
ResolverAstLoweringExt,
2424
};
25+
use crate::GenericArgsMode;
2526

2627
pub(super) struct ItemLowerer<'a, 'hir> {
2728
pub(super) tcx: TyCtxt<'hir>,
@@ -472,7 +473,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
472473
);
473474
hir::ItemKind::TraitAlias(ident, generics, bounds)
474475
}
475-
ItemKind::MacroDef(MacroDef { body, macro_rules }) => {
476+
ItemKind::MacroDef(MacroDef { body, macro_rules, eii_macro_for }) => {
476477
debug_assert_ne!(ident.name, kw::Empty);
477478
let ident = self.lower_ident(ident);
478479
let body = P(self.lower_delim_args(body));
@@ -484,8 +485,35 @@ impl<'hir> LoweringContext<'_, 'hir> {
484485
def_kind.descr(def_id.to_def_id())
485486
);
486487
};
487-
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
488-
hir::ItemKind::Macro(ident, macro_def, macro_kind)
488+
489+
let ast_macro_def = self.arena.alloc(ast::MacroDef {
490+
body,
491+
macro_rules: *macro_rules,
492+
eii_macro_for: None,
493+
});
494+
495+
hir::ItemKind::Macro {
496+
name: ident,
497+
ast_macro_def,
498+
kind: macro_kind,
499+
eii_macro_for: eii_macro_for.as_ref().map(|path| {
500+
let lowered = self.lower_qpath(
501+
id,
502+
&None,
503+
path,
504+
ParamMode::Explicit,
505+
crate::AllowReturnTypeNotation::No,
506+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
507+
None,
508+
);
509+
510+
let QPath::Resolved(None, path) = lowered else {
511+
panic!("{lowered:?}");
512+
};
513+
514+
path.res.def_id()
515+
}),
516+
}
489517
}
490518
ItemKind::Delegation(box delegation) => {
491519
debug_assert_ne!(ident.name, kw::Empty);

compiler/rustc_builtin_macros/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ mod define_opaque;
4545
mod derive;
4646
mod deriving;
4747
mod edition_panic;
48+
mod eii;
4849
mod env;
4950
mod errors;
5051
mod format;
@@ -123,6 +124,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
123124
global_allocator: global_allocator::expand,
124125
test: test::expand_test,
125126
test_case: test::expand_test_case,
127+
eii_macro_for: eii::eii_macro_for,
126128
}
127129

128130
register_derive! {

compiler/rustc_feature/src/builtin_attrs.rs

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

1044+
gated!(
1045+
eii, Normal, template!(Word),
1046+
ErrorPreceding, EncodeCrossCrate::No,
1047+
eii_internals, "internally used to implement EII"
1048+
),
1049+
gated!(
1050+
eii_impl, Normal, template!(List: "/*opt*/ default"),
1051+
ErrorPreceding, EncodeCrossCrate::No,
1052+
eii_internals, "internally used to implement EII"
1053+
),
1054+
gated!(
1055+
eii_macro_for, Normal, template!(List: "path"),
1056+
ErrorPreceding, EncodeCrossCrate::No,
1057+
eii_internals, "internally used to implement EII"
1058+
),
10441059
// ==========================================================================
10451060
// Internal attributes, Testing:
10461061
// ==========================================================================

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3992,8 +3992,7 @@ impl<'hir> Item<'hir> {
39923992
expect_fn, (Ident, &FnSig<'hir>, &'hir Generics<'hir>, BodyId),
39933993
ItemKind::Fn { ident, sig, generics, body, .. }, (*ident, sig, generics, *body);
39943994

3995-
expect_macro, (Ident, &ast::MacroDef, MacroKind),
3996-
ItemKind::Macro(ident, def, mk), (*ident, def, *mk);
3995+
expect_macro, (Ident, &ast::MacroDef, MacroKind, Option<DefId>), ItemKind::Macro {name: ident, ast_macro_def, kind, eii_macro_for}, (*ident, ast_macro_def, *kind, *eii_macro_for);
39973996

39983997
expect_mod, (Ident, &'hir Mod<'hir>), ItemKind::Mod(ident, m), (*ident, m);
39993998

@@ -4170,7 +4169,7 @@ pub enum ItemKind<'hir> {
41704169
has_body: bool,
41714170
},
41724171
/// A MBE macro definition (`macro_rules!` or `macro`).
4173-
Macro(Ident, &'hir ast::MacroDef, MacroKind),
4172+
Macro { name: Ident, ast_macro_def: &'hir ast::MacroDef, kind: MacroKind, eii_macro_for: Option<DefId> },
41744173
/// A module.
41754174
Mod(Ident, &'hir Mod<'hir>),
41764175
/// An external module, e.g. `extern { .. }`.
@@ -4270,7 +4269,7 @@ impl ItemKind<'_> {
42704269
ItemKind::Static(..) => "static item",
42714270
ItemKind::Const(..) => "constant item",
42724271
ItemKind::Fn { .. } => "function",
4273-
ItemKind::Macro(..) => "macro",
4272+
ItemKind::Macro { .. } => "macro",
42744273
ItemKind::Mod(..) => "module",
42754274
ItemKind::ForeignMod { .. } => "extern block",
42764275
ItemKind::GlobalAsm { .. } => "global asm item",

compiler/rustc_hir/src/intravisit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,8 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::
566566
item.owner_id.def_id,
567567
));
568568
}
569-
ItemKind::Macro(ident, _def, _kind) => {
570-
try_visit!(visitor.visit_ident(ident));
569+
ItemKind::Macro(name, _def, _kind) => {
570+
try_visit!(visitor.visit_ident(name));
571571
}
572572
ItemKind::Mod(ident, ref module) => {
573573
try_visit!(visitor.visit_ident(ident));

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
@@ -675,7 +675,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
675675
// These don't define types.
676676
hir::ItemKind::ExternCrate(..)
677677
| hir::ItemKind::Use(..)
678-
| hir::ItemKind::Macro(..)
678+
| hir::ItemKind::Macro { .. }
679679
| hir::ItemKind::Mod(..)
680680
| hir::ItemKind::GlobalAsm { .. } => {}
681681
hir::ItemKind::ForeignMod { items, .. } => {

0 commit comments

Comments
 (0)