Skip to content

Commit 3646bc2

Browse files
committed
Merge branch '9dce366ee25b279a7f4c81f5194d259f6b53d555'
2 parents a992a11 + 9dce366 commit 3646bc2

File tree

42 files changed

+282
-378
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+282
-378
lines changed

compiler/rustc_ast_lowering/src/item.rs

+6-24
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
170170
self.lower_item_id_use_tree(use_tree, i.id, &mut vec);
171171
vec
172172
}
173-
ItemKind::MacroDef(..) => SmallVec::new(),
174173
ItemKind::Fn(..) | ItemKind::Impl(box ImplKind { of_trait: None, .. }) => {
175174
smallvec![i.id]
176175
}
@@ -212,28 +211,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
212211
pub fn lower_item(&mut self, i: &Item) -> Option<hir::Item<'hir>> {
213212
let mut ident = i.ident;
214213
let mut vis = self.lower_visibility(&i.vis, None);
215-
216-
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
217-
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
218-
let hir_id = self.lower_node_id(i.id);
219-
self.lower_attrs(hir_id, &i.attrs);
220-
let body = P(self.lower_mac_args(body));
221-
self.insert_macro_def(hir::MacroDef {
222-
ident,
223-
vis,
224-
def_id: hir_id.expect_owner(),
225-
span: i.span,
226-
ast: MacroDef { body, macro_rules },
227-
});
228-
} else {
229-
for a in i.attrs.iter() {
230-
let a = self.lower_attr(a);
231-
self.non_exported_macro_attrs.push(a);
232-
}
233-
}
234-
return None;
235-
}
236-
237214
let hir_id = self.lower_node_id(i.id);
238215
let attrs = self.lower_attrs(hir_id, &i.attrs);
239216
let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, &mut vis, &i.kind);
@@ -465,7 +442,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
465442
self.lower_generics(generics, ImplTraitContext::disallowed()),
466443
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
467444
),
468-
ItemKind::MacroDef(..) | ItemKind::MacCall(..) => {
445+
ItemKind::MacroDef(MacroDef { ref body, macro_rules }) => {
446+
let body = P(self.lower_mac_args(body));
447+
448+
hir::ItemKind::Macro(ast::MacroDef { body, macro_rules })
449+
}
450+
ItemKind::MacCall(..) => {
469451
panic!("`TyMac` should have been expanded by now")
470452
}
471453
}

compiler/rustc_ast_lowering/src/lib.rs

-10
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ struct LoweringContext<'a, 'hir: 'a> {
103103
/// The items being lowered are collected here.
104104
owners: IndexVec<LocalDefId, Option<hir::OwnerNode<'hir>>>,
105105
bodies: BTreeMap<hir::BodyId, hir::Body<'hir>>,
106-
non_exported_macro_attrs: Vec<ast::Attribute>,
107106

108107
trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
109108

@@ -330,7 +329,6 @@ pub fn lower_crate<'a, 'hir>(
330329
trait_impls: BTreeMap::new(),
331330
modules: BTreeMap::new(),
332331
attrs: BTreeMap::default(),
333-
non_exported_macro_attrs: Vec::new(),
334332
catch_scopes: Vec::new(),
335333
loop_scopes: Vec::new(),
336334
is_in_loop_condition: false,
@@ -551,7 +549,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
551549
}
552550

553551
let krate = hir::Crate {
554-
non_exported_macro_attrs: self.arena.alloc_from_iter(self.non_exported_macro_attrs),
555552
owners: self.owners,
556553
bodies: self.bodies,
557554
body_ids,
@@ -600,13 +597,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
600597
id
601598
}
602599

