Skip to content

Commit 785364c

Browse files
committed
Auto merge of rust-lang#114682 - bvanjoi:fix-36837, r=<try>
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`
2 parents 4d1bd0d + 135d522 commit 785364c

31 files changed

+400
-82
lines changed

compiler/rustc_errors/src/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1841,6 +1841,9 @@ pub fn report_ambiguity_error<'a, G: EmissionGuarantee>(
18411841
for help_msg in ambiguity.b1_help_msgs {
18421842
db.help(help_msg);
18431843
}
1844+
if ambiguity.extern_crate {
1845+
return;
1846+
}
18441847
db.span_note(ambiguity.b2_span, ambiguity.b2_note_msg);
18451848
for help_msg in ambiguity.b2_help_msgs {
18461849
db.help(help_msg);

compiler/rustc_lint_defs/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,8 @@ impl<HCX> ToStableHashKey<HCX> for LintId {
543543

544544
#[derive(Debug)]
545545
pub struct AmbiguityErrorDiag {
546+
/// Does this ambiguity binding come from a different crate?
547+
pub extern_crate: bool,
546548
pub msg: String,
547549
pub span: Span,
548550
pub label_span: Span,

compiler/rustc_metadata/src/rmeta/decoder.rs

+15
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,21 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
12571257
})
12581258
}
12591259

1260+
fn get_ambiguity_module_children(
1261+
self,
1262+
id: DefIndex,
1263+
sess: &'a Session,
1264+
) -> impl Iterator<Item = AmbiguityModChild> + 'a {
1265+
iter::from_coroutine(move || {
1266+
let children = self.root.tables.ambiguity_module_children.get(self, id);
1267+
if !children.is_default() {
1268+
for child in children.decode((self, sess)) {
1269+
yield child;
1270+
}
1271+
}
1272+
})
1273+
}
1274+
12601275
fn is_ctfe_mir_available(self, id: DefIndex) -> bool {
12611276
self.root.tables.mir_for_ctfe.get(self, id).is_some()
12621277
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
1111
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
1212
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
1313
use rustc_middle::arena::ArenaAllocatable;
14-
use rustc_middle::metadata::ModChild;
14+
use rustc_middle::metadata::{AmbiguityModChild, ModChild};
1515
use rustc_middle::middle::exported_symbols::ExportedSymbol;
1616
use rustc_middle::middle::stability::DeprecationEntry;
1717
use rustc_middle::query::ExternProviders;
@@ -581,6 +581,16 @@ impl CStore {
581581
) -> Span {
582582
self.get_crate_data(cnum).get_proc_macro_quoted_span(id, sess)
583583
}
584+
585+
pub fn ambiguity_module_children_untracked(
586+
&self,
587+
def_id: DefId,
588+
sess: &Session,
589+
) -> Vec<AmbiguityModChild> {
590+
self.get_crate_data(def_id.krate)
591+
.get_ambiguity_module_children(def_id.index, sess)
592+
.collect()
593+
}
584594
}
585595

586596
impl CrateStore for CStore {

compiler/rustc_metadata/src/rmeta/encoder.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
15471547

15481548
record_defaulted_array!(self.tables.module_children_reexports[def_id] <-
15491549
module_children.iter().filter(|child| !child.reexport_chain.is_empty()));
1550+
1551+
record_defaulted_array!(self.tables.ambiguity_module_children[def_id] <- tcx.ambiguity_module_children_local(local_def_id));
15501552
}
15511553
}
15521554

compiler/rustc_metadata/src/rmeta/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::definitions::DefKey;
1717
use rustc_hir::lang_items::LangItem;
1818
use rustc_index::bit_set::BitSet;
1919
use rustc_index::IndexVec;
20-
use rustc_middle::metadata::ModChild;
20+
use rustc_middle::metadata::{AmbiguityModChild, ModChild};
2121
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2222
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
2323
use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
@@ -398,6 +398,7 @@ define_tables! {
398398
// That's why the encoded list needs to contain `ModChild` structures describing all the names
399399
// individually instead of `DefId`s.
400400
module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
401+
ambiguity_module_children: Table<DefIndex, LazyArray<AmbiguityModChild>>,
401402

402403
- optional:
403404
attributes: Table<DefIndex, LazyArray<ast::Attribute>>,

compiler/rustc_middle/src/metadata.rs

+3
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,6 @@ pub struct ModChild {
4444
/// Empty if the module child is a proper item.
4545
pub reexport_chain: SmallVec<[Reexport; 2]>,
4646
}
47+
48+
/// Same as `ModChild`, however, it includes ambiguity error.
49+
pub type AmbiguityModChild = ModChild;

compiler/rustc_middle/src/ty/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::arena::Arena;
88
use crate::dep_graph::{DepGraph, DepKindStruct};
99
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarInfo, CanonicalVarInfos};
1010
use crate::lint::struct_lint_level;
11-
use crate::metadata::ModChild;
11+
use crate::metadata::{AmbiguityModChild, ModChild};
1212
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
1313
use crate::middle::resolve_bound_vars;
1414
use crate::middle::stability;
@@ -2256,6 +2256,10 @@ impl<'tcx> TyCtxt<'tcx> {
22562256
pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] {
22572257
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
22582258
}
2259+
2260+
pub fn ambiguity_module_children_local(self, def_id: LocalDefId) -> &'tcx [AmbiguityModChild] {
2261+
self.resolutions(()).ambiguity_module_children.get(&def_id).map_or(&[], |v| &v[..])
2262+
}
22592263
}
22602264

22612265
/// Parameter attributes that can only be determined by examining the body of a function instead

compiler/rustc_middle/src/ty/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub use self::BorrowKind::*;
1818
pub use self::IntVarValue::*;
1919
pub use self::Variance::*;
2020
use crate::error::{OpaqueHiddenTypeMismatch, TypeMismatchReason};
21-
use crate::metadata::ModChild;
21+
use crate::metadata::{AmbiguityModChild, ModChild};
2222
use crate::middle::privacy::EffectiveVisibilities;
2323
use crate::mir::{Body, CoroutineLayout};
2424
use crate::query::Providers;
@@ -159,6 +159,7 @@ pub struct ResolverGlobalCtxt {
159159
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
160160
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
161161
pub module_children: LocalDefIdMap<Vec<ModChild>>,
162+
pub ambiguity_module_children: LocalDefIdMap<Vec<AmbiguityModChild>>,
162163
pub glob_map: FxHashMap<LocalDefId, FxHashSet<Symbol>>,
163164
pub main_def: Option<MainDefinition>,
164165
pub trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,

0 commit comments

Comments
 (0)