Skip to content
Merged
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
11 changes: 6 additions & 5 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
match vis.kind {
ast::VisibilityKind::Public => Ok(Visibility::Public),
ast::VisibilityKind::Inherited => {
Ok(match parent_scope.module.kind {
Ok(match parent_scope.module.expect_local().kind {
// Any inherited visibility resolved directly inside an enum or trait
// (i.e. variants, fields, and trait items) inherits from the visibility
// of the enum or trait.
Expand Down Expand Up @@ -535,7 +535,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
root_id: NodeId,
vis: Visibility,
) {
let current_module = self.parent_scope.module;
let current_module = self.parent_scope.module.expect_local();
let import = self.r.arenas.alloc_import(ImportData {
kind,
parent_scope: self.parent_scope,
Expand All @@ -560,7 +560,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
if target.name != kw::Underscore {
self.r.per_ns(|this, ns| {
let key = BindingKey::new(IdentKey::new(target), ns);
this.resolution_or_default(current_module, key, target.span)
this.resolution_or_default(current_module.to_module(), key, target.span)
.borrow_mut(this)
.single_imports
.insert(import);
Expand Down Expand Up @@ -1136,7 +1136,7 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
if let Some(Attribute::Parsed(AttributeKind::MacroUse { span, arguments })) =
AttributeParser::parse_limited(self.r.tcx.sess, &item.attrs, &[sym::macro_use])
{
if self.parent_scope.module.parent.is_some() {
if self.parent_scope.module.expect_local().parent.is_some() {
self.r
.dcx()
.emit_err(errors::ExternCrateLoadingMacroNotAtCrateRoot { span: item.span });
Expand Down Expand Up @@ -1247,7 +1247,8 @@ impl<'a, 'ra, 'tcx> DefCollector<'a, 'ra, 'tcx> {
/// directly into its parent scope's module.
pub(crate) fn visit_invoc_in_module(&mut self, id: NodeId) -> MacroRulesScopeRef<'ra> {
let invoc_id = self.visit_invoc(id);
self.parent_scope.module.unexpanded_invocations.borrow_mut(self.r).insert(invoc_id);
let module = self.parent_scope.module.expect_local();
module.unexpanded_invocations.borrow_mut(self.r).insert(invoc_id);
self.r.arenas.alloc_macro_rules_scope(MacroRulesScope::Invocation(invoc_id))
}

Expand Down
41 changes: 22 additions & 19 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
return self.report_conflict(ident, ns, new_binding, old_binding);
}

let container = match old_binding.parent_module.unwrap().kind {
let container = match old_binding.parent_module.unwrap().expect_local().kind {
// Avoid using TyCtxt::def_kind_descr in the resolver, because it
// indirectly *calls* the resolver, and would cause a query cycle.
ModuleKind::Def(kind, def_id, _, _) => kind.descr(def_id),
Expand Down Expand Up @@ -2597,7 +2597,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let module = module.to_module();
current_module.is_ancestor_of(module) && current_module != module
})
.flat_map(|(_, module)| module.kind.name()),
.flat_map(|(_, module)| module.name()),
)
.chain(
self.extern_module_map
Expand All @@ -2607,7 +2607,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let module = module.to_module();
current_module.is_ancestor_of(module) && current_module != module
})
.flat_map(|(_, module)| module.kind.name()),
.flat_map(|(_, module)| module.name()),
)
.filter(|c| !c.to_string().is_empty())
.collect::<Vec<_>>();
Expand All @@ -2631,8 +2631,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
) -> (String, String, Option<Suggestion>) {
let is_last = failed_segment_idx == path.len() - 1;
let ns = if is_last { opt_ns.unwrap_or(TypeNS) } else { TypeNS };
let module_res = match module {
Some(ModuleOrUniformRoot::Module(module)) => module.res(),
let module_def_id = match module {
Some(ModuleOrUniformRoot::Module(module)) => module.opt_def_id(),
_ => None,
};
let scope = match &path[..failed_segment_idx] {
Expand All @@ -2647,7 +2647,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
};
let message = format!("cannot find `{ident}` in {scope}");

if module_res == self.graph_root.res() {
if module_def_id == Some(CRATE_DEF_ID.to_def_id()) {
let is_mod = |res| matches!(res, Res::Def(DefKind::Mod, _));
let mut candidates = self.lookup_import_candidates(ident, TypeNS, parent_scope, is_mod);
candidates
Expand Down Expand Up @@ -3145,7 +3145,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
if !kinds.contains(MacroKinds::BANG) {
return None;
}
let module_name = crate_module.kind.name().unwrap_or(kw::Crate);
let module_name = crate_module.name().unwrap_or(kw::Crate);
let import_snippet = match import.kind {
ImportKind::Single { source, target, .. } if source != target => {
format!("{source} as {target}")
Expand Down Expand Up @@ -3290,20 +3290,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
return cached;
}
visited.insert(parent_module, false);
let m = r.expect_module(parent_module);
let mut res = false;
for importer in m.glob_importers.borrow().iter() {
if let Some(next_parent_module) = importer.parent_scope.module.opt_def_id() {
if next_parent_module == module
|| comes_from_same_module_for_glob(
r,
next_parent_module,
module,
visited,
)
let m = r.expect_module(parent_module);
if m.is_local() {
for importer in m.glob_importers.borrow().iter() {
if let Some(next_parent_module) = importer.parent_scope.module.opt_def_id()
{
res = true;
break;
if next_parent_module == module
|| comes_from_same_module_for_glob(
r,
next_parent_module,
module,
visited,
)
{
res = true;
break;
}
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,10 +643,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
Err(ControlFlow::Break(..)) => return decl,
}
}
Scope::ModuleGlobs(module, _)
if let ModuleKind::Def(_, def_id, _, _) = module.kind
&& !def_id.is_local() =>
{
Scope::ModuleGlobs(module, _) if !module.is_local() => {
// Fast path: external module decoding only creates non-glob declarations.
Err(Determined)
}
Expand Down Expand Up @@ -699,9 +696,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
Scope::MacroUsePrelude => match self.macro_use_prelude.get(&ident.name).cloned() {
Some(decl) => Ok(decl),
None => Err(Determinacy::determined(
self.graph_root.unexpanded_invocations.borrow().is_empty(),
)),
None => Err(Determinacy::determined(!self.graph_root.has_unexpanded_invocations())),
},
Scope::BuiltinAttrs => match self.builtin_attr_decls.get(&ident.name) {
Some(decl) => Ok(*decl),
Expand All @@ -714,9 +709,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
finalize.is_some(),
) {
Some(decl) => Ok(decl),
None => Err(Determinacy::determined(
self.graph_root.unexpanded_invocations.borrow().is_empty(),
)),
None => {
Err(Determinacy::determined(!self.graph_root.has_unexpanded_invocations()))
}
}
}
Scope::ExternPreludeFlags => {
Expand Down Expand Up @@ -1126,6 +1121,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
return if accessible { Ok(binding) } else { Err(ControlFlow::Break(Determined)) };
}

// In extern modules everything is determined from the start.
if !module.is_local() {
return Err(ControlFlow::Continue(Determined));
};

// Check if one of single imports can still define the name, block if it can.
if self.reborrow().single_import_can_define_name(
&resolution,
Expand All @@ -1139,7 +1139,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}

// Check if one of unexpanded macros can still define the name.
if !module.unexpanded_invocations.borrow().is_empty() {
if module.has_unexpanded_invocations() {
return Err(ControlFlow::Continue(Undetermined));
}

Expand Down Expand Up @@ -1223,7 +1223,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
// scopes we return `Undetermined` with `ControlFlow::Continue`.
// Check if one of unexpanded macros can still define the name,
// if it can then our "no resolution" result is not determined and can be invalidated.
if !module.unexpanded_invocations.borrow().is_empty() {
if module.has_unexpanded_invocations() {
return Err(ControlFlow::Continue(Undetermined));
}

Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_resolve/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}

// Add to module's glob_importers
module.glob_importers.borrow_mut_unchecked().push(import);
if module.is_local() {
module.glob_importers.borrow_mut_unchecked().push(import);
}

// Ensure that `resolutions` isn't borrowed during `try_define`,
// since it might get updated via a glob cycle.
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1477,7 +1477,7 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
// During late resolution we only track the module component of the parent scope,
// although it may be useful to track other components as well for diagnostics.
let graph_root = resolver.graph_root;
let parent_scope = ParentScope::module(graph_root.to_module(), resolver.arenas);
let parent_scope = ParentScope::module(graph_root, resolver.arenas);
let start_rib_kind = RibKind::Module(graph_root);
LateResolutionVisitor {
r: resolver,
Expand Down
13 changes: 4 additions & 9 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ use crate::late::{
};
use crate::ty::fast_reject::SimplifiedType;
use crate::{
Finalize, Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, PathSource, Res,
Resolver, ScopeSet, Segment, errors, path_names_to_string,
Finalize, Module, ModuleOrUniformRoot, ParentScope, PathResult, PathSource, Res, Resolver,
ScopeSet, Segment, errors, path_names_to_string,
};

/// A field or associated item from self type suggested in case of resolution failure.
Expand Down Expand Up @@ -1698,7 +1698,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
&& let TyKind::Path(_, self_ty_path) = &self_ty.kind
&& let PathResult::Module(ModuleOrUniformRoot::Module(module)) =
self.resolve_path(&Segment::from_path(self_ty_path), Some(TypeNS), None, source)
&& let ModuleKind::Def(DefKind::Trait, ..) = module.kind
&& module.def_kind() == Some(DefKind::Trait)
&& trait_ref.path.span == span
&& let PathSource::Trait(_) = source
&& let Some(Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) = res
Expand Down Expand Up @@ -3014,12 +3014,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
fn find_module(&self, def_id: DefId) -> Option<(Module<'ra>, ImportSuggestion)> {
let mut result = None;
let mut seen_modules = FxHashSet::default();
let root_did = self.r.graph_root.def_id();
let mut worklist = vec![(
self.r.graph_root.to_module(),
ThinVec::new(),
root_did.is_local() || !self.r.tcx.is_doc_hidden(root_did),
)];
let mut worklist = vec![(self.r.graph_root.to_module(), ThinVec::new(), true)];

while let Some((in_module, path_segments, doc_visible)) = worklist.pop() {
// abort if the module is already found
Expand Down
Loading
Loading