Skip to content

Commit 020a391

Browse files
Stop tracking whether macros are exported in HIR
1 parent 9347710 commit 020a391

File tree

10 files changed

+42
-46
lines changed

10 files changed

+42
-46
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -443,23 +443,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
443443
self.lower_param_bounds(bounds, ImplTraitContext::disallowed()),
444444
),
445445
ItemKind::MacroDef(MacroDef { ref body, macro_rules }) => {
446-
let is_exported = !macro_rules
447-
|| attrs.map_or(false, |a| self.sess.contains_name(a, sym::macro_export));
448-
449446
let body = P(self.lower_mac_args(body));
450447

451-
hir::ItemKind::Macro {
452-
// `is_exported` is duplicated, to make matching more convenient.
453-
is_exported,
454-
macro_def: hir::MacroDef {
455-
is_exported,
456-
ident: *ident,
457-
vis: *vis,
458-
def_id: hir_id.expect_owner(),
459-
span,
460-
ast: MacroDef { body, macro_rules },
461-
},
462-
}
448+
hir::ItemKind::Macro(hir::MacroDef {
449+
ident: *ident,
450+
vis: *vis,
451+
def_id: hir_id.expect_owner(),
452+
span,
453+
ast: MacroDef { body, macro_rules },
454+
})
463455
}
464456
ItemKind::MacCall(..) => {
465457
panic!("`TyMac` should have been expanded by now")

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,6 @@ impl Crate<'_> {
772772
/// Not parsed directly, but created on macro import or `macro_rules!` expansion.
773773
#[derive(Debug)]
774774
pub struct MacroDef<'hir> {
775-
pub is_exported: bool,
776775
pub ident: Ident,
777776
pub vis: Visibility<'hir>,
778777
pub def_id: LocalDefId,
@@ -2781,7 +2780,7 @@ pub enum ItemKind<'hir> {
27812780
/// An external module, e.g. `extern { .. }`.
27822781
ForeignMod { abi: Abi, items: &'hir [ForeignItemRef<'hir>] },
27832782
/// A MBE macro (`macro_rules!` or `macro`).
2784-
Macro { is_exported: bool, macro_def: MacroDef<'hir> },
2783+
Macro(MacroDef<'hir>),
27852784
/// Module-level inline assembly (from `global_asm!`).
27862785
GlobalAsm(&'hir InlineAsm<'hir>),
27872786
/// A type alias, e.g., `type Foo = Bar<u8>`.

compiler/rustc_hir/src/intravisit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
593593
visitor.visit_id(item.hir_id());
594594
walk_list!(visitor, visit_foreign_item_ref, items);
595595
}
596-
ItemKind::Macro { ref macro_def, .. } => {
596+
ItemKind::Macro(ref macro_def) => {
597597
visitor.visit_id(item.hir_id());
598598
visitor.visit_macro_def(macro_def)
599599
}

compiler/rustc_hir/src/stable_hash_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
193193

194194
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for MacroDef<'_> {
195195
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
196-
let MacroDef { is_exported: _, ident, def_id: _, ref ast, ref vis, span } = *self;
196+
let MacroDef { ident, def_id: _, ref ast, ref vis, span } = *self;
197197

198198
hcx.hash_hir_item_like(|hcx| {
199199
ident.name.hash_stable(hcx, hasher);

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ impl<'a> State<'a> {
660660
}
661661
self.bclose(item.span);
662662
}
663-
hir::ItemKind::Macro { ref macro_def, .. } => {
663+
hir::ItemKind::Macro(ref macro_def) => {
664664
let (kw, has_bang) = if macro_def.ast.macro_rules {
665665
("macro_rules", true)
666666
} else {

compiler/rustc_lint/src/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingDoc {
617617
return;
618618
}
619619

620-
hir::ItemKind::Macro { is_exported: true, .. }
620+
hir::ItemKind::Macro { .. }
621621
| hir::ItemKind::TyAlias(..)
622622
| hir::ItemKind::Fn(..)
623623
| hir::ItemKind::Mod(..)

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,12 +1374,8 @@ impl EncodeContext<'a, 'tcx> {
13741374
return self.encode_info_for_mod(item.def_id, m);
13751375
}
13761376
hir::ItemKind::ForeignMod { .. } => EntryKind::ForeignMod,
1377-
hir::ItemKind::Macro { is_exported, ref macro_def } => {
1378-
if is_exported {
1379-
EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))
1380-
} else {
1381-
return;
1382-
}
1377+
hir::ItemKind::Macro(ref macro_def) => {
1378+
EntryKind::MacroDef(self.lazy(macro_def.ast.clone()))
13831379
}
13841380
hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm,
13851381
hir::ItemKind::TyAlias(..) => EntryKind::Type,

compiler/rustc_passes/src/check_attr.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,8 +1512,11 @@ impl Visitor<'tcx> for CheckAttrVisitor<'tcx> {
15121512
// Historically we've run more checks on non-exported than exported macros,
15131513
// so this lets us continue to run them while maintaining backwards compatibility.
15141514
// In the long run, the checks should be harmonized.
1515-
if let ItemKind::Macro { is_exported: false, .. } = item.kind {
1516-
check_non_exported_macro_for_invalid_attrs(self.tcx, item);
1515+
if let ItemKind::Macro(ref macro_def) = item.kind {
1516+
let def_id = item.def_id.to_def_id();
1517+
if macro_def.ast.macro_rules && !self.tcx.has_attr(def_id, sym::macro_export) {
1518+
check_non_exported_macro_for_invalid_attrs(self.tcx, item);
1519+
}
15171520
}
15181521

15191522
let target = Target::from_item(item);

compiler/rustc_passes/src/stability.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,6 @@ impl<'tcx> Visitor<'tcx> for MissingStabilityAnnotations<'tcx> {
622622
i.kind,
623623
hir::ItemKind::Impl(hir::Impl { of_trait: None, .. })
624624
| hir::ItemKind::ForeignMod { .. }
625-
| hir::ItemKind::Macro { is_exported: false, .. }
626625
) {
627626
self.check_missing_stability(i.def_id, i.span);
628627
}

compiler/rustc_privacy/src/lib.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_middle::ty::subst::{InternalSubsts, Subst};
2525
use rustc_middle::ty::{self, Const, GenericParamDefKind, TraitRef, Ty, TyCtxt, TypeFoldable};
2626
use rustc_session::lint;
2727
use rustc_span::hygiene::Transparency;
28-
use rustc_span::symbol::{kw, Ident};
28+
use rustc_span::symbol::{kw, sym, Ident};
2929
use rustc_span::Span;
3030
use rustc_trait_selection::traits::const_evaluatable::{self, AbstractConst};
3131

@@ -507,11 +507,7 @@ impl EmbargoVisitor<'tcx> {
507507
}
508508
match def_kind {
509509
// No type privacy, so can be directly marked as reachable.
510-
DefKind::Const
511-
| DefKind::Macro(_)
512-
| DefKind::Static
513-
| DefKind::TraitAlias
514-
| DefKind::TyAlias => {
510+
DefKind::Const | DefKind::Static | DefKind::TraitAlias | DefKind::TyAlias => {
515511
if vis.is_accessible_from(module.to_def_id(), self.tcx) {
516512
self.update(def_id, level);
517513
}
@@ -566,6 +562,7 @@ impl EmbargoVisitor<'tcx> {
566562
| DefKind::ExternCrate
567563
| DefKind::Use
568564
| DefKind::ForeignMod
565+
| DefKind::Macro(_)
569566
| DefKind::AnonConst
570567
| DefKind::Field
571568
| DefKind::GlobalAsm
@@ -642,11 +639,23 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
642639
}
643640
// Foreign modules inherit level from parents.
644641
hir::ItemKind::ForeignMod { .. } => self.prev_level,
645-
// Non-exported macros can't be visible by definition;
646-
hir::ItemKind::Macro { is_exported: false, .. } => None,
642+
hir::ItemKind::Macro(ref macro_def) => {
643+
let def_id = item.def_id.to_def_id();
644+
let is_macro_export = self.tcx.has_attr(def_id, sym::macro_export);
645+
match (macro_def.ast.macro_rules, is_macro_export) {
646+
(true, true) => Some(AccessLevel::Public),
647+
(true, false) => None,
648+
(false, _) => {
649+
if item.vis.node.is_pub() {
650+
self.prev_level
651+
} else {
652+
None
653+
}
654+
}
655+
}
656+
}
647657
// Other `pub` items inherit levels from parents.
648-
hir::ItemKind::Macro { is_exported: true, .. }
649-
| hir::ItemKind::Const(..)
658+
hir::ItemKind::Const(..)
650659
| hir::ItemKind::Enum(..)
651660
| hir::ItemKind::ExternCrate(..)
652661
| hir::ItemKind::GlobalAsm(..)
@@ -714,17 +723,15 @@ impl Visitor<'tcx> for EmbargoVisitor<'tcx> {
714723
}
715724
}
716725
}
717-
hir::ItemKind::Macro { macro_def: ref md, .. } => {
726+
hir::ItemKind::Macro(ref md) => {
727+
// We have to make sure that the items that macros might reference
728+
// are reachable, since they might be exported transitively.
729+
718730
// Non-opaque macros cannot make other items more accessible than they already are.
719731
let attrs = self.tcx.hir().attrs(md.hir_id());
720732
if attr::find_transparency(&self.tcx.sess, &attrs, md.ast.macro_rules).0
721733
!= Transparency::Opaque
722734
{
723-
// `#[macro_export]`-ed `macro_rules!` are `Public` since they
724-
// ignore their containing path to always appear at the crate root.
725-
if md.ast.macro_rules {
726-
self.update(item.def_id, Some(AccessLevel::Public));
727-
}
728735
return;
729736
}
730737

0 commit comments

Comments
 (0)