603-
fn insert_macro_def(&mut self, item: hir::MacroDef<'hir>) {
604-
let def_id = item.def_id;
605-
let item = self.arena.alloc(item);
606-
self.owners.ensure_contains_elem(def_id, || None);
607-
self.owners[def_id] = Some(hir::OwnerNode::MacroDef(item));
608-
}
609-
610600
fn allocate_hir_id_counter(&mut self, owner: NodeId) -> hir::HirId {
611601
// Set up the counter if needed.
612602
self.item_local_id_counters.entry(owner).or_insert(0);

compiler/rustc_hir/src/arena.rs

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ macro_rules! arena_types {
3535
[few] inline_asm: rustc_hir::InlineAsm<$tcx>,
3636
[few] llvm_inline_asm: rustc_hir::LlvmInlineAsm<$tcx>,
3737
[] local: rustc_hir::Local<$tcx>,
38-
[few] macro_def: rustc_hir::MacroDef<$tcx>,
3938
[few] mod_: rustc_hir::Mod<$tcx>,
4039
[] param: rustc_hir::Param<$tcx>,
4140
[] pat: rustc_hir::Pat<$tcx>,

compiler/rustc_hir/src/def.rs

+7
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,13 @@ impl DefKind {
218218
| DefKind::Impl => None,
219219
}
220220
}
221+
222+
pub fn is_macro(self) -> bool {
223+
match self {
224+
DefKind::Macro(..) => true,
225+
_ => false,
226+
}
227+
}
221228
}
222229

223230
/// The resolution of a path or export.

compiler/rustc_hir/src/hir.rs

+9-57
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,6 @@ pub struct ModuleItems {
670670
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/hir.html
671671
#[derive(Debug)]
672672
pub struct Crate<'hir> {
673-
// Attributes from non-exported macros, kept only for collecting the library feature list.
674-
pub non_exported_macro_attrs: &'hir [Attribute],
675-
676673
pub owners: IndexVec<LocalDefId, Option<OwnerNode<'hir>>>,
677674
pub bodies: BTreeMap<BodyId, Body<'hir>>,
678675
pub trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
@@ -743,7 +740,7 @@ impl Crate<'_> {
743740
OwnerNode::ForeignItem(item) => visitor.visit_foreign_item(item),
744741
OwnerNode::ImplItem(item) => visitor.visit_impl_item(item),
745742
OwnerNode::TraitItem(item) => visitor.visit_trait_item(item),
746-
OwnerNode::MacroDef(_) | OwnerNode::Crate(_) => {}
743+
OwnerNode::Crate(_) => {}
747744
}
748745
}
749746
}
@@ -758,7 +755,7 @@ impl Crate<'_> {
758755
Some(OwnerNode::ForeignItem(item)) => visitor.visit_foreign_item(item),
759756
Some(OwnerNode::ImplItem(item)) => visitor.visit_impl_item(item),
760757
Some(OwnerNode::TraitItem(item)) => visitor.visit_trait_item(item),
761-
Some(OwnerNode::MacroDef(_)) | Some(OwnerNode::Crate(_)) | None => {}
758+
Some(OwnerNode::Crate(_)) | None => {}
762759
})
763760
}
764761

@@ -768,32 +765,6 @@ impl Crate<'_> {
768765
_ => None,
769766
})
770767
}
771-
772-
pub fn exported_macros<'hir>(&'hir self) -> impl Iterator<Item = &'hir MacroDef<'hir>> + 'hir {
773-
self.owners.iter().filter_map(|owner| match owner {
774-
Some(OwnerNode::MacroDef(macro_def)) => Some(*macro_def),
775-
_ => None,
776-
})
777-
}
778-
}
779-
780-
/// A macro definition, in this crate or imported from another.
781-
///
782-
/// Not parsed directly, but created on macro import or `macro_rules!` expansion.
783-
#[derive(Debug)]
784-
pub struct MacroDef<'hir> {
785-
pub ident: Ident,
786-
pub vis: Visibility<'hir>,
787-
pub def_id: LocalDefId,
788-
pub span: Span,
789-
pub ast: ast::MacroDef,
790-
}
791-
792-
impl MacroDef<'_> {
793-
#[inline]
794-
pub fn hir_id(&self) -> HirId {
795-
HirId::make_owner(self.def_id)
796-
}
797768
}
798769

