Skip to content

Commit

Permalink
resolve: re-export ambiguity in extern crate as warning
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Nov 18, 2023
1 parent 33688d2 commit ff15d97
Show file tree
Hide file tree
Showing 32 changed files with 380 additions and 82 deletions.
3 changes: 3 additions & 0 deletions compiler/rustc_errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,6 +1898,9 @@ pub fn report_ambiguity_error<'a, G: EmissionGuarantee>(
for help_msg in ambiguity.b1_help_msgs {
db.help(help_msg);
}
if ambiguity.extern_crate {
return;
}
db.span_note(ambiguity.b2_span, ambiguity.b2_note_msg);
for help_msg in ambiguity.b2_help_msgs {
db.help(help_msg);
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ impl<HCX> ToStableHashKey<HCX> for LintId {

#[derive(Debug)]
pub struct AmbiguityErrorDiag {
/// Does this ambiguity binding come from a different crate?
pub extern_crate: bool,
pub msg: String,
pub span: Span,
pub label_span: Span,
Expand Down
33 changes: 33 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1265,6 +1265,39 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
})
}

fn get_ambiguity_mod_child(self, id: DefIndex, sess: &Session) -> AmbiguityModChild {
let ident = self.item_ident(id, sess);
let res = Res::Def(self.def_kind(id), self.local_def_id(id));
let vis = self.get_visibility(id);

AmbiguityModChild { ident, res, vis, reexport_chain: Default::default() }
}

fn get_ambiguity_module_children(
self,
id: DefIndex,
sess: &'a Session,
) -> impl Iterator<Item = AmbiguityModChild> + 'a {
iter::from_coroutine(move || {
if self.root.proc_macro_data.is_none() {
if let Some(non_reexports) =
self.root.tables.ambiguity_module_children_non_reexports.get(self, id)
{
for child_index in non_reexports.decode(self) {
yield self.get_ambiguity_mod_child(child_index, sess);
}
}

let reexports = self.root.tables.ambiguity_module_children_reexports.get(self, id);
if !reexports.is_default() {
for reexport in reexports.decode((self, sess)) {
yield reexport;
}
}
}
})
}

