Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#![feature(min_specialization)]
#![feature(never_type)]
#![feature(proc_macro_internals)]
#![feature(result_option_map_or_default)]
#![feature(rustdoc_internals)]
#![feature(trusted_len)]
// tidy-alphabetical-end
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 @@ -1308,6 +1308,21 @@ impl<'a> CrateMetadataRef<'a> {
}
}

fn get_ambig_module_children(
self,
id: DefIndex,
sess: &Session,
) -> impl Iterator<Item = AmbigModChild> {
gen move {
let children = self.root.tables.ambig_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
10 changes: 9 additions & 1 deletion compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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::bug;
use rustc_middle::metadata::ModChild;
use rustc_middle::metadata::{AmbigModChild, ModChild};
use rustc_middle::middle::exported_symbols::ExportedSymbol;
use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::query::{ExternProviders, LocalCrate};
Expand Down Expand Up @@ -582,6 +582,14 @@ impl CStore {
self.get_crate_data(def_id.krate).get_expn_that_defined(def_id.index, sess)
}

pub fn ambig_module_children_untracked(
&self,
def_id: DefId,
sess: &Session,
) -> impl Iterator<Item = AmbigModChild> {
self.get_crate_data(def_id.krate).get_ambig_module_children(def_id.index, sess)
}

/// Only public-facing way to traverse all the definitions in a non-local crate.
/// Critically useful for this third-party project: <https://github.com/hacspec/hacspec>.
/// See <https://github.com/rust-lang/rust/pull/85889> for context.
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,6 +1705,14 @@ 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()));

let ambig_module_children = tcx
.resolutions(())
.ambig_module_children
.get(&local_def_id)
.map_or_default(|v| &v[..]);
record_defaulted_array!(self.tables.ambig_module_children[def_id] <-
ambig_module_children);
}
}

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 @@ -21,7 +21,7 @@ use rustc_index::bit_set::DenseBitSet;
use rustc_macros::{
Decodable, Encodable, MetadataDecodable, MetadataEncodable, TyDecodable, TyEncodable,
};
use rustc_middle::metadata::ModChild;
use rustc_middle::metadata::{AmbigModChild, ModChild};
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
use rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs;
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>>,
ambig_module_children: Table<DefIndex, LazyArray<AmbigModChild>>,
cross_crate_inlinable: Table<DefIndex, bool>,

