Skip to content

Commit 5a55dd2

Browse files
committed
eii_macro_for
1 parent df67b90 commit 5a55dd2

File tree

30 files changed

+135
-68
lines changed

30 files changed

+135
-68
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1960,6 +1960,8 @@ pub struct MacroDef {
19601960
pub body: P<DelimArgs>,
19611961
/// `true` if macro was defined with `macro_rules`.
19621962
pub macro_rules: bool,
1963+
1964+
pub eii_macro_for: Option<Path>,
19631965
}
19641966

19651967
#[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: 4 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,
@@ -479,7 +478,10 @@ impl WalkItemKind for ItemKind {
479478
ItemKind::MacCall(mac) => try_visit!(visitor.visit_mac_call(mac)),
480479
ItemKind::MacroDef(ident, ts) => {
481480
try_visit!(visitor.visit_ident(ident));
482-
try_visit!(visitor.visit_mac_def(ts, id))
481+
try_visit!(visitor.visit_mac_def(ts, id));
482+
if let Some(i) = &ts.eii_macro_for {
483+
try_visit!(visitor.visit_path(i, id));
484+
}
483485
}
484486
ItemKind::Delegation(box Delegation {
485487
id,

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>,
@@ -462,7 +463,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
462463
);
463464
hir::ItemKind::TraitAlias(ident, generics, bounds)
464465
}
465-
ItemKind::MacroDef(ident, MacroDef { body, macro_rules }) => {
466+
ItemKind::MacroDef(ident, MacroDef { body, macro_rules, eii_macro_for }) => {
466467
let ident = self.lower_ident(*ident);
467468
let body = P(self.lower_delim_args(body));
468469
let def_id = self.local_def_id(id);
@@ -473,8 +474,35 @@ impl<'hir> LoweringContext<'_, 'hir> {
473474
def_kind.descr(def_id.to_def_id())
474475
);
475476
};
476-
let macro_def = self.arena.alloc(ast::MacroDef { body, macro_rules: *macro_rules });
477-
hir::ItemKind::Macro(ident, macro_def, macro_kind)
477+
478+
let ast_macro_def = self.arena.alloc(ast::MacroDef {
479+
body,
480+
macro_rules: *macro_rules,
481+
eii_macro_for: None,
482+
});
483+
484+
hir::ItemKind::Macro {
485+
name: ident,
486+
ast_macro_def,
487+
kind: macro_kind,
488+
eii_macro_for: eii_macro_for.as_ref().map(|path| {
489+
let lowered = self.lower_qpath(
490+
id,
491+
&None,
492+
path,
493+
ParamMode::Explicit,
494+
crate::AllowReturnTypeNotation::No,
495+
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
496+
None,
497+
);
498+
499+
let QPath::Resolved(None, path) = lowered else {
500+
panic!("{lowered:?}");
501+
};
502+
503+
path.res.def_id()
504+
}),
505+
}
478506
}
479507
ItemKind::Delegation(box delegation) => {
480508
let delegation_results = self.lower_delegation(delegation, id, false);

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
@@ -1056,6 +1056,21 @@ pub static BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
10561056
"#[rustc_force_inline] forces a free function to be inlined"
10571057
),
10581058

1059+
gated!(
1060+
eii, Normal, template!(Word),
1061+
ErrorPreceding, EncodeCrossCrate::No,
1062+
eii_internals, "internally used to implement EII"
1063+
),
1064+
gated!(
1065+
eii_impl, Normal, template!(List: "/*opt*/ default"),
1066+
ErrorPreceding, EncodeCrossCrate::No,
1067+
eii_internals, "internally used to implement EII"
1068+
),
1069+
gated!(
1070+
eii_macro_for, Normal, template!(List: "path"),
1071+
ErrorPreceding, EncodeCrossCrate::No,
1072+
eii_internals, "internally used to implement EII"
1073+
),
10591074
// ==========================================================================
10601075
// Internal attributes, Testing:
10611076
// ==========================================================================

compiler/rustc_hir/src/hir.rs

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

4001-
expect_macro, (Ident, &ast::MacroDef, MacroKind),
4002-
ItemKind::Macro(ident, def, mk), (*ident, def, *mk);
4001+
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);
40034002

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

@@ -4176,7 +4175,7 @@ pub enum ItemKind<'hir> {
41764175
has_body: bool,
41774176
},
41784177
/// A MBE macro definition (`macro_rules!` or `macro`).
4179-
Macro(Ident, &'hir ast::MacroDef, MacroKind),
4178+
Macro { name: Ident, ast_macro_def: &'hir ast::MacroDef, kind: MacroKind, eii_macro_for: Option<DefId> },
41804179
/// A module.
41814180
Mod(Ident, &'hir Mod<'hir>),
41824181
/// An external module, e.g. `extern { .. }`.
@@ -4276,7 +4275,7 @@ impl ItemKind<'_> {
42764275
ItemKind::Static(..) => "static item",
42774276
ItemKind::Const(..) => "constant item",
42784277
ItemKind::Fn { .. } => "function",
4279-
ItemKind::Macro(..) => "macro",
4278+
ItemKind::Macro { .. } => "macro",
42804279
ItemKind::Mod(..) => "module",
42814280
ItemKind::ForeignMod { .. } => "extern block",
42824281
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
@@ -676,7 +676,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
676676
// These don't define types.
677677
hir::ItemKind::ExternCrate(..)
678678
| hir::ItemKind::Use(..)
679-
| hir::ItemKind::Macro(..)
679+
| hir::ItemKind::Macro { .. }
680680
| hir::ItemKind::Mod(..)
681681
| hir::ItemKind::GlobalAsm { .. } => {}
682682
hir::ItemKind::ForeignMod { items, .. } => {

0 commit comments

Comments
 (0)