Skip to content

Commit 0a6d58a

Browse files
committed
resolve: Preserve ambiguous glob reexports in crate metadata
So in cross-crate scenarios they can work in the same way as in crate-local scenarios.
1 parent b2ee1b3 commit 0a6d58a

36 files changed

+605
-97
lines changed

compiler/rustc_metadata/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#![feature(min_specialization)]
1212
#![feature(never_type)]
1313
#![feature(proc_macro_internals)]
14+
#![feature(result_option_map_or_default)]
1415
#![feature(rustdoc_internals)]
1516
#![feature(trusted_len)]
1617
// tidy-alphabetical-end

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,21 @@ impl<'a> CrateMetadataRef<'a> {
13081308
}
13091309
}
13101310

1311+
fn get_ambig_module_children(
1312+
self,
1313+
id: DefIndex,
1314+
sess: &Session,
1315+
) -> impl Iterator<Item = AmbigModChild> {
1316+
gen move {
1317+
let children = self.root.tables.ambig_module_children.get(self, id);
1318+
if !children.is_default() {
1319+
for child in children.decode((self, sess)) {
1320+
yield child;
1321+
}
1322+
}
1323+
}
1324+
}
1325+
13111326
fn is_ctfe_mir_available(self, id: DefIndex) -> bool {
13121327
self.root.tables.mir_for_ctfe.get(self, id).is_some()
13131328
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
88
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
99
use rustc_middle::arena::ArenaAllocatable;
1010
use rustc_middle::bug;
11-
use rustc_middle::metadata::ModChild;
11+
use rustc_middle::metadata::{AmbigModChild, ModChild};
1212
use rustc_middle::middle::exported_symbols::ExportedSymbol;
1313
use rustc_middle::middle::stability::DeprecationEntry;
1414
use rustc_middle::query::{ExternProviders, LocalCrate};
@@ -582,6 +582,14 @@ impl CStore {
582582
self.get_crate_data(def_id.krate).get_expn_that_defined(def_id.index, sess)
583583
}
584584

585+
pub fn ambig_module_children_untracked(
586+
&self,
587+
def_id: DefId,
588+
sess: &Session,
589+
) -> impl Iterator<Item = AmbigModChild> {
590+
self.get_crate_data(def_id.krate).get_ambig_module_children(def_id.index, sess)
591+
}
592+
585593
/// Only public-facing way to traverse all the definitions in a non-local crate.
586594
/// Critically useful for this third-party project: <https://github.com/hacspec/hacspec>.
587595
/// See <https://github.com/rust-lang/rust/pull/85889> for context.

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,6 +1705,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
17051705

17061706
record_defaulted_array!(self.tables.module_children_reexports[def_id] <-
17071707
module_children.iter().filter(|child| !child.reexport_chain.is_empty()));
1708+
1709+
let ambig_module_children = tcx
1710+
.resolutions(())
1711+
.ambig_module_children
1712+
.get(&local_def_id)
1713+
.map_or_default(|v| &v[..]);
1714+
record_defaulted_array!(self.tables.ambig_module_children[def_id] <-
1715+
ambig_module_children);
17081716
}
17091717
}
17101718

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc_index::bit_set::DenseBitSet;
2121
use rustc_macros::{
2222
Decodable, Encodable, MetadataDecodable, MetadataEncodable, TyDecodable, TyEncodable,
2323
};
24-
use rustc_middle::metadata::ModChild;
24+
use rustc_middle::metadata::{AmbigModChild, ModChild};
2525
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2626
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
2727
use rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs;
@@ -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+
ambig_module_children: Table<DefIndex, LazyArray<AmbigModChild>>,
401402
cross_crate_inlinable: Table<DefIndex, bool>,
402403

403404
- optional:

