Skip to content

Commit

Permalink
Do not reexport macros that are already at the crate root.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Aug 25, 2021
1 parent 3646bc2 commit fc67af1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
4 changes: 1 addition & 3 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1102,9 +1102,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
let def_id = self.local_def_id(child_index);
let res = Res::Def(kind, def_id);

if !kind.is_macro() {
callback(Export { res, ident, vis, span });
}
callback(Export { res, ident, vis, span });

// For non-re-export structs and variants add their constructors to children.
// Re-export lists automatically contain constructors when necessary.
Expand Down
22 changes: 18 additions & 4 deletions compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_data_structures::ptr_key::PtrKey;
use rustc_errors::{pluralize, struct_span_err, Applicability};
use rustc_hir::def::{self, PartialRes};
use rustc_hir::def_id::DefId;
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
use rustc_middle::hir::exports::Export;
use rustc_middle::span_bug;
use rustc_middle::ty;
Expand Down Expand Up @@ -1392,9 +1392,23 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
// FIXME: Implement actual cross-crate hygiene.
let is_good_import =
binding.is_import() && !binding.is_ambiguity() && !ident.span.from_expansion();
if is_good_import || binding.is_macro_def() {
let res = binding.res().map_id(|id| this.local_def_id(id));
if res != def::Res::Err {
let res = binding.res();
if res == Res::Err {
// Do not insert failed resolutions.
return;
} else if is_good_import {
let res = res.map_id(|id| this.local_def_id(id));
reexports.push(Export { ident, res, span: binding.span, vis: binding.vis });
} else if let NameBindingKind::Res(Res::Def(def_kind, macro_def_id), true) =
binding.kind
{
let macro_is_at_root = macro_def_id
.as_local()
.and_then(|macro_def_id| this.definitions.def_key(macro_def_id).parent)
== Some(CRATE_DEF_INDEX);
// Insert a re-export at crate root for exported macro_rules defined elsewhere.
if module.parent.is_none() && !macro_is_at_root {
let res = def::Res::Def(def_kind, macro_def_id);
reexports.push(Export { ident, res, span: binding.span, vis: binding.vis });
}
}
Expand Down
4 changes: 0 additions & 4 deletions compiler/rustc_resolve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,10 +815,6 @@ impl<'a> NameBinding<'a> {
)
}

fn is_macro_def(&self) -> bool {
matches!(self.kind, NameBindingKind::Res(Res::Def(DefKind::Macro(..), _), _))
}

fn macro_kind(&self) -> Option<MacroKind> {
self.res().macro_kind()
}
Expand Down

0 comments on commit fc67af1

Please sign in to comment.