799770
/// A block of statements `{ .. }`, which may have a label (in this case the
@@ -2601,7 +2572,7 @@ pub struct PolyTraitRef<'hir> {
26012572

26022573
pub type Visibility<'hir> = Spanned<VisibilityKind<'hir>>;
26032574

2604-
#[derive(Debug)]
2575+
#[derive(Copy, Clone, Debug)]
26052576
pub enum VisibilityKind<'hir> {
26062577
Public,
26072578
Crate(CrateSugar),
@@ -2794,6 +2765,8 @@ pub enum ItemKind<'hir> {
27942765
Mod(Mod<'hir>),
27952766
/// An external module, e.g. `extern { .. }`.
27962767
ForeignMod { abi: Abi, items: &'hir [ForeignItemRef<'hir>] },
2768+
/// A MBE macro (`macro_rules!` or `macro`).
2769+
Macro(ast::MacroDef),
27972770
/// Module-level inline assembly (from `global_asm!`).
27982771
GlobalAsm(&'hir InlineAsm<'hir>),
27992772
/// A type alias, e.g., `type Foo = Bar<u8>`.
@@ -2857,6 +2830,7 @@ impl ItemKind<'_> {
28572830
ItemKind::Fn(..) => "function",
28582831
ItemKind::Mod(..) => "module",
28592832
ItemKind::ForeignMod { .. } => "extern block",
2833+
ItemKind::Macro(..) => "macro",
28602834
ItemKind::GlobalAsm(..) => "global asm item",
28612835
ItemKind::TyAlias(..) => "type alias",
28622836
ItemKind::OpaqueTy(..) => "opaque type",
@@ -2995,7 +2969,6 @@ pub enum OwnerNode<'hir> {
29952969
ForeignItem(&'hir ForeignItem<'hir>),
29962970
TraitItem(&'hir TraitItem<'hir>),
29972971
ImplItem(&'hir ImplItem<'hir>),
2998-
MacroDef(&'hir MacroDef<'hir>),
29992972
Crate(&'hir Mod<'hir>),
30002973
}
30012974

@@ -3005,8 +2978,7 @@ impl<'hir> OwnerNode<'hir> {
30052978
OwnerNode::Item(Item { ident, .. })
30062979
| OwnerNode::ForeignItem(ForeignItem { ident, .. })
30072980
| OwnerNode::ImplItem(ImplItem { ident, .. })
3008-
| OwnerNode::TraitItem(TraitItem { ident, .. })
3009-
| OwnerNode::MacroDef(MacroDef { ident, .. }) => Some(*ident),
2981+
| OwnerNode::TraitItem(TraitItem { ident, .. }) => Some(*ident),
30102982
OwnerNode::Crate(..) => None,
30112983
}
30122984
}
@@ -3017,7 +2989,6 @@ impl<'hir> OwnerNode<'hir> {
30172989
| OwnerNode::ForeignItem(ForeignItem { span, .. })
30182990
| OwnerNode::ImplItem(ImplItem { span, .. })
30192991
| OwnerNode::TraitItem(TraitItem { span, .. })
3020-
| OwnerNode::MacroDef(MacroDef { span, .. })
30212992
| OwnerNode::Crate(Mod { inner: span, .. }) => *span,
30222993
}
30232994
}
@@ -3061,8 +3032,7 @@ impl<'hir> OwnerNode<'hir> {
30613032
OwnerNode::Item(Item { def_id, .. })
30623033
| OwnerNode::TraitItem(TraitItem { def_id, .. })
30633034
| OwnerNode::ImplItem(ImplItem { def_id, .. })
3064-
| OwnerNode::ForeignItem(ForeignItem { def_id, .. })
3065-
| OwnerNode::MacroDef(MacroDef { def_id, .. }) => *def_id,
3035+
| OwnerNode::ForeignItem(ForeignItem { def_id, .. }) => *def_id,
30663036
OwnerNode::Crate(..) => crate::CRATE_HIR_ID.owner,
30673037
}
30683038
}
@@ -3094,13 +3064,6 @@ impl<'hir> OwnerNode<'hir> {
30943064
_ => panic!(),
30953065
}
30963066
}
3097-
3098-
pub fn expect_macro_def(self) -> &'hir MacroDef<'hir> {
3099-
match self {
3100-
OwnerNode::MacroDef(n) => n,
3101-
_ => panic!(),
3102-
}
3103-
}
31043067
}
31053068