fn is_ctfe_mir_available(self, id: DefIndex) -> bool {
self.root.tables.mir_for_ctfe.get(self, id).is_some()
}
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ provide! { tcx, def_id, other, cdata,
module_children => {
tcx.arena.alloc_from_iter(cdata.get_module_children(def_id.index, tcx.sess))
}
ambiguity_module_children => {
tcx.arena.alloc_from_iter(cdata.get_ambiguity_module_children(def_id.index, tcx.sess))
}
defined_lib_features => { cdata.get_lib_features(tcx) }
stability_implications => {
cdata.get_stability_implications(tcx).iter().copied().collect()
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {

record_defaulted_array!(self.tables.module_children_reexports[def_id] <-
module_children.iter().filter(|child| !child.reexport_chain.is_empty()));

let ambiguity_module_children = tcx.ambiguity_module_children_local(local_def_id);

record_array!(self.tables.ambiguity_module_children_non_reexports[def_id] <-
ambiguity_module_children.iter().filter(|child| child.reexport_chain.is_empty())
.map(|child| child.res.def_id().index));

record_defaulted_array!(self.tables.ambiguity_module_children_reexports[def_id] <-
ambiguity_module_children.iter().filter(|child| !child.reexport_chain.is_empty()));
}
}

Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use rustc_hir::definitions::DefKey;
use rustc_hir::lang_items::LangItem;
use rustc_index::bit_set::BitSet;
use rustc_index::IndexVec;
use rustc_middle::metadata::ModChild;
use rustc_middle::metadata::{AmbiguityModChild, ModChild};
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
Expand Down Expand Up @@ -397,12 +397,14 @@ define_tables! {
// That's why the encoded list needs to contain `ModChild` structures describing all the names
// individually instead of `DefId`s.
module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
ambiguity_module_children_reexports: Table<DefIndex, LazyArray<AmbiguityModChild>>,

- optional:
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,
// For non-reexported names in a module every name is associated with a separate `DefId`,
// so we can take their names, visibilities etc from other encoded tables.
module_children_non_reexports: Table<DefIndex, LazyArray<DefIndex>>,
ambiguity_module_children_non_reexports: Table<DefIndex, LazyArray<DefIndex>>,
associated_item_or_field_def_ids: Table<DefIndex, LazyArray<DefIndex>>,
opt_def_kind: Table<DefIndex, DefKind>,
visibility: Table<DefIndex, LazyValue<ty::Visibility<DefIndex>>>,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ macro_rules! arena_types {
[] closure_kind_origin: (rustc_span::Span, rustc_middle::hir::place::Place<'tcx>),
[] stripped_cfg_items: rustc_ast::expand::StrippedCfgItem,
[] mod_child: rustc_middle::metadata::ModChild,
[] ambiguity_mod_child: rustc_middle::metadata::AmbiguityModChild,
[] features: rustc_feature::Features,
]);
)
Expand Down
9 changes: 9 additions & 0 deletions compiler/rustc_middle/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,12 @@ pub struct ModChild {
/// Empty if the module child is a proper item.
pub reexport_chain: SmallVec<[Reexport; 2]>,
}

/// This is similar to `ModChild`, however, it includes ambiguity error.
#[derive(Debug, TyEncodable, TyDecodable, HashStable)]
pub struct AmbiguityModChild {
pub ident: Ident,
pub res: Res<!>,
pub vis: ty::Visibility<DefId>,
pub reexport_chain: SmallVec<[Reexport; 2]>,
}
6 changes: 5 additions & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use crate::dep_graph;
use crate::infer::canonical::{self, Canonical};
use crate::lint::LintExpectation;
use crate::metadata::ModChild;
use crate::metadata::{AmbiguityModChild, ModChild};
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
use crate::middle::debugger_visualizer::DebuggerVisualizerFile;
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
Expand Down Expand Up @@ -1728,6 +1728,10 @@ rustc_queries! {
desc { |tcx| "collecting child items of module `{}`", tcx.def_path_str(def_id) }
separate_provide_extern
}
query ambiguity_module_children(def_id: DefId) -> &'tcx [AmbiguityModChild] {
desc { |tcx| "collecting ambiguity child items of module `{}`", tcx.def_path_str(def_id) }
separate_provide_extern
}
query extern_mod_stmt_cnum(def_id: LocalDefId) -> Option<CrateNum> {
desc { |tcx| "computing crate imported by `{}`", tcx.def_path_str(def_id) }
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::arena::Arena;
use crate::dep_graph::{DepGraph, DepKindStruct};
use crate::infer::canonical::{CanonicalVarInfo, CanonicalVarInfos};
use crate::lint::struct_lint_level;
use crate::metadata::ModChild;
use crate::metadata::{AmbiguityModChild, ModChild};
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
use crate::middle::resolve_bound_vars;
use crate::middle::stability;
Expand Down Expand Up @@ -2172,6 +2172,10 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] {
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
}

pub fn ambiguity_module_children_local(self, def_id: LocalDefId) -> &'tcx [AmbiguityModChild] {
self.resolutions(()).ambiguity_module_children.get(&def_id).map_or(&[], |v| &v[..])
}
}

/// Parameter attributes that can only be determined by examining the body of a function instead
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub use self::BorrowKind::*;
pub use self::IntVarValue::*;
pub use self::Variance::*;
use crate::error::{OpaqueHiddenTypeMismatch, TypeMismatchReason};
use crate::metadata::ModChild;
use crate::metadata::{AmbiguityModChild, ModChild};
use crate::middle::privacy::EffectiveVisibilities;
use crate::mir::{Body, CoroutineLayout};
use crate::query::Providers;
Expand Down Expand Up @@ -168,6 +168,7 @@ pub struct ResolverGlobalCtxt {
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
pub module_children: LocalDefIdMap<Vec<ModChild>>,
pub ambiguity_module_children: LocalDefIdMap<Vec<AmbiguityModChild>>,
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
pub main_def: Option<MainDefinition>,
pub trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/ty/parameterized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ trivially_parameterized_over_tcx! {
bool,
std::string::String,
crate::metadata::ModChild,
crate::metadata::AmbiguityModChild,
crate::middle::codegen_fn_attrs::CodegenFnAttrs,
crate::middle::debugger_visualizer::DebuggerVisualizerFile,
crate::middle::exported_symbols::SymbolExportInfo,
Expand Down
Loading

0 comments on commit ff15d97

Please sign in to comment.