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 Jan 7, 2024
1 parent 78c988f commit 73c5062
Show file tree
Hide file tree
Showing 31 changed files with 400 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 @@ -1821,6 +1821,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 @@ -553,6 +553,8 @@ impl StableCompare 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
15 changes: 15 additions & 0 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,21 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
})
}

fn get_ambiguity_module_children(
self,
id: DefIndex,
sess: &'a Session,
) -> impl Iterator<Item = AmbiguityModChild> + 'a {
iter::from_coroutine(move || {
let children = self.root.tables.ambiguity_module_children.get(self, id);
if !children.is_default() {
for child in children.decode((self, sess)) {
yield child;
}
}
})
}

fn is_ctfe_mir_available(self, id: DefIndex) -> bool {
self.root.tables.mir_for_ctfe.get(self, id).is_some()
}
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
use rustc_middle::arena::ArenaAllocatable;
use rustc_middle::metadata::ModChild;
use rustc_middle::metadata::{AmbiguityModChild, ModChild};
use rustc_middle::middle::exported_symbols::ExportedSymbol;
use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::query::ExternProviders;
Expand Down Expand Up @@ -581,6 +581,16 @@ impl CStore {
self.get_crate_data_mut(cnum).dependencies = dependencies;
}
}

pub fn ambiguity_module_children_untracked(
&self,
def_id: DefId,
sess: &Session,
) -> Vec<AmbiguityModChild> {
self.get_crate_data(def_id.krate)
.get_ambiguity_module_children(def_id.index, sess)
.collect()
}
}

impl CrateStore for CStore {
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1568,6 +1568,8 @@ 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()));

record_defaulted_array!(self.tables.ambiguity_module_children[def_id] <- tcx.ambiguity_module_children_local(local_def_id));
}
}

Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 @@ -398,6 +398,7 @@ define_tables! {
// individually instead of `DefId`s.
module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
cross_crate_inlinable: Table<DefIndex, bool>,
ambiguity_module_children: Table<DefIndex, LazyArray<AmbiguityModChild>>,

- optional:
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_middle/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,6 @@ pub struct ModChild {
/// Empty if the module child is a proper item.
pub reexport_chain: SmallVec<[Reexport; 2]>,
}

/// Same as `ModChild`, however, it includes ambiguity error.
pub type AmbiguityModChild = ModChild;
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::{CanonicalParamEnvCache, 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 @@ -2286,6 +2286,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 @@ -160,6 +160,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
Loading

0 comments on commit 73c5062

Please sign in to comment.