31063069
impl<'hir> Into<OwnerNode<'hir>> for &'hir Item<'hir> {
@@ -3127,20 +3090,13 @@ impl<'hir> Into<OwnerNode<'hir>> for &'hir TraitItem<'hir> {
31273090
}
31283091
}
31293092

3130-
impl<'hir> Into<OwnerNode<'hir>> for &'hir MacroDef<'hir> {
3131-
fn into(self) -> OwnerNode<'hir> {
3132-
OwnerNode::MacroDef(self)
3133-
}
3134-
}
3135-
31363093
impl<'hir> Into<Node<'hir>> for OwnerNode<'hir> {
31373094
fn into(self) -> Node<'hir> {
31383095
match self {
31393096
OwnerNode::Item(n) => Node::Item(n),
31403097
OwnerNode::ForeignItem(n) => Node::ForeignItem(n),
31413098
OwnerNode::ImplItem(n) => Node::ImplItem(n),
31423099
OwnerNode::TraitItem(n) => Node::TraitItem(n),
3143-
OwnerNode::MacroDef(n) => Node::MacroDef(n),
31443100
OwnerNode::Crate(n) => Node::Crate(n),
31453101
}
31463102
}
@@ -3166,7 +3122,6 @@ pub enum Node<'hir> {
31663122
Arm(&'hir Arm<'hir>),
31673123
Block(&'hir Block<'hir>),
31683124
Local(&'hir Local<'hir>),
3169-
MacroDef(&'hir MacroDef<'hir>),
31703125