compiler/rustc_metadata/src/rmeta/parameterized.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ trivially_parameterized_over_tcx! {
9494
rustc_hir::def_id::DefIndex,
9595
rustc_hir::definitions::DefKey,
9696
rustc_index::bit_set::DenseBitSet<u32>,
97+
rustc_middle::metadata::AmbigModChild,
9798
rustc_middle::metadata::ModChild,
9899
rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs,
99100
rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile,

compiler/rustc_middle/src/metadata.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ pub struct ModChild {
4444
/// Empty if the module child is a proper item.
4545
pub reexport_chain: SmallVec<[Reexport; 2]>,
4646
}
47+
48+
#[derive(Debug, TyEncodable, TyDecodable, HashStable)]
49+
pub enum AmbigModChildKind {
50+
GlobVsGlob,
51+
GlobVsExpanded,
52+
}
53+
54+
/// Same as `ModChild`, however, it includes ambiguity error.
55+
#[derive(Debug, TyEncodable, TyDecodable, HashStable)]
56+
pub struct AmbigModChild {
57+
pub main: ModChild,
58+
pub second: ModChild,
59+
pub kind: AmbigModChildKind,
60+
pub warn_ambiguity: bool,
61+
}

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ pub use self::typeck_results::{
110110
Rust2024IncompatiblePatInfo, TypeckResults, UserType, UserTypeAnnotationIndex, UserTypeKind,
111111
};
112112
use crate::error::{OpaqueHiddenTypeMismatch, TypeMismatchReason};
113-
use crate::metadata::ModChild;
113+
use crate::metadata::{AmbigModChild, ModChild};
114114
use crate::middle::privacy::EffectiveVisibilities;
115115
use crate::mir::{Body, CoroutineLayout, CoroutineSavedLocal, SourceInfo};
116116
use crate::query::{IntoQueryParam, Providers};
@@ -175,6 +175,7 @@ pub struct ResolverGlobalCtxt {
175175
pub extern_crate_map: UnordMap<LocalDefId, CrateNum>,
176176
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
177177
pub module_children: LocalDefIdMap<Vec<ModChild>>,
178+
pub ambig_module_children: LocalDefIdMap<Vec<AmbigModChild>>,
178179
pub glob_map: FxIndexMap<LocalDefId, FxIndexSet<Symbol>>,
179180
pub main_def: Option<MainDefinition>,
180181
pub trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 84 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_hir::def::{self, *};
2222
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
2323
use rustc_index::bit_set::DenseBitSet;
2424
use rustc_metadata::creader::LoadedMacro;
25-
use rustc_middle::metadata::ModChild;
25+
use rustc_middle::metadata::{AmbigModChildKind, ModChild};
2626
use rustc_middle::ty::{Feed, Visibility};
2727
use rustc_middle::{bug, span_bug};
2828
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
@@ -36,9 +36,9 @@ use crate::imports::{ImportData, ImportKind};
3636
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
3737
use crate::ref_mut::CmCell;
3838
use crate::{
39-
BindingKey, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind, ModuleOrUniformRoot,
40-
NameBinding, ParentScope, PathResult, ResolutionError, Resolver, Segment, Used,
41-
VisResolutionError, errors,
39+
AmbiguityKind, BindingKey, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind,
40+
ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind, ParentScope, PathResult,
41+
ResolutionError, Resolver, Segment, Used, VisResolutionError, errors,
4242
};
4343

4444
type Res = def::Res<NodeId>;
@@ -81,9 +81,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
8181
res: Res,
8282
vis: Visibility<DefId>,
8383
span: Span,
84-
expn_id: LocalExpnId,
84+
expansion: LocalExpnId,
85+
ambiguity: Option<(NameBinding<'ra>, AmbiguityKind)>,
86+
warn_ambiguity: bool,
8587
) {
86-
let binding = self.arenas.new_res_binding(res, vis, span, expn_id);
88+
let binding = self.arenas.alloc_name_binding(NameBindingData {
89+
kind: NameBindingKind::Res(res),
90+
ambiguity,
91+
warn_ambiguity,
92+
vis,
93+
span,
94+
expansion,
95+
});
8796
// Even if underscore names cannot be looked up, we still need to add them to modules,
8897
// because they can be fetched by glob imports from those modules, and bring traits
8998
// into scope both directly and through glob imports.
@@ -232,9 +241,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
232241
}
233242

234243
pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
235-
for (i, child) in self.tcx.module_children(module.def_id()).into_iter().enumerate() {
236-
let parent_scope = ParentScope::module(module, self.arenas);
237-
self.build_reduced_graph_for_external_crate_res(child, parent_scope, i)
244+
let def_id = module.def_id();
245+
let children = self.tcx.module_children(def_id);
246+
let parent_scope = ParentScope::module(module, self.arenas);
247+
for (i, child) in self.tcx.module_children(def_id).iter().enumerate() {
248+
self.build_reduced_graph_for_external_crate_res(child, parent_scope, i, None, false)
249+
}
250+
for (i, child) in
251+
self.cstore().ambig_module_children_untracked(def_id, self.tcx.sess).enumerate()
252+
{
253+
self.build_reduced_graph_for_external_crate_res(
254+
&child.main,
255+
parent_scope,
256+
children.len() + i,
257+
Some((&child.second, child.kind)),
258+
child.warn_ambiguity,
259+
)
238260
}
239261
}
240262

@@ -244,6 +266,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
244266
child: &ModChild,
245267
parent_scope: ParentScope<'ra>,
246268
child_index: usize,
269+
ambig_child: Option<(&ModChild, AmbigModChildKind)>,
270+
warn_ambiguity: bool,
247271
) {
248272
let parent = parent_scope.module;
249273
let ModChild { ident, res, vis, ref reexport_chain } = *child;
@@ -255,6 +279,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
255279
);
256280
let res = res.expect_non_local();
257281
let expansion = parent_scope.expansion;
282+
let ambig = ambig_child.map(|(ambig_child, ambig_kind)| {
283+
let ModChild { ident: _, res, vis, ref reexport_chain } = *ambig_child;
284+
let span = self.def_span(
285+
reexport_chain
286+
.first()
287+
.and_then(|reexport| reexport.id())
288+
.unwrap_or_else(|| res.def_id()),
289+
);
290+
let res = res.expect_non_local();
291+
let ambig_kind = match ambig_kind {
292+
AmbigModChildKind::GlobVsGlob => AmbiguityKind::GlobVsGlob,
293+
AmbigModChildKind::GlobVsExpanded => AmbiguityKind::GlobVsExpanded,
294+
};
295+
(self.arenas.new_res_binding(res, vis, span, expansion), ambig_kind)
296+
});
258297
// Record primary definitions.
259298
match res {
260299
Res::Def(
@@ -272,9 +311,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
272311
_,
273312
)
274313
| Res::PrimTy(..)
275-
| Res::ToolMod => {
276-
self.define_extern(parent, ident, TypeNS, child_index, res, vis, span, expansion)
277-
}
314+
| Res::ToolMod => self.define_extern(
315+
parent,
316+
ident,
317+
TypeNS,
318+
child_index,
319+
res,
320+
vis,
321+
span,
322+
expansion,
323+
ambig,
324+
warn_ambiguity,
325+
),
278326
Res::Def(
279327
DefKind::Fn
280328
| DefKind::AssocFn
@@ -283,10 +331,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
283331
| DefKind::AssocConst
284332
| DefKind::Ctor(..),
285333
_,
286-
) => self.define_extern(parent, ident, ValueNS, child_index, res, vis, span, expansion),
287-
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
288-
self.define_extern(parent, ident, MacroNS, child_index, res, vis, span, expansion)
289-
}
334+
) => self.define_extern(
335+
parent,
336+
ident,
337+
ValueNS,
338+
child_index,
339+
res,
340+
vis,
341+
span,
342+
expansion,
343+
ambig,
344+
warn_ambiguity,
345+
),
346+
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => self.define_extern(
347+
parent,
348+
ident,
349+
MacroNS,
350+
child_index,
351+
res,
352+
vis,
353+
span,
354+
expansion,
355+
ambig,
356+
warn_ambiguity,
357+
),
290358
Res::Def(
291359
DefKind::TyParam
292360
| DefKind::ConstParam

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
145145
for ambiguity_error in &self.ambiguity_errors {
146146
let diag = self.ambiguity_diagnostics(ambiguity_error);
147147
if ambiguity_error.warning {
148-
let NameBindingKind::Import { import, .. } = ambiguity_error.b1.0.kind else {
149-
unreachable!()
148+
let node_id = match ambiguity_error.b1.0.kind {
149+
NameBindingKind::Import { import, .. } => import.root_id,
150+
NameBindingKind::Res(_) => CRATE_NODE_ID,
150151
};
151152
self.lint_buffer.buffer_lint(
152153
AMBIGUOUS_GLOB_IMPORTS,
153-
import.root_id,
154+
node_id,
154155
ambiguity_error.ident.span,
155156
BuiltinLintDiag::AmbiguousGlobImports { diag },
156157
);

0 commit comments

Comments
 (0)