- optional:
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/src/rmeta/parameterized.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ trivially_parameterized_over_tcx! {
rustc_hir::def_id::DefIndex,
rustc_hir::definitions::DefKey,
rustc_index::bit_set::DenseBitSet<u32>,
rustc_middle::metadata::AmbigModChild,
rustc_middle::metadata::ModChild,
rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs,
rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile,
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_middle/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ pub struct ModChild {
/// Empty if the module child is a proper item.
pub reexport_chain: SmallVec<[Reexport; 2]>,
}

#[derive(Debug, TyEncodable, TyDecodable, HashStable)]
pub enum AmbigModChildKind {
GlobVsGlob,
GlobVsExpanded,
}

/// Same as `ModChild`, however, it includes ambiguity error.
#[derive(Debug, TyEncodable, TyDecodable, HashStable)]
pub struct AmbigModChild {
pub main: ModChild,
pub second: ModChild,
pub kind: AmbigModChildKind,
pub warn_ambiguity: bool,
}
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 @@ -110,7 +110,7 @@ pub use self::typeck_results::{
Rust2024IncompatiblePatInfo, TypeckResults, UserType, UserTypeAnnotationIndex, UserTypeKind,
};
use crate::error::{OpaqueHiddenTypeMismatch, TypeMismatchReason};
use crate::metadata::ModChild;
use crate::metadata::{AmbigModChild, ModChild};
use crate::middle::privacy::EffectiveVisibilities;
use crate::mir::{Body, CoroutineLayout, CoroutineSavedLocal, SourceInfo};
use crate::query::{IntoQueryParam, Providers};
Expand Down Expand Up @@ -175,6 +175,7 @@ pub struct ResolverGlobalCtxt {
pub extern_crate_map: UnordMap<LocalDefId, CrateNum>,
pub maybe_unused_trait_imports: FxIndexSet<LocalDefId>,
pub module_children: LocalDefIdMap<Vec<ModChild>>,
pub ambig_module_children: LocalDefIdMap<Vec<AmbigModChild>>,
pub glob_map: FxIndexMap<LocalDefId, FxIndexSet<Symbol>>,
pub main_def: Option<MainDefinition>,
pub trait_impls: FxIndexMap<DefId, Vec<LocalDefId>>,
Expand Down
101 changes: 85 additions & 16 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_hir::def::{self, *};
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
use rustc_index::bit_set::DenseBitSet;
use rustc_metadata::creader::LoadedMacro;
use rustc_middle::metadata::ModChild;
use rustc_middle::metadata::{AmbigModChildKind, ModChild};
use rustc_middle::ty::{Feed, Visibility};
use rustc_middle::{bug, span_bug};
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind};
Expand All @@ -36,9 +36,9 @@ use crate::imports::{ImportData, ImportKind};
use crate::macros::{MacroRulesBinding, MacroRulesScope, MacroRulesScopeRef};
use crate::ref_mut::CmCell;
use crate::{
BindingKey, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind, ModuleOrUniformRoot,
NameBinding, ParentScope, PathResult, ResolutionError, Resolver, Segment, Used,
VisResolutionError, errors,
AmbiguityKind, BindingKey, ExternPreludeEntry, Finalize, MacroData, Module, ModuleKind,
ModuleOrUniformRoot, NameBinding, NameBindingData, NameBindingKind, ParentScope, PathResult,
ResolutionError, Resolver, Segment, Used, VisResolutionError, errors,
};

type Res = def::Res<NodeId>;
Expand Down Expand Up @@ -81,9 +81,19 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
res: Res,
vis: Visibility<DefId>,
span: Span,
expn_id: LocalExpnId,
expansion: LocalExpnId,
ambiguity: Option<(NameBinding<'ra>, AmbiguityKind)>,
_warn_ambiguity: bool,
) {
let binding = self.arenas.new_res_binding(res, vis, span, expn_id);
let binding = self.arenas.alloc_name_binding(NameBindingData {
kind: NameBindingKind::Res(res),
ambiguity,
// FIXME: report everything as an error for crater
warn_ambiguity: false,
vis,
span,
expansion,
});
// Even if underscore names cannot be looked up, we still need to add them to modules,
// because they can be fetched by glob imports from those modules, and bring traits
// into scope both directly and through glob imports.
Expand Down Expand Up @@ -232,9 +242,22 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}

pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
for (i, child) in self.tcx.module_children(module.def_id()).into_iter().enumerate() {
let parent_scope = ParentScope::module(module, self.arenas);
self.build_reduced_graph_for_external_crate_res(child, parent_scope, i)
let def_id = module.def_id();
let children = self.tcx.module_children(def_id);
let parent_scope = ParentScope::module(module, self.arenas);
for (i, child) in self.tcx.module_children(def_id).iter().enumerate() {
self.build_reduced_graph_for_external_crate_res(child, parent_scope, i, None, false)
}
for (i, child) in
self.cstore().ambig_module_children_untracked(def_id, self.tcx.sess).enumerate()
{
self.build_reduced_graph_for_external_crate_res(
&child.main,
parent_scope,
children.len() + i,
Some((&child.second, child.kind)),
child.warn_ambiguity,
)
}
}

Expand All @@ -244,6 +267,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
child: &ModChild,
parent_scope: ParentScope<'ra>,
child_index: usize,
ambig_child: Option<(&ModChild, AmbigModChildKind)>,
warn_ambiguity: bool,
) {
let parent = parent_scope.module;
let ModChild { ident, res, vis, ref reexport_chain } = *child;
Expand All @@ -255,6 +280,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
);
let res = res.expect_non_local();
let expansion = parent_scope.expansion;
let ambig = ambig_child.map(|(ambig_child, ambig_kind)| {
let ModChild { ident: _, res, vis, ref reexport_chain } = *ambig_child;
let span = self.def_span(
reexport_chain
.first()
.and_then(|reexport| reexport.id())
.unwrap_or_else(|| res.def_id()),
);
let res = res.expect_non_local();
let ambig_kind = match ambig_kind {
AmbigModChildKind::GlobVsGlob => AmbiguityKind::GlobVsGlob,
AmbigModChildKind::GlobVsExpanded => AmbiguityKind::GlobVsExpanded,
};
(self.arenas.new_res_binding(res, vis, span, expansion), ambig_kind)
});
// Record primary definitions.
match res {
Res::Def(
Expand All @@ -272,9 +312,18 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
_,
)
| Res::PrimTy(..)
| Res::ToolMod => {
self.define_extern(parent, ident, TypeNS, child_index, res, vis, span, expansion)
}
| Res::ToolMod => self.define_extern(
parent,
ident,
TypeNS,
child_index,
res,
vis,
span,
expansion,
ambig,
warn_ambiguity,
),
Res::Def(
DefKind::Fn
| DefKind::AssocFn
Expand All @@ -283,10 +332,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
| DefKind::AssocConst
| DefKind::Ctor(..),
_,
) => self.define_extern(parent, ident, ValueNS, child_index, res, vis, span, expansion),
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => {
self.define_extern(parent, ident, MacroNS, child_index, res, vis, span, expansion)
}
) => self.define_extern(
parent,
ident,
ValueNS,
child_index,
res,
vis,
span,
expansion,
ambig,
warn_ambiguity,
),
Res::Def(DefKind::Macro(..), _) | Res::NonMacroAttr(..) => self.define_extern(
parent,
ident,
MacroNS,
child_index,
res,
vis,
span,
expansion,
ambig,
warn_ambiguity,
),
Res::Def(
DefKind::TyParam
| DefKind::ConstParam
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
for ambiguity_error in &self.ambiguity_errors {
let diag = self.ambiguity_diagnostics(ambiguity_error);
if ambiguity_error.warning {
let NameBindingKind::Import { import, .. } = ambiguity_error.b1.0.kind else {
unreachable!()
let node_id = match ambiguity_error.b1.0.kind {
NameBindingKind::Import { import, .. } => import.root_id,
NameBindingKind::Res(_) => CRATE_NODE_ID,
};
self.lint_buffer.buffer_lint(
AMBIGUOUS_GLOB_IMPORTS,
import.root_id,
node_id,
ambiguity_error.ident.span,
BuiltinLintDiag::AmbiguousGlobImports { diag },
);
Expand Down
Loading
Loading