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
28 changes: 18 additions & 10 deletions compiler/rustc_middle/src/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::hash::Hash;
use rustc_data_structures::fx::{FxIndexMap, IndexEntry};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_hir::def::DefKind;
use rustc_hir::{ItemKind, Node, UseKind};
use rustc_macros::HashStable;
use rustc_span::def_id::{CRATE_DEF_ID, LocalDefId};

Expand Down Expand Up @@ -152,9 +153,9 @@ impl EffectiveVisibilities {
}

pub fn check_invariants(&self, tcx: TyCtxt<'_>) {
if !cfg!(debug_assertions) {
return;
}
// if !cfg!(debug_assertions) {
// return;
// }
for (&def_id, ev) in &self.map {
// More direct visibility levels can never go farther than less direct ones,
// and all effective visibilities are larger or equal than private visibility.
Expand Down Expand Up @@ -184,13 +185,20 @@ impl EffectiveVisibilities {
if !is_impl && tcx.trait_impl_of_assoc(def_id.to_def_id()).is_none() {
let nominal_vis = tcx.visibility(def_id);
if !nominal_vis.is_at_least(ev.reachable, tcx) {
span_bug!(
span,
"{:?}: reachable {:?} > nominal {:?}",
def_id,
ev.reachable,
nominal_vis,
);
if let Node::Item(item) = tcx.hir_node_by_def_id(def_id)
&& let ItemKind::Use(_, UseKind::Glob) = item.kind
{
// Glob import visibilities can be increasee by other
// more public glob imports in cases of ambiguity.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fix for #151124.

} else {
span_bug!(
span,
"{:?}: reachable {:?} > nominal {:?}",
def_id,
ev.reachable,
nominal_vis,
);
}
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions compiler/rustc_resolve/src/build_reduced_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ambiguity: CmCell::new(ambiguity),
// External ambiguities always report the `AMBIGUOUS_GLOB_IMPORTS` lint at the moment.
warn_ambiguity: CmCell::new(true),
vis: CmCell::new(vis),
initial_vis: vis,
ambiguity_vis_max: CmCell::new(None),
ambiguity_vis_min: CmCell::new(None),
span,
expansion,
parent_module: Some(parent),
Expand Down Expand Up @@ -250,16 +252,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
pub(crate) fn build_reduced_graph_external(&self, module: Module<'ra>) {
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 children.iter().enumerate() {
self.build_reduced_graph_for_external_crate_res(child, parent_scope, i, None)
self.build_reduced_graph_for_external_crate_res(child, module, i, None)
}
for (i, child) in
self.cstore().ambig_module_children_untracked(self.tcx, def_id).enumerate()
{
self.build_reduced_graph_for_external_crate_res(
&child.main,
parent_scope,
module,
children.len() + i,
Some(&child.second),
)
Expand All @@ -270,11 +271,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
fn build_reduced_graph_for_external_crate_res(
&self,
child: &ModChild,
parent_scope: ParentScope<'ra>,
parent: Module<'ra>,
child_index: usize,
ambig_child: Option<&ModChild>,
) {
let parent = parent_scope.module;
let child_span = |this: &Self, reexport_chain: &[Reexport], res: def::Res<_>| {
this.def_span(
reexport_chain
Expand All @@ -287,7 +287,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
let ident = IdentKey::new(orig_ident);
let span = child_span(self, reexport_chain, res);
let res = res.expect_non_local();
let expansion = parent_scope.expansion;
let expansion = LocalExpnId::ROOT;
let ambig = ambig_child.map(|ambig_child| {
let ModChild { ident: _, res, vis, ref reexport_chain } = *ambig_child;
let span = child_span(self, reexport_chain, res);
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_resolve/src/effective_visibilities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ impl<'a, 'ra, 'tcx> EffectiveVisibilitiesVisitor<'a, 'ra, 'tcx> {
parent_id.level(),
tcx,
);
if let Some(max_vis_decl) = decl.ambiguity_vis_max.get() {
// Avoid the most visible import in an ambiguous glob set being reported as unused.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a fix for #152004.

self.update_import(max_vis_decl, parent_id);
}
}

fn update_def(
Expand Down
46 changes: 37 additions & 9 deletions compiler/rustc_resolve/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use Namespace::*;
use rustc_ast::{self as ast, NodeId};
use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::{DefKind, MacroKinds, Namespace, NonMacroAttrKind, PartialRes, PerNS};
use rustc_middle::ty::Visibility;
use rustc_middle::{bug, span_bug};
use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK;
use rustc_session::parse::feature_err;
Expand All @@ -24,9 +23,9 @@ use crate::late::{
use crate::macros::{MacroRulesScope, sub_namespace_match};
use crate::{
AmbiguityError, AmbiguityKind, AmbiguityWarning, BindingKey, CmResolver, Decl, DeclKind,
Determinacy, Finalize, IdentKey, ImportKind, LateDecl, Module, ModuleKind, ModuleOrUniformRoot,
ParentScope, PathResult, PrivacyError, Res, ResolutionError, Resolver, Scope, ScopeSet,
Segment, Stage, Symbol, Used, errors,
Determinacy, Finalize, IdentKey, ImportKind, ImportSummary, LateDecl, Module, ModuleKind,
ModuleOrUniformRoot, ParentScope, PathResult, PrivacyError, Res, ResolutionError, Resolver,
Scope, ScopeSet, Segment, Stage, Symbol, Used, errors,
};

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -491,6 +490,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
}
Some(Finalize { import_vis, .. }) => import_vis,
};
this.get_mut().maybe_push_glob_vs_glob_vis_ambiguity(
ident,
orig_ident_span,
decl,
import_vis,
);

if let Some(&(innermost_decl, _)) = innermost_results.first() {
// Found another solution, if the first one was "weak", report an error.
Expand Down Expand Up @@ -780,6 +785,30 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
ret.map_err(ControlFlow::Continue)
}

fn maybe_push_glob_vs_glob_vis_ambiguity(
&mut self,
ident: IdentKey,
orig_ident_span: Span,
decl: Decl<'ra>,
import_vis: Option<ImportSummary>,
) {
let Some(import) = import_vis else { return };
let min1 = self.import_decl_vis(decl, import);
let min2 = self.import_decl_vis_ext(decl, import, true);
if min1 != min2 {
self.ambiguity_errors.push(AmbiguityError {
kind: AmbiguityKind::GlobVsGlob,
ambig_vis: Some((min1, min2)),
ident: ident.orig(orig_ident_span),
b1: decl.ambiguity_vis_max.get().unwrap_or(decl),
b2: decl.ambiguity_vis_min.get().unwrap_or(decl),
scope1: Scope::ModuleGlobs(decl.parent_module.unwrap(), None),
scope2: Scope::ModuleGlobs(decl.parent_module.unwrap(), None),
warning: Some(AmbiguityWarning::GlobImport),
});
}
}

fn maybe_push_ambiguity(
&mut self,
ident: IdentKey,
Expand All @@ -790,16 +819,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
decl: Decl<'ra>,
scope: Scope<'ra>,
innermost_results: &[(Decl<'ra>, Scope<'ra>)],
import_vis: Option<Visibility>,
import_vis: Option<ImportSummary>,
) -> bool {
let (innermost_decl, innermost_scope) = innermost_results[0];
let (res, innermost_res) = (decl.res(), innermost_decl.res());
let ambig_vis = if res != innermost_res {
None
} else if let Some(import_vis) = import_vis
&& let min =
(|d: Decl<'_>| d.vis().min(import_vis.to_def_id(), self.tcx).expect_local())
&& let (min1, min2) = (min(decl), min(innermost_decl))
} else if let Some(import) = import_vis
&& let min1 = self.import_decl_vis(decl, import)
&& let min2 = self.import_decl_vis(innermost_decl, import)
&& min1 != min2
{
Some((min1, min2))
Expand Down
Loading
Loading