Skip to content

Commit 305268a

Browse files
committed
eii_macro_for
1 parent 94aff96 commit 305268a

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
@@ -1951,6 +1951,8 @@ pub struct MacroDef {
19511951
pub body: P<DelimArgs>,
19521952
/// `true` if macro was defined with `macro_rules`.
19531953
pub macro_rules: bool,
1954+
1955+
pub eii_macro_for: Option<Path>,
19541956
}
19551957

19561958
#[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
@@ -734,7 +734,10 @@ fn walk_mac<T: MutVisitor>(vis: &mut T, mac: &mut MacCall) {
734734
}
735735

736736
fn walk_macro_def<T: MutVisitor>(vis: &mut T, macro_def: &mut MacroDef) {
737-
let MacroDef { body, macro_rules: _ } = macro_def;
737+
let MacroDef { body, macro_rules: _, eii_macro_for } = macro_def;
738+
if let Some(path) = eii_macro_for {
739+
vis.visit_path(path);
740+
}
738741
visit_delim_args(vis, body);
739742
}
740743

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

1076+
gated!(
1077+
eii, Normal, template!(Word),
1078+
ErrorPreceding, EncodeCrossCrate::No,
1079+
eii_internals, "internally used to implement EII"
1080+
),
1081+
gated!(
1082+
eii_impl, Normal, template!(List: "/*opt*/ default"),
1083+
ErrorPreceding, EncodeCrossCrate::No,
1084+
eii_internals, "internally used to implement EII"
1085+
),
1086+
gated!(
1087+
eii_macro_for, Normal, template!(List: "path"),
1088+
ErrorPreceding, EncodeCrossCrate::No,
1089+
eii_internals, "internally used to implement EII"
1090+
),
10761091
// ==========================================================================
10771092
// Internal attributes, Testing:
10781093
// ==========================================================================

compiler/rustc_hir/src/hir.rs

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

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

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

@@ -4291,7 +4290,7 @@ pub enum ItemKind<'hir> {
42914290
has_body: bool,
42924291
},
42934292
/// A MBE macro definition (`macro_rules!` or `macro`).
4294-
Macro(Ident, &'hir ast::MacroDef, MacroKind),
4293+
Macro { name: Ident, ast_macro_def: &'hir ast::MacroDef, kind: MacroKind, eii_macro_for: Option<DefId> },
42954294
/// A module.
42964295
Mod(Ident, &'hir Mod<'hir>),
42974296
/// An external module, e.g. `extern { .. }`.
@@ -4391,7 +4390,7 @@ impl ItemKind<'_> {
43914390
ItemKind::Static(..) => "static item",
43924391
ItemKind::Const(..) => "constant item",
43934392
ItemKind::Fn { .. } => "function",
4394-
ItemKind::Macro(..) => "macro",
4393+
ItemKind::Macro { .. } => "macro",
43954394
ItemKind::Mod(..) => "module",
43964395
ItemKind::ForeignMod { .. } => "extern block",
43974396
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)