Skip to content

Commit c6359cd

Browse files
Auto merge of #147984 - petrochenkov:expambig, r=<try>
resolve: Preserve ambiguous glob reexports in crate metadata
2 parents 75948c8 + e388d62 commit c6359cd

36 files changed

+860
-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: 85 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,19 @@ 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+
// FIXME: report everything as a lint for crater
92+
warn_ambiguity: true,
93+
vis,
94+
span,
95+
expansion,
96+
});
8797
// Even if underscore names cannot be looked up, we still need to add them to modules,
8898
// because they can be fetched by glob imports from those modules, and bring traits
8999
// into scope both directly and through glob imports.
@@ -232,9 +242,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
232242
}
233243

234244
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)
245+
let def_id = module.def_id();
246+
let children = self.tcx.module_children(def_id);
247+
let parent_scope = ParentScope::module(module, self.arenas);
248+
for (i, child) in self.tcx.module_children(def_id).iter().enumerate() {
249+
self.build_reduced_graph_for_external_crate_res(child, parent_scope, i, None, false)
250+
}
251+
for (i, child) in
252+
self.cstore().ambig_module_children_untracked(def_id, self.tcx.sess).enumerate()
253+
{
254+
self.build_reduced_graph_for_external_crate_res(
255+
&child.main,
256+
parent_scope,
257+
children.len() + i,
258+
Some((&child.second, child.kind)),
259+
child.warn_ambiguity,
260+
)
238261
}
239262
}
240263

@@ -244,6 +267,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
244267
child: &ModChild,
245268
parent_scope: ParentScope<'ra>,
246269
child_index: usize,
270+
ambig_child: Option<(&ModChild, AmbigModChildKind)>,
271+
warn_ambiguity: bool,
247272
) {
248273
let parent = parent_scope.module;
249274
let ModChild { ident, res, vis, ref reexport_chain } = *child;
@@ -255,6 +280,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
255280
);
256281
let res = res.expect_non_local();
257282
let expansion = parent_scope.expansion;
283+
let ambig = ambig_child.map(|(ambig_child, ambig_kind)| {
284+
let ModChild { ident: _, res, vis, ref reexport_chain } = *ambig_child;
285+
let span = self.def_span(
286+
reexport_chain
287+
.first()
288+
.and_then(|reexport| reexport.id())
289+
.unwrap_or_else(|| res.def_id()),
290+
);
291+
let res = res.expect_non_local();
292+
let ambig_kind = match ambig_kind {
293+
AmbigModChildKind::GlobVsGlob => AmbiguityKind::GlobVsGlob,
294+
AmbigModChildKind::GlobVsExpanded => AmbiguityKind::GlobVsExpanded,
295+
};
296+
(self.arenas.new_res_binding(res, vis, span, expansion), ambig_kind)
297+
});
258298
// Record primary definitions.
259299
match res {
260300
Res::Def(
@@ -272,9 +312,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
272312
_,
273313
)
274314
| Res::PrimTy(..)
275-
| Res::ToolMod => {
276-
self.define_extern(parent, ident, TypeNS, child_index, res, vis, span, expansion)
277-
}
315+
| Res::ToolMod => self.define_extern(
316+
parent,
317+
ident,
318+
TypeNS,
319+
child_index,
320+
res,
321+
vis,
322+
span,
323+
expansion,
324+
ambig,
325+
warn_ambiguity,
326+
),
278327
Res::Def(
279328
DefKind::Fn
280329
| DefKind::AssocFn
@@ -283,10 +332,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
283332
| DefKind::AssocConst
284333
| DefKind::Ctor(..),
285334
_,
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-
}
335+
) => self.define_extern(
336+
parent,
337+
ident,
338+
ValueNS,
339+
child_index,
340+
res,
341+
vis,
342+
span,
343+
expansion,
344+
ambig,
345+
warn_ambiguity,
346+
),
347+
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => self.define_extern(
348+
parent,
349+
ident,
350+
MacroNS,
351+
child_index,
352+
res,
353+
vis,
354+
span,
355+
expansion,
356+
ambig,
357+
warn_ambiguity,
358+
),
290359
Res::Def(
291360
DefKind::TyParam
292361
| 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)