31713126
/// `Ctor` refers to the constructor of an enum variant or struct. Only tuple or unit variants
31723127
/// with synthesized constructors.
@@ -3189,7 +3144,6 @@ impl<'hir> Node<'hir> {
31893144
| Node::ForeignItem(ForeignItem { ident, .. })
31903145
| Node::Field(FieldDef { ident, .. })
31913146
| Node::Variant(Variant { ident, .. })
3192-
| Node::MacroDef(MacroDef { ident, .. })
31933147
| Node::Item(Item { ident, .. }) => Some(*ident),
31943148
_ => None,
31953149
}
@@ -3233,8 +3187,7 @@ impl<'hir> Node<'hir> {
32333187
Node::Item(Item { def_id, .. })
32343188
| Node::TraitItem(TraitItem { def_id, .. })
32353189
| Node::ImplItem(ImplItem { def_id, .. })
3236-
| Node::ForeignItem(ForeignItem { def_id, .. })
3237-
| Node::MacroDef(MacroDef { def_id, .. }) => Some(HirId::make_owner(*def_id)),
3190+
| Node::ForeignItem(ForeignItem { def_id, .. }) => Some(HirId::make_owner(*def_id)),
32383191
Node::Field(FieldDef { hir_id, .. })
32393192
| Node::AnonConst(AnonConst { hir_id, .. })
32403193
| Node::Expr(Expr { hir_id, .. })
@@ -3294,7 +3247,6 @@ impl<'hir> Node<'hir> {
32943247
Node::ForeignItem(i) => Some(OwnerNode::ForeignItem(i)),
32953248
Node::TraitItem(i) => Some(OwnerNode::TraitItem(i)),
32963249
Node::ImplItem(i) => Some(OwnerNode::ImplItem(i)),
3297-
Node::MacroDef(i) => Some(OwnerNode::MacroDef(i)),
32983250
Node::Crate(i) => Some(OwnerNode::Crate(i)),
32993251
_ => None,
33003252
}

compiler/rustc_hir/src/intravisit.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,6 @@ pub trait Visitor<'v>: Sized {
466466
walk_assoc_type_binding(self, type_binding)
467467
}
468468
fn visit_attribute(&mut self, _id: HirId, _attr: &'v Attribute) {}
469-
fn visit_macro_def(&mut self, macro_def: &'v MacroDef<'v>) {
470-
walk_macro_def(self, macro_def)
471-
}
472469
fn visit_vis(&mut self, vis: &'v Visibility<'v>) {
473470
walk_vis(self, vis)
474471
}
@@ -484,19 +481,13 @@ pub trait Visitor<'v>: Sized {
484481
pub fn walk_crate<'v, V: Visitor<'v>>(visitor: &mut V, krate: &'v Crate<'v>) {
485482
let top_mod = krate.module();
486483
visitor.visit_mod(top_mod, top_mod.inner, CRATE_HIR_ID);
487-
walk_list!(visitor, visit_macro_def, krate.exported_macros());
488484
for (&id, attrs) in krate.attrs.iter() {
489485
for a in *attrs {
490486
visitor.visit_attribute(id, a)
491487
}
492488
}
493489
}
494490

495-
pub fn walk_macro_def<'v, V: Visitor<'v>>(visitor: &mut V, macro_def: &'v MacroDef<'v>) {
496-
visitor.visit_id(macro_def.hir_id());
497-
visitor.visit_ident(macro_def.ident);
498-
}
499-
500491
pub fn walk_mod<'v, V: Visitor<'v>>(visitor: &mut V, module: &'v Mod<'v>, mod_hir_id: HirId) {
501492
visitor.visit_id(mod_hir_id);
502493
for &item_id in module.item_ids {
@@ -594,6 +585,9 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
594585
visitor.visit_id(item.hir_id());
595586
walk_list!(visitor, visit_foreign_item_ref, items);
596587
}
588+
ItemKind::Macro(_) => {
589+
visitor.visit_id(item.hir_id());
590+
}
597591
ItemKind::GlobalAsm(asm) => {
598592
visitor.visit_id(item.hir_id());
599593
walk_inline_asm(visitor, asm);

compiler/rustc_hir/src/stable_hash_impls.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, ToStableHashKey};
22

33
use crate::hir::{
4-
BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, MacroDef, Mod,
5-
TraitItem, TraitItemId, Ty, VisibilityKind,
4+
BodyId, Expr, ForeignItem, ForeignItemId, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem,
5+
TraitItemId, Ty, VisibilityKind,
66
};
77
use crate::hir_id::{HirId, ItemLocalId};
88
use rustc_span::def_id::DefPathHash;
@@ -190,16 +190,3 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
190190
});
191191
}
192192
}
193-
194-
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for MacroDef<'_> {
195-
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
196-
let MacroDef { ident, def_id: _, ref ast, ref vis, span } = *self;
197-
198-
hcx.hash_hir_item_like(|hcx| {
199-
ident.name.hash_stable(hcx, hasher);
200-
ast.hash_stable(hcx, hasher);
201-
vis.hash_stable(hcx, hasher);
202-
span.hash_stable(hcx, hasher);
203-
});
204-
}
205-
}

compiler/rustc_hir/src/target.rs

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ impl Target {
113113
ItemKind::Fn(..) => Target::Fn,
114114
ItemKind::Mod(..) => Target::Mod,
115115
ItemKind::ForeignMod { .. } => Target::ForeignMod,
116+
ItemKind::Macro(..) => Target::MacroDef,
116117
ItemKind::GlobalAsm(..) => Target::GlobalAsm,
117118
ItemKind::TyAlias(..) => Target::TyAlias,
118119
ItemKind::OpaqueTy(..) => Target::OpaqueTy,

0 commit comments

Comments
 (0)