Skip to content

Commit 72a4557

Browse files
authored
Unrolled build for #155353
Rollup merge of #155353 - petrochenkov:globdedup, r=mu001999 resolve: Remove `inaccessible_ctor_reexport` resolver field Collect the necessary information during error reporting instead of doing it on a good path from the core name resolution infra. Also cleanup the related diagnostic code for struct constructors in general, see the individual commits. This mitigates most of the harm brought by #133477.
2 parents d227e48 + 19c7df6 commit 72a4557

8 files changed

Lines changed: 104 additions & 162 deletions

File tree

compiler/rustc_resolve/src/build_reduced_graph.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,16 @@ use tracing::debug;
3232

3333
use crate::Namespace::{MacroNS, TypeNS, ValueNS};
3434
use crate::def_collector::collect_definitions;
35+
use crate::diagnostics::StructCtor;
3536
use crate::imports::{ImportData, ImportKind, OnUnknownData};
3637
use crate::macros::{MacroRulesDecl, MacroRulesScope, MacroRulesScopeRef};
3738
use crate::ref_mut::CmCell;
3839
use crate::{
3940
BindingKey, Decl, DeclData, DeclKind, ExternPreludeEntry, Finalize, IdentKey, MacroData,
40-
Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, ResolutionError, Resolver,
41-
Segment, Used, VisResolutionError, errors,
41+
Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, Res, ResolutionError,
42+
Resolver, Segment, Used, VisResolutionError, errors,
4243
};
4344

44-
type Res = def::Res<NodeId>;
45-
4645
impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
4746
/// Attempt to put the declaration with the given name and namespace into the module,
4847
/// and report an error in case of a collision.
@@ -929,7 +928,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
929928
vis
930929
};
931930

932-
let mut ret_fields = Vec::with_capacity(vdata.fields().len());
931+
let mut field_visibilities = Vec::with_capacity(vdata.fields().len());
933932

