From 17127f3e781a97c791f00b8d3cd6aab3d686246a Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 22 Feb 2023 01:11:16 +0400 Subject: [PATCH 1/8] resolve: Remove `visibility_untracked` --- .../rustc_metadata/src/rmeta/decoder/cstore_impl.rs | 4 ---- compiler/rustc_resolve/src/build_reduced_graph.rs | 11 ++++------- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 83a0e833edc1d..119011a9726d2 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -526,10 +526,6 @@ impl CStore { self.get_crate_data(def.krate).get_ctor(def.index) } - pub fn visibility_untracked(&self, def: DefId) -> Visibility { - self.get_crate_data(def.krate).get_visibility(def.index) - } - pub fn module_children_untracked<'a>( &'a self, def_id: DefId, diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 9526bca3df267..586c4b022ab2d 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -1007,18 +1007,15 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { // Record some extra data for better diagnostics. match res { Res::Def(DefKind::Struct, def_id) => { - let cstore = self.r.cstore(); - if let Some((ctor_kind, ctor_def_id)) = cstore.ctor_untracked(def_id) { + let ctor = self.r.cstore().ctor_untracked(def_id); + if let Some((ctor_kind, ctor_def_id)) = ctor { let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id); - let ctor_vis = cstore.visibility_untracked(ctor_def_id); + let ctor_vis = self.r.tcx.visibility(ctor_def_id); let field_visibilities = - cstore.struct_field_visibilities_untracked(def_id).collect(); - drop(cstore); + self.r.cstore().struct_field_visibilities_untracked(def_id).collect(); self.r .struct_constructors .insert(def_id, (ctor_res, ctor_vis, field_visibilities)); - } else { - drop(cstore); } self.insert_field_names_extern(def_id) } From 901f1c9c6265168b910b4c5eac4f71ca61621134 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 22 Feb 2023 01:24:55 +0400 Subject: [PATCH 2/8] resolve: Partially remove `item_attrs_untracked` --- compiler/rustc_resolve/src/diagnostics.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index adec7973671dd..13b93626d8f04 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1216,15 +1216,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { // a note about editions let note = if let Some(did) = did { let requires_note = !did.is_local() - && this.cstore().item_attrs_untracked(did, this.tcx.sess).any( + && this.tcx.get_attrs(did, sym::rustc_diagnostic_item).any( |attr| { - if attr.has_name(sym::rustc_diagnostic_item) { - [sym::TryInto, sym::TryFrom, sym::FromIterator] - .map(|x| Some(x)) - .contains(&attr.value_str()) - } else { - false - } + [sym::TryInto, sym::TryFrom, sym::FromIterator] + .map(|x| Some(x)) + .contains(&attr.value_str()) }, ); From c05b7bd7d05125cfc9cc34a7426a3e20ad561a15 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 22 Feb 2023 02:03:29 +0400 Subject: [PATCH 3/8] resolve: Remove `struct_field_visibilities_untracked` --- compiler/rustc_metadata/src/rmeta/decoder.rs | 15 +------ .../src/rmeta/decoder/cstore_impl.rs | 9 +--- .../rustc_resolve/src/build_reduced_graph.rs | 16 +------ .../rustc_resolve/src/late/diagnostics.rs | 43 +++++++++++++------ compiler/rustc_resolve/src/lib.rs | 4 +- 5 files changed, 37 insertions(+), 50 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 771fb09113437..f642902320d23 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -930,7 +930,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { self.root.tables.generics_of.get(self, item_id).unwrap().decode((self, sess)) } - fn get_visibility(self, id: DefIndex) -> ty::Visibility { + fn get_visibility(self, id: DefIndex) -> Visibility { self.root .tables .visibility @@ -1148,19 +1148,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .map(move |index| respan(self.get_span(index, sess), self.item_name(index))) } - fn get_struct_field_visibilities( - self, - id: DefIndex, - ) -> impl Iterator> + 'a { - self.root - .tables - .children - .get(self, id) - .expect("fields not encoded for a struct") - .decode(self) - .map(move |field_index| self.get_visibility(field_index)) - } - fn get_inherent_implementations_for_type( self, tcx: TyCtxt<'tcx>, diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 119011a9726d2..f6cc596d442cc 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -15,7 +15,7 @@ use rustc_middle::middle::exported_symbols::ExportedSymbol; use rustc_middle::middle::stability::DeprecationEntry; use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::query::{ExternProviders, Providers}; -use rustc_middle::ty::{self, TyCtxt, Visibility}; +use rustc_middle::ty::{self, TyCtxt}; use rustc_session::cstore::{CrateSource, CrateStore}; use rustc_session::{Session, StableCrateId}; use rustc_span::hygiene::{ExpnHash, ExpnId}; @@ -515,13 +515,6 @@ impl CStore { self.get_crate_data(def.krate).get_struct_field_names(def.index, sess) } - pub fn struct_field_visibilities_untracked( - &self, - def: DefId, - ) -> impl Iterator> + '_ { - self.get_crate_data(def.krate).get_struct_field_visibilities(def.index) - } - pub fn ctor_untracked(&self, def: DefId) -> Option<(CtorKind, DefId)> { self.get_crate_data(def.krate).get_ctor(def.index) } diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 586c4b022ab2d..b5a3130919b91 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -789,7 +789,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { self.r .struct_constructors - .insert(def_id, (ctor_res, ctor_vis.to_def_id(), ret_fields)); + .insert(local_def_id, (ctor_res, ctor_vis.to_def_id(), ret_fields)); } } @@ -1006,19 +1006,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { } // Record some extra data for better diagnostics. match res { - Res::Def(DefKind::Struct, def_id) => { - let ctor = self.r.cstore().ctor_untracked(def_id); - if let Some((ctor_kind, ctor_def_id)) = ctor { - let ctor_res = Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id); - let ctor_vis = self.r.tcx.visibility(ctor_def_id); - let field_visibilities = - self.r.cstore().struct_field_visibilities_untracked(def_id).collect(); - self.r - .struct_constructors - .insert(def_id, (ctor_res, ctor_vis, field_visibilities)); - } - self.insert_field_names_extern(def_id) - } + Res::Def(DefKind::Struct, def_id) => self.insert_field_names_extern(def_id), Res::Def(DefKind::Union, def_id) => self.insert_field_names_extern(def_id), Res::Def(DefKind::AssocFn, def_id) => { if self.r.cstore().fn_has_self_parameter_untracked(def_id, self.r.tcx.sess) { diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 6133e75a78fff..67cd66e359bcf 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1408,19 +1408,38 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { self.suggest_using_enum_variant(err, source, def_id, span); } (Res::Def(DefKind::Struct, def_id), source) if ns == ValueNS => { - let (ctor_def, ctor_vis, fields) = - if let Some(struct_ctor) = self.r.struct_constructors.get(&def_id).cloned() { - if let PathSource::Expr(Some(parent)) = source { - if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind { - bad_struct_syntax_suggestion(def_id); - return true; - } + let struct_ctor = match def_id.as_local() { + Some(def_id) => self.r.struct_constructors.get(&def_id).cloned(), + None => { + let ctor = self.r.cstore().ctor_untracked(def_id); + ctor.map(|(ctor_kind, ctor_def_id)| { + let ctor_res = + Res::Def(DefKind::Ctor(CtorOf::Struct, ctor_kind), ctor_def_id); + let ctor_vis = self.r.tcx.visibility(ctor_def_id); + let field_visibilities = self + .r + .tcx + .associated_item_def_ids(def_id) + .iter() + .map(|field_id| self.r.tcx.visibility(field_id)) + .collect(); + (ctor_res, ctor_vis, field_visibilities) + }) + } + }; + + let (ctor_def, ctor_vis, fields) = if let Some(struct_ctor) = struct_ctor { + if let PathSource::Expr(Some(parent)) = source { + if let ExprKind::Field(..) | ExprKind::MethodCall(..) = parent.kind { + bad_struct_syntax_suggestion(def_id); + return true; } - struct_ctor - } else { - bad_struct_syntax_suggestion(def_id); - return true; - }; + } + struct_ctor + } else { + bad_struct_syntax_suggestion(def_id); + return true; + }; let is_accessible = self.r.is_accessible_from(ctor_vis, self.parent_scope.module); if !is_expected(ctor_def) || is_accessible { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index ce4834decfd5e..a53de2be6c635 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -35,7 +35,7 @@ use rustc_errors::{ use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind}; use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorOf, DefKind, DocLinkResMap, LifetimeRes, PartialRes, PerNS}; -use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap}; use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::definitions::DefPathData; use rustc_hir::TraitCandidate; @@ -1009,7 +1009,7 @@ pub struct Resolver<'a, 'tcx> { /// Table for mapping struct IDs into struct constructor IDs, /// it's not used during normal resolution, only for better error reporting. /// Also includes of list of each fields visibility - struct_constructors: DefIdMap<(Res, ty::Visibility, Vec>)>, + struct_constructors: LocalDefIdMap<(Res, ty::Visibility, Vec>)>, /// Features enabled for this crate. active_features: FxHashSet, From c7f424b80ab734cfeb5215be62cff3e48ed5da60 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 22 Feb 2023 02:25:35 +0400 Subject: [PATCH 4/8] resolve: Remove `fn_has_self_parameter_untracked` --- .../rustc_metadata/src/rmeta/decoder/cstore_impl.rs | 4 ---- compiler/rustc_resolve/src/build_reduced_graph.rs | 7 +------ compiler/rustc_resolve/src/late/diagnostics.rs | 12 +++++++++++- compiler/rustc_resolve/src/lib.rs | 6 +++--- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index f6cc596d442cc..7e08ab0448cf0 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -555,10 +555,6 @@ impl CStore { ) } - pub fn fn_has_self_parameter_untracked(&self, def: DefId, sess: &Session) -> bool { - self.get_crate_data(def.krate).get_fn_has_self_parameter(def.index, sess) - } - pub fn crate_source_untracked(&self, cnum: CrateNum) -> Lrc { self.get_crate_data(cnum).source.clone() } diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index b5a3130919b91..61b8ae34848c3 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -1008,11 +1008,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { match res { Res::Def(DefKind::Struct, def_id) => self.insert_field_names_extern(def_id), Res::Def(DefKind::Union, def_id) => self.insert_field_names_extern(def_id), - Res::Def(DefKind::AssocFn, def_id) => { - if self.r.cstore().fn_has_self_parameter_untracked(def_id, self.r.tcx.sess) { - self.r.has_self.insert(def_id); - } - } _ => {} } } @@ -1411,7 +1406,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> { AssocItemKind::Const(..) => (DefKind::AssocConst, ValueNS), AssocItemKind::Fn(box Fn { ref sig, .. }) => { if sig.decl.has_self() { - self.r.has_self.insert(def_id); + self.r.has_self.insert(local_def_id); } (DefKind::AssocFn, ValueNS) } diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 67cd66e359bcf..2f0ff011d7627 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1649,7 +1649,17 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { ) { let res = binding.res(); if filter_fn(res) { - if self.r.has_self.contains(&res.def_id()) { + let def_id = res.def_id(); + let has_self = match def_id.as_local() { + Some(def_id) => self.r.has_self.contains(&def_id), + None => self + .r + .tcx + .fn_arg_names(def_id) + .first() + .map_or(false, |ident| ident.name == kw::SelfLower), + }; + if has_self { return Some(AssocSuggestion::MethodWithSelf { called }); } else { match res { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index a53de2be6c635..02cf71f77c1e3 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -35,7 +35,7 @@ use rustc_errors::{ use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind}; use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorOf, DefKind, DocLinkResMap, LifetimeRes, PartialRes, PerNS}; -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap, LocalDefIdSet}; use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::definitions::DefPathData; use rustc_hir::TraitCandidate; @@ -881,7 +881,7 @@ pub struct Resolver<'a, 'tcx> { extern_prelude: FxHashMap>, /// N.B., this is used only for better diagnostics, not name resolution itself. - has_self: FxHashSet, + has_self: LocalDefIdSet, /// Names of fields of an item `DefId` accessible with dot syntax. /// Used for hints during error reporting. @@ -1249,7 +1249,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { prelude: None, extern_prelude, - has_self: FxHashSet::default(), + has_self: Default::default(), field_names: FxHashMap::default(), field_visibility_spans: FxHashMap::default(), From 2a716f35636678fde3f5f71f3163909484b99a55 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 22 Feb 2023 11:14:20 +0400 Subject: [PATCH 5/8] resolve: Centralize retrieval of items span and item name --- .../rustc_resolve/src/build_reduced_graph.rs | 3 +- compiler/rustc_resolve/src/diagnostics.rs | 33 +++++-------- compiler/rustc_resolve/src/late.rs | 2 +- .../rustc_resolve/src/late/diagnostics.rs | 47 +++++++------------ compiler/rustc_resolve/src/lib.rs | 21 +++------ 5 files changed, 37 insertions(+), 69 deletions(-) diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 61b8ae34848c3..415942405ae97 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -130,12 +130,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { }; let expn_id = self.cstore().module_expansion_untracked(def_id, &self.tcx.sess); - let span = self.cstore().get_span_untracked(def_id, &self.tcx.sess); Some(self.new_module( parent, ModuleKind::Def(def_kind, def_id, name), expn_id, - span, + self.def_span(def_id), // FIXME: Account for `#[no_implicit_prelude]` attributes. parent.map_or(false, |module| module.no_implicit_prelude), )) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 13b93626d8f04..62873342c2e4e 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -12,7 +12,7 @@ use rustc_errors::{struct_span_err, SuggestionStyle}; use rustc_feature::BUILTIN_ATTRIBUTES; use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind, PerNS}; -use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, CRATE_DEF_ID}; use rustc_hir::PrimTy; use rustc_middle::bug; use rustc_middle::ty::TyCtxt; @@ -555,25 +555,22 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { return err; } Res::SelfTyAlias { alias_to: def_id, .. } => { - if let Some(impl_span) = self.opt_span(def_id) { - err.span_label( - reduce_impl_span_to_impl_keyword(sm, impl_span), - "`Self` type implicitly declared here, by this `impl`", - ); - } + err.span_label( + reduce_impl_span_to_impl_keyword(sm, self.def_span(def_id)), + "`Self` type implicitly declared here, by this `impl`", + ); err.span_label(span, "use a type here instead"); return err; } Res::Def(DefKind::TyParam, def_id) => { - if let Some(span) = self.opt_span(def_id) { - err.span_label(span, "type parameter from outer function"); - } + err.span_label(self.def_span(def_id), "type parameter from outer function"); def_id } Res::Def(DefKind::ConstParam, def_id) => { - if let Some(span) = self.opt_span(def_id) { - err.span_label(span, "const parameter from outer function"); - } + err.span_label( + self.def_span(def_id), + "const parameter from outer function", + ); def_id } _ => { @@ -589,7 +586,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { // Try to retrieve the span of the function signature and generate a new // message with a local type or const parameter. let sugg_msg = "try using a local generic parameter instead"; - let name = self.opt_name(def_id).unwrap_or(sym::T); + let name = self.tcx.item_name(def_id); let (span, snippet) = if span.is_empty() { let snippet = format!("<{}>", name); (span, snippet) @@ -1369,8 +1366,8 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { } if ident.name == kw::Default && let ModuleKind::Def(DefKind::Enum, def_id, _) = parent_scope.module.kind - && let Some(span) = self.opt_span(def_id) { + let span = self.def_span(def_id); let source_map = self.tcx.sess.source_map(); let head_span = source_map.guess_head_span(span); if let Ok(head) = source_map.span_to_snippet(head_span) { @@ -1446,11 +1443,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { Some(suggestion) if suggestion.candidate == kw::Underscore => return false, Some(suggestion) => suggestion, }; - let def_span = suggestion.res.opt_def_id().and_then(|def_id| match def_id.krate { - LOCAL_CRATE => self.opt_span(def_id), - _ => Some(self.cstore().get_span_untracked(def_id, self.tcx.sess)), - }); - if let Some(def_span) = def_span { + if let Some(def_span) = suggestion.res.opt_def_id().map(|def_id| self.def_span(def_id)) { if span.overlaps(def_span) { // Don't suggest typo suggestion for itself like in the following: // error[E0423]: expected function, tuple struct or tuple variant, found struct `X` diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index cc3e142a5fd5f..eff10e5af9fe8 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -3376,7 +3376,7 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { participle: "defined", article: res.article(), shadowed_binding: res, - shadowed_binding_span: self.r.opt_span(def_id).expect("const parameter defined outside of local crate"), + shadowed_binding_span: self.r.def_span(def_id), } ); None diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 2f0ff011d7627..6a9b1505712cd 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -19,7 +19,7 @@ use rustc_errors::{ use rustc_hir as hir; use rustc_hir::def::Namespace::{self, *}; use rustc_hir::def::{self, CtorKind, CtorOf, DefKind}; -use rustc_hir::def_id::{DefId, CRATE_DEF_ID, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, CRATE_DEF_ID}; use rustc_hir::PrimTy; use rustc_session::lint; use rustc_session::parse::feature_err; @@ -166,13 +166,6 @@ impl TypoCandidate { } impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { - fn def_span(&self, def_id: DefId) -> Option { - match def_id.krate { - LOCAL_CRATE => self.r.opt_span(def_id), - _ => Some(self.r.cstore().get_span_untracked(def_id, self.r.tcx.sess)), - } - } - fn make_base_error( &mut self, path: &[Segment], @@ -191,7 +184,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { span, span_label: match res { Res::Def(kind, def_id) if kind == DefKind::TyParam => { - self.def_span(def_id).map(|span| (span, "found this type parameter")) + Some((self.r.def_span(def_id), "found this type parameter")) } _ => None, }, @@ -1295,9 +1288,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { } PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => { let span = find_span(&source, err); - if let Some(span) = self.def_span(def_id) { - err.span_label(span, &format!("`{}` defined here", path_str)); - } + err.span_label(self.r.def_span(def_id), &format!("`{path_str}` defined here")); let (tail, descr, applicability) = match source { PathSource::Pat | PathSource::TupleStruct(..) => { ("", "pattern", Applicability::MachineApplicable) @@ -1359,17 +1350,14 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { if self.r.tcx.sess.is_nightly_build() { let msg = "you might have meant to use `#![feature(trait_alias)]` instead of a \ `type` alias"; - if let Some(span) = self.def_span(def_id) { - if let Ok(snip) = self.r.tcx.sess.source_map().span_to_snippet(span) { - // The span contains a type alias so we should be able to - // replace `type` with `trait`. - let snip = snip.replacen("type", "trait", 1); - err.span_suggestion(span, msg, snip, Applicability::MaybeIncorrect); - } else { - err.span_help(span, msg); - } + let span = self.r.def_span(def_id); + if let Ok(snip) = self.r.tcx.sess.source_map().span_to_snippet(span) { + // The span contains a type alias so we should be able to + // replace `type` with `trait`. + let snip = snip.replacen("type", "trait", 1); + err.span_suggestion(span, msg, snip, Applicability::MaybeIncorrect); } else { - err.help(msg); + err.span_help(span, msg); } } } @@ -1512,9 +1500,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { match source { PathSource::Expr(_) | PathSource::TupleStruct(..) | PathSource::Pat => { let span = find_span(&source, err); - if let Some(span) = self.def_span(def_id) { - err.span_label(span, &format!("`{}` defined here", path_str)); - } + err.span_label( + self.r.def_span(def_id), + &format!("`{path_str}` defined here"), + ); err.span_suggestion( span, "use this syntax instead", @@ -1527,9 +1516,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { } (Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_def_id), _) if ns == ValueNS => { let def_id = self.r.tcx.parent(ctor_def_id); - if let Some(span) = self.def_span(def_id) { - err.span_label(span, &format!("`{}` defined here", path_str)); - } + err.span_label(self.r.def_span(def_id), &format!("`{path_str}` defined here")); let fields = self.r.field_names.get(&def_id).map_or_else( || "/* fields */".to_string(), |fields| vec!["_"; fields.len()].join(", "), @@ -2093,9 +2080,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { }; if def_id.is_local() { - if let Some(span) = self.def_span(def_id) { - err.span_note(span, "the enum is defined here"); - } + err.span_note(self.r.def_span(def_id), "the enum is defined here"); } } diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 02cf71f77c1e3..4277e427c4668 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -48,7 +48,6 @@ use rustc_middle::span_bug; use rustc_middle::ty::{self, MainDefinition, RegisteredTools, TyCtxt}; use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs}; use rustc_query_system::ich::StableHashingContext; -use rustc_session::cstore::CrateStore; use rustc_session::lint::LintBuffer; use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency}; use rustc_span::source_map::Spanned; @@ -1870,20 +1869,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { } } - /// Retrieves the span of the given `DefId` if `DefId` is in the local crate. - #[inline] - fn opt_span(&self, def_id: DefId) -> Option { - def_id.as_local().map(|def_id| self.tcx.source_span(def_id)) - } - - /// Retrieves the name of the given `DefId`. - #[inline] - fn opt_name(&self, def_id: DefId) -> Option { - let def_key = match def_id.as_local() { - Some(def_id) => self.tcx.definitions_untracked().def_key(def_id), - None => self.cstore().def_key(def_id), - }; - def_key.get_opt_name() + /// Retrieves definition span of the given `DefId`. + fn def_span(&self, def_id: DefId) -> Span { + match def_id.as_local() { + Some(def_id) => self.tcx.source_span(def_id), + None => self.cstore().get_span_untracked(def_id, self.tcx.sess), + } } /// Checks if an expression refers to a function marked with From b3ee735993db51074c8f494336696978a77dc16b Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 22 Feb 2023 13:55:06 +0400 Subject: [PATCH 6/8] resolve: Remove `struct_field_names_untracked` --- compiler/rustc_metadata/src/rmeta/decoder.rs | 15 ------- .../src/rmeta/decoder/cstore_impl.rs | 10 +---- .../rustc_resolve/src/build_reduced_graph.rs | 33 +++++--------- compiler/rustc_resolve/src/diagnostics.rs | 7 ++- .../rustc_resolve/src/late/diagnostics.rs | 44 ++++++++++++------- compiler/rustc_resolve/src/lib.rs | 15 ++++--- tests/ui/empty/empty-struct-tuple-pat.stderr | 4 +- .../pat-tuple-field-count-cross.stderr | 4 +- 8 files changed, 56 insertions(+), 76 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index f642902320d23..a680730d1f52d 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -30,7 +30,6 @@ use rustc_session::cstore::{ }; use rustc_session::Session; use rustc_span::hygiene::ExpnIndex; -use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{kw, Ident, Symbol}; use rustc_span::{self, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP}; @@ -1134,20 +1133,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .decode((self, sess)) } - fn get_struct_field_names( - self, - id: DefIndex, - sess: &'a Session, - ) -> impl Iterator> + 'a { - self.root - .tables - .children - .get(self, id) - .expect("fields not encoded for a struct") - .decode(self) - .map(move |index| respan(self.get_span(index, sess), self.item_name(index))) - } - fn get_inherent_implementations_for_type( self, tcx: TyCtxt<'tcx>, diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 7e08ab0448cf0..7ace335dd3dc9 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -19,8 +19,8 @@ use rustc_middle::ty::{self, TyCtxt}; use rustc_session::cstore::{CrateSource, CrateStore}; use rustc_session::{Session, StableCrateId}; use rustc_span::hygiene::{ExpnHash, ExpnId}; -use rustc_span::source_map::{Span, Spanned}; use rustc_span::symbol::{kw, Symbol}; +use rustc_span::Span; use rustc_data_structures::sync::Lrc; use std::any::Any; @@ -507,14 +507,6 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) { } impl CStore { - pub fn struct_field_names_untracked<'a>( - &'a self, - def: DefId, - sess: &'a Session, - ) -> impl Iterator> + 'a { - self.get_crate_data(def.krate).get_struct_field_names(def.index, sess) - } - pub fn ctor_untracked(&self, def: DefId) -> Option<(CtorKind, DefId)> { self.get_crate_data(def.krate).get_ctor(def.index) } diff --git a/compiler/rustc_resolve/src/build_reduced_graph.rs b/compiler/rustc_resolve/src/build_reduced_graph.rs index 415942405ae97..362ef693c48d5 100644 --- a/compiler/rustc_resolve/src/build_reduced_graph.rs +++ b/compiler/rustc_resolve/src/build_reduced_graph.rs @@ -29,7 +29,6 @@ use rustc_middle::metadata::ModChild; use rustc_middle::{bug, ty}; use rustc_session::cstore::CrateStore; use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind}; -use rustc_span::source_map::respan; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::Span; @@ -327,13 +326,13 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { } } - fn insert_field_names_local(&mut self, def_id: DefId, vdata: &ast::VariantData) { - let field_names = vdata - .fields() - .iter() - .map(|field| respan(field.span, field.ident.map_or(kw::Empty, |ident| ident.name))) - .collect(); - self.r.field_names.insert(def_id, field_names); + fn insert_field_def_ids(&mut self, def_id: LocalDefId, vdata: &ast::VariantData) { + if vdata.fields().iter().any(|field| field.is_placeholder) { + // The fields are not expanded yet. + return; + } + let def_ids = vdata.fields().iter().map(|field| self.r.local_def_id(field.id).to_def_id()); + self.r.field_def_ids.insert(def_id, self.r.tcx.arena.alloc_from_iter(def_ids)); } fn insert_field_visibilities_local(&mut self, def_id: DefId, vdata: &ast::VariantData) { @@ -345,12 +344,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { self.r.field_visibility_spans.insert(def_id, field_vis); } - fn insert_field_names_extern(&mut self, def_id: DefId) { - let field_names = - self.r.cstore().struct_field_names_untracked(def_id, self.r.tcx.sess).collect(); - self.r.field_names.insert(def_id, field_names); - } - fn block_needs_anonymous_module(&mut self, block: &Block) -> bool { // If any statements are items, we need to create an anonymous module block @@ -748,7 +741,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion)); // Record field names for error reporting. - self.insert_field_names_local(def_id, vdata); + self.insert_field_def_ids(local_def_id, vdata); self.insert_field_visibilities_local(def_id, vdata); // If this is a tuple or unit struct, define a name @@ -797,7 +790,7 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { self.r.define(parent, ident, TypeNS, (res, vis, sp, expansion)); // Record field names for error reporting. - self.insert_field_names_local(def_id, vdata); + self.insert_field_def_ids(local_def_id, vdata); self.insert_field_visibilities_local(def_id, vdata); } @@ -1003,12 +996,6 @@ impl<'a, 'b, 'tcx> BuildReducedGraphVisitor<'a, 'b, 'tcx> { | Res::SelfCtor(..) | Res::Err => bug!("unexpected resolution: {:?}", res), } - // Record some extra data for better diagnostics. - match res { - Res::Def(DefKind::Struct, def_id) => self.insert_field_names_extern(def_id), - Res::Def(DefKind::Union, def_id) => self.insert_field_names_extern(def_id), - _ => {} - } } fn add_macro_use_binding( @@ -1519,7 +1506,7 @@ impl<'a, 'b, 'tcx> Visitor<'b> for BuildReducedGraphVisitor<'a, 'b, 'tcx> { } // Record field names for error reporting. - self.insert_field_names_local(def_id.to_def_id(), &variant.data); + self.insert_field_def_ids(def_id, &variant.data); self.insert_field_visibilities_local(def_id.to_def_id(), &variant.data); visit::walk_variant(self, variant); diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 62873342c2e4e..44a3d4e628ebc 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -1581,8 +1581,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { )) = binding.kind { let def_id = self.tcx.parent(ctor_def_id); - let fields = self.field_names.get(&def_id)?; - return fields.iter().map(|name| name.span).reduce(Span::to); // None for `struct Foo()` + return self + .field_def_ids(def_id)? + .iter() + .map(|&field_id| self.def_span(field_id)) + .reduce(Span::to); // None for `struct Foo()` } None } diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 6a9b1505712cd..805c2ff280d8b 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1295,19 +1295,23 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { } _ => (": val", "literal", Applicability::HasPlaceholders), }; - let (fields, applicability) = match self.r.field_names.get(&def_id) { - Some(fields) => ( - fields + + let field_ids = self.r.field_def_ids(def_id); + let (fields, applicability) = match field_ids { + Some(field_ids) => ( + field_ids .iter() - .map(|f| format!("{}{}", f.node, tail)) + .map(|&field_id| { + format!("{}{tail}", self.r.tcx.item_name(field_id)) + }) .collect::>() .join(", "), applicability, ), None => ("/* fields */".to_string(), Applicability::HasPlaceholders), }; - let pad = match self.r.field_names.get(&def_id) { - Some(fields) if fields.is_empty() => "", + let pad = match field_ids { + Some(field_ids) if field_ids.is_empty() => "", _ => " ", }; err.span_suggestion( @@ -1451,10 +1455,12 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { ); // Use spans of the tuple struct definition. - self.r - .field_names - .get(&def_id) - .map(|fields| fields.iter().map(|f| f.span).collect::>()) + self.r.field_def_ids(def_id).map(|field_ids| { + field_ids + .iter() + .map(|&field_id| self.r.def_span(field_id)) + .collect::>() + }) } _ => None, }; @@ -1517,9 +1523,9 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { (Res::Def(DefKind::Ctor(_, CtorKind::Fn), ctor_def_id), _) if ns == ValueNS => { let def_id = self.r.tcx.parent(ctor_def_id); err.span_label(self.r.def_span(def_id), &format!("`{path_str}` defined here")); - let fields = self.r.field_names.get(&def_id).map_or_else( + let fields = self.r.field_def_ids(def_id).map_or_else( || "/* fields */".to_string(), - |fields| vec!["_"; fields.len()].join(", "), + |field_ids| vec!["_"; field_ids.len()].join(", "), ); err.span_suggestion( span, @@ -1600,8 +1606,11 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { if let Some(Res::Def(DefKind::Struct | DefKind::Union, did)) = resolution.full_res() { - if let Some(field_names) = self.r.field_names.get(&did) { - if field_names.iter().any(|&field_name| ident.name == field_name.node) { + if let Some(field_ids) = self.r.field_def_ids(did) { + if field_ids + .iter() + .any(|&field_id| ident.name == self.r.tcx.item_name(field_id)) + { return Some(AssocSuggestion::Field); } } @@ -2015,11 +2024,12 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> { } else { let needs_placeholder = |ctor_def_id: DefId, kind: CtorKind| { let def_id = self.r.tcx.parent(ctor_def_id); - let has_no_fields = self.r.field_names.get(&def_id).map_or(false, |f| f.is_empty()); match kind { CtorKind::Const => false, - CtorKind::Fn if has_no_fields => false, - _ => true, + CtorKind::Fn => !self + .r + .field_def_ids(def_id) + .map_or(false, |field_ids| field_ids.is_empty()), } }; diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index 4277e427c4668..ae1d9406467d6 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -50,7 +50,6 @@ use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs}; use rustc_query_system::ich::StableHashingContext; use rustc_session::lint::LintBuffer; use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency}; -use rustc_span::source_map::Spanned; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; @@ -881,10 +880,7 @@ pub struct Resolver<'a, 'tcx> { /// N.B., this is used only for better diagnostics, not name resolution itself. has_self: LocalDefIdSet, - - /// Names of fields of an item `DefId` accessible with dot syntax. - /// Used for hints during error reporting. - field_names: FxHashMap>>, + field_def_ids: LocalDefIdMap<&'tcx [DefId]>, /// Span of the privacy modifier in fields of an item `DefId` accessible with dot syntax. /// Used for hints during error reporting. @@ -1249,7 +1245,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { extern_prelude, has_self: Default::default(), - field_names: FxHashMap::default(), + field_def_ids: Default::default(), field_visibility_spans: FxHashMap::default(), determined_imports: Vec::new(), @@ -1877,6 +1873,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { } } + fn field_def_ids(&self, def_id: DefId) -> Option<&'tcx [DefId]> { + match def_id.as_local() { + Some(def_id) => self.field_def_ids.get(&def_id).copied(), + None => Some(self.tcx.associated_item_def_ids(def_id)), + } + } + /// Checks if an expression refers to a function marked with /// `#[rustc_legacy_const_generics]` and returns the argument index list /// from the attribute. diff --git a/tests/ui/empty/empty-struct-tuple-pat.stderr b/tests/ui/empty/empty-struct-tuple-pat.stderr index 8d0f75d204c2f..45001c7975324 100644 --- a/tests/ui/empty/empty-struct-tuple-pat.stderr +++ b/tests/ui/empty/empty-struct-tuple-pat.stderr @@ -46,8 +46,8 @@ LL | XEmpty5(), | help: use the tuple variant pattern syntax instead | -LL | XE::XEmpty5(/* fields */) => (), - | ~~~~~~~~~~~~~~~~~~~~~~~~~ +LL | XE::XEmpty5() => (), + | ~~~~~~~~~~~~~ help: a unit variant with a similar name exists | LL | XE::XEmpty4 => (), diff --git a/tests/ui/pattern/pat-tuple-field-count-cross.stderr b/tests/ui/pattern/pat-tuple-field-count-cross.stderr index d9295746158bb..0d7f2e4af69aa 100644 --- a/tests/ui/pattern/pat-tuple-field-count-cross.stderr +++ b/tests/ui/pattern/pat-tuple-field-count-cross.stderr @@ -113,8 +113,8 @@ LL | pub enum E1 { Z0, Z1(), S(u8, u8, u8) } | help: use the tuple variant pattern syntax instead | -LL | E1::Z1(/* fields */) => {} - | ~~~~~~~~~~~~~~~~~~~~ +LL | E1::Z1() => {} + | ~~~~~~~~ help: a unit variant with a similar name exists | LL | E1::Z0 => {} From 98cce8191788de9103231b1892195e27500aa1f5 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 22 Feb 2023 18:08:44 +0400 Subject: [PATCH 7/8] metadata: Remove some more untracked `CStore` methods --- compiler/rustc_interface/src/passes.rs | 17 ++++++----------- .../src/rmeta/decoder/cstore_impl.rs | 14 ++++---------- 2 files changed, 10 insertions(+), 21 deletions(-) diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index 4a02981f9543d..71bdd4df95ba3 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -26,7 +26,7 @@ use rustc_plugin_impl as plugin; use rustc_query_impl::{OnDiskCache, Queries as TcxQueries}; use rustc_resolve::Resolver; use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType}; -use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, Untracked}; +use rustc_session::cstore::{MetadataLoader, Untracked}; use rustc_session::output::filename_for_input; use rustc_session::search_paths::PathKind; use rustc_session::{Limit, Session}; @@ -442,13 +442,9 @@ fn escape_dep_env(symbol: Symbol) -> String { escaped } -fn write_out_deps( - sess: &Session, - cstore: &CrateStoreDyn, - outputs: &OutputFilenames, - out_filenames: &[PathBuf], -) { +fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[PathBuf]) { // Write out dependency rules to the dep-info file if requested + let sess = tcx.sess; if !sess.opts.output_types.contains_key(&OutputType::DepInfo) { return; } @@ -496,9 +492,8 @@ fn write_out_deps( } } - let cstore = cstore.as_any().downcast_ref::().unwrap(); - for cnum in cstore.crates_untracked() { - let source = cstore.crate_source_untracked(cnum); + for &cnum in tcx.crates(()) { + let source = tcx.used_crate_source(cnum); if let Some((path, _)) = &source.dylib { files.push(escape_dep_filename(&path.display().to_string())); } @@ -612,7 +607,7 @@ fn output_filenames(tcx: TyCtxt<'_>, (): ()) -> Arc { } } - write_out_deps(sess, &*tcx.cstore_untracked(), &outputs, &output_paths); + write_out_deps(tcx, &outputs, &output_paths); let only_dep_info = sess.opts.output_types.contains_key(&OutputType::DepInfo) && sess.opts.output_types.len() == 1; diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index 7ace335dd3dc9..6c5e886301085 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -16,7 +16,7 @@ use rustc_middle::middle::stability::DeprecationEntry; use rustc_middle::ty::fast_reject::SimplifiedType; use rustc_middle::ty::query::{ExternProviders, Providers}; use rustc_middle::ty::{self, TyCtxt}; -use rustc_session::cstore::{CrateSource, CrateStore}; +use rustc_session::cstore::CrateStore; use rustc_session::{Session, StableCrateId}; use rustc_span::hygiene::{ExpnHash, ExpnId}; use rustc_span::symbol::{kw, Symbol}; @@ -501,7 +501,9 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) { tcx.arena .alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE)) }, - crates: |tcx, ()| tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).crates_untracked()), + crates: |tcx, ()| { + tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).iter_crate_data().map(|(cnum, _)| cnum)) + }, ..*providers }; } @@ -547,10 +549,6 @@ impl CStore { ) } - pub fn crate_source_untracked(&self, cnum: CrateNum) -> Lrc { - self.get_crate_data(cnum).source.clone() - } - pub fn get_span_untracked(&self, def_id: DefId, sess: &Session) -> Span { self.get_crate_data(def_id.krate).get_span(def_id.index, sess) } @@ -559,10 +557,6 @@ impl CStore { self.get_crate_data(def.krate).def_kind(def.index) } - pub fn crates_untracked(&self) -> impl Iterator + '_ { - self.iter_crate_data().map(|(cnum, _)| cnum) - } - pub fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize { self.get_crate_data(def_id.krate).get_generics(def_id.index, sess).own_counts().lifetimes } From 4a61922ef00153a2c6e74d3cc97f687c9c8c6817 Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Wed, 22 Feb 2023 18:47:59 +0400 Subject: [PATCH 8/8] metadata/resolve: Minor refactoring to "tcx -> cstore" conversions --- compiler/rustc_metadata/src/creader.rs | 15 +++++++++------ compiler/rustc_resolve/src/lib.rs | 8 +++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_metadata/src/creader.rs b/compiler/rustc_metadata/src/creader.rs index b05626311e8e1..f870a1db82d9c 100644 --- a/compiler/rustc_metadata/src/creader.rs +++ b/compiler/rustc_metadata/src/creader.rs @@ -8,7 +8,7 @@ use rustc_ast::expand::allocator::AllocatorKind; use rustc_ast::{self as ast, *}; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::svh::Svh; -use rustc_data_structures::sync::MappedReadGuard; +use rustc_data_structures::sync::{MappedReadGuard, MappedWriteGuard, ReadGuard, WriteGuard}; use rustc_expand::base::SyntaxExtension; use rustc_hir::def_id::{CrateNum, LocalDefId, StableCrateId, LOCAL_CRATE}; use rustc_hir::definitions::Definitions; @@ -133,8 +133,14 @@ impl<'a> std::fmt::Debug for CrateDump<'a> { impl CStore { pub fn from_tcx(tcx: TyCtxt<'_>) -> MappedReadGuard<'_, CStore> { - MappedReadGuard::map(tcx.cstore_untracked(), |c| { - c.as_any().downcast_ref::().expect("`tcx.cstore` is not a `CStore`") + ReadGuard::map(tcx.untracked().cstore.read(), |cstore| { + cstore.as_any().downcast_ref::().expect("`tcx.cstore` is not a `CStore`") + }) + } + + pub fn from_tcx_mut(tcx: TyCtxt<'_>) -> MappedWriteGuard<'_, CStore> { + WriteGuard::map(tcx.untracked().cstore.write(), |cstore| { + cstore.untracked_as_any().downcast_mut().expect("`tcx.cstore` is not a `CStore`") }) } @@ -268,9 +274,6 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> { ) -> Self { CrateLoader { tcx, cstore, used_extern_options } } - pub fn cstore(&self) -> &CStore { - &self.cstore - } fn existing_match(&self, name: Symbol, hash: Option, kind: PathKind) -> Option { for (cnum, data) in self.cstore.iter_crate_data() { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index ae1d9406467d6..cd90fd3ef84d8 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -1431,9 +1431,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> { } fn crate_loader(&mut self, f: impl FnOnce(&mut CrateLoader<'_, '_>) -> T) -> T { - let mut cstore = self.tcx.untracked().cstore.write(); - let cstore = cstore.untracked_as_any().downcast_mut().unwrap(); - f(&mut CrateLoader::new(self.tcx, &mut *cstore, &mut self.used_extern_options)) + f(&mut CrateLoader::new( + self.tcx, + &mut CStore::from_tcx_mut(self.tcx), + &mut self.used_extern_options, + )) } fn cstore(&self) -> MappedReadGuard<'_, CStore> {