Skip to content

Commit

Permalink
Auto merge of rust-lang#114682 - bvanjoi:fix-36837, r=<try>
Browse files Browse the repository at this point in the history
resolve: re-export ambiguity as warning

Fixes rust-lang#36837

Expose these ambiguous bindings as warnings instead of simply concealing them. This change introduces potential breaking alterations. For instance, code that previously compiled successfully may now fail to compile as expected:

```rs
// lib.rs
mod a {
  pub type C = i8;
}

mod b {
  pub type C = i16;
}

pub use a::*;
pub use b::*;

// main.rs
extern crate lib;

mod a {
    pub type C = i32;
}

use lib::*;
use a::*;

fn main() {
    let _: C = 1; // it will throw an ambiguity error but previous it will not.
}
```
r? `@petrochenkov`
  • Loading branch information
bors committed Dec 15, 2023
2 parents 4d1bd0d + 135d522 commit 785364c
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 @@ -1841,6 +1841,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 @@ -543,6 +543,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
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 @@ -1257,6 +1257,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 {
) -> Span {
self.get_crate_data(cnum).get_proc_macro_quoted_span(id, sess)
}

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 @@ -1547,6 +1547,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! {
// 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: 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 @@ -2256,6 +2256,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 @@ -159,6 +159,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 785364c

Please sign in to comment.