934933
for field in vdata.fields() {
935934
// NOTE: The field may be an expansion placeholder, but expansion sets
@@ -941,7 +940,7 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
941940
if ctor_vis.is_at_least(field_vis, self.r.tcx) {
942941
ctor_vis = field_vis;
943942
}
944-
ret_fields.push(field_vis.to_def_id());
943+
field_visibilities.push(field_vis.to_def_id());
945944
}
946945
let feed = self.r.feed(ctor_node_id);
947946
let ctor_def_id = feed.key();
@@ -951,9 +950,9 @@ impl<'a, 'ra, 'tcx> BuildReducedGraphVisitor<'a, 'ra, 'tcx> {
951950
// We need the field visibility spans also for the constructor for E0603.
952951
self.insert_field_visibilities_local(ctor_def_id.to_def_id(), vdata.fields());
953952

954-
self.r
955-
.struct_constructors
956-
.insert(local_def_id, (ctor_res, ctor_vis.to_def_id(), ret_fields));
953+
let ctor =
954+
StructCtor { res: ctor_res, vis: ctor_vis.to_def_id(), field_visibilities };
955+
self.r.struct_ctors.insert(local_def_id, ctor);
957956
}
958957
self.r.struct_generics.insert(local_def_id, generics.clone());
959958
}

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ use rustc_errors::{
1717
use rustc_feature::BUILTIN_ATTRIBUTES;
1818
use rustc_hir::attrs::{CfgEntry, StrippedCfgItem};
1919
use rustc_hir::def::Namespace::{self, *};
20-
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, MacroKinds, NonMacroAttrKind, PerNS};
20+
use rustc_hir::def::{CtorKind, CtorOf, DefKind, MacroKinds, NonMacroAttrKind, PerNS};
2121
use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
2222
use rustc_hir::{PrimTy, Stability, StabilityLevel, find_attr};
2323
use rustc_middle::bug;
24-
use rustc_middle::ty::TyCtxt;
24+
use rustc_middle::ty::{TyCtxt, Visibility};
2525
use rustc_session::Session;
2626
use rustc_session::lint::builtin::{
2727
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE, AMBIGUOUS_GLOB_IMPORTS, AMBIGUOUS_IMPORT_VISIBILITIES,
@@ -49,20 +49,31 @@ use crate::late::{DiagMetadata, PatternSource, Rib};
4949
use crate::{
5050
AmbiguityError, AmbiguityKind, AmbiguityWarning, BindingError, BindingKey, Decl, DeclKind,
5151
Finalize, ForwardGenericParamBanReason, HasGenericParams, IdentKey, LateDecl, MacroRulesScope,
52-
Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, PrivacyError,
52+
Module, ModuleKind, ModuleOrUniformRoot, ParentScope, PathResult, PrivacyError, Res,
5353
ResolutionError, Resolver, Scope, ScopeSet, Segment, UseError, Used, VisResolutionError,
5454
errors as errs, path_names_to_string,
5555
};
5656

57-
type Res = def::Res<ast::NodeId>;
58-
5957
/// A vector of spans and replacements, a message and applicability.
6058
pub(crate) type Suggestion = (Vec<(Span, String)>, String, Applicability);
6159

6260
/// Potential candidate for an undeclared or out-of-scope label - contains the ident of a
6361
/// similarly named label and whether or not it is reachable.
6462
pub(crate) type LabelSuggestion = (Ident, bool);
6563

64+
#[derive(Clone)]
65+
pub(crate) struct StructCtor {
66+
pub res: Res,
67+
pub vis: Visibility<DefId>,
68+
pub field_visibilities: Vec<Visibility<DefId>>,
69+
}
70+
71+
impl StructCtor {
72+
pub(crate) fn has_private_fields<'ra>(&self, m: Module<'ra>, r: &Resolver<'ra, '_>) -> bool {
73+
self.field_visibilities.iter().any(|&vis| !r.is_accessible_from(vis, m))
74+
}
75+
}
76+
6677
#[derive(Debug)]
6778
pub(crate) enum SuggestionTarget {
6879
/// The target has a similar name as the name used by the programmer (probably a typo)
@@ -3176,6 +3187,25 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
31763187
err.subdiagnostic(note);
31773188
}
31783189
}
3190+
3191+
pub(crate) fn struct_ctor(&self, def_id: DefId) -> Option<StructCtor> {
3192+
match def_id.as_local() {
3193+
Some(def_id) => self.struct_ctors.get(&def_id).cloned(),
3194+
None => {
3195+
self.cstore().ctor_untracked(self.tcx, def_id).map(|(ctor_kind, ctor_def_id)| {
3196+
let res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id);
3197+
let vis = self.tcx.visibility(ctor_def_id);
3198+
let field_visibilities = self
3199+
.tcx
3200+
.associated_item_def_ids(def_id)
3201+
.iter()
3202+
.map(|&field_id| self.tcx.visibility(field_id))
3203+
.collect();
3204+
StructCtor { res, vis, field_visibilities }
3205+
})
3206+
}
3207+
}
3208+
}
31793209
}
31803210

31813211
/// Given a `binding_span` of a binding within a use statement:

compiler/rustc_resolve/src/ident.rs

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
10871087
orig_ident_span,
10881088
binding,
10891089
parent_scope,
1090-
module,
10911090
finalize,
10921091
shadowing,
10931092
);
@@ -1150,7 +1149,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
11501149
orig_ident_span,
11511150
binding,
11521151
parent_scope,
1153-
module,
11541152
finalize,
11551153
shadowing,
11561154
);
@@ -1260,7 +1258,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12601258
orig_ident_span: Span,
12611259
binding: Option<Decl<'ra>>,
12621260
parent_scope: &ParentScope<'ra>,
1263-
module: Module<'ra>,
12641261
finalize: Finalize,
12651262
shadowing: Shadowing,
12661263
) -> Result<Decl<'ra>, ControlFlow<Determinacy, Determinacy>> {
@@ -1295,37 +1292,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12951292
self.macro_expanded_macro_export_errors.insert((path_span, binding.span));
12961293
}
12971294

