From 4e8199b572f2035b7749cba276ece3a26630d23e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:21 +0200 Subject: Adding upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_metadata/src/rmeta/decoder.rs | 156 +++++++-------------- .../src/rmeta/decoder/cstore_impl.rs | 38 +++-- compiler/rustc_metadata/src/rmeta/encoder.rs | 123 +++++++--------- compiler/rustc_metadata/src/rmeta/mod.rs | 18 +-- compiler/rustc_metadata/src/rmeta/table.rs | 2 - 5 files changed, 134 insertions(+), 203 deletions(-) (limited to 'compiler/rustc_metadata/src/rmeta') diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 691e3d0f8..af7b0793a 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -11,11 +11,10 @@ use rustc_data_structures::sync::{Lock, LockGuard, Lrc, OnceCell}; use rustc_data_structures::unhash::UnhashMap; use rustc_expand::base::{SyntaxExtension, SyntaxExtensionKind}; use rustc_expand::proc_macro::{AttrProcMacro, BangProcMacro, DeriveProcMacro}; -use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res}; +use rustc_hir::def::{CtorKind, DefKind, Res}; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash}; use rustc_hir::diagnostic_items::DiagnosticItems; -use rustc_hir::lang_items; use rustc_index::vec::{Idx, IndexVec}; use rustc_middle::metadata::ModChild; use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo}; @@ -30,17 +29,16 @@ use rustc_session::cstore::{ CrateSource, ExternCrate, ForeignModule, LinkagePreference, NativeLib, }; use rustc_session::Session; -use rustc_span::hygiene::{ExpnIndex, MacroKind}; +use rustc_span::hygiene::ExpnIndex; use rustc_span::source_map::{respan, Spanned}; -use rustc_span::symbol::{kw, sym, Ident, Symbol}; +use rustc_span::symbol::{kw, Ident, Symbol}; use rustc_span::{self, BytePos, ExpnId, Pos, Span, SyntaxContext, DUMMY_SP}; use proc_macro::bridge::client::ProcMacro; -use std::io; use std::iter::TrustedLen; -use std::mem; use std::num::NonZeroUsize; use std::path::Path; +use std::{io, iter, mem}; pub(super) use cstore_impl::provide; pub use cstore_impl::provide_extern; @@ -69,10 +67,10 @@ impl std::ops::Deref for MetadataBlob { } } -// A map from external crate numbers (as decoded from some crate file) to -// local crate numbers (as generated during this session). Each external -// crate may refer to types in other external crates, and each has their -// own crate numbers. +/// A map from external crate numbers (as decoded from some crate file) to +/// local crate numbers (as generated during this session). Each external +/// crate may refer to types in other external crates, and each has their +/// own crate numbers. pub(crate) type CrateNumMap = IndexVec; pub(crate) struct CrateMetadata { @@ -645,12 +643,6 @@ impl<'a, 'tcx> Decodable> for Symbol { } } -impl<'a, 'tcx> Decodable> for &'tcx [ty::abstract_const::Node<'tcx>] { - fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self { - ty::codec::RefDecodable::decode(d) - } -} - impl<'a, 'tcx> Decodable> for &'tcx [(ty::Predicate<'tcx>, Span)] { fn decode(d: &mut DecodeContext<'a, 'tcx>) -> Self { ty::codec::RefDecodable::decode(d) @@ -867,12 +859,12 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { let variant_did = if adt_kind == ty::AdtKind::Enum { Some(self.local_def_id(index)) } else { None }; - let ctor_did = data.ctor.map(|index| self.local_def_id(index)); + let ctor = data.ctor.map(|(kind, index)| (kind, self.local_def_id(index))); ty::VariantDef::new( self.item_name(index), variant_did, - ctor_did, + ctor, data.discr, self.root .tables @@ -886,7 +878,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { vis: self.get_visibility(index), }) .collect(), - data.ctor_kind, adt_kind, parent_did, false, @@ -967,7 +958,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } /// Iterates over the language items in the given crate. - fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, usize)] { + fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, LangItem)] { tcx.arena.alloc_from_iter( self.root .lang_items @@ -992,87 +983,52 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { DiagnosticItems { id_to_name, name_to_id } } + fn get_mod_child(self, id: DefIndex, sess: &Session) -> ModChild { + let ident = self.item_ident(id, sess); + let kind = self.def_kind(id); + let def_id = self.local_def_id(id); + let res = Res::Def(kind, def_id); + let vis = self.get_visibility(id); + let span = self.get_span(id, sess); + let macro_rules = match kind { + DefKind::Macro(..) => self.root.tables.macro_rules.get(self, id).is_some(), + _ => false, + }; + + ModChild { ident, res, vis, span, macro_rules } + } + /// Iterates over all named children of the given module, /// including both proper items and reexports. /// Module here is understood in name resolution sense - it can be a `mod` item, /// or a crate root, or an enum, or a trait. - fn for_each_module_child( + fn get_module_children( self, id: DefIndex, - mut callback: impl FnMut(ModChild), - sess: &Session, - ) { - if let Some(data) = &self.root.proc_macro_data { - // If we are loading as a proc macro, we want to return - // the view of this crate as a proc macro crate. - if id == CRATE_DEF_INDEX { - for def_index in data.macros.decode(self) { - let raw_macro = self.raw_proc_macro(def_index); - let res = Res::Def( - DefKind::Macro(macro_kind(raw_macro)), - self.local_def_id(def_index), - ); - let ident = self.item_ident(def_index, sess); - callback(ModChild { - ident, - res, - vis: ty::Visibility::Public, - span: ident.span, - macro_rules: false, - }); - } - } - return; - } - - // Iterate over all children. - if let Some(children) = self.root.tables.children.get(self, id) { - for child_index in children.decode((self, sess)) { - let ident = self.item_ident(child_index, sess); - let kind = self.def_kind(child_index); - let def_id = self.local_def_id(child_index); - let res = Res::Def(kind, def_id); - let vis = self.get_visibility(child_index); - let span = self.get_span(child_index, sess); - let macro_rules = match kind { - DefKind::Macro(..) => { - self.root.tables.macro_rules.get(self, child_index).is_some() + sess: &'a Session, + ) -> impl Iterator + 'a { + iter::from_generator(move || { + if let Some(data) = &self.root.proc_macro_data { + // If we are loading as a proc macro, we want to return + // the view of this crate as a proc macro crate. + if id == CRATE_DEF_INDEX { + for child_index in data.macros.decode(self) { + yield self.get_mod_child(child_index, sess); } - _ => false, - }; - - callback(ModChild { ident, res, vis, span, macro_rules }); + } + } else { + // Iterate over all children. + for child_index in self.root.tables.children.get(self, id).unwrap().decode(self) { + yield self.get_mod_child(child_index, sess); + } - // For non-reexport variants add their fictive constructors to children. - // Braced variants, unlike structs, generate unusable names in value namespace, - // they are reserved for possible future use. It's ok to use the variant's id as - // a ctor id since an error will be reported on any use of such resolution anyway. - // Reexport lists automatically contain such constructors when necessary. - if kind == DefKind::Variant && self.get_ctor_def_id_and_kind(child_index).is_none() - { - let ctor_res = - Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive), def_id); - let mut vis = vis; - if vis.is_public() { - // For non-exhaustive variants lower the constructor visibility to - // within the crate. We only need this for fictive constructors, - // for other constructors correct visibilities - // were already encoded in metadata. - let mut attrs = self.get_item_attrs(def_id.index, sess); - if attrs.any(|item| item.has_name(sym::non_exhaustive)) { - vis = ty::Visibility::Restricted(self.local_def_id(CRATE_DEF_INDEX)); - } + if let Some(reexports) = self.root.tables.module_reexports.get(self, id) { + for reexport in reexports.decode((self, sess)) { + yield reexport; } - callback(ModChild { ident, res: ctor_res, vis, span, macro_rules: false }); } } - } - - if let Some(exports) = self.root.tables.module_reexports.get(self, id) { - for exp in exports.decode((self, sess)) { - callback(exp); - } - } + }) } fn is_ctfe_mir_available(self, id: DefIndex) -> bool { @@ -1137,11 +1093,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { } } - fn get_ctor_def_id_and_kind(self, node_id: DefIndex) -> Option<(DefId, CtorKind)> { + fn get_ctor(self, node_id: DefIndex) -> Option<(CtorKind, DefId)> { match self.def_kind(node_id) { DefKind::Struct | DefKind::Variant => { let vdata = self.root.tables.variant_data.get(self, node_id).unwrap().decode(self); - vdata.ctor.map(|index| (self.local_def_id(index), vdata.ctor_kind)) + vdata.ctor.map(|(kind, index)| (kind, self.local_def_id(index))) } _ => None, } @@ -1319,7 +1275,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { ) } - fn get_missing_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [lang_items::LangItem] { + fn get_missing_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [LangItem] { tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode(self)) } @@ -1765,6 +1721,10 @@ impl CrateMetadata { self.root.has_global_allocator } + pub(crate) fn has_alloc_error_handler(&self) -> bool { + self.root.has_alloc_error_handler + } + pub(crate) fn has_default_lib_allocator(&self) -> bool { self.root.has_default_lib_allocator } @@ -1805,13 +1765,3 @@ impl CrateMetadata { None } } - -// Cannot be implemented on 'ProcMacro', as libproc_macro -// does not depend on librustc_ast -fn macro_kind(raw: &ProcMacro) -> MacroKind { - match raw { - ProcMacro::CustomDerive { .. } => MacroKind::Derive, - ProcMacro::Attr { .. } => MacroKind::Attr, - ProcMacro::Bang { .. } => MacroKind::Bang, - } -} diff --git a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs index a0a085525..c5d4da079 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs @@ -21,7 +21,6 @@ use rustc_span::source_map::{Span, Spanned}; use rustc_span::symbol::{kw, Symbol}; use rustc_data_structures::sync::Lrc; -use smallvec::SmallVec; use std::any::Any; use super::{Decodable, DecodeContext, DecodeIterator}; @@ -224,6 +223,15 @@ provide! { tcx, def_id, other, cdata, generator_kind => { table } trait_def => { table } deduced_param_attrs => { table } + is_type_alias_impl_trait => { + debug_assert_eq!(tcx.def_kind(def_id), DefKind::OpaqueTy); + cdata + .root + .tables + .is_type_alias_impl_trait + .get(cdata, def_id.index) + .is_some() + } collect_trait_impl_trait_tys => { Ok(cdata .root @@ -255,6 +263,7 @@ provide! { tcx, def_id, other, cdata, is_panic_runtime => { cdata.root.panic_runtime } is_compiler_builtins => { cdata.root.compiler_builtins } has_global_allocator => { cdata.root.has_global_allocator } + has_alloc_error_handler => { cdata.root.has_alloc_error_handler } has_panic_handler => { cdata.root.has_panic_handler } is_profiler_runtime => { cdata.root.profiler_runtime } required_panic_strategy => { cdata.root.required_panic_strategy } @@ -297,9 +306,7 @@ provide! { tcx, def_id, other, cdata, r } module_children => { - let mut result = SmallVec::<[_; 8]>::new(); - cdata.for_each_module_child(def_id.index, |child| result.push(child), tcx.sess); - tcx.arena.alloc_slice(&result) + tcx.arena.alloc_from_iter(cdata.get_module_children(def_id.index, tcx.sess)) } defined_lib_features => { cdata.get_lib_features(tcx) } stability_implications => { @@ -339,6 +346,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) { // resolve! Does this work? Unsure! That's what the issue is about *providers = Providers { allocator_kind: |tcx, ()| CStore::from_tcx(tcx).allocator_kind(), + alloc_error_handler_kind: |tcx, ()| CStore::from_tcx(tcx).alloc_error_handler_kind(), is_private_dep: |_tcx, cnum| { assert_eq!(cnum, LOCAL_CRATE); false @@ -464,6 +472,10 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) { assert_eq!(cnum, LOCAL_CRATE); CStore::from_tcx(tcx).has_global_allocator() }, + has_alloc_error_handler: |tcx, cnum| { + assert_eq!(cnum, LOCAL_CRATE); + CStore::from_tcx(tcx).has_alloc_error_handler() + }, postorder_cnums: |tcx, ()| { tcx.arena .alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE)) @@ -489,22 +501,20 @@ impl CStore { self.get_crate_data(def.krate).get_struct_field_visibilities(def.index) } - pub fn ctor_def_id_and_kind_untracked(&self, def: DefId) -> Option<(DefId, CtorKind)> { - self.get_crate_data(def.krate).get_ctor_def_id_and_kind(def.index) + pub fn ctor_untracked(&self, def: DefId) -> Option<(CtorKind, DefId)> { + 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(&self, def_id: DefId, sess: &Session) -> Vec { - let mut result = vec![]; - self.get_crate_data(def_id.krate).for_each_module_child( - def_id.index, - |child| result.push(child), - sess, - ); - result + pub fn module_children_untracked<'a>( + &'a self, + def_id: DefId, + sess: &'a Session, + ) -> impl Iterator + 'a { + self.get_crate_data(def_id.krate).get_module_children(def_id.index, sess) } pub fn load_macro_untracked(&self, id: DefId, sess: &Session) -> LoadedMacro { diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 049514ec7..7304c891e 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -17,7 +17,7 @@ use rustc_hir::def_id::{ }; use rustc_hir::definitions::DefPathData; use rustc_hir::intravisit::{self, Visitor}; -use rustc_hir::lang_items; +use rustc_hir::lang_items::LangItem; use rustc_middle::hir::nested_filter; use rustc_middle::middle::dependency_format::Linkage; use rustc_middle::middle::exported_symbols::{ @@ -670,6 +670,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { panic_in_drop_strategy: tcx.sess.opts.unstable_opts.panic_in_drop, edition: tcx.sess.edition(), has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE), + has_alloc_error_handler: tcx.has_alloc_error_handler(LOCAL_CRATE), has_panic_handler: tcx.has_panic_handler(LOCAL_CRATE), has_default_lib_allocator: tcx .sess @@ -787,8 +788,7 @@ fn should_encode_attr( } else if attr.doc_str().is_some() { // We keep all public doc comments because they might be "imported" into downstream crates // if they use `#[doc(inline)]` to copy an item's documentation into their own. - *is_def_id_public - .get_or_insert_with(|| tcx.effective_visibilities(()).effective_vis(def_id).is_some()) + *is_def_id_public.get_or_insert_with(|| tcx.effective_visibilities(()).is_exported(def_id)) } else if attr.has_name(sym::doc) { // If this is a `doc` attribute, and it's marked `inline` (as in `#[doc(inline)]`), we can // remove it. It won't be inlinable in downstream crates. @@ -928,6 +928,8 @@ fn should_encode_variances(def_kind: DefKind) -> bool { | DefKind::Union | DefKind::Enum | DefKind::Variant + | DefKind::OpaqueTy + | DefKind::ImplTraitPlaceholder | DefKind::Fn | DefKind::Ctor(..) | DefKind::AssocFn => true, @@ -941,8 +943,6 @@ fn should_encode_variances(def_kind: DefKind) -> bool { | DefKind::Const | DefKind::ForeignMod | DefKind::TyAlias - | DefKind::OpaqueTy - | DefKind::ImplTraitPlaceholder | DefKind::Impl | DefKind::Trait | DefKind::TraitAlias @@ -1221,9 +1221,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { debug!("EncodeContext::encode_enum_variant_info({:?})", def_id); let data = VariantData { - ctor_kind: variant.ctor_kind, discr: variant.discr, - ctor: variant.ctor_def_id.map(|did| did.index), + ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)), is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; @@ -1233,32 +1232,28 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { assert!(f.did.is_local()); f.did.index })); - if variant.ctor_kind == CtorKind::Fn { + if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor { // FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`. - if let Some(ctor_def_id) = variant.ctor_def_id { - record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(ctor_def_id)); - } + record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(ctor_def_id)); } } fn encode_enum_variant_ctor(&mut self, def: ty::AdtDef<'tcx>, index: VariantIdx) { - let tcx = self.tcx; let variant = &def.variant(index); - let def_id = variant.ctor_def_id.unwrap(); + let Some((ctor_kind, def_id)) = variant.ctor else { return }; debug!("EncodeContext::encode_enum_variant_ctor({:?})", def_id); // FIXME(eddyb) encode only the `CtorKind` for constructors. let data = VariantData { - ctor_kind: variant.ctor_kind, discr: variant.discr, - ctor: Some(def_id.index), + ctor: Some((ctor_kind, def_id.index)), is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; record!(self.tables.variant_data[def_id] <- data); self.tables.constness.set(def_id.index, hir::Constness::Const); - if variant.ctor_kind == CtorKind::Fn { - record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); + if ctor_kind == CtorKind::Fn { + record!(self.tables.fn_sig[def_id] <- self.tcx.fn_sig(def_id)); } } @@ -1272,13 +1267,6 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { // the crate root for consistency with other crates (some of the resolver // code uses it). However, we skip encoding anything relating to child // items - we encode information about proc-macros later on. - let reexports = if !self.is_proc_macro { - tcx.module_reexports(local_def_id).unwrap_or(&[]) - } else { - &[] - }; - - record_array!(self.tables.module_reexports[def_id] <- reexports); if self.is_proc_macro { // Encode this here because we don't do it in encode_def_ids. record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id)); @@ -1310,26 +1298,30 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } })); + + if let Some(reexports) = tcx.module_reexports(local_def_id) { + assert!(!reexports.is_empty()); + record_array!(self.tables.module_reexports[def_id] <- reexports); + } } } - fn encode_struct_ctor(&mut self, adt_def: ty::AdtDef<'tcx>, def_id: DefId) { - debug!("EncodeContext::encode_struct_ctor({:?})", def_id); - let tcx = self.tcx; + fn encode_struct_ctor(&mut self, adt_def: ty::AdtDef<'tcx>) { let variant = adt_def.non_enum_variant(); + let Some((ctor_kind, def_id)) = variant.ctor else { return }; + debug!("EncodeContext::encode_struct_ctor({:?})", def_id); let data = VariantData { - ctor_kind: variant.ctor_kind, discr: variant.discr, - ctor: Some(def_id.index), + ctor: Some((ctor_kind, def_id.index)), is_non_exhaustive: variant.is_field_list_non_exhaustive(), }; record!(self.tables.repr_options[def_id] <- adt_def.repr()); record!(self.tables.variant_data[def_id] <- data); self.tables.constness.set(def_id.index, hir::Constness::Const); - if variant.ctor_kind == CtorKind::Fn { - record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); + if ctor_kind == CtorKind::Fn { + record!(self.tables.fn_sig[def_id] <- self.tcx.fn_sig(def_id)); } } @@ -1543,30 +1535,25 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { hir::ItemKind::Mod(ref m) => { return self.encode_info_for_mod(item.owner_id.def_id, m); } - hir::ItemKind::OpaqueTy(..) => { + hir::ItemKind::OpaqueTy(ref opaque) => { self.encode_explicit_item_bounds(def_id); + if matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias) { + self.tables.is_type_alias_impl_trait.set(def_id.index, ()); + } } hir::ItemKind::Enum(..) => { let adt_def = self.tcx.adt_def(def_id); record!(self.tables.repr_options[def_id] <- adt_def.repr()); } - hir::ItemKind::Struct(ref struct_def, _) => { + hir::ItemKind::Struct(..) => { let adt_def = self.tcx.adt_def(def_id); record!(self.tables.repr_options[def_id] <- adt_def.repr()); self.tables.constness.set(def_id.index, hir::Constness::Const); - // Encode def_ids for each field and method - // for methods, write all the stuff get_trait_method - // needs to know - let ctor = struct_def - .ctor_hir_id() - .map(|ctor_hir_id| self.tcx.hir().local_def_id(ctor_hir_id).local_def_index); - let variant = adt_def.non_enum_variant(); record!(self.tables.variant_data[def_id] <- VariantData { - ctor_kind: variant.ctor_kind, discr: variant.discr, - ctor, + ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)), is_non_exhaustive: variant.is_field_list_non_exhaustive(), }); } @@ -1576,9 +1563,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { let variant = adt_def.non_enum_variant(); record!(self.tables.variant_data[def_id] <- VariantData { - ctor_kind: variant.ctor_kind, discr: variant.discr, - ctor: None, + ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)), is_non_exhaustive: variant.is_field_list_non_exhaustive(), }); } @@ -1631,7 +1617,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { for variant in tcx.adt_def(def_id).variants() { yield variant.def_id.index; // Encode constructors which take a separate slot in value namespace. - if let Some(ctor_def_id) = variant.ctor_def_id { + if let Some(ctor_def_id) = variant.ctor_def_id() { yield ctor_def_id.index; } } @@ -1674,21 +1660,14 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { match item.kind { hir::ItemKind::Enum(..) => { let def = self.tcx.adt_def(item.owner_id.to_def_id()); - for (i, variant) in def.variants().iter_enumerated() { + for (i, _) in def.variants().iter_enumerated() { self.encode_enum_variant_info(def, i); - - if let Some(_ctor_def_id) = variant.ctor_def_id { - self.encode_enum_variant_ctor(def, i); - } + self.encode_enum_variant_ctor(def, i); } } - hir::ItemKind::Struct(ref struct_def, _) => { + hir::ItemKind::Struct(..) => { let def = self.tcx.adt_def(item.owner_id.to_def_id()); - // If the struct has a constructor, encode it. - if let Some(ctor_hir_id) = struct_def.ctor_hir_id() { - let ctor_def_id = self.tcx.hir().local_def_id(ctor_hir_id); - self.encode_struct_ctor(def, ctor_def_id.to_def_id()); - } + self.encode_struct_ctor(def); } hir::ItemKind::Impl { .. } => { for &trait_item_def_id in @@ -1708,12 +1687,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } } - fn encode_info_for_closure(&mut self, hir_id: hir::HirId) { - let def_id = self.tcx.hir().local_def_id(hir_id); - debug!("EncodeContext::encode_info_for_closure({:?})", def_id); + #[instrument(level = "debug", skip(self))] + fn encode_info_for_closure(&mut self, def_id: LocalDefId) { // NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic, // including on the signature, which is inferred in `typeck. let typeck_result: &'tcx ty::TypeckResults<'tcx> = self.tcx.typeck(def_id); + let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); let ty = typeck_result.node_type(hir_id); match ty.kind() { ty::Generator(..) => { @@ -1905,22 +1884,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { self.lazy_array(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index))) } - fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, usize)> { + fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, LangItem)> { empty_proc_macro!(self); - let tcx = self.tcx; - let lang_items = tcx.lang_items(); - let lang_items = lang_items.items().iter(); - self.lazy_array(lang_items.enumerate().filter_map(|(i, &opt_def_id)| { - if let Some(def_id) = opt_def_id { - if def_id.is_local() { - return Some((def_id.index, i)); - } - } - None + let lang_items = self.tcx.lang_items().iter(); + self.lazy_array(lang_items.filter_map(|(lang_item, def_id)| { + def_id.as_local().map(|id| (id.local_def_index, lang_item)) })) } - fn encode_lang_items_missing(&mut self) -> LazyArray { + fn encode_lang_items_missing(&mut self) -> LazyArray { empty_proc_macro!(self); let tcx = self.tcx; self.lazy_array(&tcx.lang_items().missing) @@ -2108,11 +2080,10 @@ impl<'a, 'tcx> Visitor<'tcx> for EncodeContext<'a, 'tcx> { impl<'a, 'tcx> EncodeContext<'a, 'tcx> { fn encode_info_for_generics(&mut self, generics: &hir::Generics<'tcx>) { for param in generics.params { - let def_id = self.tcx.hir().local_def_id(param.hir_id); match param.kind { hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => {} hir::GenericParamKind::Const { ref default, .. } => { - let def_id = def_id.to_def_id(); + let def_id = param.def_id.to_def_id(); if default.is_some() { record!(self.tables.const_param_default[def_id] <- self.tcx.const_param_default(def_id)) } @@ -2122,8 +2093,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { } fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) { - if let hir::ExprKind::Closure { .. } = expr.kind { - self.encode_info_for_closure(expr.hir_id); + if let hir::ExprKind::Closure(closure) = expr.kind { + self.encode_info_for_closure(closure.def_id); } } } @@ -2196,7 +2167,7 @@ impl EncodedMetadata { #[inline] pub fn raw_data(&self) -> &[u8] { - self.mmap.as_ref().map(|mmap| mmap.as_ref()).unwrap_or_default() + self.mmap.as_deref().unwrap_or_default() } } diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 27dc8ff16..571804644 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -12,7 +12,7 @@ use rustc_hir as hir; use rustc_hir::def::{CtorKind, DefKind}; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, DefPathHash, StableCrateId}; use rustc_hir::definitions::DefKey; -use rustc_hir::lang_items; +use rustc_hir::lang_items::LangItem; use rustc_index::bit_set::{BitSet, FiniteBitSet}; use rustc_index::vec::IndexVec; use rustc_middle::metadata::ModChild; @@ -223,6 +223,7 @@ pub(crate) struct CrateRoot { panic_in_drop_strategy: PanicStrategy, edition: Edition, has_global_allocator: bool, + has_alloc_error_handler: bool, has_panic_handler: bool, has_default_lib_allocator: bool, @@ -230,8 +231,8 @@ pub(crate) struct CrateRoot { dylib_dependency_formats: LazyArray>, lib_features: LazyArray<(Symbol, Option)>, stability_implications: LazyArray<(Symbol, Symbol)>, - lang_items: LazyArray<(DefIndex, usize)>, - lang_items_missing: LazyArray, + lang_items: LazyArray<(DefIndex, LangItem)>, + lang_items_missing: LazyArray, diagnostic_items: LazyArray<(Symbol, DefIndex)>, native_libraries: LazyArray, foreign_modules: LazyArray, @@ -352,7 +353,7 @@ define_tables! { explicit_predicates_of: Table>>, generics_of: Table>, // As an optimization, a missing entry indicates an empty `&[]`. - inferred_outlives_of: Table, Span)>>, + inferred_outlives_of: Table, Span)>>, super_predicates_of: Table>>, type_of: Table>>, variances_of: Table>, @@ -365,7 +366,7 @@ define_tables! { mir_for_ctfe: Table>>, promoted_mir: Table>>>, // FIXME(compiler-errors): Why isn't this a LazyArray? - thir_abstract_const: Table]>>, + thir_abstract_const: Table>>, impl_parent: Table, impl_polarity: Table, constness: Table, @@ -399,20 +400,21 @@ define_tables! { assoc_container: Table, // Slot is full when macro is macro_rules. macro_rules: Table, - macro_definition: Table>, + macro_definition: Table>, proc_macro: Table, module_reexports: Table>, deduced_param_attrs: Table>, + // Slot is full when opaque is TAIT. + is_type_alias_impl_trait: Table, trait_impl_trait_tys: Table>>>, } #[derive(TyEncodable, TyDecodable)] struct VariantData { - ctor_kind: CtorKind, discr: ty::VariantDiscr, /// If this is unit or tuple-variant/struct, then this is the index of the ctor id. - ctor: Option, + ctor: Option<(CtorKind, DefIndex)>, is_non_exhaustive: bool, } diff --git a/compiler/rustc_metadata/src/rmeta/table.rs b/compiler/rustc_metadata/src/rmeta/table.rs index e7c1abd12..29fe61107 100644 --- a/compiler/rustc_metadata/src/rmeta/table.rs +++ b/compiler/rustc_metadata/src/rmeta/table.rs @@ -101,10 +101,8 @@ fixed_size_enum! { ( Static(ast::Mutability::Mut) ) ( Ctor(CtorOf::Struct, CtorKind::Fn) ) ( Ctor(CtorOf::Struct, CtorKind::Const) ) - ( Ctor(CtorOf::Struct, CtorKind::Fictive) ) ( Ctor(CtorOf::Variant, CtorKind::Fn) ) ( Ctor(CtorOf::Variant, CtorKind::Const) ) - ( Ctor(CtorOf::Variant, CtorKind::Fictive) ) ( Macro(MacroKind::Bang) ) ( Macro(MacroKind::Attr) ) ( Macro(MacroKind::Derive) ) -- cgit v1.2.3