1298-
// If we encounter a re-export for a type with private fields, it will not be able to
1299-
// be constructed through this re-export. We track that case here to expand later
1300-
// privacy errors with appropriate information.
1301-
if let Res::Def(_, def_id) = binding.res() {
1302-
let struct_ctor = match def_id.as_local() {
1303-
Some(def_id) => self.struct_constructors.get(&def_id).cloned(),
1304-
None => {
1305-
let ctor = self.cstore().ctor_untracked(self.tcx(), def_id);
1306-
ctor.map(|(ctor_kind, ctor_def_id)| {
1307-
let ctor_res = Res::Def(
1308-
DefKind::Ctor(rustc_hir::def::CtorOf::Struct, ctor_kind),
1309-
ctor_def_id,
1310-
);
1311-
let ctor_vis = self.tcx.visibility(ctor_def_id);
1312-
let field_visibilities = self
1313-
.tcx
1314-
.associated_item_def_ids(def_id)
1315-
.iter()
1316-
.map(|&field_id| self.tcx.visibility(field_id))
1317-
.collect();
1318-
(ctor_res, ctor_vis, field_visibilities)
1319-
})
1320-
}
1321-
};
1322-
if let Some((_, _, fields)) = struct_ctor
1323-
&& fields.iter().any(|vis| !self.is_accessible_from(*vis, module))
1324-
{
1325-
self.inaccessible_ctor_reexport.insert(path_span, binding.span);
1326-
}
1327-
}
1328-
13291295
self.record_use(ident, binding, used);
13301296
return Ok(binding);
13311297
}

compiler/rustc_resolve/src/imports.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ use crate::errors::{
3737
use crate::ref_mut::CmCell;
3838
use crate::{
3939
AmbiguityError, BindingKey, CmResolver, Decl, DeclData, DeclKind, Determinacy, Finalize,
40-
IdentKey, ImportSuggestion, Module, ModuleOrUniformRoot, ParentScope, PathResult, PerNS,
40+
IdentKey, ImportSuggestion, Module, ModuleOrUniformRoot, ParentScope, PathResult, PerNS, Res,
4141
ResolutionError, Resolver, ScopeSet, Segment, Used, module_to_string, names_to_string,
4242
};
4343

44-
type Res = def::Res<NodeId>;
45-
4644
/// A potential import declaration in the process of being planted into a module.
4745
/// Also used for lazily planting names from `--extern` flags to extern prelude.
4846
#[derive(Clone, Copy, Default, PartialEq, Debug)]

compiler/rustc_resolve/src/late.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_errors::{
2525
StashKey, Suggestions, elided_lifetime_in_path_suggestion, pluralize,
2626
};
2727
use rustc_hir::def::Namespace::{self, *};
28-
use rustc_hir::def::{self, CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS};
28+
use rustc_hir::def::{CtorKind, DefKind, LifetimeRes, NonMacroAttrKind, PartialRes, PerNS};
2929
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LOCAL_CRATE, LocalDefId};
3030
use rustc_hir::{MissingLifetimeKind, PrimTy, TraitCandidate};
3131
use rustc_middle::middle::resolve_bound_vars::Set1;
@@ -41,14 +41,12 @@ use tracing::{debug, instrument, trace};
4141

4242
use crate::{
4343
BindingError, BindingKey, Decl, DelegationFnSig, Finalize, IdentKey, LateDecl, Module,
44-
ModuleOrUniformRoot, ParentScope, PathResult, ResolutionError, Resolver, Segment, Stage,
44+
ModuleOrUniformRoot, ParentScope, PathResult, Res, ResolutionError, Resolver, Segment, Stage,
4545
TyCtxt, UseError, Used, errors, path_names_to_string, rustdoc,
4646
};
4747

4848
mod diagnostics;
4949

50-
type Res = def::Res<NodeId>;
51-
5250
use diagnostics::{ElisionFnParameter, LifetimeElisionCandidate, MissingLifetime};
5351

5452
#[derive(Copy, Clone, Debug)]

0 commit comments

Comments
 (0)