diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:24 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-30 03:59:24 +0000 |
commit | 023939b627b7dc93b01471f7d41fb8553ddb4ffa (patch) | |
tree | 60fc59477c605c72b0a1051409062ddecc43f877 /compiler/rustc_middle/src | |
parent | Adding debian version 1.72.1+dfsg1-1. (diff) | |
download | rustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.tar.xz rustc-023939b627b7dc93b01471f7d41fb8553ddb4ffa.zip |
Merging upstream version 1.73.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_middle/src')
98 files changed, 2683 insertions, 2216 deletions
diff --git a/compiler/rustc_middle/src/arena.rs b/compiler/rustc_middle/src/arena.rs index 5a320865c..952c796f5 100644 --- a/compiler/rustc_middle/src/arena.rs +++ b/compiler/rustc_middle/src/arena.rs @@ -40,7 +40,6 @@ macro_rules! arena_types { rustc_data_structures::sync::Lrc<rustc_ast::Crate>, )>, [] output_filenames: std::sync::Arc<rustc_session::config::OutputFilenames>, - [] metadata_loader: rustc_data_structures::steal::Steal<Box<rustc_session::cstore::MetadataLoaderDyn>>, [] crate_for_resolver: rustc_data_structures::steal::Steal<(rustc_ast::Crate, rustc_ast::AttrVec)>, [] resolutions: rustc_middle::ty::ResolverGlobalCtxt, [decode] unsafety_check_result: rustc_middle::mir::UnsafetyCheckResult, @@ -131,6 +130,7 @@ macro_rules! arena_types { [] closure_kind_origin: (rustc_span::Span, rustc_middle::hir::place::Place<'tcx>), [] stripped_cfg_items: rustc_ast::expand::StrippedCfgItem, [] mod_child: rustc_middle::metadata::ModChild, + [] features: rustc_feature::Features, ]); ) } diff --git a/compiler/rustc_middle/src/dep_graph/dep_node.rs b/compiler/rustc_middle/src/dep_graph/dep_node.rs index 2dc5b8969..04c09d334 100644 --- a/compiler/rustc_middle/src/dep_graph/dep_node.rs +++ b/compiler/rustc_middle/src/dep_graph/dep_node.rs @@ -60,7 +60,7 @@ use crate::mir::mono::MonoItem; use crate::ty::TyCtxt; use rustc_data_structures::fingerprint::Fingerprint; -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalModDefId, ModDefId, LOCAL_CRATE}; use rustc_hir::definitions::DefPathHash; use rustc_hir::{HirId, ItemLocalId, OwnerId}; use rustc_query_system::dep_graph::FingerprintStyle; @@ -371,7 +371,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId { fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> { if tcx.fingerprint_style(dep_node.kind) == FingerprintStyle::HirId { let (local_hash, local_id) = Fingerprint::from(dep_node.hash).split(); - let def_path_hash = DefPathHash::new(tcx.sess.local_stable_crate_id(), local_hash); + let def_path_hash = DefPathHash::new(tcx.stable_crate_id(LOCAL_CRATE), local_hash); let def_id = tcx .def_path_hash_to_def_id(def_path_hash, &mut || { panic!("Failed to extract HirId: {:?} {}", dep_node.kind, dep_node.hash) @@ -380,10 +380,60 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId { let local_id = local_id .as_u64() .try_into() - .unwrap_or_else(|_| panic!("local id should be u32, found {:?}", local_id)); + .unwrap_or_else(|_| panic!("local id should be u32, found {local_id:?}")); Some(HirId { owner: OwnerId { def_id }, local_id: ItemLocalId::from_u32(local_id) }) } else { None } } } + +macro_rules! impl_for_typed_def_id { + ($Name:ident, $LocalName:ident) => { + impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for $Name { + #[inline(always)] + fn fingerprint_style() -> FingerprintStyle { + FingerprintStyle::DefPathHash + } + + #[inline(always)] + fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint { + self.to_def_id().to_fingerprint(tcx) + } + + #[inline(always)] + fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String { + self.to_def_id().to_debug_str(tcx) + } + + #[inline(always)] + fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> { + DefId::recover(tcx, dep_node).map($Name::new_unchecked) + } + } + + impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for $LocalName { + #[inline(always)] + fn fingerprint_style() -> FingerprintStyle { + FingerprintStyle::DefPathHash + } + + #[inline(always)] + fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint { + self.to_def_id().to_fingerprint(tcx) + } + + #[inline(always)] + fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String { + self.to_def_id().to_debug_str(tcx) + } + + #[inline(always)] + fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> { + LocalDefId::recover(tcx, dep_node).map($LocalName::new_unchecked) + } + } + }; +} + +impl_for_typed_def_id! { ModDefId, LocalModDefId } diff --git a/compiler/rustc_middle/src/dep_graph/mod.rs b/compiler/rustc_middle/src/dep_graph/mod.rs index 0ddbe7d1c..f79ce08b8 100644 --- a/compiler/rustc_middle/src/dep_graph/mod.rs +++ b/compiler/rustc_middle/src/dep_graph/mod.rs @@ -8,7 +8,7 @@ mod dep_node; pub use rustc_query_system::dep_graph::{ debug::DepNodeFilter, hash_result, DepContext, DepNodeColor, DepNodeIndex, - SerializedDepNodeIndex, WorkProduct, WorkProductId, + SerializedDepNodeIndex, WorkProduct, WorkProductId, WorkProductMap, }; pub use dep_node::{label_strs, DepKind, DepNode, DepNodeExt}; @@ -35,7 +35,7 @@ impl rustc_query_system::dep_graph::DepKind for DepKind { if let Some(def_id) = node.extract_def_id(tcx) { write!(f, "{}", tcx.def_path_debug_str(def_id))?; } else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) { - write!(f, "{}", s)?; + write!(f, "{s}")?; } else { write!(f, "{}", node.hash)?; } diff --git a/compiler/rustc_middle/src/error.rs b/compiler/rustc_middle/src/error.rs index 57b2de84b..b346cd453 100644 --- a/compiler/rustc_middle/src/error.rs +++ b/compiler/rustc_middle/src/error.rs @@ -132,6 +132,9 @@ pub enum LayoutError<'tcx> { #[diag(middle_cycle)] Cycle, + + #[diag(middle_layout_references_error)] + ReferencesError, } #[derive(Diagnostic)] diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 5f2eb890c..467962b39 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -8,7 +8,7 @@ use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::svh::Svh; use rustc_data_structures::sync::{par_for_each_in, DynSend, DynSync}; use rustc_hir::def::{DefKind, Res}; -use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash}; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::*; @@ -24,7 +24,7 @@ pub fn associated_body(node: Node<'_>) -> Option<(LocalDefId, BodyId)> { match node { Node::Item(Item { owner_id, - kind: ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body), + kind: ItemKind::Const(_, _, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body), .. }) | Node::TraitItem(TraitItem { @@ -148,7 +148,7 @@ impl<'hir> Map<'hir> { } #[inline] - pub fn module_items(self, module: LocalDefId) -> impl Iterator<Item = ItemId> + 'hir { + pub fn module_items(self, module: LocalModDefId) -> impl Iterator<Item = ItemId> + 'hir { self.tcx.hir_module_items(module).items() } @@ -169,8 +169,8 @@ impl<'hir> Map<'hir> { } #[inline] - pub fn local_def_id_to_hir_id(self, def_id: LocalDefId) -> HirId { - self.tcx.local_def_id_to_hir_id(def_id) + pub fn local_def_id_to_hir_id(self, def_id: impl Into<LocalDefId>) -> HirId { + self.tcx.local_def_id_to_hir_id(def_id.into()) } /// Do not call this function directly. The query should be called. @@ -195,14 +195,10 @@ impl<'hir> Map<'hir> { ItemKind::Fn(..) => DefKind::Fn, ItemKind::Macro(_, macro_kind) => DefKind::Macro(macro_kind), ItemKind::Mod(..) => DefKind::Mod, - ItemKind::OpaqueTy(ref opaque) => { - if opaque.in_trait && !self.tcx.lower_impl_trait_in_trait_to_assoc_ty() { - DefKind::ImplTraitPlaceholder - } else { - DefKind::OpaqueTy - } + ItemKind::OpaqueTy(..) => DefKind::OpaqueTy, + ItemKind::TyAlias(..) => { + DefKind::TyAlias { lazy: self.tcx.features().lazy_type_alias } } - ItemKind::TyAlias(..) => DefKind::TyAlias, ItemKind::Enum(..) => DefKind::Enum, ItemKind::Struct(..) => DefKind::Struct, ItemKind::Union(..) => DefKind::Union, @@ -533,20 +529,20 @@ impl<'hir> Map<'hir> { self.krate_attrs().iter().any(|attr| attr.has_name(sym::rustc_coherence_is_core)) } - pub fn get_module(self, module: LocalDefId) -> (&'hir Mod<'hir>, Span, HirId) { - let hir_id = HirId::make_owner(module); + pub fn get_module(self, module: LocalModDefId) -> (&'hir Mod<'hir>, Span, HirId) { + let hir_id = HirId::make_owner(module.to_local_def_id()); match self.tcx.hir_owner(hir_id.owner).map(|o| o.node) { Some(OwnerNode::Item(&Item { span, kind: ItemKind::Mod(ref m), .. })) => { (m, span, hir_id) } Some(OwnerNode::Crate(item)) => (item, item.spans.inner_span, hir_id), - node => panic!("not a module: {:?}", node), + node => panic!("not a module: {node:?}"), } } /// Walks the contents of the local crate. See also `visit_all_item_likes_in_crate`. pub fn walk_toplevel_module(self, visitor: &mut impl Visitor<'hir>) { - let (top_mod, span, hir_id) = self.get_module(CRATE_DEF_ID); + let (top_mod, span, hir_id) = self.get_module(LocalModDefId::CRATE_DEF_ID); visitor.visit_mod(top_mod, span, hir_id); } @@ -599,7 +595,7 @@ impl<'hir> Map<'hir> { /// This method is the equivalent of `visit_all_item_likes_in_crate` but restricted to /// item-likes in a single module. - pub fn visit_item_likes_in_module<V>(self, module: LocalDefId, visitor: &mut V) + pub fn visit_item_likes_in_module<V>(self, module: LocalModDefId, visitor: &mut V) where V: Visitor<'hir>, { @@ -622,17 +618,19 @@ impl<'hir> Map<'hir> { } } - pub fn for_each_module(self, mut f: impl FnMut(LocalDefId)) { + pub fn for_each_module(self, mut f: impl FnMut(LocalModDefId)) { let crate_items = self.tcx.hir_crate_items(()); for module in crate_items.submodules.iter() { - f(module.def_id) + f(LocalModDefId::new_unchecked(module.def_id)) } } #[inline] - pub fn par_for_each_module(self, f: impl Fn(LocalDefId) + DynSend + DynSync) { + pub fn par_for_each_module(self, f: impl Fn(LocalModDefId) + DynSend + DynSync) { let crate_items = self.tcx.hir_crate_items(()); - par_for_each_in(&crate_items.submodules[..], |module| f(module.def_id)) + par_for_each_in(&crate_items.submodules[..], |module| { + f(LocalModDefId::new_unchecked(module.def_id)) + }) } /// Returns an iterator for the nodes in the ancestor tree of the `current_id` @@ -741,17 +739,6 @@ impl<'hir> Map<'hir> { } } - /// Returns the `OwnerId` of `id`'s nearest module parent, or `id` itself if no - /// module parent is in this map. - pub(super) fn get_module_parent_node(self, hir_id: HirId) -> OwnerId { - for (def_id, node) in self.parent_owner_iter(hir_id) { - if let OwnerNode::Item(&Item { kind: ItemKind::Mod(_), .. }) = node { - return def_id; - } - } - CRATE_OWNER_ID - } - /// When on an if expression, a match arm tail expression or a match arm, give back /// the enclosing `if` or `match` expression. /// @@ -1109,6 +1096,33 @@ impl<'hir> Map<'hir> { _ => None, } } + + pub fn maybe_get_struct_pattern_shorthand_field(&self, expr: &Expr<'_>) -> Option<Symbol> { + let local = match expr { + Expr { + kind: + ExprKind::Path(QPath::Resolved( + None, + Path { + res: def::Res::Local(_), segments: [PathSegment { ident, .. }], .. + }, + )), + .. + } => Some(ident), + _ => None, + }?; + + match self.find_parent(expr.hir_id)? { + Node::ExprField(field) => { + if field.ident.name == local.name && field.is_shorthand { + return Some(local.name); + } + } + _ => {} + } + + None + } } impl<'hir> intravisit::Map<'hir> for Map<'hir> { @@ -1198,7 +1212,7 @@ pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh { owner_spans.hash_stable(&mut hcx, &mut stable_hasher); } tcx.sess.opts.dep_tracking_hash(true).hash_stable(&mut hcx, &mut stable_hasher); - tcx.sess.local_stable_crate_id().hash_stable(&mut hcx, &mut stable_hasher); + tcx.stable_crate_id(LOCAL_CRATE).hash_stable(&mut hcx, &mut stable_hasher); // Hash visibility information since it does not appear in HIR. resolutions.visibilities.hash_stable(&mut hcx, &mut stable_hasher); resolutions.has_pub_restricted.hash_stable(&mut hcx, &mut stable_hasher); @@ -1312,7 +1326,7 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String { } } -pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalDefId) -> ModuleItems { +pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> ModuleItems { let mut collector = ItemCollector::new(tcx, false); let (hir_mod, span, hir_id) = tcx.hir().get_module(module_id); diff --git a/compiler/rustc_middle/src/hir/mod.rs b/compiler/rustc_middle/src/hir/mod.rs index 45a07fdd2..0da8fe9cc 100644 --- a/compiler/rustc_middle/src/hir/mod.rs +++ b/compiler/rustc_middle/src/hir/mod.rs @@ -11,7 +11,7 @@ use crate::ty::{EarlyBinder, ImplSubject, TyCtxt}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::{par_for_each_in, DynSend, DynSync}; use rustc_hir::def::DefKind; -use rustc_hir::def_id::{DefId, LocalDefId}; +use rustc_hir::def_id::{DefId, LocalDefId, LocalModDefId}; use rustc_hir::*; use rustc_query_system::ich::StableHashingContext; use rustc_span::{ExpnId, DUMMY_SP}; @@ -101,8 +101,22 @@ impl<'tcx> TyCtxt<'tcx> { map::Map { tcx: self } } - pub fn parent_module(self, id: HirId) -> LocalDefId { - self.parent_module_from_def_id(id.owner.def_id) + pub fn parent_module(self, id: HirId) -> LocalModDefId { + if !id.is_owner() && self.def_kind(id.owner) == DefKind::Mod { + LocalModDefId::new_unchecked(id.owner.def_id) + } else { + self.parent_module_from_def_id(id.owner.def_id) + } + } + + pub fn parent_module_from_def_id(self, mut id: LocalDefId) -> LocalModDefId { + while let Some(parent) = self.opt_local_parent(id) { + id = parent; + if self.def_kind(id) == DefKind::Mod { + break; + } + } + LocalModDefId::new_unchecked(id) } pub fn impl_subject(self, def_id: DefId) -> EarlyBinder<ImplSubject<'tcx>> { @@ -120,10 +134,6 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn provide(providers: &mut Providers) { - providers.parent_module_from_def_id = |tcx, id| { - let hir = tcx.hir(); - hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)).def_id - }; providers.hir_crate_items = map::hir_crate_items; providers.crate_hash = map::crate_hash; providers.hir_module_items = map::hir_module_items; @@ -154,18 +164,15 @@ pub fn provide(providers: &mut Providers) { tcx.hir_crate(()).owners[id.def_id].as_owner().map_or(AttributeMap::EMPTY, |o| &o.attrs) }; providers.def_span = |tcx, def_id| { - let def_id = def_id; let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); tcx.hir().opt_span(hir_id).unwrap_or(DUMMY_SP) }; providers.def_ident_span = |tcx, def_id| { - let def_id = def_id; let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); tcx.hir().opt_ident_span(hir_id) }; - providers.fn_arg_names = |tcx, id| { + providers.fn_arg_names = |tcx, def_id| { let hir = tcx.hir(); - let def_id = id; let hir_id = hir.local_def_id_to_hir_id(def_id); if let Some(body_id) = hir.maybe_body_owned_by(def_id) { tcx.arena.alloc_from_iter(hir.body_param_names(body_id)) @@ -180,7 +187,7 @@ pub fn provide(providers: &mut Providers) { { idents } else { - span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", id); + span_bug!(hir.span(hir_id), "fn_arg_names: unexpected item {:?}", def_id); } }; providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id); diff --git a/compiler/rustc_middle/src/hir/place.rs b/compiler/rustc_middle/src/hir/place.rs index 8a22de931..32f3a1775 100644 --- a/compiler/rustc_middle/src/hir/place.rs +++ b/compiler/rustc_middle/src/hir/place.rs @@ -36,6 +36,10 @@ pub enum ProjectionKind { /// A subslice covering a range of values like `B[x..y]`. Subslice, + + /// A conversion from an opaque type to its hidden type so we can + /// do further projections on it. + OpaqueCast, } #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable)] diff --git a/compiler/rustc_middle/src/infer/canonical.rs b/compiler/rustc_middle/src/infer/canonical.rs index d5e8330b3..81823118a 100644 --- a/compiler/rustc_middle/src/infer/canonical.rs +++ b/compiler/rustc_middle/src/infer/canonical.rs @@ -23,7 +23,7 @@ use crate::infer::MemberConstraint; use crate::mir::ConstraintCategory; -use crate::ty::subst::GenericArg; +use crate::ty::GenericArg; use crate::ty::{self, BoundVar, List, Region, Ty, TyCtxt}; use rustc_macros::HashStable; use smallvec::SmallVec; @@ -63,7 +63,7 @@ impl<'tcx> ty::TypeFoldable<TyCtxt<'tcx>> for CanonicalVarInfos<'tcx> { #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TyDecodable, TyEncodable)] #[derive(HashStable, TypeFoldable, TypeVisitable, Lift)] pub struct CanonicalVarValues<'tcx> { - pub var_values: ty::SubstsRef<'tcx>, + pub var_values: ty::GenericArgsRef<'tcx>, } impl CanonicalVarValues<'_> { @@ -429,7 +429,7 @@ impl<'tcx> CanonicalVarValues<'tcx> { infos: CanonicalVarInfos<'tcx>, ) -> CanonicalVarValues<'tcx> { CanonicalVarValues { - var_values: tcx.mk_substs_from_iter(infos.iter().enumerate().map( + var_values: tcx.mk_args_from_iter(infos.iter().enumerate().map( |(i, info)| -> ty::GenericArg<'tcx> { match info.kind { CanonicalVarKind::Ty(_) | CanonicalVarKind::PlaceholderTy(_) => { diff --git a/compiler/rustc_middle/src/infer/mod.rs b/compiler/rustc_middle/src/infer/mod.rs index 2db59f37f..493bb8a68 100644 --- a/compiler/rustc_middle/src/infer/mod.rs +++ b/compiler/rustc_middle/src/infer/mod.rs @@ -15,7 +15,7 @@ use rustc_span::Span; #[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(HashStable, TypeFoldable, TypeVisitable, Lift)] pub struct MemberConstraint<'tcx> { - /// The `DefId` and substs of the opaque type causing this constraint. + /// The `DefId` and args of the opaque type causing this constraint. /// Used for error reporting. pub key: OpaqueTypeKey<'tcx>, diff --git a/compiler/rustc_middle/src/lib.rs b/compiler/rustc_middle/src/lib.rs index 1b125e8e2..d3fc1b285 100644 --- a/compiler/rustc_middle/src/lib.rs +++ b/compiler/rustc_middle/src/lib.rs @@ -35,7 +35,6 @@ #![feature(if_let_guard)] #![feature(inline_const)] #![feature(iter_from_generator)] -#![feature(local_key_cell_methods)] #![feature(negative_impls)] #![feature(never_type)] #![feature(extern_types)] @@ -64,6 +63,7 @@ #![feature(macro_metavar_expr)] #![recursion_limit = "512"] #![allow(rustc::potential_query_instability)] +#![cfg_attr(not(bootstrap), allow(internal_features))] #[macro_use] extern crate bitflags; diff --git a/compiler/rustc_middle/src/lint.rs b/compiler/rustc_middle/src/lint.rs index 81c1ae4f6..f62e40669 100644 --- a/compiler/rustc_middle/src/lint.rs +++ b/compiler/rustc_middle/src/lint.rs @@ -169,26 +169,6 @@ impl TyCtxt<'_> { pub fn lint_level_at_node(self, lint: &'static Lint, id: HirId) -> (Level, LintLevelSource) { self.shallow_lint_levels_on(id.owner).lint_level_id_at_node(self, LintId::of(lint), id) } - - /// Walks upwards from `id` to find a node which might change lint levels with attributes. - /// It stops at `bound` and just returns it if reached. - pub fn maybe_lint_level_root_bounded(self, mut id: HirId, bound: HirId) -> HirId { - let hir = self.hir(); - loop { - if id == bound { - return bound; - } - - if hir.attrs(id).iter().any(|attr| Level::from_attr(attr).is_some()) { - return id; - } - let next = hir.parent_id(id); - if next == id { - bug!("lint traversal reached the root of the crate"); - } - id = next; - } - } } /// This struct represents a lint expectation and holds all required information @@ -238,14 +218,12 @@ pub fn explain_lint_level_source( let hyphen_case_lint_name = name.replace('_', "-"); if lint_flag_val.as_str() == name { err.note_once(format!( - "requested on the command line with `{} {}`", - flag, hyphen_case_lint_name + "requested on the command line with `{flag} {hyphen_case_lint_name}`" )); } else { let hyphen_case_flag_val = lint_flag_val.as_str().replace('_', "-"); err.note_once(format!( - "`{} {}` implied by `{} {}`", - flag, hyphen_case_lint_name, flag, hyphen_case_flag_val + "`{flag} {hyphen_case_lint_name}` implied by `{flag} {hyphen_case_flag_val}`" )); } } @@ -257,8 +235,7 @@ pub fn explain_lint_level_source( if lint_attr_name.as_str() != name { let level_str = level.as_str(); err.note_once(format!( - "`#[{}({})]` implied by `#[{}({})]`", - level_str, name, level_str, lint_attr_name + "`#[{level_str}({name})]` implied by `#[{level_str}({lint_attr_name})]`" )); } } @@ -298,6 +275,7 @@ pub fn explain_lint_level_source( /// // ^^^^^^^^^^^^^^^^^^^^^ returns `&mut DiagnosticBuilder` by default /// ) /// ``` +#[track_caller] pub fn struct_lint_level( sess: &Session, lint: &'static Lint, @@ -311,6 +289,7 @@ pub fn struct_lint_level( ) { // Avoid codegen bloat from monomorphization by immediately doing dyn dispatch of `decorate` to // the "real" work. + #[track_caller] fn struct_lint_level_impl( sess: &Session, lint: &'static Lint, @@ -434,12 +413,11 @@ pub fn struct_lint_level( FutureIncompatibilityReason::EditionError(edition) => { let current_edition = sess.edition(); format!( - "this is accepted in the current edition (Rust {}) but is a hard error in Rust {}!", - current_edition, edition + "this is accepted in the current edition (Rust {current_edition}) but is a hard error in Rust {edition}!" ) } FutureIncompatibilityReason::EditionSemanticsChange(edition) => { - format!("this changes meaning in Rust {}", edition) + format!("this changes meaning in Rust {edition}") } FutureIncompatibilityReason::Custom(reason) => reason.to_owned(), }; @@ -471,7 +449,11 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool { match expn_data.kind { ExpnKind::Root | ExpnKind::Desugaring( - DesugaringKind::ForLoop | DesugaringKind::WhileLoop | DesugaringKind::OpaqueTy, + DesugaringKind::ForLoop + | DesugaringKind::WhileLoop + | DesugaringKind::OpaqueTy + | DesugaringKind::Async + | DesugaringKind::Await, ) => false, ExpnKind::AstPass(_) | ExpnKind::Desugaring(_) => true, // well, it's "external" ExpnKind::Macro(MacroKind::Bang, _) => { @@ -481,3 +463,12 @@ pub fn in_external_macro(sess: &Session, span: Span) -> bool { ExpnKind::Macro { .. } => true, // definitely a plugin } } + +/// Return whether `span` is generated by `async` or `await`. +pub fn is_from_async_await(span: Span) -> bool { + let expn_data = span.ctxt().outer_expn_data(); + match expn_data.kind { + ExpnKind::Desugaring(DesugaringKind::Async | DesugaringKind::Await) => true, + _ => false, + } +} diff --git a/compiler/rustc_middle/src/macros.rs b/compiler/rustc_middle/src/macros.rs index cd1c6c330..fca16d8e5 100644 --- a/compiler/rustc_middle/src/macros.rs +++ b/compiler/rustc_middle/src/macros.rs @@ -43,7 +43,7 @@ macro_rules! span_bug { #[macro_export] macro_rules! CloneLiftImpls { - ($($ty:ty,)+) => { + ($($ty:ty),+ $(,)?) => { $( impl<'tcx> $crate::ty::Lift<'tcx> for $ty { type Lifted = Self; @@ -59,7 +59,7 @@ macro_rules! CloneLiftImpls { /// allocated data** (i.e., don't need to be folded). #[macro_export] macro_rules! TrivialTypeTraversalImpls { - ($($ty:ty,)+) => { + ($($ty:ty),+ $(,)?) => { $( impl<'tcx> $crate::ty::fold::TypeFoldable<$crate::ty::TyCtxt<'tcx>> for $ty { fn try_fold_with<F: $crate::ty::fold::FallibleTypeFolder<$crate::ty::TyCtxt<'tcx>>>( diff --git a/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs b/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs index c4601a1fb..02fd6ed7b 100644 --- a/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs +++ b/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs @@ -100,6 +100,8 @@ bitflags! { const REALLOCATOR = 1 << 18; /// `#[rustc_allocator_zeroed]`: a hint to LLVM that the function only allocates zeroed memory. const ALLOCATOR_ZEROED = 1 << 19; + /// `#[no_builtins]`: indicates that disable implicit builtin knowledge of functions for the function. + const NO_BUILTINS = 1 << 20; } } diff --git a/compiler/rustc_middle/src/middle/exported_symbols.rs b/compiler/rustc_middle/src/middle/exported_symbols.rs index 9041da9a0..e30b6b203 100644 --- a/compiler/rustc_middle/src/middle/exported_symbols.rs +++ b/compiler/rustc_middle/src/middle/exported_symbols.rs @@ -1,4 +1,4 @@ -use crate::ty::subst::SubstsRef; +use crate::ty::GenericArgsRef; use crate::ty::{self, Ty, TyCtxt}; use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_macros::HashStable; @@ -41,7 +41,7 @@ pub struct SymbolExportInfo { #[derive(Eq, PartialEq, Debug, Copy, Clone, TyEncodable, TyDecodable, HashStable)] pub enum ExportedSymbol<'tcx> { NonGeneric(DefId), - Generic(DefId, SubstsRef<'tcx>), + Generic(DefId, GenericArgsRef<'tcx>), DropGlue(Ty<'tcx>), ThreadLocalShim(DefId), NoDefId(ty::SymbolName<'tcx>), @@ -53,15 +53,15 @@ impl<'tcx> ExportedSymbol<'tcx> { pub fn symbol_name_for_local_instance(&self, tcx: TyCtxt<'tcx>) -> ty::SymbolName<'tcx> { match *self { ExportedSymbol::NonGeneric(def_id) => tcx.symbol_name(ty::Instance::mono(tcx, def_id)), - ExportedSymbol::Generic(def_id, substs) => { - tcx.symbol_name(ty::Instance::new(def_id, substs)) + ExportedSymbol::Generic(def_id, args) => { + tcx.symbol_name(ty::Instance::new(def_id, args)) } ExportedSymbol::DropGlue(ty) => { tcx.symbol_name(ty::Instance::resolve_drop_in_place(tcx, ty)) } ExportedSymbol::ThreadLocalShim(def_id) => tcx.symbol_name(ty::Instance { def: ty::InstanceDef::ThreadLocalShim(def_id), - substs: ty::InternalSubsts::empty(), + args: ty::GenericArgs::empty(), }), ExportedSymbol::NoDefId(symbol_name) => symbol_name, } @@ -72,6 +72,6 @@ pub fn metadata_symbol_name(tcx: TyCtxt<'_>) -> String { format!( "rust_metadata_{}_{:08x}", tcx.crate_name(LOCAL_CRATE), - tcx.sess.local_stable_crate_id(), + tcx.stable_crate_id(LOCAL_CRATE), ) } diff --git a/compiler/rustc_middle/src/middle/privacy.rs b/compiler/rustc_middle/src/middle/privacy.rs index 5baeb1ee0..1913421f5 100644 --- a/compiler/rustc_middle/src/middle/privacy.rs +++ b/compiler/rustc_middle/src/middle/privacy.rs @@ -178,7 +178,12 @@ impl EffectiveVisibilities { // All effective visibilities except `reachable_through_impl_trait` are limited to // nominal visibility. For some items nominal visibility doesn't make sense so we // don't check this condition for them. - if !matches!(tcx.def_kind(def_id), DefKind::Impl { .. }) { + let is_impl = matches!(tcx.def_kind(def_id), DefKind::Impl { .. }); + let is_associated_item_in_trait_impl = tcx + .impl_of_method(def_id.to_def_id()) + .and_then(|impl_id| tcx.trait_id_of_impl(impl_id)) + .is_some(); + if !is_impl && !is_associated_item_in_trait_impl { let nominal_vis = tcx.visibility(def_id); if !nominal_vis.is_at_least(ev.reachable, tcx) { span_bug!( @@ -186,7 +191,7 @@ impl EffectiveVisibilities { "{:?}: reachable {:?} > nominal {:?}", def_id, ev.reachable, - nominal_vis + nominal_vis, ); } } diff --git a/compiler/rustc_middle/src/middle/region.rs b/compiler/rustc_middle/src/middle/region.rs index 10712e146..c50c5e6f7 100644 --- a/compiler/rustc_middle/src/middle/region.rs +++ b/compiler/rustc_middle/src/middle/region.rs @@ -10,7 +10,7 @@ use crate::ty::TyCtxt; use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_hir as hir; -use rustc_hir::Node; +use rustc_hir::{HirIdMap, Node}; use rustc_macros::HashStable; use rustc_query_system::ich::StableHashingContext; use rustc_span::{Span, DUMMY_SP}; @@ -228,7 +228,7 @@ pub struct ScopeTree { /// and not the enclosing *statement*. Expressions that are not present in this /// table are not rvalue candidates. The set of rvalue candidates is computed /// during type check based on a traversal of the AST. - pub rvalue_candidates: FxHashMap<hir::HirId, RvalueCandidateType>, + pub rvalue_candidates: HirIdMap<RvalueCandidateType>, /// If there are any `yield` nested within a scope, this map /// stores the `Span` of the last one and its index in the diff --git a/compiler/rustc_middle/src/middle/stability.rs b/compiler/rustc_middle/src/middle/stability.rs index 60844c17e..908ab8b61 100644 --- a/compiler/rustc_middle/src/middle/stability.rs +++ b/compiler/rustc_middle/src/middle/stability.rs @@ -107,7 +107,7 @@ pub fn report_unstable( soft_handler: impl FnOnce(&'static Lint, Span, String), ) { let msg = match reason { - Some(r) => format!("use of unstable library feature '{}': {}", feature, r), + Some(r) => format!("use of unstable library feature '{feature}': {r}"), None => format!("use of unstable library feature '{}'", &feature), }; @@ -170,7 +170,7 @@ pub fn deprecation_suggestion( if let Some(suggestion) = suggestion { diag.span_suggestion_verbose( span, - format!("replace the use of the deprecated {}", kind), + format!("replace the use of the deprecated {kind}"), suggestion, Applicability::MachineApplicable, ); @@ -189,12 +189,12 @@ fn deprecation_message( path: &str, ) -> String { let message = if is_in_effect { - format!("use of deprecated {} `{}`", kind, path) + format!("use of deprecated {kind} `{path}`") } else { let since = since.as_ref().map(Symbol::as_str); if since == Some("TBD") { - format!("use of {} `{}` that will be deprecated in a future Rust version", kind, path) + format!("use of {kind} `{path}` that will be deprecated in a future Rust version") } else { format!( "use of {} `{}` that will be deprecated in future version {}", @@ -206,7 +206,7 @@ fn deprecation_message( }; match note { - Some(reason) => format!("{}: {}", message, reason), + Some(reason) => format!("{message}: {reason}"), None => message, } } @@ -312,7 +312,7 @@ fn suggestion_for_allocator_api( return Some(( inner_types, "consider wrapping the inner types in tuple".to_string(), - format!("({})", snippet), + format!("({snippet})"), Applicability::MaybeIncorrect, )); } @@ -599,7 +599,7 @@ impl<'tcx> TyCtxt<'tcx> { |span, def_id| { // The API could be uncallable for other reasons, for example when a private module // was referenced. - self.sess.delay_span_bug(span, format!("encountered unmarked API: {:?}", def_id)); + self.sess.delay_span_bug(span, format!("encountered unmarked API: {def_id:?}")); }, ) } diff --git a/compiler/rustc_middle/src/mir/basic_blocks.rs b/compiler/rustc_middle/src/mir/basic_blocks.rs index 7722e7b47..0ad17e819 100644 --- a/compiler/rustc_middle/src/mir/basic_blocks.rs +++ b/compiler/rustc_middle/src/mir/basic_blocks.rs @@ -178,9 +178,7 @@ impl<'tcx> graph::WithPredecessors for BasicBlocks<'tcx> { } } -TrivialTypeTraversalAndLiftImpls! { - Cache, -} +TrivialTypeTraversalAndLiftImpls! { Cache } impl<S: Encoder> Encodable<S> for Cache { #[inline] diff --git a/compiler/rustc_middle/src/mir/coverage.rs b/compiler/rustc_middle/src/mir/coverage.rs index db24dae11..1efb54bdb 100644 --- a/compiler/rustc_middle/src/mir/coverage.rs +++ b/compiler/rustc_middle/src/mir/coverage.rs @@ -6,69 +6,43 @@ use rustc_span::Symbol; use std::fmt::{self, Debug, Formatter}; rustc_index::newtype_index! { - /// An ExpressionOperandId value is assigned directly from either a - /// CounterValueReference.as_u32() (which ascend from 1) or an ExpressionOperandId.as_u32() - /// (which _*descend*_ from u32::MAX). Id value `0` (zero) represents a virtual counter with a - /// constant value of `0`. - #[derive(HashStable)] - #[max = 0xFFFF_FFFF] - #[debug_format = "ExpressionOperandId({})"] - pub struct ExpressionOperandId { - } -} - -impl ExpressionOperandId { - /// An expression operand for a "zero counter", as described in the following references: - /// - /// * <https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#counter> - /// * <https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#tag> - /// * <https://github.com/rust-lang/llvm-project/blob/rustc/13.0-2021-09-30/llvm/docs/CoverageMappingFormat.rst#counter-expressions> + /// ID of a coverage counter. Values ascend from 0. /// - /// This operand can be used to count two or more separate code regions with a single counter, - /// if they run sequentially with no branches, by injecting the `Counter` in a `BasicBlock` for - /// one of the code regions, and inserting `CounterExpression`s ("add ZERO to the counter") in - /// the coverage map for the other code regions. - pub const ZERO: Self = Self::from_u32(0); -} - -rustc_index::newtype_index! { + /// Note that LLVM handles counter IDs as `uint32_t`, so there is no need + /// to use a larger representation on the Rust side. #[derive(HashStable)] #[max = 0xFFFF_FFFF] - #[debug_format = "CounterValueReference({})"] - pub struct CounterValueReference {} + #[debug_format = "CounterId({})"] + pub struct CounterId {} } -impl CounterValueReference { - /// Counters start at 1 to reserve 0 for ExpressionOperandId::ZERO. - pub const START: Self = Self::from_u32(1); +impl CounterId { + pub const START: Self = Self::from_u32(0); - /// Returns explicitly-requested zero-based version of the counter id, used - /// during codegen. LLVM expects zero-based indexes. - pub fn zero_based_index(self) -> u32 { - let one_based_index = self.as_u32(); - debug_assert!(one_based_index > 0); - one_based_index - 1 + #[inline(always)] + pub fn next_id(self) -> Self { + Self::from_u32(self.as_u32() + 1) } } rustc_index::newtype_index! { - /// InjectedExpressionId.as_u32() converts to ExpressionOperandId.as_u32() + /// ID of a coverage-counter expression. Values ascend from 0. /// - /// Values descend from u32::MAX. + /// Note that LLVM handles expression IDs as `uint32_t`, so there is no need + /// to use a larger representation on the Rust side. #[derive(HashStable)] #[max = 0xFFFF_FFFF] - #[debug_format = "InjectedExpressionId({})"] - pub struct InjectedExpressionId {} + #[debug_format = "ExpressionId({})"] + pub struct ExpressionId {} } -rustc_index::newtype_index! { - /// InjectedExpressionIndex.as_u32() translates to u32::MAX - ExpressionOperandId.as_u32() - /// - /// Values ascend from 0. - #[derive(HashStable)] - #[max = 0xFFFF_FFFF] - #[debug_format = "InjectedExpressionIndex({})"] - pub struct InjectedExpressionIndex {} +impl ExpressionId { + pub const START: Self = Self::from_u32(0); + + #[inline(always)] + pub fn next_id(self) -> Self { + Self::from_u32(self.as_u32() + 1) + } } rustc_index::newtype_index! { @@ -81,17 +55,25 @@ rustc_index::newtype_index! { pub struct MappedExpressionIndex {} } -impl From<CounterValueReference> for ExpressionOperandId { - #[inline] - fn from(v: CounterValueReference) -> ExpressionOperandId { - ExpressionOperandId::from(v.as_u32()) - } +/// Operand of a coverage-counter expression. +/// +/// Operands can be a constant zero value, an actual coverage counter, or another +/// expression. Counter/expression operands are referred to by ID. +#[derive(Copy, Clone, PartialEq, Eq)] +#[derive(TyEncodable, TyDecodable, Hash, HashStable, TypeFoldable, TypeVisitable)] +pub enum Operand { + Zero, + Counter(CounterId), + Expression(ExpressionId), } -impl From<InjectedExpressionId> for ExpressionOperandId { - #[inline] - fn from(v: InjectedExpressionId) -> ExpressionOperandId { - ExpressionOperandId::from(v.as_u32()) +impl Debug for Operand { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + match self { + Self::Zero => write!(f, "Zero"), + Self::Counter(id) => f.debug_tuple("Counter").field(&id.as_u32()).finish(), + Self::Expression(id) => f.debug_tuple("Expression").field(&id.as_u32()).finish(), + } } } @@ -99,32 +81,21 @@ impl From<InjectedExpressionId> for ExpressionOperandId { pub enum CoverageKind { Counter { function_source_hash: u64, - id: CounterValueReference, + /// ID of this counter within its enclosing function. + /// Expressions in the same function can refer to it as an operand. + id: CounterId, }, Expression { - id: InjectedExpressionId, - lhs: ExpressionOperandId, + /// ID of this coverage-counter expression within its enclosing function. + /// Other expressions in the same function can refer to it as an operand. + id: ExpressionId, + lhs: Operand, op: Op, - rhs: ExpressionOperandId, + rhs: Operand, }, Unreachable, } -impl CoverageKind { - pub fn as_operand_id(&self) -> ExpressionOperandId { - use CoverageKind::*; - match *self { - Counter { id, .. } => ExpressionOperandId::from(id), - Expression { id, .. } => ExpressionOperandId::from(id), - Unreachable => bug!("Unreachable coverage cannot be part of an expression"), - } - } - - pub fn is_expression(&self) -> bool { - matches!(self, Self::Expression { .. }) - } -} - impl Debug for CoverageKind { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { use CoverageKind::*; @@ -132,14 +103,14 @@ impl Debug for CoverageKind { Counter { id, .. } => write!(fmt, "Counter({:?})", id.index()), Expression { id, lhs, op, rhs } => write!( fmt, - "Expression({:?}) = {} {} {}", + "Expression({:?}) = {:?} {} {:?}", id.index(), - lhs.index(), + lhs, match op { Op::Add => "+", Op::Subtract => "-", }, - rhs.index(), + rhs, ), Unreachable => write!(fmt, "Unreachable"), } diff --git a/compiler/rustc_middle/src/mir/generic_graph.rs b/compiler/rustc_middle/src/mir/generic_graph.rs index d1f3561c0..d1753427e 100644 --- a/compiler/rustc_middle/src/mir/generic_graph.rs +++ b/compiler/rustc_middle/src/mir/generic_graph.rs @@ -7,7 +7,7 @@ use rustc_middle::ty::TyCtxt; pub fn mir_fn_to_generic_graph<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'_>) -> Graph { let def_id = body.source.def_id(); let def_name = graphviz_safe_def_name(def_id); - let graph_name = format!("Mir_{}", def_name); + let graph_name = format!("Mir_{def_name}"); let dark_mode = tcx.sess.opts.unstable_opts.graphviz_dark_mode; // Nodes @@ -48,7 +48,7 @@ fn bb_to_graph_node(block: BasicBlock, body: &Body<'_>, dark_mode: bool) -> Node }; let style = NodeStyle { title_bg: Some(bgcolor.to_owned()), ..Default::default() }; - let mut stmts: Vec<String> = data.statements.iter().map(|x| format!("{:?}", x)).collect(); + let mut stmts: Vec<String> = data.statements.iter().map(|x| format!("{x:?}")).collect(); // add the terminator to the stmts, gsgdt can print it out separately let mut terminator_head = String::new(); diff --git a/compiler/rustc_middle/src/mir/generic_graphviz.rs b/compiler/rustc_middle/src/mir/generic_graphviz.rs index ccae7e159..299b50525 100644 --- a/compiler/rustc_middle/src/mir/generic_graphviz.rs +++ b/compiler/rustc_middle/src/mir/generic_graphviz.rs @@ -70,8 +70,8 @@ impl< writeln!(w, r#" graph [{}];"#, graph_attrs.join(" "))?; let content_attrs_str = content_attrs.join(" "); - writeln!(w, r#" node [{}];"#, content_attrs_str)?; - writeln!(w, r#" edge [{}];"#, content_attrs_str)?; + writeln!(w, r#" node [{content_attrs_str}];"#)?; + writeln!(w, r#" edge [{content_attrs_str}];"#)?; // Graph label if let Some(graph_label) = &self.graph_label { @@ -112,7 +112,7 @@ impl< // (format!("{:?}", node), color) // }; let color = if dark_mode { "dimgray" } else { "gray" }; - let (blk, bgcolor) = (format!("{:?}", node), color); + let (blk, bgcolor) = (format!("{node:?}"), color); write!( w, r#"<tr><td bgcolor="{bgcolor}" {attrs} colspan="{colspan}">{blk}</td></tr>"#, @@ -151,7 +151,7 @@ impl< } else { "".to_owned() }; - writeln!(w, r#" {} -> {} [label=<{}>];"#, src, trg, escaped_edge_label)?; + writeln!(w, r#" {src} -> {trg} [label=<{escaped_edge_label}>];"#)?; } Ok(()) } @@ -163,7 +163,7 @@ impl< W: Write, { let escaped_label = dot::escape_html(label); - writeln!(w, r#" label=<<br/><br/>{}<br align="left"/><br/><br/><br/>>;"#, escaped_label) + writeln!(w, r#" label=<<br/><br/>{escaped_label}<br align="left"/><br/><br/><br/>>;"#) } fn node(&self, node: G::Node) -> String { diff --git a/compiler/rustc_middle/src/mir/graphviz.rs b/compiler/rustc_middle/src/mir/graphviz.rs index 2de73db3a..5c7de8644 100644 --- a/compiler/rustc_middle/src/mir/graphviz.rs +++ b/compiler/rustc_middle/src/mir/graphviz.rs @@ -127,5 +127,5 @@ fn write_graph_label<'tcx, W: std::fmt::Write>( } fn escape<T: Debug>(t: &T) -> String { - dot::escape_html(&format!("{:?}", t)) + dot::escape_html(&format!("{t:?}")) } diff --git a/compiler/rustc_middle/src/mir/interpret/allocation.rs b/compiler/rustc_middle/src/mir/interpret/allocation.rs index b8030d9db..c787481bf 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation.rs @@ -18,9 +18,9 @@ use rustc_span::DUMMY_SP; use rustc_target::abi::{Align, HasDataLayout, Size}; use super::{ - read_target_uint, write_target_uint, AllocId, InterpError, InterpResult, Pointer, Provenance, - ResourceExhaustionInfo, Scalar, ScalarSizeMismatch, UndefinedBehaviorInfo, UninitBytesAccess, - UnsupportedOpInfo, + read_target_uint, write_target_uint, AllocId, BadBytesAccess, InterpError, InterpResult, + Pointer, PointerArithmetic, Provenance, ResourceExhaustionInfo, Scalar, ScalarSizeMismatch, + UndefinedBehaviorInfo, UnsupportedOpInfo, }; use crate::ty; use init_mask::*; @@ -173,13 +173,13 @@ pub enum AllocError { /// A scalar had the wrong size. ScalarSizeMismatch(ScalarSizeMismatch), /// Encountered a pointer where we needed raw bytes. - ReadPointerAsBytes, + ReadPointerAsInt(Option<BadBytesAccess>), /// Partially overwriting a pointer. - PartialPointerOverwrite(Size), + OverwritePartialPointer(Size), /// Partially copying a pointer. - PartialPointerCopy(Size), + ReadPartialPointer(Size), /// Using uninitialized data where it is not allowed. - InvalidUninitBytes(Option<UninitBytesAccess>), + InvalidUninitBytes(Option<BadBytesAccess>), } pub type AllocResult<T = ()> = Result<T, AllocError>; @@ -196,12 +196,14 @@ impl AllocError { ScalarSizeMismatch(s) => { InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ScalarSizeMismatch(s)) } - ReadPointerAsBytes => InterpError::Unsupported(UnsupportedOpInfo::ReadPointerAsBytes), - PartialPointerOverwrite(offset) => InterpError::Unsupported( - UnsupportedOpInfo::PartialPointerOverwrite(Pointer::new(alloc_id, offset)), + ReadPointerAsInt(info) => InterpError::Unsupported( + UnsupportedOpInfo::ReadPointerAsInt(info.map(|b| (alloc_id, b))), ), - PartialPointerCopy(offset) => InterpError::Unsupported( - UnsupportedOpInfo::PartialPointerCopy(Pointer::new(alloc_id, offset)), + OverwritePartialPointer(offset) => InterpError::Unsupported( + UnsupportedOpInfo::OverwritePartialPointer(Pointer::new(alloc_id, offset)), + ), + ReadPartialPointer(offset) => InterpError::Unsupported( + UnsupportedOpInfo::ReadPartialPointer(Pointer::new(alloc_id, offset)), ), InvalidUninitBytes(info) => InterpError::UndefinedBehavior( UndefinedBehaviorInfo::InvalidUninitBytes(info.map(|b| (alloc_id, b))), @@ -327,6 +329,9 @@ impl<Prov: Provenance, Bytes: AllocBytes> Allocation<Prov, (), Bytes> { /// Try to create an Allocation of `size` bytes, panics if there is not enough memory /// available to the compiler to do so. + /// + /// Example use case: To obtain an Allocation filled with specific data, + /// first call this function and then call write_scalar to fill in the right data. pub fn uninit(size: Size, align: Align) -> Self { match Self::uninit_inner(size, align, || { panic!("Allocation::uninit called with panic_on_fail had allocation failure"); @@ -433,14 +438,26 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> range: AllocRange, ) -> AllocResult<&[u8]> { self.init_mask.is_range_initialized(range).map_err(|uninit_range| { - AllocError::InvalidUninitBytes(Some(UninitBytesAccess { + AllocError::InvalidUninitBytes(Some(BadBytesAccess { access: range, - uninit: uninit_range, + bad: uninit_range, })) })?; if !Prov::OFFSET_IS_ADDR { if !self.provenance.range_empty(range, cx) { - return Err(AllocError::ReadPointerAsBytes); + // Find the provenance. + let (offset, _prov) = self + .provenance + .range_get_ptrs(range, cx) + .first() + .copied() + .expect("there must be provenance somewhere here"); + let start = offset.max(range.start); // the pointer might begin before `range`! + let end = (offset + cx.pointer_size()).min(range.end()); // the pointer might end after `range`! + return Err(AllocError::ReadPointerAsInt(Some(BadBytesAccess { + access: range, + bad: AllocRange::from(start..end), + }))); } } Ok(self.get_bytes_unchecked(range)) @@ -536,23 +553,25 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> // Now use this provenance. let ptr = Pointer::new(prov, Size::from_bytes(bits)); return Ok(Scalar::from_maybe_pointer(ptr, cx)); + } else { + // Without OFFSET_IS_ADDR, the only remaining case we can handle is total absence of + // provenance. + if self.provenance.range_empty(range, cx) { + return Ok(Scalar::from_uint(bits, range.size)); + } + // Else we have mixed provenance, that doesn't work. + return Err(AllocError::ReadPartialPointer(range.start)); } } else { // We are *not* reading a pointer. - // If we can just ignore provenance, do exactly that. - if Prov::OFFSET_IS_ADDR { + // If we can just ignore provenance or there is none, that's easy. + if Prov::OFFSET_IS_ADDR || self.provenance.range_empty(range, cx) { // We just strip provenance. return Ok(Scalar::from_uint(bits, range.size)); } + // There is some provenance and we don't have OFFSET_IS_ADDR. This doesn't work. + return Err(AllocError::ReadPointerAsInt(None)); } - - // Fallback path for when we cannot treat provenance bytewise or ignore it. - assert!(!Prov::OFFSET_IS_ADDR); - if !self.provenance.range_empty(range, cx) { - return Err(AllocError::ReadPointerAsBytes); - } - // There is no provenance, we can just return the bits. - Ok(Scalar::from_uint(bits, range.size)) } /// Writes a *non-ZST* scalar. @@ -571,7 +590,7 @@ impl<Prov: Provenance, Extra, Bytes: AllocBytes> Allocation<Prov, Extra, Bytes> assert!(self.mutability == Mutability::Mut); // `to_bits_or_ptr_internal` is the right method because we just want to store this data - // as-is into memory. + // as-is into memory. This also double-checks that `val.size()` matches `range.size`. let (bytes, provenance) = match val.to_bits_or_ptr_internal(range.size)? { Right(ptr) => { let (provenance, offset) = ptr.into_parts(); diff --git a/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs b/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs index d4dd56a42..2c6bb908f 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation/init_mask.rs @@ -542,11 +542,7 @@ impl InitMaskMaterialized { debug_assert_eq!( result, find_bit_slow(self, start, end, is_init), - "optimized implementation of find_bit is wrong for start={:?} end={:?} is_init={} init_mask={:#?}", - start, - end, - is_init, - self + "optimized implementation of find_bit is wrong for start={start:?} end={end:?} is_init={is_init} init_mask={self:#?}" ); result diff --git a/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs b/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs index 318f93e12..0243fc451 100644 --- a/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs +++ b/compiler/rustc_middle/src/mir/interpret/allocation/provenance_map.rs @@ -66,7 +66,11 @@ impl<Prov: Provenance> ProvenanceMap<Prov> { /// Returns all ptr-sized provenance in the given range. /// If the range has length 0, returns provenance that crosses the edge between `start-1` and /// `start`. - fn range_get_ptrs(&self, range: AllocRange, cx: &impl HasDataLayout) -> &[(Size, Prov)] { + pub(super) fn range_get_ptrs( + &self, + range: AllocRange, + cx: &impl HasDataLayout, + ) -> &[(Size, Prov)] { // We have to go back `pointer_size - 1` bytes, as that one would still overlap with // the beginning of this range. let adjusted_start = Size::from_bytes( @@ -158,7 +162,7 @@ impl<Prov: Provenance> ProvenanceMap<Prov> { if first < start { if !Prov::OFFSET_IS_ADDR { // We can't split up the provenance into less than a pointer. - return Err(AllocError::PartialPointerOverwrite(first)); + return Err(AllocError::OverwritePartialPointer(first)); } // Insert the remaining part in the bytewise provenance. let prov = self.ptrs[&first]; @@ -171,7 +175,7 @@ impl<Prov: Provenance> ProvenanceMap<Prov> { let begin_of_last = last - cx.data_layout().pointer_size; if !Prov::OFFSET_IS_ADDR { // We can't split up the provenance into less than a pointer. - return Err(AllocError::PartialPointerOverwrite(begin_of_last)); + return Err(AllocError::OverwritePartialPointer(begin_of_last)); } // Insert the remaining part in the bytewise provenance. let prov = self.ptrs[&begin_of_last]; @@ -246,10 +250,10 @@ impl<Prov: Provenance> ProvenanceMap<Prov> { if !Prov::OFFSET_IS_ADDR { // There can't be any bytewise provenance, and we cannot split up the begin/end overlap. if let Some(entry) = begin_overlap { - return Err(AllocError::PartialPointerCopy(entry.0)); + return Err(AllocError::ReadPartialPointer(entry.0)); } if let Some(entry) = end_overlap { - return Err(AllocError::PartialPointerCopy(entry.0)); + return Err(AllocError::ReadPartialPointer(entry.0)); } debug_assert!(self.bytes.is_none()); } else { diff --git a/compiler/rustc_middle/src/mir/interpret/error.rs b/compiler/rustc_middle/src/mir/interpret/error.rs index 2435bc59e..e6ef5a41e 100644 --- a/compiler/rustc_middle/src/mir/interpret/error.rs +++ b/compiler/rustc_middle/src/mir/interpret/error.rs @@ -12,7 +12,8 @@ use rustc_errors::{ use rustc_macros::HashStable; use rustc_session::CtfeBacktrace; use rustc_span::def_id::DefId; -use rustc_target::abi::{call, Align, Size, WrappingRange}; +use rustc_target::abi::{call, Align, Size, VariantIdx, WrappingRange}; + use std::borrow::Cow; use std::{any::Any, backtrace::Backtrace, fmt}; @@ -22,7 +23,7 @@ pub enum ErrorHandled { /// *guaranteed* to fail. Warnings/lints *must not* produce `Reported`. Reported(ReportedErrorInfo), /// Don't emit an error, the evaluation failed because the MIR was generic - /// and the substs didn't fully monomorphize it. + /// and the args didn't fully monomorphize it. TooGeneric, } @@ -66,9 +67,7 @@ impl Into<ErrorGuaranteed> for ReportedErrorInfo { } } -TrivialTypeTraversalAndLiftImpls! { - ErrorHandled, -} +TrivialTypeTraversalAndLiftImpls! { ErrorHandled } pub type EvalToAllocationRawResult<'tcx> = Result<ConstAlloc<'tcx>, ErrorHandled>; pub type EvalToConstValueResult<'tcx> = Result<ConstValue<'tcx>, ErrorHandled>; @@ -135,10 +134,6 @@ impl InterpErrorBacktrace { } impl<'tcx> InterpErrorInfo<'tcx> { - pub fn from_parts(kind: InterpError<'tcx>, backtrace: InterpErrorBacktrace) -> Self { - Self(Box::new(InterpErrorInfoInner { kind, backtrace })) - } - pub fn into_parts(self) -> (InterpError<'tcx>, InterpErrorBacktrace) { let InterpErrorInfo(box InterpErrorInfoInner { kind, backtrace }) = self; (kind, backtrace) @@ -156,7 +151,7 @@ impl<'tcx> InterpErrorInfo<'tcx> { } fn print_backtrace(backtrace: &Backtrace) { - eprintln!("\n\nAn error occurred in miri:\n{}", backtrace); + eprintln!("\n\nAn error occurred in the MIR interpreter:\n{backtrace}"); } impl From<ErrorGuaranteed> for InterpErrorInfo<'_> { @@ -189,11 +184,8 @@ pub enum InvalidProgramInfo<'tcx> { /// (which unfortunately typeck does not reject). /// Not using `FnAbiError` as that contains a nested `LayoutError`. FnAbiAdjustForForeignAbi(call::AdjustForForeignAbiError), - /// SizeOf of unsized type was requested. - SizeOfUnsizedType(Ty<'tcx>), - /// An unsized local was accessed without having been initialized. - /// This is not meaningful as we can't even have backing memory for such locals. - UninitUnsizedLocal, + /// We are runnning into a nonsense situation due to ConstProp violating our invariants. + ConstPropNonsense, } /// Details of why a pointer had to be in-bounds. @@ -228,13 +220,13 @@ impl IntoDiagnosticArg for InvalidMetaKind { } } -/// Details of an access to uninitialized bytes where it is not allowed. +/// Details of an access to uninitialized bytes / bad pointer bytes where it is not allowed. #[derive(Debug, Clone, Copy)] -pub struct UninitBytesAccess { +pub struct BadBytesAccess { /// Range of the original memory access. pub access: AllocRange, - /// Range of the uninit memory that was encountered. (Might not be maximal.) - pub uninit: AllocRange, + /// Range of the bad memory that was encountered. (Might not be maximal.) + pub bad: AllocRange, } /// Information about a size mismatch. @@ -284,8 +276,8 @@ pub enum UndefinedBehaviorInfo<'a> { InvalidMeta(InvalidMetaKind), /// Reading a C string that does not end within its allocation. UnterminatedCString(Pointer), - /// Dereferencing a dangling pointer after it got freed. - PointerUseAfterFree(AllocId), + /// Using a pointer after it got freed. + PointerUseAfterFree(AllocId, CheckInAllocMsg), /// Used a pointer outside the bounds it is valid for. /// (If `ptr_size > 0`, determines the size of the memory range that was expected to be in-bounds.) PointerOutOfBounds { @@ -318,15 +310,17 @@ pub enum UndefinedBehaviorInfo<'a> { /// Using a string that is not valid UTF-8, InvalidStr(std::str::Utf8Error), /// Using uninitialized data where it is not allowed. - InvalidUninitBytes(Option<(AllocId, UninitBytesAccess)>), + InvalidUninitBytes(Option<(AllocId, BadBytesAccess)>), /// Working with a local that is not currently live. DeadLocal, /// Data size is not equal to target size. ScalarSizeMismatch(ScalarSizeMismatch), /// A discriminant of an uninhabited enum variant is written. - UninhabitedEnumVariantWritten, + UninhabitedEnumVariantWritten(VariantIdx), + /// An uninhabited enum variant is projected. + UninhabitedEnumVariantRead(VariantIdx), /// Validation error. - Validation(ValidationErrorInfo<'a>), + ValidationError(ValidationErrorInfo<'a>), // FIXME(fee1-dead) these should all be actual variants of the enum instead of dynamically // dispatched /// A custom (free-form) error, created by `err_ub_custom!`. @@ -368,6 +362,8 @@ pub enum ExpectedKind { Float, Int, FnPtr, + EnumTag, + Str, } impl From<PointerKind> for ExpectedKind { @@ -381,10 +377,11 @@ impl From<PointerKind> for ExpectedKind { #[derive(Debug)] pub enum ValidationErrorKind<'tcx> { + PointerAsInt { expected: ExpectedKind }, + PartialPointer, PtrToUninhabited { ptr_kind: PointerKind, ty: Ty<'tcx> }, PtrToStatic { ptr_kind: PointerKind }, PtrToMut { ptr_kind: PointerKind }, - ExpectedNonPtr { value: String }, MutableRefInConst, NullFnPtr, NeverVal, @@ -394,10 +391,8 @@ pub enum ValidationErrorKind<'tcx> { UnsafeCell, UninhabitedVal { ty: Ty<'tcx> }, InvalidEnumTag { value: String }, - UninitEnumTag, - UninitStr, + UninhabitedEnumVariant, Uninit { expected: ExpectedKind }, - UninitVal, InvalidVTablePtr { value: String }, InvalidMetaSliceTooLarge { ptr_kind: PointerKind }, InvalidMetaTooLarge { ptr_kind: PointerKind }, @@ -425,12 +420,12 @@ pub enum UnsupportedOpInfo { // /// Overwriting parts of a pointer; without knowing absolute addresses, the resulting state /// cannot be represented by the CTFE interpreter. - PartialPointerOverwrite(Pointer<AllocId>), - /// Attempting to `copy` parts of a pointer to somewhere else; without knowing absolute + OverwritePartialPointer(Pointer<AllocId>), + /// Attempting to read or copy parts of a pointer to somewhere else; without knowing absolute /// addresses, the resulting state cannot be represented by the CTFE interpreter. - PartialPointerCopy(Pointer<AllocId>), - /// Encountered a pointer where we needed raw bytes. - ReadPointerAsBytes, + ReadPartialPointer(Pointer<AllocId>), + /// Encountered a pointer where we needed an integer. + ReadPointerAsInt(Option<(AllocId, BadBytesAccess)>), /// Accessing thread local statics ThreadLocalStatic(DefId), /// Accessing an unsupported extern static. @@ -496,7 +491,7 @@ impl InterpError<'_> { matches!( self, InterpError::Unsupported(UnsupportedOpInfo::Unsupported(_)) - | InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Validation { .. }) + | InterpError::UndefinedBehavior(UndefinedBehaviorInfo::ValidationError { .. }) | InterpError::UndefinedBehavior(UndefinedBehaviorInfo::Ub(_)) ) } diff --git a/compiler/rustc_middle/src/mir/interpret/mod.rs b/compiler/rustc_middle/src/mir/interpret/mod.rs index 2d2cfee1b..3543158bf 100644 --- a/compiler/rustc_middle/src/mir/interpret/mod.rs +++ b/compiler/rustc_middle/src/mir/interpret/mod.rs @@ -138,15 +138,15 @@ use rustc_target::abi::{AddressSpace, Endian, HasDataLayout}; use crate::mir; use crate::ty::codec::{TyDecoder, TyEncoder}; -use crate::ty::subst::GenericArgKind; +use crate::ty::GenericArgKind; use crate::ty::{self, Instance, Ty, TyCtxt}; pub use self::error::{ - struct_error, CheckInAllocMsg, ErrorHandled, EvalToAllocationRawResult, EvalToConstValueResult, - EvalToValTreeResult, ExpectedKind, InterpError, InterpErrorInfo, InterpResult, InvalidMetaKind, - InvalidProgramInfo, MachineStopType, PointerKind, ReportedErrorInfo, ResourceExhaustionInfo, - ScalarSizeMismatch, UndefinedBehaviorInfo, UninitBytesAccess, UnsupportedOpInfo, - ValidationErrorInfo, ValidationErrorKind, + struct_error, BadBytesAccess, CheckInAllocMsg, ErrorHandled, EvalToAllocationRawResult, + EvalToConstValueResult, EvalToValTreeResult, ExpectedKind, InterpError, InterpErrorInfo, + InterpResult, InvalidMetaKind, InvalidProgramInfo, MachineStopType, PointerKind, + ReportedErrorInfo, ResourceExhaustionInfo, ScalarSizeMismatch, UndefinedBehaviorInfo, + UnsupportedOpInfo, ValidationErrorInfo, ValidationErrorKind, }; pub use self::value::{get_slice_bytes, ConstAlloc, ConstValue, Scalar}; @@ -176,7 +176,7 @@ impl<'tcx> GlobalId<'tcx> { pub fn display(self, tcx: TyCtxt<'tcx>) -> String { let instance_name = with_no_trimmed_paths!(tcx.def_path_str(self.instance.def.def_id())); if let Some(promoted) = self.promoted { - format!("{}::{:?}", instance_name, promoted) + format!("{instance_name}::{promoted:?}") } else { instance_name } @@ -274,7 +274,7 @@ pub struct AllocDecodingState { // For each `AllocId`, we keep track of which decoding state it's currently in. decoding_state: Vec<Lock<State>>, // The offsets of each allocation in the data stream. - data_offsets: Vec<u32>, + data_offsets: Vec<u64>, } impl AllocDecodingState { @@ -289,7 +289,7 @@ impl AllocDecodingState { AllocDecodingSession { state: self, session_id } } - pub fn new(data_offsets: Vec<u32>) -> Self { + pub fn new(data_offsets: Vec<u64>) -> Self { let decoding_state = std::iter::repeat_with(|| Lock::new(State::Empty)).take(data_offsets.len()).collect(); @@ -559,7 +559,7 @@ impl<'tcx> TyCtxt<'tcx> { // However, formatting code relies on function identity (see #58320), so we only do // this for generic functions. Lifetime parameters are ignored. let is_generic = instance - .substs + .args .into_iter() .any(|kind| !matches!(kind.unpack(), GenericArgKind::Lifetime(_))); if is_generic { @@ -609,7 +609,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Panics in case the `AllocId` is dangling. Since that is impossible for `AllocId`s in /// constants (as all constants must pass interning and validation that check for dangling /// ids), this function is frequently used throughout rustc, but should not be used within - /// the miri engine. + /// the interpreter. pub fn global_alloc(self, id: AllocId) -> GlobalAlloc<'tcx> { match self.try_get_global_alloc(id) { Some(alloc) => alloc, diff --git a/compiler/rustc_middle/src/mir/interpret/queries.rs b/compiler/rustc_middle/src/mir/interpret/queries.rs index 9c97431f3..fc659ce18 100644 --- a/compiler/rustc_middle/src/mir/interpret/queries.rs +++ b/compiler/rustc_middle/src/mir/interpret/queries.rs @@ -2,8 +2,8 @@ use super::{ErrorHandled, EvalToConstValueResult, EvalToValTreeResult, GlobalId} use crate::mir; use crate::query::{TyCtxtAt, TyCtxtEnsure}; -use crate::ty::subst::InternalSubsts; use crate::ty::visit::TypeVisitableExt; +use crate::ty::GenericArgs; use crate::ty::{self, TyCtxt}; use rustc_hir::def::DefKind; use rustc_hir::def_id::DefId; @@ -20,8 +20,8 @@ impl<'tcx> TyCtxt<'tcx> { // to be used. So we can't use `Instance::mono`, instead we feed unresolved substitutions // into `const_eval` which will return `ErrorHandled::ToGeneric` if any of them are // encountered. - let substs = InternalSubsts::identity_for_item(self, def_id); - let instance = ty::Instance::new(def_id, substs); + let args = GenericArgs::identity_for_item(self, def_id); + let instance = ty::Instance::new(def_id, args); let cid = GlobalId { instance, promoted: None }; let param_env = self.param_env(def_id).with_reveal_all_normalized(self); self.const_eval_global_id(param_env, cid, None) @@ -48,14 +48,14 @@ impl<'tcx> TyCtxt<'tcx> { // // When trying to evaluate constants containing inference variables, // use `Infcx::const_eval_resolve` instead. - if ct.substs.has_non_region_infer() { + if ct.args.has_non_region_infer() { bug!("did not expect inference variables here"); } match ty::Instance::resolve( self, param_env, // FIXME: maybe have a separate version for resolving mir::UnevaluatedConst? - ct.def, ct.substs, + ct.def, ct.args, ) { Ok(Some(instance)) => { let cid = GlobalId { instance, promoted: ct.promoted }; @@ -79,11 +79,11 @@ impl<'tcx> TyCtxt<'tcx> { // // When trying to evaluate constants containing inference variables, // use `Infcx::const_eval_resolve` instead. - if ct.substs.has_non_region_infer() { + if ct.args.has_non_region_infer() { bug!("did not expect inference variables here"); } - match ty::Instance::resolve(self, param_env, ct.def, ct.substs) { + match ty::Instance::resolve(self, param_env, ct.def, ct.args) { Ok(Some(instance)) => { let cid = GlobalId { instance, promoted: None }; self.const_eval_global_id_for_typeck(param_env, cid, span).inspect(|_| { @@ -94,7 +94,7 @@ impl<'tcx> TyCtxt<'tcx> { // @lcnr believes that successfully evaluating even though there are // used generic parameters is a bug of evaluation, so checking for it // here does feel somewhat sensible. - if !self.features().generic_const_exprs && ct.substs.has_non_region_param() { + if !self.features().generic_const_exprs && ct.args.has_non_region_param() { let def_kind = self.def_kind(instance.def_id()); assert!( matches!( @@ -139,7 +139,6 @@ impl<'tcx> TyCtxt<'tcx> { cid: GlobalId<'tcx>, span: Option<Span>, ) -> EvalToConstValueResult<'tcx> { - let param_env = param_env.with_const(); // Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should // improve caching of queries. let inputs = self.erase_regions(param_env.and(cid)); @@ -158,8 +157,6 @@ impl<'tcx> TyCtxt<'tcx> { cid: GlobalId<'tcx>, span: Option<Span>, ) -> EvalToValTreeResult<'tcx> { - let param_env = param_env.with_const(); - debug!(?param_env); // Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should // improve caching of queries. let inputs = self.erase_regions(param_env.and(cid)); @@ -204,7 +201,6 @@ impl<'tcx> TyCtxtAt<'tcx> { gid: GlobalId<'tcx>, param_env: ty::ParamEnv<'tcx>, ) -> Result<mir::ConstAllocation<'tcx>, ErrorHandled> { - let param_env = param_env.with_const(); trace!("eval_to_allocation: Need to compute {:?}", gid); let raw_const = self.eval_to_allocation_raw(param_env.and(gid))?; Ok(self.global_alloc(raw_const.alloc_id).unwrap_memory()) @@ -221,11 +217,10 @@ impl<'tcx> TyCtxtEnsure<'tcx> { // to be used. So we can't use `Instance::mono`, instead we feed unresolved substitutions // into `const_eval` which will return `ErrorHandled::ToGeneric` if any of them are // encountered. - let substs = InternalSubsts::identity_for_item(self.tcx, def_id); - let instance = ty::Instance::new(def_id, substs); + let args = GenericArgs::identity_for_item(self.tcx, def_id); + let instance = ty::Instance::new(def_id, args); let cid = GlobalId { instance, promoted: None }; - let param_env = - self.tcx.param_env(def_id).with_reveal_all_normalized(self.tcx).with_const(); + let param_env = self.tcx.param_env(def_id).with_reveal_all_normalized(self.tcx); // Const-eval shouldn't depend on lifetimes at all, so we can erase them, which should // improve caching of queries. let inputs = self.tcx.erase_regions(param_env.and(cid)); @@ -238,7 +233,7 @@ impl<'tcx> TyCtxtEnsure<'tcx> { assert!(self.tcx.is_static(def_id)); let instance = ty::Instance::mono(self.tcx, def_id); let gid = GlobalId { instance, promoted: None }; - let param_env = ty::ParamEnv::reveal_all().with_const(); + let param_env = ty::ParamEnv::reveal_all(); trace!("eval_to_allocation: Need to compute {:?}", gid); self.eval_to_allocation_raw(param_env.and(gid)) } diff --git a/compiler/rustc_middle/src/mir/interpret/value.rs b/compiler/rustc_middle/src/mir/interpret/value.rs index 0416411df..5345a6588 100644 --- a/compiler/rustc_middle/src/mir/interpret/value.rs +++ b/compiler/rustc_middle/src/mir/interpret/value.rs @@ -135,8 +135,8 @@ static_assert_size!(Scalar, 24); impl<Prov: Provenance> fmt::Debug for Scalar<Prov> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Scalar::Ptr(ptr, _size) => write!(f, "{:?}", ptr), - Scalar::Int(int) => write!(f, "{:?}", int), + Scalar::Ptr(ptr, _size) => write!(f, "{ptr:?}"), + Scalar::Int(int) => write!(f, "{int:?}"), } } } @@ -144,8 +144,8 @@ impl<Prov: Provenance> fmt::Debug for Scalar<Prov> { impl<Prov: Provenance> fmt::Display for Scalar<Prov> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr), - Scalar::Int(int) => write!(f, "{}", int), + Scalar::Ptr(ptr, _size) => write!(f, "pointer to {ptr:?}"), + Scalar::Int(int) => write!(f, "{int}"), } } } @@ -153,8 +153,8 @@ impl<Prov: Provenance> fmt::Display for Scalar<Prov> { impl<Prov: Provenance> fmt::LowerHex for Scalar<Prov> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Scalar::Ptr(ptr, _size) => write!(f, "pointer to {:?}", ptr), - Scalar::Int(int) => write!(f, "{:#x}", int), + Scalar::Ptr(ptr, _size) => write!(f, "pointer to {ptr:?}"), + Scalar::Int(int) => write!(f, "{int:#x}"), } } } @@ -320,6 +320,14 @@ impl<Prov> Scalar<Prov> { } }) } + + #[inline] + pub fn size(self) -> Size { + match self { + Scalar::Int(int) => int.size(), + Scalar::Ptr(_ptr, sz) => Size::from_bytes(sz), + } + } } impl<'tcx, Prov: Provenance> Scalar<Prov> { @@ -370,15 +378,16 @@ impl<'tcx, Prov: Provenance> Scalar<Prov> { #[inline] pub fn to_bits(self, target_size: Size) -> InterpResult<'tcx, u128> { assert_ne!(target_size.bytes(), 0, "you should never look at the bits of a ZST"); - self.try_to_int().map_err(|_| err_unsup!(ReadPointerAsBytes))?.to_bits(target_size).map_err( - |size| { + self.try_to_int() + .map_err(|_| err_unsup!(ReadPointerAsInt(None)))? + .to_bits(target_size) + .map_err(|size| { err_ub!(ScalarSizeMismatch(ScalarSizeMismatch { target_size: target_size.bytes(), data_size: size.bytes(), })) .into() - }, - ) + }) } #[inline(always)] diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 28c505878..9ef3a1b30 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -12,7 +12,7 @@ use crate::ty::print::{FmtPrinter, Printer}; use crate::ty::visit::TypeVisitableExt; use crate::ty::{self, List, Ty, TyCtxt}; use crate::ty::{AdtDef, InstanceDef, ScalarInt, UserTypeAnnotationIndex}; -use crate::ty::{GenericArg, InternalSubsts, SubstsRef}; +use crate::ty::{GenericArg, GenericArgs, GenericArgsRef}; use rustc_data_structures::captures::Captures; use rustc_errors::{DiagnosticArgValue, DiagnosticMessage, ErrorGuaranteed, IntoDiagnosticArg}; @@ -619,7 +619,7 @@ impl<D: TyDecoder, T: Decodable<D>> Decodable<D> for ClearCrossCrate<T> { let val = T::decode(d); ClearCrossCrate::Set(val) } - tag => panic!("Invalid tag for ClearCrossCrate: {:?}", tag), + tag => panic!("Invalid tag for ClearCrossCrate: {tag:?}"), } } } @@ -706,9 +706,7 @@ pub enum BindingForm<'tcx> { RefForGuard, } -TrivialTypeTraversalAndLiftImpls! { - BindingForm<'tcx>, -} +TrivialTypeTraversalAndLiftImpls! { BindingForm<'tcx> } mod binding_form_impl { use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; @@ -1048,12 +1046,12 @@ pub enum VarDebugInfoContents<'tcx> { impl<'tcx> Debug for VarDebugInfoContents<'tcx> { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { match self { - VarDebugInfoContents::Const(c) => write!(fmt, "{}", c), - VarDebugInfoContents::Place(p) => write!(fmt, "{:?}", p), + VarDebugInfoContents::Const(c) => write!(fmt, "{c}"), + VarDebugInfoContents::Place(p) => write!(fmt, "{p:?}"), VarDebugInfoContents::Composite { ty, fragments } => { - write!(fmt, "{:?}{{ ", ty)?; + write!(fmt, "{ty:?}{{ ")?; for f in fragments.iter() { - write!(fmt, "{:?}, ", f)?; + write!(fmt, "{f:?}, ")?; } write!(fmt, "}}") } @@ -1111,10 +1109,6 @@ pub struct VarDebugInfo<'tcx> { /// originated from (starting from 1). Note, if MIR inlining is enabled, then this is the /// argument number in the original function before it was inlined. pub argument_index: Option<u16>, - - /// The data represents `name` dereferenced `references` times, - /// and not the direct value. - pub references: u8, } /////////////////////////////////////////////////////////////////////////// @@ -1317,55 +1311,47 @@ impl<O> AssertKind<O> { match self { BoundsCheck { ref len, ref index } => write!( f, - "\"index out of bounds: the length is {{}} but the index is {{}}\", {:?}, {:?}", - len, index + "\"index out of bounds: the length is {{}} but the index is {{}}\", {len:?}, {index:?}" ), OverflowNeg(op) => { - write!(f, "\"attempt to negate `{{}}`, which would overflow\", {:?}", op) + write!(f, "\"attempt to negate `{{}}`, which would overflow\", {op:?}") } - DivisionByZero(op) => write!(f, "\"attempt to divide `{{}}` by zero\", {:?}", op), + DivisionByZero(op) => write!(f, "\"attempt to divide `{{}}` by zero\", {op:?}"), RemainderByZero(op) => write!( f, - "\"attempt to calculate the remainder of `{{}}` with a divisor of zero\", {:?}", - op + "\"attempt to calculate the remainder of `{{}}` with a divisor of zero\", {op:?}" ), Overflow(BinOp::Add, l, r) => write!( f, - "\"attempt to compute `{{}} + {{}}`, which would overflow\", {:?}, {:?}", - l, r + "\"attempt to compute `{{}} + {{}}`, which would overflow\", {l:?}, {r:?}" ), Overflow(BinOp::Sub, l, r) => write!( f, - "\"attempt to compute `{{}} - {{}}`, which would overflow\", {:?}, {:?}", - l, r + "\"attempt to compute `{{}} - {{}}`, which would overflow\", {l:?}, {r:?}" ), Overflow(BinOp::Mul, l, r) => write!( f, - "\"attempt to compute `{{}} * {{}}`, which would overflow\", {:?}, {:?}", - l, r + "\"attempt to compute `{{}} * {{}}`, which would overflow\", {l:?}, {r:?}" ), Overflow(BinOp::Div, l, r) => write!( f, - "\"attempt to compute `{{}} / {{}}`, which would overflow\", {:?}, {:?}", - l, r + "\"attempt to compute `{{}} / {{}}`, which would overflow\", {l:?}, {r:?}" ), Overflow(BinOp::Rem, l, r) => write!( f, - "\"attempt to compute the remainder of `{{}} % {{}}`, which would overflow\", {:?}, {:?}", - l, r + "\"attempt to compute the remainder of `{{}} % {{}}`, which would overflow\", {l:?}, {r:?}" ), Overflow(BinOp::Shr, _, r) => { - write!(f, "\"attempt to shift right by `{{}}`, which would overflow\", {:?}", r) + write!(f, "\"attempt to shift right by `{{}}`, which would overflow\", {r:?}") } Overflow(BinOp::Shl, _, r) => { - write!(f, "\"attempt to shift left by `{{}}`, which would overflow\", {:?}", r) + write!(f, "\"attempt to shift left by `{{}}`, which would overflow\", {r:?}") } MisalignedPointerDereference { required, found } => { write!( f, - "\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\", {:?}, {:?}", - required, found + "\"misaligned pointer dereference: address must be a multiple of {{}} but is {{}}\", {required:?}, {found:?}" ) } _ => write!(f, "\"{}\"", self.description()), @@ -1461,9 +1447,9 @@ impl Debug for Statement<'_> { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { use self::StatementKind::*; match self.kind { - Assign(box (ref place, ref rv)) => write!(fmt, "{:?} = {:?}", place, rv), + Assign(box (ref place, ref rv)) => write!(fmt, "{place:?} = {rv:?}"), FakeRead(box (ref cause, ref place)) => { - write!(fmt, "FakeRead({:?}, {:?})", cause, place) + write!(fmt, "FakeRead({cause:?}, {place:?})") } Retag(ref kind, ref place) => write!( fmt, @@ -1476,20 +1462,20 @@ impl Debug for Statement<'_> { }, place, ), - StorageLive(ref place) => write!(fmt, "StorageLive({:?})", place), - StorageDead(ref place) => write!(fmt, "StorageDead({:?})", place), + StorageLive(ref place) => write!(fmt, "StorageLive({place:?})"), + StorageDead(ref place) => write!(fmt, "StorageDead({place:?})"), SetDiscriminant { ref place, variant_index } => { - write!(fmt, "discriminant({:?}) = {:?}", place, variant_index) + write!(fmt, "discriminant({place:?}) = {variant_index:?}") } - Deinit(ref place) => write!(fmt, "Deinit({:?})", place), + Deinit(ref place) => write!(fmt, "Deinit({place:?})"), PlaceMention(ref place) => { - write!(fmt, "PlaceMention({:?})", place) + write!(fmt, "PlaceMention({place:?})") } AscribeUserType(box (ref place, ref c_ty), ref variance) => { - write!(fmt, "AscribeUserType({:?}, {:?}, {:?})", place, variance, c_ty) + write!(fmt, "AscribeUserType({place:?}, {variance:?}, {c_ty:?})") } Coverage(box self::Coverage { ref kind, code_region: Some(ref rgn) }) => { - write!(fmt, "Coverage::{:?} for {:?}", kind, rgn) + write!(fmt, "Coverage::{kind:?} for {rgn:?}") } Coverage(box ref coverage) => write!(fmt, "Coverage::{:?}", coverage.kind), Intrinsic(box ref intrinsic) => write!(fmt, "{intrinsic}"), @@ -1602,14 +1588,13 @@ impl<'tcx> Place<'tcx> { self.projection.iter().any(|elem| elem.is_indirect()) } - /// If MirPhase >= Derefered and if projection contains Deref, - /// It's guaranteed to be in the first place - pub fn has_deref(&self) -> bool { - // To make sure this is not accidentally used in wrong mir phase - debug_assert!( - self.projection.is_empty() || !self.projection[1..].contains(&PlaceElem::Deref) - ); - self.projection.first() == Some(&PlaceElem::Deref) + /// Returns `true` if this `Place`'s first projection is `Deref`. + /// + /// This is useful because for MIR phases `AnalysisPhase::PostCleanup` and later, + /// `Deref` projections can only occur as the first projection. In that case this method + /// is equivalent to `is_indirect`, but faster. + pub fn is_indirect_first_projection(&self) -> bool { + self.as_ref().is_indirect_first_projection() } /// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or @@ -1682,9 +1667,16 @@ impl<'tcx> PlaceRef<'tcx> { self.projection.iter().any(|elem| elem.is_indirect()) } - /// If MirPhase >= Derefered and if projection contains Deref, - /// It's guaranteed to be in the first place - pub fn has_deref(&self) -> bool { + /// Returns `true` if this `Place`'s first projection is `Deref`. + /// + /// This is useful because for MIR phases `AnalysisPhase::PostCleanup` and later, + /// `Deref` projections can only occur as the first projection. In that case this method + /// is equivalent to `is_indirect`, but faster. + pub fn is_indirect_first_projection(&self) -> bool { + // To make sure this is not accidentally used in wrong mir phase + debug_assert!( + self.projection.is_empty() || !self.projection[1..].contains(&PlaceElem::Deref) + ); self.projection.first() == Some(&PlaceElem::Deref) } @@ -1769,13 +1761,13 @@ impl Debug for Place<'_> { for elem in self.projection.iter() { match elem { ProjectionElem::OpaqueCast(ty) => { - write!(fmt, " as {})", ty)?; + write!(fmt, " as {ty})")?; } ProjectionElem::Downcast(Some(name), _index) => { - write!(fmt, " as {})", name)?; + write!(fmt, " as {name})")?; } ProjectionElem::Downcast(None, index) => { - write!(fmt, " as variant#{:?})", index)?; + write!(fmt, " as variant#{index:?})")?; } ProjectionElem::Deref => { write!(fmt, ")")?; @@ -1784,25 +1776,25 @@ impl Debug for Place<'_> { write!(fmt, ".{:?}: {:?})", field.index(), ty)?; } ProjectionElem::Index(ref index) => { - write!(fmt, "[{:?}]", index)?; + write!(fmt, "[{index:?}]")?; } ProjectionElem::ConstantIndex { offset, min_length, from_end: false } => { - write!(fmt, "[{:?} of {:?}]", offset, min_length)?; + write!(fmt, "[{offset:?} of {min_length:?}]")?; } ProjectionElem::ConstantIndex { offset, min_length, from_end: true } => { - write!(fmt, "[-{:?} of {:?}]", offset, min_length)?; + write!(fmt, "[-{offset:?} of {min_length:?}]")?; } ProjectionElem::Subslice { from, to, from_end: true } if to == 0 => { - write!(fmt, "[{:?}:]", from)?; + write!(fmt, "[{from:?}:]")?; } ProjectionElem::Subslice { from, to, from_end: true } if from == 0 => { - write!(fmt, "[:-{:?}]", to)?; + write!(fmt, "[:-{to:?}]")?; } ProjectionElem::Subslice { from, to, from_end: true } => { - write!(fmt, "[{:?}:-{:?}]", from, to)?; + write!(fmt, "[{from:?}:-{to:?}]")?; } ProjectionElem::Subslice { from, to, from_end: false } => { - write!(fmt, "[{:?}..{:?}]", from, to)?; + write!(fmt, "[{from:?}..{to:?}]")?; } } } @@ -1896,24 +1888,24 @@ impl<'tcx> Debug for Operand<'tcx> { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { use self::Operand::*; match *self { - Constant(ref a) => write!(fmt, "{:?}", a), - Copy(ref place) => write!(fmt, "{:?}", place), - Move(ref place) => write!(fmt, "move {:?}", place), + Constant(ref a) => write!(fmt, "{a:?}"), + Copy(ref place) => write!(fmt, "{place:?}"), + Move(ref place) => write!(fmt, "move {place:?}"), } } } impl<'tcx> Operand<'tcx> { /// Convenience helper to make a constant that refers to the fn - /// with given `DefId` and substs. Since this is used to synthesize + /// with given `DefId` and args. Since this is used to synthesize /// MIR, assumes `user_ty` is None. pub fn function_handle( tcx: TyCtxt<'tcx>, def_id: DefId, - substs: impl IntoIterator<Item = GenericArg<'tcx>>, + args: impl IntoIterator<Item = GenericArg<'tcx>>, span: Span, ) -> Self { - let ty = Ty::new_fn_def(tcx, def_id, substs); + let ty = Ty::new_fn_def(tcx, def_id, args); Operand::Constant(Box::new(Constant { span, user_ty: None, @@ -1937,11 +1929,11 @@ impl<'tcx> Operand<'tcx> { let param_env_and_ty = ty::ParamEnv::empty().and(ty); let type_size = tcx .layout_of(param_env_and_ty) - .unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e)) + .unwrap_or_else(|e| panic!("could not compute layout for {ty:?}: {e:?}")) .size; let scalar_size = match val { Scalar::Int(int) => int.size(), - _ => panic!("Invalid scalar type {:?}", val), + _ => panic!("Invalid scalar type {val:?}"), }; scalar_size == type_size }); @@ -1981,9 +1973,9 @@ impl<'tcx> Operand<'tcx> { /// /// While this is unlikely in general, it's the normal case of what you'll /// find as the `func` in a [`TerminatorKind::Call`]. - pub fn const_fn_def(&self) -> Option<(DefId, SubstsRef<'tcx>)> { + pub fn const_fn_def(&self) -> Option<(DefId, GenericArgsRef<'tcx>)> { let const_ty = self.constant()?.literal.ty(); - if let ty::FnDef(def_id, substs) = *const_ty.kind() { Some((def_id, substs)) } else { None } + if let ty::FnDef(def_id, args) = *const_ty.kind() { Some((def_id, args)) } else { None } } } @@ -2057,26 +2049,26 @@ impl<'tcx> Debug for Rvalue<'tcx> { use self::Rvalue::*; match *self { - Use(ref place) => write!(fmt, "{:?}", place), + Use(ref place) => write!(fmt, "{place:?}"), Repeat(ref a, b) => { - write!(fmt, "[{:?}; ", a)?; + write!(fmt, "[{a:?}; ")?; pretty_print_const(b, fmt, false)?; write!(fmt, "]") } - Len(ref a) => write!(fmt, "Len({:?})", a), + Len(ref a) => write!(fmt, "Len({a:?})"), Cast(ref kind, ref place, ref ty) => { - write!(fmt, "{:?} as {:?} ({:?})", place, ty, kind) + write!(fmt, "{place:?} as {ty:?} ({kind:?})") } - BinaryOp(ref op, box (ref a, ref b)) => write!(fmt, "{:?}({:?}, {:?})", op, a, b), + BinaryOp(ref op, box (ref a, ref b)) => write!(fmt, "{op:?}({a:?}, {b:?})"), CheckedBinaryOp(ref op, box (ref a, ref b)) => { - write!(fmt, "Checked{:?}({:?}, {:?})", op, a, b) + write!(fmt, "Checked{op:?}({a:?}, {b:?})") } - UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a), - Discriminant(ref place) => write!(fmt, "discriminant({:?})", place), + UnaryOp(ref op, ref a) => write!(fmt, "{op:?}({a:?})"), + Discriminant(ref place) => write!(fmt, "discriminant({place:?})"), NullaryOp(ref op, ref t) => match op { - NullOp::SizeOf => write!(fmt, "SizeOf({:?})", t), - NullOp::AlignOf => write!(fmt, "AlignOf({:?})", t), - NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({:?}, {:?})", t, fields), + NullOp::SizeOf => write!(fmt, "SizeOf({t:?})"), + NullOp::AlignOf => write!(fmt, "AlignOf({t:?})"), + NullOp::OffsetOf(fields) => write!(fmt, "OffsetOf({t:?}, {fields:?})"), }, ThreadLocalRef(did) => ty::tls::with(|tcx| { let muta = tcx.static_mutability(did).unwrap().prefix_str(); @@ -2103,10 +2095,10 @@ impl<'tcx> Debug for Rvalue<'tcx> { // Do not even print 'static String::new() }; - write!(fmt, "&{}{}{:?}", region, kind_str, place) + write!(fmt, "&{region}{kind_str}{place:?}") } - CopyForDeref(ref place) => write!(fmt, "deref_copy {:#?}", place), + CopyForDeref(ref place) => write!(fmt, "deref_copy {place:#?}"), AddressOf(mutability, ref place) => { let kind_str = match mutability { @@ -2114,7 +2106,7 @@ impl<'tcx> Debug for Rvalue<'tcx> { Mutability::Not => "const", }; - write!(fmt, "&raw {} {:?}", kind_str, place) + write!(fmt, "&raw {kind_str} {place:?}") } Aggregate(ref kind, ref places) => { @@ -2127,7 +2119,7 @@ impl<'tcx> Debug for Rvalue<'tcx> { }; match **kind { - AggregateKind::Array(_) => write!(fmt, "{:?}", places), + AggregateKind::Array(_) => write!(fmt, "{places:?}"), AggregateKind::Tuple => { if places.is_empty() { @@ -2137,12 +2129,12 @@ impl<'tcx> Debug for Rvalue<'tcx> { } } - AggregateKind::Adt(adt_did, variant, substs, _user_ty, _) => { + AggregateKind::Adt(adt_did, variant, args, _user_ty, _) => { ty::tls::with(|tcx| { let variant_def = &tcx.adt_def(adt_did).variant(variant); - let substs = tcx.lift(substs).expect("could not lift for printing"); + let args = tcx.lift(args).expect("could not lift for printing"); let name = FmtPrinter::new(tcx, Namespace::ValueNS) - .print_def_path(variant_def.def_id, substs)? + .print_def_path(variant_def.def_id, args)? .into_buffer(); match variant_def.ctor_kind() { @@ -2159,10 +2151,10 @@ impl<'tcx> Debug for Rvalue<'tcx> { }) } - AggregateKind::Closure(def_id, substs) => ty::tls::with(|tcx| { + AggregateKind::Closure(def_id, args) => ty::tls::with(|tcx| { let name = if tcx.sess.opts.unstable_opts.span_free_formats { - let substs = tcx.lift(substs).unwrap(); - format!("[closure@{}]", tcx.def_path_str_with_substs(def_id, substs),) + let args = tcx.lift(args).unwrap(); + format!("[closure@{}]", tcx.def_path_str_with_args(def_id, args),) } else { let span = tcx.def_span(def_id); format!( @@ -2213,7 +2205,7 @@ impl<'tcx> Debug for Rvalue<'tcx> { } ShallowInitBox(ref place, ref ty) => { - write!(fmt, "ShallowInitBox({:?}, {:?})", place, ty) + write!(fmt, "ShallowInitBox({place:?}, {ty:?})") } } } @@ -2493,7 +2485,7 @@ impl<'tcx> ConstantKind<'tcx> { }; debug!("expr.kind: {:?}", expr.kind); - let ty = tcx.type_of(def).subst_identity(); + let ty = tcx.type_of(def).instantiate_identity(); debug!(?ty); // FIXME(const_generics): We currently have to special case parameters because `min_const_generics` @@ -2521,23 +2513,22 @@ impl<'tcx> ConstantKind<'tcx> { } let hir_id = tcx.hir().local_def_id_to_hir_id(def); - let parent_substs = if let Some(parent_hir_id) = tcx.hir().opt_parent_id(hir_id) + let parent_args = if let Some(parent_hir_id) = tcx.hir().opt_parent_id(hir_id) && let Some(parent_did) = parent_hir_id.as_owner() { - InternalSubsts::identity_for_item(tcx, parent_did) + GenericArgs::identity_for_item(tcx, parent_did) } else { List::empty() }; - debug!(?parent_substs); + debug!(?parent_args); let did = def.to_def_id(); - let child_substs = InternalSubsts::identity_for_item(tcx, did); - let substs = - tcx.mk_substs_from_iter(parent_substs.into_iter().chain(child_substs.into_iter())); - debug!(?substs); + let child_args = GenericArgs::identity_for_item(tcx, did); + let args = tcx.mk_args_from_iter(parent_args.into_iter().chain(child_args.into_iter())); + debug!(?args); let span = tcx.def_span(def); - let uneval = UnevaluatedConst::new(did, substs); + let uneval = UnevaluatedConst::new(did, args); debug!(?span, ?param_env); match tcx.const_eval_resolve(param_env, uneval, Some(span)) { @@ -2552,7 +2543,7 @@ impl<'tcx> ConstantKind<'tcx> { Self::Unevaluated( UnevaluatedConst { def: did, - substs: InternalSubsts::identity_for_item(tcx, did), + args: GenericArgs::identity_for_item(tcx, did), promoted: None, }, ty, @@ -2578,7 +2569,7 @@ impl<'tcx> ConstantKind<'tcx> { #[derive(Hash, HashStable, TypeFoldable, TypeVisitable)] pub struct UnevaluatedConst<'tcx> { pub def: DefId, - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, pub promoted: Option<Promoted>, } @@ -2586,14 +2577,14 @@ impl<'tcx> UnevaluatedConst<'tcx> { #[inline] pub fn shrink(self) -> ty::UnevaluatedConst<'tcx> { assert_eq!(self.promoted, None); - ty::UnevaluatedConst { def: self.def, substs: self.substs } + ty::UnevaluatedConst { def: self.def, args: self.args } } } impl<'tcx> UnevaluatedConst<'tcx> { #[inline] - pub fn new(def: DefId, substs: SubstsRef<'tcx>) -> UnevaluatedConst<'tcx> { - UnevaluatedConst { def, substs, promoted: Default::default() } + pub fn new(def: DefId, args: GenericArgsRef<'tcx>) -> UnevaluatedConst<'tcx> { + UnevaluatedConst { def, args, promoted: Default::default() } } } @@ -2758,7 +2749,7 @@ rustc_index::newtype_index! { impl<'tcx> Debug for Constant<'tcx> { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { - write!(fmt, "{}", self) + write!(fmt, "{self}") } } @@ -2834,7 +2825,7 @@ fn pretty_print_const_value<'tcx>( let ty = tcx.lift(ty).unwrap(); if tcx.sess.verbose() { - fmt.write_str(&format!("ConstValue({:?}: {})", ct, ty))?; + fmt.write_str(&format!("ConstValue({ct:?}: {ty})"))?; return Ok(()); } @@ -2904,17 +2895,17 @@ fn pretty_print_const_value<'tcx>( fmt.write_str(")")?; } ty::Adt(def, _) if def.variants().is_empty() => { - fmt.write_str(&format!("{{unreachable(): {}}}", ty))?; + fmt.write_str(&format!("{{unreachable(): {ty}}}"))?; } - ty::Adt(def, substs) => { + ty::Adt(def, args) => { let variant_idx = contents .variant .expect("destructed mir constant of adt without variant idx"); let variant_def = &def.variant(variant_idx); - let substs = tcx.lift(substs).unwrap(); + let args = tcx.lift(args).unwrap(); let mut cx = FmtPrinter::new(tcx, Namespace::ValueNS); cx.print_alloc_ids = true; - let cx = cx.print_value_path(variant_def.def_id, substs)?; + let cx = cx.print_value_path(variant_def.def_id, args)?; fmt.write_str(&cx.into_buffer())?; match variant_def.ctor_kind() { diff --git a/compiler/rustc_middle/src/mir/mono.rs b/compiler/rustc_middle/src/mir/mono.rs index ca735d523..8fd980d5a 100644 --- a/compiler/rustc_middle/src/mir/mono.rs +++ b/compiler/rustc_middle/src/mir/mono.rs @@ -1,5 +1,5 @@ use crate::dep_graph::{DepNode, WorkProduct, WorkProductId}; -use crate::ty::{subst::InternalSubsts, Instance, InstanceDef, SymbolName, TyCtxt}; +use crate::ty::{GenericArgs, Instance, InstanceDef, SymbolName, TyCtxt}; use rustc_attr::InlineAttr; use rustc_data_structures::base_n; use rustc_data_structures::fingerprint::Fingerprint; @@ -56,22 +56,31 @@ impl<'tcx> MonoItem<'tcx> { } } + // Note: if you change how item size estimates work, you might need to + // change NON_INCR_MIN_CGU_SIZE as well. pub fn size_estimate(&self, tcx: TyCtxt<'tcx>) -> usize { match *self { MonoItem::Fn(instance) => { - // Estimate the size of a function based on how many statements - // it contains. - tcx.instance_def_size_estimate(instance.def) + match instance.def { + // "Normal" functions size estimate: the number of + // statements, plus one for the terminator. + InstanceDef::Item(..) | InstanceDef::DropGlue(..) => { + let mir = tcx.instance_mir(instance.def); + mir.basic_blocks.iter().map(|bb| bb.statements.len() + 1).sum() + } + // Other compiler-generated shims size estimate: 1 + _ => 1, + } } - // Conservatively estimate the size of a static declaration - // or assembly to be 1. + // Conservatively estimate the size of a static declaration or + // assembly item to be 1. MonoItem::Static(_) | MonoItem::GlobalAsm(_) => 1, } } pub fn is_generic_fn(&self) -> bool { match *self { - MonoItem::Fn(ref instance) => instance.substs.non_erasable_generics().next().is_some(), + MonoItem::Fn(ref instance) => instance.args.non_erasable_generics().next().is_some(), MonoItem::Static(..) | MonoItem::GlobalAsm(..) => false, } } @@ -168,14 +177,14 @@ impl<'tcx> MonoItem<'tcx> { /// which will never be accessed) in its place. pub fn is_instantiable(&self, tcx: TyCtxt<'tcx>) -> bool { debug!("is_instantiable({:?})", self); - let (def_id, substs) = match *self { - MonoItem::Fn(ref instance) => (instance.def_id(), instance.substs), - MonoItem::Static(def_id) => (def_id, InternalSubsts::empty()), + let (def_id, args) = match *self { + MonoItem::Fn(ref instance) => (instance.def_id(), instance.args), + MonoItem::Static(def_id) => (def_id, GenericArgs::empty()), // global asm never has predicates MonoItem::GlobalAsm(..) => return true, }; - !tcx.subst_and_check_impossible_predicates((def_id, &substs)) + !tcx.subst_and_check_impossible_predicates((def_id, &args)) } pub fn local_span(&self, tcx: TyCtxt<'tcx>) -> Option<Span> { @@ -214,9 +223,9 @@ impl<'tcx> MonoItem<'tcx> { impl<'tcx> fmt::Display for MonoItem<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - MonoItem::Fn(instance) => write!(f, "fn {}", instance), + MonoItem::Fn(instance) => write!(f, "fn {instance}"), MonoItem::Static(def_id) => { - write!(f, "static {}", Instance::new(def_id, InternalSubsts::empty())) + write!(f, "static {}", Instance::new(def_id, GenericArgs::empty())) } MonoItem::GlobalAsm(..) => write!(f, "global_asm"), } @@ -230,7 +239,7 @@ pub struct CodegenUnit<'tcx> { /// contain something unique to this crate (e.g., a module path) /// as well as the crate name and disambiguator. name: Symbol, - items: FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)>, + items: FxHashMap<MonoItem<'tcx>, MonoItemData>, size_estimate: usize, primary: bool, /// True if this is CGU is used to hold code coverage information for dead code, @@ -238,6 +247,20 @@ pub struct CodegenUnit<'tcx> { is_code_coverage_dead_code_cgu: bool, } +/// Auxiliary info about a `MonoItem`. +#[derive(Copy, Clone, PartialEq, Debug, HashStable)] +pub struct MonoItemData { + /// A cached copy of the result of `MonoItem::instantiation_mode`, where + /// `GloballyShared` maps to `false` and `LocalCopy` maps to `true`. + pub inlined: bool, + + pub linkage: Linkage, + pub visibility: Visibility, + + /// A cached copy of the result of `MonoItem::size_estimate`. + pub size_estimate: usize, +} + /// Specifies the linkage type for a `MonoItem`. /// /// See <https://llvm.org/docs/LangRef.html#linkage-types> for more details about these variants. @@ -292,12 +315,12 @@ impl<'tcx> CodegenUnit<'tcx> { } /// The order of these items is non-determinstic. - pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> { + pub fn items(&self) -> &FxHashMap<MonoItem<'tcx>, MonoItemData> { &self.items } /// The order of these items is non-determinstic. - pub fn items_mut(&mut self) -> &mut FxHashMap<MonoItem<'tcx>, (Linkage, Visibility)> { + pub fn items_mut(&mut self) -> &mut FxHashMap<MonoItem<'tcx>, MonoItemData> { &mut self.items } @@ -320,16 +343,16 @@ impl<'tcx> CodegenUnit<'tcx> { base_n::encode(hash, base_n::CASE_INSENSITIVE) } - pub fn compute_size_estimate(&mut self, tcx: TyCtxt<'tcx>) { - // Estimate the size of a codegen unit as (approximately) the number of MIR - // statements it corresponds to. - self.size_estimate = self.items.keys().map(|mi| mi.size_estimate(tcx)).sum(); + pub fn compute_size_estimate(&mut self) { + // The size of a codegen unit as the sum of the sizes of the items + // within it. + self.size_estimate = self.items.values().map(|data| data.size_estimate).sum(); } - #[inline] /// Should only be called if [`compute_size_estimate`] has previously been called. /// /// [`compute_size_estimate`]: Self::compute_size_estimate + #[inline] pub fn size_estimate(&self) -> usize { // Items are never zero-sized, so if we have items the estimate must be // non-zero, unless we forgot to call `compute_size_estimate` first. @@ -355,7 +378,7 @@ impl<'tcx> CodegenUnit<'tcx> { pub fn items_in_deterministic_order( &self, tcx: TyCtxt<'tcx>, - ) -> Vec<(MonoItem<'tcx>, (Linkage, Visibility))> { + ) -> Vec<(MonoItem<'tcx>, MonoItemData)> { // The codegen tests rely on items being process in the same order as // they appear in the file, so for local items, we sort by node_id first #[derive(PartialEq, Eq, PartialOrd, Ord)] @@ -390,7 +413,7 @@ impl<'tcx> CodegenUnit<'tcx> { ) } - let mut items: Vec<_> = self.items().iter().map(|(&i, &l)| (i, l)).collect(); + let mut items: Vec<_> = self.items().iter().map(|(&i, &data)| (i, data)).collect(); items.sort_by_cached_key(|&(i, _)| item_sort_key(tcx, i)); items } @@ -501,27 +524,27 @@ impl<'tcx> CodegenUnitNameBuilder<'tcx> { // local crate's ID. Otherwise there can be collisions between CGUs // instantiating stuff for upstream crates. let local_crate_id = if cnum != LOCAL_CRATE { - let local_stable_crate_id = tcx.sess.local_stable_crate_id(); + let local_stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE); format!("-in-{}.{:08x}", tcx.crate_name(LOCAL_CRATE), local_stable_crate_id) } else { String::new() }; - let stable_crate_id = tcx.sess.local_stable_crate_id(); + let stable_crate_id = tcx.stable_crate_id(LOCAL_CRATE); format!("{}.{:08x}{}", tcx.crate_name(cnum), stable_crate_id, local_crate_id) }); - write!(cgu_name, "{}", crate_prefix).unwrap(); + write!(cgu_name, "{crate_prefix}").unwrap(); // Add the components for component in components { - write!(cgu_name, "-{}", component).unwrap(); + write!(cgu_name, "-{component}").unwrap(); } if let Some(special_suffix) = special_suffix { // We add a dot in here so it cannot clash with anything in a regular // Rust identifier - write!(cgu_name, ".{}", special_suffix).unwrap(); + write!(cgu_name, ".{special_suffix}").unwrap(); } Symbol::intern(&cgu_name) diff --git a/compiler/rustc_middle/src/mir/pretty.rs b/compiler/rustc_middle/src/mir/pretty.rs index ffa7a5400..773056e8a 100644 --- a/compiler/rustc_middle/src/mir/pretty.rs +++ b/compiler/rustc_middle/src/mir/pretty.rs @@ -124,14 +124,14 @@ fn dump_matched_mir_node<'tcx, F>( let def_path = ty::print::with_forced_impl_filename_line!(tcx.def_path_str(body.source.def_id())); // ignore-tidy-odd-backticks the literal below is fine - write!(file, "// MIR for `{}", def_path)?; + write!(file, "// MIR for `{def_path}")?; match body.source.promoted { None => write!(file, "`")?, - Some(promoted) => write!(file, "::{:?}`", promoted)?, + Some(promoted) => write!(file, "::{promoted:?}`")?, } - writeln!(file, " {} {}", disambiguator, pass_name)?; + writeln!(file, " {disambiguator} {pass_name}")?; if let Some(ref layout) = body.generator_layout() { - writeln!(file, "/* generator_layout = {:#?} */", layout)?; + writeln!(file, "/* generator_layout = {layout:#?} */")?; } writeln!(file)?; extra_data(PassWhere::BeforeCFG, &mut file)?; @@ -169,7 +169,7 @@ fn dump_file_basename<'tcx>( ) -> String { let source = body.source; let promotion_id = match source.promoted { - Some(id) => format!("-{:?}", id), + Some(id) => format!("-{id:?}"), None => String::new(), }; @@ -203,8 +203,7 @@ fn dump_file_basename<'tcx>( }; format!( - "{}.{}{}{}{}.{}.{}", - crate_name, item_name, shim_disambiguator, promotion_id, pass_num, pass_name, disambiguator, + "{crate_name}.{item_name}{shim_disambiguator}{promotion_id}{pass_num}.{pass_name}.{disambiguator}", ) } @@ -215,7 +214,7 @@ fn dump_path(tcx: TyCtxt<'_>, basename: &str, extension: &str) -> PathBuf { let mut file_path = PathBuf::new(); file_path.push(Path::new(&tcx.sess.opts.unstable_opts.dump_mir_dir)); - let file_name = format!("{}.{}", basename, extension,); + let file_name = format!("{basename}.{extension}",); file_path.push(&file_name); @@ -233,12 +232,12 @@ fn create_dump_file_with_basename( fs::create_dir_all(parent).map_err(|e| { io::Error::new( e.kind(), - format!("IO error creating MIR dump directory: {:?}; {}", parent, e), + format!("IO error creating MIR dump directory: {parent:?}; {e}"), ) })?; } Ok(io::BufWriter::new(fs::File::create(&file_path).map_err(|e| { - io::Error::new(e.kind(), format!("IO error creating MIR dump file: {:?}; {}", file_path, e)) + io::Error::new(e.kind(), format!("IO error creating MIR dump file: {file_path:?}; {e}")) })?)) } @@ -346,28 +345,24 @@ where // Basic block label at the top. let cleanup_text = if data.is_cleanup { " (cleanup)" } else { "" }; - writeln!(w, "{}{:?}{}: {{", INDENT, block, cleanup_text)?; + writeln!(w, "{INDENT}{block:?}{cleanup_text}: {{")?; // List of statements in the middle. let mut current_location = Location { block, statement_index: 0 }; for statement in &data.statements { extra_data(PassWhere::BeforeLocation(current_location), w)?; - let indented_body = format!("{0}{0}{1:?};", INDENT, statement); + let indented_body = format!("{INDENT}{INDENT}{statement:?};"); if tcx.sess.opts.unstable_opts.mir_include_spans { writeln!( w, "{:A$} // {}{}", indented_body, - if tcx.sess.verbose() { - format!("{:?}: ", current_location) - } else { - String::new() - }, + if tcx.sess.verbose() { format!("{current_location:?}: ") } else { String::new() }, comment(tcx, statement.source_info), A = ALIGN, )?; } else { - writeln!(w, "{}", indented_body)?; + writeln!(w, "{indented_body}")?; } write_extra(tcx, w, |visitor| { @@ -387,12 +382,12 @@ where w, "{:A$} // {}{}", indented_terminator, - if tcx.sess.verbose() { format!("{:?}: ", current_location) } else { String::new() }, + if tcx.sess.verbose() { format!("{current_location:?}: ") } else { String::new() }, comment(tcx, data.terminator().source_info), A = ALIGN, )?; } else { - writeln!(w, "{}", indented_terminator)?; + writeln!(w, "{indented_terminator}")?; } write_extra(tcx, w, |visitor| { @@ -402,7 +397,7 @@ where extra_data(PassWhere::AfterLocation(current_location), w)?; extra_data(PassWhere::AfterTerminator(block), w)?; - writeln!(w, "{}}}", INDENT) + writeln!(w, "{INDENT}}}") } /// After we print the main statement, we sometimes dump extra @@ -457,27 +452,27 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> { self.tcx.sess.source_map().span_to_embeddable_string(*span) )); if let Some(user_ty) = user_ty { - self.push(&format!("+ user_ty: {:?}", user_ty)); + self.push(&format!("+ user_ty: {user_ty:?}")); } // FIXME: this is a poor version of `pretty_print_const_value`. let fmt_val = |val: &ConstValue<'tcx>| match val { ConstValue::ZeroSized => "<ZST>".to_string(), - ConstValue::Scalar(s) => format!("Scalar({:?})", s), + ConstValue::Scalar(s) => format!("Scalar({s:?})"), ConstValue::Slice { .. } => "Slice(..)".to_string(), ConstValue::ByRef { .. } => "ByRef(..)".to_string(), }; let fmt_valtree = |valtree: &ty::ValTree<'tcx>| match valtree { - ty::ValTree::Leaf(leaf) => format!("ValTree::Leaf({:?})", leaf), + ty::ValTree::Leaf(leaf) => format!("ValTree::Leaf({leaf:?})"), ty::ValTree::Branch(_) => "ValTree::Branch(..)".to_string(), }; let val = match literal { ConstantKind::Ty(ct) => match ct.kind() { - ty::ConstKind::Param(p) => format!("Param({})", p), + ty::ConstKind::Param(p) => format!("Param({p})"), ty::ConstKind::Unevaluated(uv) => { - format!("Unevaluated({}, {:?})", self.tcx.def_path_str(uv.def), uv.substs,) + format!("Unevaluated({}, {:?})", self.tcx.def_path_str(uv.def), uv.args,) } ty::ConstKind::Value(val) => format!("Value({})", fmt_valtree(&val)), ty::ConstKind::Error(_) => "Error".to_string(), @@ -491,7 +486,7 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> { format!( "Unevaluated({}, {:?}, {:?})", self.tcx.def_path_str(uv.def), - uv.substs, + uv.args, uv.promoted, ) } @@ -512,22 +507,22 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> { self.super_rvalue(rvalue, location); if let Rvalue::Aggregate(kind, _) = rvalue { match **kind { - AggregateKind::Closure(def_id, substs) => { + AggregateKind::Closure(def_id, args) => { self.push("closure"); - self.push(&format!("+ def_id: {:?}", def_id)); - self.push(&format!("+ substs: {:#?}", substs)); + self.push(&format!("+ def_id: {def_id:?}")); + self.push(&format!("+ args: {args:#?}")); } - AggregateKind::Generator(def_id, substs, movability) => { + AggregateKind::Generator(def_id, args, movability) => { self.push("generator"); - self.push(&format!("+ def_id: {:?}", def_id)); - self.push(&format!("+ substs: {:#?}", substs)); - self.push(&format!("+ movability: {:?}", movability)); + self.push(&format!("+ def_id: {def_id:?}")); + self.push(&format!("+ args: {args:#?}")); + self.push(&format!("+ movability: {movability:?}")); } AggregateKind::Adt(_, _, _, Some(user_ty), _) => { self.push("adt"); - self.push(&format!("+ user_ty: {:?}", user_ty)); + self.push(&format!("+ user_ty: {user_ty:?}")); } _ => {} @@ -560,13 +555,8 @@ fn write_scope_tree( } let indented_debug_info = format!( - "{0:1$}debug {2} => {3:&<4$}{5:?};", - INDENT, - indent, - var_debug_info.name, - "", - var_debug_info.references as usize, - var_debug_info.value, + "{0:1$}debug {2} => {3:?};", + INDENT, indent, var_debug_info.name, var_debug_info.value, ); if tcx.sess.opts.unstable_opts.mir_include_spans { @@ -578,7 +568,7 @@ fn write_scope_tree( comment(tcx, var_debug_info.source_info), )?; } else { - writeln!(w, "{}", indented_debug_info)?; + writeln!(w, "{indented_debug_info}")?; } } @@ -600,7 +590,7 @@ fn write_scope_tree( format!("{0:1$}let {2}{3:?}: {4:?}", INDENT, indent, mut_str, local, local_decl.ty); if let Some(user_ty) = &local_decl.user_ty { for user_ty in user_ty.projections() { - write!(indented_decl, " as {:?}", user_ty).unwrap(); + write!(indented_decl, " as {user_ty:?}").unwrap(); } } indented_decl.push(';'); @@ -617,7 +607,7 @@ fn write_scope_tree( comment(tcx, local_decl.source_info), )?; } else { - writeln!(w, "{}", indented_decl,)?; + writeln!(w, "{indented_decl}",)?; } } @@ -654,10 +644,10 @@ fn write_scope_tree( tcx.sess.source_map().span_to_embeddable_string(span), )?; } else { - writeln!(w, "{}", indented_header)?; + writeln!(w, "{indented_header}")?; } } else { - writeln!(w, "{}", indented_header)?; + writeln!(w, "{indented_header}")?; } write_scope_tree(tcx, body, scope_tree, w, child, depth + 1)?; @@ -844,7 +834,7 @@ fn write_allocation_endline(w: &mut dyn std::fmt::Write, ascii: &str) -> std::fm for _ in 0..(BYTES_PER_LINE - ascii.chars().count()) { write!(w, " ")?; } - writeln!(w, " │ {}", ascii) + writeln!(w, " │ {ascii}") } /// Number of bytes to print per allocation hex dump line. @@ -880,7 +870,7 @@ pub fn write_allocation_bytes<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>( if num_lines > 0 { write!(w, "{}0x{:02$x} │ ", prefix, 0, pos_width)?; } else { - write!(w, "{}", prefix)?; + write!(w, "{prefix}")?; } let mut i = Size::ZERO; @@ -913,10 +903,10 @@ pub fn write_allocation_bytes<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>( let offset = Size::from_bytes(offset); let provenance_width = |bytes| bytes * 3; let ptr = Pointer::new(prov, offset); - let mut target = format!("{:?}", ptr); + let mut target = format!("{ptr:?}"); if target.len() > provenance_width(ptr_size.bytes_usize() - 1) { // This is too long, try to save some space. - target = format!("{:#?}", ptr); + target = format!("{ptr:#?}"); } if ((i - line_start) + ptr_size).bytes_usize() > BYTES_PER_LINE { // This branch handles the situation where a provenance starts in the current line @@ -935,10 +925,10 @@ pub fn write_allocation_bytes<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>( line_start = write_allocation_newline(w, line_start, &ascii, pos_width, prefix)?; ascii.clear(); - write!(w, "{0:─^1$}╼", target, overflow_width)?; + write!(w, "{target:─^overflow_width$}╼")?; } else { oversized_ptr(&mut target, remainder_width); - write!(w, "╾{0:─^1$}", target, remainder_width)?; + write!(w, "╾{target:─^remainder_width$}")?; line_start = write_allocation_newline(w, line_start, &ascii, pos_width, prefix)?; write!(w, "{0:─^1$}╼", "", overflow_width)?; @@ -955,7 +945,7 @@ pub fn write_allocation_bytes<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>( let provenance_width = provenance_width(ptr_size.bytes_usize() - 1); oversized_ptr(&mut target, provenance_width); ascii.push('╾'); - write!(w, "╾{0:─^1$}╼", target, provenance_width)?; + write!(w, "╾{target:─^provenance_width$}╼")?; for _ in 0..ptr_size.bytes() - 2 { ascii.push('─'); } @@ -972,7 +962,7 @@ pub fn write_allocation_bytes<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>( // Format is similar to "oversized" above. let j = i.bytes_usize(); let c = alloc.inspect_with_uninit_and_ptr_outside_interpreter(j..j + 1)[0]; - write!(w, "╾{:02x}{:#?} (1 ptr byte)╼", c, prov)?; + write!(w, "╾{c:02x}{prov:#?} (1 ptr byte)╼")?; i += Size::from_bytes(1); } else if alloc .init_mask() @@ -984,7 +974,7 @@ pub fn write_allocation_bytes<'tcx, Prov: Provenance, Extra, Bytes: AllocBytes>( // Checked definedness (and thus range) and provenance. This access also doesn't // influence interpreter execution but is only for debugging. let c = alloc.inspect_with_uninit_and_ptr_outside_interpreter(j..j + 1)[0]; - write!(w, "{:02x}", c)?; + write!(w, "{c:02x}")?; if c.is_ascii_control() || c >= 0x80 { ascii.push('.'); } else { @@ -1018,7 +1008,7 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn Write) -> io::Res _ => tcx.is_closure(def_id), }; match (kind, body.source.promoted) { - (_, Some(i)) => write!(w, "{:?} in ", i)?, + (_, Some(i)) => write!(w, "{i:?} in ")?, (DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?, (DefKind::Static(hir::Mutability::Not), _) => write!(w, "static ")?, (DefKind::Static(hir::Mutability::Mut), _) => write!(w, "static mut ")?, @@ -1051,7 +1041,7 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn Write) -> io::Res if let Some(yield_ty) = body.yield_ty() { writeln!(w)?; - writeln!(w, "yields {}", yield_ty)?; + writeln!(w, "yields {yield_ty}")?; } write!(w, " ")?; diff --git a/compiler/rustc_middle/src/mir/query.rs b/compiler/rustc_middle/src/mir/query.rs index 613b132ff..71bec49af 100644 --- a/compiler/rustc_middle/src/mir/query.rs +++ b/compiler/rustc_middle/src/mir/query.rs @@ -194,11 +194,11 @@ impl Debug for GeneratorLayout<'_> { } impl Debug for GenVariantPrinter { fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result { - let variant_name = ty::GeneratorSubsts::variant_name(self.0); + let variant_name = ty::GeneratorArgs::variant_name(self.0); if fmt.alternate() { write!(fmt, "{:9}({:?})", variant_name, self.0) } else { - write!(fmt, "{}", variant_name) + write!(fmt, "{variant_name}") } } } @@ -265,10 +265,10 @@ pub struct ConstQualifs { /// `UniversalRegions::closure_mapping`.) Note the free regions in the /// closure's signature and captures are erased. /// -/// Example: If type check produces a closure with the closure substs: +/// Example: If type check produces a closure with the closure args: /// /// ```text -/// ClosureSubsts = [ +/// ClosureArgs = [ /// 'a, // From the parent. /// 'b, /// i8, // the "closure kind" @@ -280,7 +280,7 @@ pub struct ConstQualifs { /// We would "renumber" each free region to a unique vid, as follows: /// /// ```text -/// ClosureSubsts = [ +/// ClosureArgs = [ /// '1, // From the parent. /// '2, /// i8, // the "closure kind" diff --git a/compiler/rustc_middle/src/mir/spanview.rs b/compiler/rustc_middle/src/mir/spanview.rs index 730c55157..20a9e6889 100644 --- a/compiler/rustc_middle/src/mir/spanview.rs +++ b/compiler/rustc_middle/src/mir/spanview.rs @@ -159,10 +159,10 @@ where indent_to_initial_start_col, source_map.span_to_snippet(spanview_span).expect("function should have printable source") ); - writeln!(w, "{}", HEADER)?; - writeln!(w, "<title>{}</title>", title)?; - writeln!(w, "{}", STYLE_SECTION)?; - writeln!(w, "{}", START_BODY)?; + writeln!(w, "{HEADER}")?; + writeln!(w, "<title>{title}</title>")?; + writeln!(w, "{STYLE_SECTION}")?; + writeln!(w, "{START_BODY}")?; write!( w, r#"<div class="code" style="counter-reset: line {}"><span class="line">{}"#, @@ -226,7 +226,7 @@ where write_coverage_gap(tcx, from_pos, end_pos, w)?; } writeln!(w, r#"</span></div>"#)?; - writeln!(w, "{}", FOOTER)?; + writeln!(w, "{FOOTER}")?; Ok(()) } @@ -561,17 +561,16 @@ where } for (i, line) in html_snippet.lines().enumerate() { if i > 0 { - write!(w, "{}", NEW_LINE_SPAN)?; + write!(w, "{NEW_LINE_SPAN}")?; } write!( w, - r#"<span class="code{}" style="--layer: {}"{}>{}</span>"#, - maybe_alt_class, layer, maybe_title_attr, line + r#"<span class="code{maybe_alt_class}" style="--layer: {layer}"{maybe_title_attr}>{line}</span>"# )?; } // Check for and translate trailing newlines, because `str::lines()` ignores them if html_snippet.ends_with('\n') { - write!(w, "{}", NEW_LINE_SPAN)?; + write!(w, "{NEW_LINE_SPAN}")?; } if layer == 1 { write!(w, "</span>")?; diff --git a/compiler/rustc_middle/src/mir/syntax.rs b/compiler/rustc_middle/src/mir/syntax.rs index 7f1d38203..be27bf75d 100644 --- a/compiler/rustc_middle/src/mir/syntax.rs +++ b/compiler/rustc_middle/src/mir/syntax.rs @@ -8,7 +8,7 @@ use super::{BasicBlock, Constant, Local, SwitchTargets, UserTypeProjection}; use crate::mir::coverage::{CodeRegion, CoverageKind}; use crate::traits::Reveal; use crate::ty::adjustment::PointerCoercion; -use crate::ty::subst::SubstsRef; +use crate::ty::GenericArgsRef; use crate::ty::{self, List, Ty}; use crate::ty::{Region, UserTypeAnnotationIndex}; @@ -1050,10 +1050,6 @@ pub type PlaceElem<'tcx> = ProjectionElem<Local, Ty<'tcx>>; /// there may be other effects: if the type has a validity constraint loading the place might be UB /// if the validity constraint is not met. /// -/// **Needs clarification:** Ralf proposes that loading a place not have side-effects. -/// This is what is implemented in miri today. Are these the semantics we want for MIR? Is this -/// something we can even decide without knowing more about Rust's memory model? -/// /// **Needs clarification:** Is loading a place that has its variant index set well-formed? Miri /// currently implements it, but it seems like this may be something to check against in the /// validator. @@ -1071,6 +1067,16 @@ pub enum Operand<'tcx> { /// in [UCG#188]. You should not emit MIR that may attempt a subsequent second load of this /// place without first re-initializing it. /// + /// **Needs clarification:** The operational impact of `Move` is unclear. Currently (both in + /// Miri and codegen) it has no effect at all unless it appears in an argument to `Call`; for + /// `Call` it allows the argument to be passed to the callee "in-place", i.e. the callee might + /// just get a reference to this place instead of a full copy. Miri implements this with a + /// combination of aliasing model "protectors" and putting `uninit` into the place. Ralf + /// proposes that we don't want these semantics for `Move` in regular assignments, because + /// loading a place should not have side-effects, and the aliasing model "protectors" are + /// inherently tied to a function call. Are these the semantics we want for MIR? Is this + /// something we can even decide without knowing more about Rust's memory model? + /// /// [UCG#188]: https://github.com/rust-lang/unsafe-code-guidelines/issues/188 Move(Place<'tcx>), @@ -1262,10 +1268,10 @@ pub enum AggregateKind<'tcx> { /// active field number and is present only for union expressions /// -- e.g., for a union expression `SomeUnion { c: .. }`, the /// active field index would identity the field `c` - Adt(DefId, VariantIdx, SubstsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<FieldIdx>), + Adt(DefId, VariantIdx, GenericArgsRef<'tcx>, Option<UserTypeAnnotationIndex>, Option<FieldIdx>), - Closure(DefId, SubstsRef<'tcx>), - Generator(DefId, SubstsRef<'tcx>, hir::Movability), + Closure(DefId, GenericArgsRef<'tcx>), + Generator(DefId, GenericArgsRef<'tcx>, hir::Movability), } #[derive(Clone, Debug, PartialEq, Eq, TyEncodable, TyDecodable, Hash, HashStable)] diff --git a/compiler/rustc_middle/src/mir/tcx.rs b/compiler/rustc_middle/src/mir/tcx.rs index 8618a5315..f79697936 100644 --- a/compiler/rustc_middle/src/mir/tcx.rs +++ b/compiler/rustc_middle/src/mir/tcx.rs @@ -35,7 +35,7 @@ impl<'tcx> PlaceTy<'tcx> { #[instrument(level = "debug", skip(tcx), ret)] pub fn field_ty(self, tcx: TyCtxt<'tcx>, f: FieldIdx) -> Ty<'tcx> { match self.ty.kind() { - ty::Adt(adt_def, substs) => { + ty::Adt(adt_def, args) => { let variant_def = match self.variant_index { None => adt_def.non_enum_variant(), Some(variant_index) => { @@ -44,7 +44,7 @@ impl<'tcx> PlaceTy<'tcx> { } }; let field_def = &variant_def.fields[f]; - field_def.ty(tcx, substs) + field_def.ty(tcx, args) } ty::Tuple(tys) => tys[f.index()], _ => bug!("extracting field of non-tuple non-adt: {:?}", self), @@ -198,10 +198,10 @@ impl<'tcx> Rvalue<'tcx> { AggregateKind::Tuple => { Ty::new_tup_from_iter(tcx, ops.iter().map(|op| op.ty(local_decls, tcx))) } - AggregateKind::Adt(did, _, substs, _, _) => tcx.type_of(did).subst(tcx, substs), - AggregateKind::Closure(did, substs) => Ty::new_closure(tcx, did, substs), - AggregateKind::Generator(did, substs, movability) => { - Ty::new_generator(tcx, did, substs, movability) + AggregateKind::Adt(did, _, args, _, _) => tcx.type_of(did).instantiate(tcx, args), + AggregateKind::Closure(did, args) => Ty::new_closure(tcx, did, args), + AggregateKind::Generator(did, args, movability) => { + Ty::new_generator(tcx, did, args, movability) } }, Rvalue::ShallowInitBox(_, ty) => Ty::new_box(tcx, ty), diff --git a/compiler/rustc_middle/src/mir/terminator.rs b/compiler/rustc_middle/src/mir/terminator.rs index 1b9c1438f..1f878d23b 100644 --- a/compiler/rustc_middle/src/mir/terminator.rs +++ b/compiler/rustc_middle/src/mir/terminator.rs @@ -10,6 +10,7 @@ use std::iter; use std::slice; pub use super::query::*; +use super::*; #[derive(Debug, Clone, TyEncodable, TyDecodable, Hash, HashStable, PartialEq)] pub struct SwitchTargets { @@ -280,7 +281,7 @@ impl<'tcx> Debug for TerminatorKind<'tcx> { match (successor_count, unwind) { (0, None) => Ok(()), - (0, Some(unwind)) => write!(fmt, " -> {}", unwind), + (0, Some(unwind)) => write!(fmt, " -> {unwind}"), (1, None) => write!(fmt, " -> {:?}", self.successors().next().unwrap()), _ => { write!(fmt, " -> [")?; @@ -307,22 +308,22 @@ impl<'tcx> TerminatorKind<'tcx> { use self::TerminatorKind::*; match self { Goto { .. } => write!(fmt, "goto"), - SwitchInt { discr, .. } => write!(fmt, "switchInt({:?})", discr), + SwitchInt { discr, .. } => write!(fmt, "switchInt({discr:?})"), Return => write!(fmt, "return"), GeneratorDrop => write!(fmt, "generator_drop"), Resume => write!(fmt, "resume"), Terminate => write!(fmt, "abort"), - Yield { value, resume_arg, .. } => write!(fmt, "{:?} = yield({:?})", resume_arg, value), + Yield { value, resume_arg, .. } => write!(fmt, "{resume_arg:?} = yield({value:?})"), Unreachable => write!(fmt, "unreachable"), - Drop { place, .. } => write!(fmt, "drop({:?})", place), + Drop { place, .. } => write!(fmt, "drop({place:?})"), Call { func, args, destination, .. } => { - write!(fmt, "{:?} = ", destination)?; - write!(fmt, "{:?}(", func)?; + write!(fmt, "{destination:?} = ")?; + write!(fmt, "{func:?}(")?; for (index, arg) in args.iter().enumerate() { if index > 0 { write!(fmt, ", ")?; } - write!(fmt, "{:?}", arg)?; + write!(fmt, "{arg:?}")?; } write!(fmt, ")") } @@ -331,7 +332,7 @@ impl<'tcx> TerminatorKind<'tcx> { if !expected { write!(fmt, "!")?; } - write!(fmt, "{:?}, ", cond)?; + write!(fmt, "{cond:?}, ")?; msg.fmt_assert_args(fmt)?; write!(fmt, ")") } @@ -344,7 +345,7 @@ impl<'tcx> TerminatorKind<'tcx> { let print_late = |&late| if late { "late" } else { "" }; match op { InlineAsmOperand::In { reg, value } => { - write!(fmt, "in({}) {:?}", reg, value)?; + write!(fmt, "in({reg}) {value:?}")?; } InlineAsmOperand::Out { reg, late, place: Some(place) } => { write!(fmt, "{}out({}) {:?}", print_late(late), reg, place)?; @@ -371,17 +372,17 @@ impl<'tcx> TerminatorKind<'tcx> { write!(fmt, "in{}out({}) {:?} => _", print_late(late), reg, in_value)?; } InlineAsmOperand::Const { value } => { - write!(fmt, "const {:?}", value)?; + write!(fmt, "const {value:?}")?; } InlineAsmOperand::SymFn { value } => { - write!(fmt, "sym_fn {:?}", value)?; + write!(fmt, "sym_fn {value:?}")?; } InlineAsmOperand::SymStatic { def_id } => { - write!(fmt, "sym_static {:?}", def_id)?; + write!(fmt, "sym_static {def_id:?}")?; } } } - write!(fmt, ", options({:?}))", options) + write!(fmt, ", options({options:?}))") } } } @@ -430,3 +431,108 @@ impl<'tcx> TerminatorKind<'tcx> { } } } + +#[derive(Copy, Clone, Debug)] +pub enum TerminatorEdges<'mir, 'tcx> { + /// For terminators that have no successor, like `return`. + None, + /// For terminators that a single successor, like `goto`, and `assert` without cleanup block. + Single(BasicBlock), + /// For terminators that two successors, `assert` with cleanup block and `falseEdge`. + Double(BasicBlock, BasicBlock), + /// Special action for `Yield`, `Call` and `InlineAsm` terminators. + AssignOnReturn { + return_: Option<BasicBlock>, + unwind: UnwindAction, + place: CallReturnPlaces<'mir, 'tcx>, + }, + /// Special edge for `SwitchInt`. + SwitchInt { targets: &'mir SwitchTargets, discr: &'mir Operand<'tcx> }, +} + +/// List of places that are written to after a successful (non-unwind) return +/// from a `Call`, `Yield` or `InlineAsm`. +#[derive(Copy, Clone, Debug)] +pub enum CallReturnPlaces<'a, 'tcx> { + Call(Place<'tcx>), + Yield(Place<'tcx>), + InlineAsm(&'a [InlineAsmOperand<'tcx>]), +} + +impl<'tcx> CallReturnPlaces<'_, 'tcx> { + pub fn for_each(&self, mut f: impl FnMut(Place<'tcx>)) { + match *self { + Self::Call(place) | Self::Yield(place) => f(place), + Self::InlineAsm(operands) => { + for op in operands { + match *op { + InlineAsmOperand::Out { place: Some(place), .. } + | InlineAsmOperand::InOut { out_place: Some(place), .. } => f(place), + _ => {} + } + } + } + } + } +} + +impl<'tcx> Terminator<'tcx> { + pub fn edges(&self) -> TerminatorEdges<'_, 'tcx> { + self.kind.edges() + } +} + +impl<'tcx> TerminatorKind<'tcx> { + pub fn edges(&self) -> TerminatorEdges<'_, 'tcx> { + use TerminatorKind::*; + match *self { + Return | Resume | Terminate | GeneratorDrop | Unreachable => TerminatorEdges::None, + + Goto { target } => TerminatorEdges::Single(target), + + Assert { target, unwind, expected: _, msg: _, cond: _ } + | Drop { target, unwind, place: _, replace: _ } + | FalseUnwind { real_target: target, unwind } => match unwind { + UnwindAction::Cleanup(unwind) => TerminatorEdges::Double(target, unwind), + UnwindAction::Continue | UnwindAction::Terminate | UnwindAction::Unreachable => { + TerminatorEdges::Single(target) + } + }, + + FalseEdge { real_target, imaginary_target } => { + TerminatorEdges::Double(real_target, imaginary_target) + } + + Yield { resume: target, drop, resume_arg, value: _ } => { + TerminatorEdges::AssignOnReturn { + return_: Some(target), + unwind: drop.map_or(UnwindAction::Terminate, UnwindAction::Cleanup), + place: CallReturnPlaces::Yield(resume_arg), + } + } + + Call { unwind, destination, target, func: _, args: _, fn_span: _, call_source: _ } => { + TerminatorEdges::AssignOnReturn { + return_: target, + unwind, + place: CallReturnPlaces::Call(destination), + } + } + + InlineAsm { + template: _, + ref operands, + options: _, + line_spans: _, + destination, + unwind, + } => TerminatorEdges::AssignOnReturn { + return_: destination, + unwind, + place: CallReturnPlaces::InlineAsm(operands), + }, + + SwitchInt { ref targets, ref discr } => TerminatorEdges::SwitchInt { targets, discr }, + } + } +} diff --git a/compiler/rustc_middle/src/mir/visit.rs b/compiler/rustc_middle/src/mir/visit.rs index 205dc9ec7..069b38591 100644 --- a/compiler/rustc_middle/src/mir/visit.rs +++ b/compiler/rustc_middle/src/mir/visit.rs @@ -63,7 +63,7 @@ //! `is_cleanup` above. use crate::mir::*; -use crate::ty::subst::SubstsRef; +use crate::ty::GenericArgsRef; use crate::ty::{self, CanonicalUserTypeAnnotation, Ty}; use rustc_span::Span; @@ -245,12 +245,12 @@ macro_rules! make_mir_visitor { self.super_region(region); } - fn visit_substs( + fn visit_args( &mut self, - substs: & $($mutability)? SubstsRef<'tcx>, + args: & $($mutability)? GenericArgsRef<'tcx>, _: Location, ) { - self.super_substs(substs); + self.super_args(args); } fn visit_local_decl( @@ -335,7 +335,7 @@ macro_rules! make_mir_visitor { self.visit_span($(& $mutability)? *callsite_span); - let ty::Instance { def: callee_def, substs: callee_substs } = callee; + let ty::Instance { def: callee_def, args: callee_args } = callee; match callee_def { ty::InstanceDef::Item(_def_id) => {} @@ -355,7 +355,7 @@ macro_rules! make_mir_visitor { self.visit_ty($(& $mutability)? *ty, TyContext::Location(location)); } } - self.visit_substs(callee_substs, location); + self.visit_args(callee_args, location); } if let Some(inlined_parent_scope) = inlined_parent_scope { self.visit_source_scope($(& $mutability)? *inlined_parent_scope); @@ -721,24 +721,24 @@ macro_rules! make_mir_visitor { AggregateKind::Adt( _adt_def, _variant_index, - substs, - _user_substs, + args, + _user_args, _active_field_index ) => { - self.visit_substs(substs, location); + self.visit_args(args, location); } AggregateKind::Closure( _, - closure_substs + closure_args ) => { - self.visit_substs(closure_substs, location); + self.visit_args(closure_args, location); } AggregateKind::Generator( _, - generator_substs, + generator_args, _movability, ) => { - self.visit_substs(generator_substs, location); + self.visit_args(generator_args, location); } } @@ -840,7 +840,6 @@ macro_rules! make_mir_visitor { source_info, value, argument_index: _, - references: _, } = var_debug_info; self.visit_source_info(source_info); @@ -933,7 +932,7 @@ macro_rules! make_mir_visitor { fn super_region(&mut self, _region: $(& $mutability)? ty::Region<'tcx>) { } - fn super_substs(&mut self, _substs: & $($mutability)? SubstsRef<'tcx>) { + fn super_args(&mut self, _args: & $($mutability)? GenericArgsRef<'tcx>) { } // Convenience methods diff --git a/compiler/rustc_middle/src/query/erase.rs b/compiler/rustc_middle/src/query/erase.rs index 2c481745d..348f79ed6 100644 --- a/compiler/rustc_middle/src/query/erase.rs +++ b/compiler/rustc_middle/src/query/erase.rs @@ -235,6 +235,7 @@ trivial! { rustc_hir::def_id::DefId, rustc_hir::def_id::DefIndex, rustc_hir::def_id::LocalDefId, + rustc_hir::def_id::LocalModDefId, rustc_hir::def::DefKind, rustc_hir::Defaultness, rustc_hir::definitions::DefKey, diff --git a/compiler/rustc_middle/src/query/keys.rs b/compiler/rustc_middle/src/query/keys.rs index 28e699cd2..01bdc4c99 100644 --- a/compiler/rustc_middle/src/query/keys.rs +++ b/compiler/rustc_middle/src/query/keys.rs @@ -6,9 +6,9 @@ use crate::mir::interpret::ConstValue; use crate::traits; use crate::ty::fast_reject::SimplifiedType; use crate::ty::layout::{TyAndLayout, ValidityRequirement}; -use crate::ty::subst::{GenericArg, SubstsRef}; use crate::ty::{self, Ty, TyCtxt}; -use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; +use crate::ty::{GenericArg, GenericArgsRef}; +use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalModDefId, ModDefId, LOCAL_CRATE}; use rustc_hir::hir_id::{HirId, OwnerId}; use rustc_query_system::query::{DefaultCacheSelector, SingleCacheSelector, VecCacheSelector}; use rustc_span::symbol::{Ident, Symbol}; @@ -175,6 +175,41 @@ impl AsLocalKey for DefId { } } +impl Key for LocalModDefId { + type CacheSelector = DefaultCacheSelector<Self>; + + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { + tcx.def_span(*self) + } + + #[inline(always)] + fn key_as_def_id(&self) -> Option<DefId> { + Some(self.to_def_id()) + } +} + +impl Key for ModDefId { + type CacheSelector = DefaultCacheSelector<Self>; + + fn default_span(&self, tcx: TyCtxt<'_>) -> Span { + tcx.def_span(*self) + } + + #[inline(always)] + fn key_as_def_id(&self) -> Option<DefId> { + Some(self.to_def_id()) + } +} + +impl AsLocalKey for ModDefId { + type LocalKey = LocalModDefId; + + #[inline(always)] + fn as_local_key(&self) -> Option<Self::LocalKey> { + self.as_local() + } +} + impl Key for SimplifiedType { type CacheSelector = DefaultCacheSelector<Self>; @@ -286,7 +321,7 @@ impl Key for (DefId, SimplifiedType) { } } -impl<'tcx> Key for SubstsRef<'tcx> { +impl<'tcx> Key for GenericArgsRef<'tcx> { type CacheSelector = DefaultCacheSelector<Self>; fn default_span(&self, _: TyCtxt<'_>) -> Span { @@ -294,7 +329,7 @@ impl<'tcx> Key for SubstsRef<'tcx> { } } -impl<'tcx> Key for (DefId, SubstsRef<'tcx>) { +impl<'tcx> Key for (DefId, GenericArgsRef<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; fn default_span(&self, tcx: TyCtxt<'_>) -> Span { @@ -310,7 +345,7 @@ impl<'tcx> Key for (ty::UnevaluatedConst<'tcx>, ty::UnevaluatedConst<'tcx>) { } } -impl<'tcx> Key for (LocalDefId, DefId, SubstsRef<'tcx>) { +impl<'tcx> Key for (LocalDefId, DefId, GenericArgsRef<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; fn default_span(&self, tcx: TyCtxt<'_>) -> Span { @@ -487,7 +522,7 @@ impl Key for (Symbol, u32, u32) { } } -impl<'tcx> Key for (DefId, Ty<'tcx>, SubstsRef<'tcx>, ty::ParamEnv<'tcx>) { +impl<'tcx> Key for (DefId, Ty<'tcx>, GenericArgsRef<'tcx>, ty::ParamEnv<'tcx>) { type CacheSelector = DefaultCacheSelector<Self>; fn default_span(&self, _tcx: TyCtxt<'_>) -> Span { diff --git a/compiler/rustc_middle/src/query/mod.rs b/compiler/rustc_middle/src/query/mod.rs index a059590e6..94ae0dcb5 100644 --- a/compiler/rustc_middle/src/query/mod.rs +++ b/compiler/rustc_middle/src/query/mod.rs @@ -44,7 +44,6 @@ use crate::traits::{ }; use crate::ty::fast_reject::SimplifiedType; use crate::ty::layout::ValidityRequirement; -use crate::ty::subst::{GenericArg, SubstsRef}; use crate::ty::util::AlwaysRequiresDrop; use crate::ty::GeneratorDiagnosticData; use crate::ty::TyCtxtFeed; @@ -52,6 +51,7 @@ use crate::ty::{ self, print::describe_as_module, CrateInherentImpls, ParamEnvAnd, Ty, TyCtxt, UnusedGenericParams, }; +use crate::ty::{GenericArg, GenericArgsRef}; use rustc_arena::TypedArena; use rustc_ast as ast; use rustc_ast::expand::{allocator::AllocatorKind, StrippedCfgItem}; @@ -67,7 +67,7 @@ use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; use rustc_hir::def::{DefKind, DocLinkResMap}; use rustc_hir::def_id::{ - CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LocalDefIdMap, LocalDefIdSet, + CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LocalDefIdMap, LocalDefIdSet, LocalModDefId, }; use rustc_hir::lang_items::{LangItem, LanguageItems}; use rustc_hir::{Crate, ItemLocalId, TraitCandidate}; @@ -167,7 +167,7 @@ rustc_queries! { /// /// This can be conveniently accessed by `tcx.hir().visit_item_likes_in_module`. /// Avoid calling this query directly. - query hir_module_items(key: LocalDefId) -> &'tcx rustc_middle::hir::ModuleItems { + query hir_module_items(key: LocalModDefId) -> &'tcx rustc_middle::hir::ModuleItems { arena_cache desc { |tcx| "getting HIR module items in `{}`", tcx.def_path_str(key) } cache_on_disk_if { true } @@ -231,7 +231,7 @@ rustc_queries! { action = { use rustc_hir::def::DefKind; match tcx.def_kind(key) { - DefKind::TyAlias => "expanding type alias", + DefKind::TyAlias { .. } => "expanding type alias", DefKind::TraitAlias => "expanding trait alias", _ => "computing type of", } @@ -388,7 +388,6 @@ rustc_queries! { } query shallow_lint_levels_on(key: hir::OwnerId) -> &'tcx rustc_middle::lint::ShallowLintLevelMap { - eval_always // fetches `resolutions` arena_cache desc { |tcx| "looking up lint levels for `{}`", tcx.def_path_str(key) } } @@ -398,11 +397,6 @@ rustc_queries! { desc { "computing `#[expect]`ed lints in this crate" } } - query parent_module_from_def_id(key: LocalDefId) -> LocalDefId { - eval_always - desc { |tcx| "getting the parent module of `{}`", tcx.def_path_str(key) } - } - query expn_that_defined(key: DefId) -> rustc_span::ExpnId { desc { |tcx| "getting the expansion that defined `{}`", tcx.def_path_str(key) } separate_provide_extern @@ -706,7 +700,7 @@ rustc_queries! { separate_provide_extern } - query adt_sized_constraint(key: DefId) -> &'tcx [Ty<'tcx>] { + query adt_sized_constraint(key: DefId) -> ty::EarlyBinder<&'tcx ty::List<Ty<'tcx>>> { desc { |tcx| "computing `Sized` constraints for `{}`", tcx.def_path_str(key) } } @@ -749,7 +743,7 @@ rustc_queries! { separate_provide_extern } - /// Gets a map with the variance of every item; use `item_variance` instead. + /// Gets a map with the variance of every item; use `variances_of` instead. query crate_variances(_: ()) -> &'tcx ty::CrateVariancesMap<'tcx> { arena_cache desc { "computing the variances for items in this crate" } @@ -885,6 +879,13 @@ rustc_queries! { desc { |tcx| "computing the implied bounds of `{}`", tcx.def_path_str(key) } } + /// We need to store the assumed_wf_types for an RPITIT so that impls of foreign + /// traits with return-position impl trait in traits can inherit the right wf types. + query assumed_wf_types_for_rpitit(key: DefId) -> &'tcx [(Ty<'tcx>, Span)] { + desc { |tcx| "computing the implied bounds of `{}`", tcx.def_path_str(key) } + separate_provide_extern + } + /// Computes the signature of the function. query fn_sig(key: DefId) -> ty::EarlyBinder<ty::PolyFnSig<'tcx>> { desc { |tcx| "computing function signature of `{}`", tcx.def_path_str(key) } @@ -894,40 +895,44 @@ rustc_queries! { } /// Performs lint checking for the module. - query lint_mod(key: LocalDefId) -> () { + query lint_mod(key: LocalModDefId) -> () { desc { |tcx| "linting {}", describe_as_module(key, tcx) } } + query check_unused_traits(_: ()) -> () { + desc { "checking unused trait imports in crate" } + } + /// Checks the attributes in the module. - query check_mod_attrs(key: LocalDefId) -> () { + query check_mod_attrs(key: LocalModDefId) -> () { desc { |tcx| "checking attributes in {}", describe_as_module(key, tcx) } } /// Checks for uses of unstable APIs in the module. - query check_mod_unstable_api_usage(key: LocalDefId) -> () { + query check_mod_unstable_api_usage(key: LocalModDefId) -> () { desc { |tcx| "checking for unstable API usage in {}", describe_as_module(key, tcx) } } /// Checks the const bodies in the module for illegal operations (e.g. `if` or `loop`). - query check_mod_const_bodies(key: LocalDefId) -> () { + query check_mod_const_bodies(key: LocalModDefId) -> () { desc { |tcx| "checking consts in {}", describe_as_module(key, tcx) } } /// Checks the loops in the module. - query check_mod_loops(key: LocalDefId) -> () { + query check_mod_loops(key: LocalModDefId) -> () { desc { |tcx| "checking loops in {}", describe_as_module(key, tcx) } } - query check_mod_naked_functions(key: LocalDefId) -> () { + query check_mod_naked_functions(key: LocalModDefId) -> () { desc { |tcx| "checking naked functions in {}", describe_as_module(key, tcx) } } - query check_mod_item_types(key: LocalDefId) -> () { + query check_mod_item_types(key: LocalModDefId) -> () { desc { |tcx| "checking item types in {}", describe_as_module(key, tcx) } } - query check_mod_privacy(key: LocalDefId) -> () { - desc { |tcx| "checking privacy in {}", describe_as_module(key, tcx) } + query check_mod_privacy(key: LocalModDefId) -> () { + desc { |tcx| "checking privacy in {}", describe_as_module(key.to_local_def_id(), tcx) } } query check_liveness(key: LocalDefId) { @@ -946,19 +951,19 @@ rustc_queries! { desc { "finding live symbols in crate" } } - query check_mod_deathness(key: LocalDefId) -> () { + query check_mod_deathness(key: LocalModDefId) -> () { desc { |tcx| "checking deathness of variables in {}", describe_as_module(key, tcx) } } - query check_mod_impl_wf(key: LocalDefId) -> () { + query check_mod_impl_wf(key: LocalModDefId) -> () { desc { |tcx| "checking that impls are well-formed in {}", describe_as_module(key, tcx) } } - query check_mod_type_wf(key: LocalDefId) -> () { + query check_mod_type_wf(key: LocalModDefId) -> () { desc { |tcx| "checking that types are well-formed in {}", describe_as_module(key, tcx) } } - query collect_mod_item_types(key: LocalDefId) -> () { + query collect_mod_item_types(key: LocalModDefId) -> () { desc { |tcx| "collecting item types in {}", describe_as_module(key, tcx) } } @@ -1032,7 +1037,7 @@ rustc_queries! { } /// Obtain all the calls into other local functions - query mir_inliner_callees(key: ty::InstanceDef<'tcx>) -> &'tcx [(DefId, SubstsRef<'tcx>)] { + query mir_inliner_callees(key: ty::InstanceDef<'tcx>) -> &'tcx [(DefId, GenericArgsRef<'tcx>)] { fatal_cycle desc { |tcx| "computing all local function calls in `{}`", @@ -1273,7 +1278,7 @@ rustc_queries! { query vtable_allocation(key: (Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>)) -> mir::interpret::AllocId { desc { |tcx| "vtable const allocation for <{} as {}>", key.0, - key.1.map(|trait_ref| format!("{}", trait_ref)).unwrap_or("_".to_owned()) + key.1.map(|trait_ref| format!("{trait_ref}")).unwrap_or("_".to_owned()) } } @@ -1537,7 +1542,7 @@ rustc_queries! { /// added or removed in any upstream crate. Instead use the narrower /// `upstream_monomorphizations_for`, `upstream_drop_glue_for`, or, even /// better, `Instance::upstream_monomorphization()`. - query upstream_monomorphizations(_: ()) -> &'tcx DefIdMap<FxHashMap<SubstsRef<'tcx>, CrateNum>> { + query upstream_monomorphizations(_: ()) -> &'tcx DefIdMap<FxHashMap<GenericArgsRef<'tcx>, CrateNum>> { arena_cache desc { "collecting available upstream monomorphizations" } } @@ -1550,7 +1555,7 @@ rustc_queries! { /// You likely want to call `Instance::upstream_monomorphization()` /// instead of invoking this query directly. query upstream_monomorphizations_for(def_id: DefId) - -> Option<&'tcx FxHashMap<SubstsRef<'tcx>, CrateNum>> + -> Option<&'tcx FxHashMap<GenericArgsRef<'tcx>, CrateNum>> { desc { |tcx| "collecting available upstream monomorphizations for `{}`", @@ -1560,7 +1565,7 @@ rustc_queries! { } /// Returns the upstream crate that exports drop-glue for the given - /// type (`substs` is expected to be a single-item list containing the + /// type (`args` is expected to be a single-item list containing the /// type one wants drop-glue for). /// /// This is a subset of `upstream_monomorphizations_for` in order to @@ -1574,17 +1579,22 @@ rustc_queries! { /// NOTE: This query could easily be extended to also support other /// common functions that have are large set of monomorphizations /// (like `Clone::clone` for example). - query upstream_drop_glue_for(substs: SubstsRef<'tcx>) -> Option<CrateNum> { - desc { "available upstream drop-glue for `{:?}`", substs } + query upstream_drop_glue_for(args: GenericArgsRef<'tcx>) -> Option<CrateNum> { + desc { "available upstream drop-glue for `{:?}`", args } } /// Returns a list of all `extern` blocks of a crate. - query foreign_modules(_: CrateNum) -> &'tcx FxHashMap<DefId, ForeignModule> { + query foreign_modules(_: CrateNum) -> &'tcx FxIndexMap<DefId, ForeignModule> { arena_cache desc { "looking up the foreign modules of a linked crate" } separate_provide_extern } + /// Lint against `extern fn` declarations having incompatible types. + query clashing_extern_declarations(_: ()) { + desc { "checking `extern fn` declarations are compatible" } + } + /// Identifies the entry-point (e.g., the `main` function) for a given /// crate, returning `None` if there is no entry point (such as for library crates). query entry_fn(_: ()) -> Option<(DefId, EntryFnType)> { @@ -2053,16 +2063,16 @@ rustc_queries! { desc { "normalizing `{:?}`", goal.value.value.value } } - query subst_and_check_impossible_predicates(key: (DefId, SubstsRef<'tcx>)) -> bool { + query subst_and_check_impossible_predicates(key: (DefId, GenericArgsRef<'tcx>)) -> bool { desc { |tcx| "checking impossible substituted predicates: `{}`", tcx.def_path_str(key.0) } } - query is_impossible_method(key: (DefId, DefId)) -> bool { + query is_impossible_associated_item(key: (DefId, DefId)) -> bool { desc { |tcx| - "checking if `{}` is impossible to call within `{}`", + "checking if `{}` is impossible to reference within `{}`", tcx.def_path_str(key.1), tcx.def_path_str(key.0), } @@ -2080,23 +2090,11 @@ rustc_queries! { desc { "looking up supported target features" } } - /// Get an estimate of the size of an InstanceDef based on its MIR for CGU partitioning. - query instance_def_size_estimate(def: ty::InstanceDef<'tcx>) - -> usize { - desc { |tcx| "estimating size for `{}`", tcx.def_path_str(def.def_id()) } - } - query features_query(_: ()) -> &'tcx rustc_feature::Features { feedable desc { "looking up enabled feature gates" } } - query metadata_loader((): ()) -> &'tcx Steal<Box<rustc_session::cstore::MetadataLoaderDyn>> { - feedable - no_hash - desc { "raw operations for metadata file access" } - } - query crate_for_resolver((): ()) -> &'tcx Steal<(rustc_ast::Crate, rustc_ast::AttrVec)> { feedable no_hash @@ -2104,16 +2102,16 @@ rustc_queries! { } /// Attempt to resolve the given `DefId` to an `Instance`, for the - /// given generics args (`SubstsRef`), returning one of: + /// given generics args (`GenericArgsRef`), returning one of: /// * `Ok(Some(instance))` on success - /// * `Ok(None)` when the `SubstsRef` are still too generic, + /// * `Ok(None)` when the `GenericArgsRef` are still too generic, /// and therefore don't allow finding the final `Instance` /// * `Err(ErrorGuaranteed)` when the `Instance` resolution process /// couldn't complete due to errors elsewhere - this is distinct /// from `Ok(None)` to avoid misleading diagnostics when an error /// has already been/will be emitted, for the original cause query resolve_instance( - key: ty::ParamEnvAnd<'tcx, (DefId, SubstsRef<'tcx>)> + key: ty::ParamEnvAnd<'tcx, (DefId, GenericArgsRef<'tcx>)> ) -> Result<Option<ty::Instance<'tcx>>, ErrorGuaranteed> { desc { "resolving instance `{}`", ty::Instance::new(key.value.0, key.value.1) } } diff --git a/compiler/rustc_middle/src/query/on_disk_cache.rs b/compiler/rustc_middle/src/query/on_disk_cache.rs index 8751d3b78..995b2140f 100644 --- a/compiler/rustc_middle/src/query/on_disk_cache.rs +++ b/compiler/rustc_middle/src/query/on_disk_cache.rs @@ -104,7 +104,9 @@ struct Footer { query_result_index: EncodedDepNodeIndex, side_effects_index: EncodedDepNodeIndex, // The location of all allocations. - interpret_alloc_index: Vec<u32>, + // Most uses only need values up to u32::MAX, but benchmarking indicates that we can use a u64 + // without measurable overhead. This permits larger const allocations without ICEing. + interpret_alloc_index: Vec<u64>, // See `OnDiskCache.syntax_contexts` syntax_contexts: FxHashMap<u32, AbsoluteBytePos>, // See `OnDiskCache.expn_data` @@ -301,7 +303,7 @@ impl<'sess> OnDiskCache<'sess> { interpret_alloc_index.reserve(new_n - n); for idx in n..new_n { let id = encoder.interpret_allocs[idx]; - let pos: u32 = encoder.position().try_into().unwrap(); + let pos: u64 = encoder.position().try_into().unwrap(); interpret_alloc_index.push(pos); interpret::specialized_encode_alloc_id(&mut encoder, tcx, id); } diff --git a/compiler/rustc_middle/src/query/plumbing.rs b/compiler/rustc_middle/src/query/plumbing.rs index 97edfc2fc..a1aac2846 100644 --- a/compiler/rustc_middle/src/query/plumbing.rs +++ b/compiler/rustc_middle/src/query/plumbing.rs @@ -545,6 +545,7 @@ macro_rules! define_feedable { mod sealed { use super::{DefId, LocalDefId, OwnerId}; + use rustc_hir::def_id::{LocalModDefId, ModDefId}; /// An analogue of the `Into` trait that's intended only for query parameters. /// @@ -588,6 +589,27 @@ mod sealed { self.to_def_id() } } + + impl IntoQueryParam<DefId> for ModDefId { + #[inline(always)] + fn into_query_param(self) -> DefId { + self.to_def_id() + } + } + + impl IntoQueryParam<DefId> for LocalModDefId { + #[inline(always)] + fn into_query_param(self) -> DefId { + self.to_def_id() + } + } + + impl IntoQueryParam<LocalDefId> for LocalModDefId { + #[inline(always)] + fn into_query_param(self) -> LocalDefId { + self.into() + } + } } pub use sealed::IntoQueryParam; diff --git a/compiler/rustc_middle/src/thir.rs b/compiler/rustc_middle/src/thir.rs index e9af5070e..ebc1c1190 100644 --- a/compiler/rustc_middle/src/thir.rs +++ b/compiler/rustc_middle/src/thir.rs @@ -19,8 +19,8 @@ use rustc_middle::middle::region; use rustc_middle::mir::interpret::AllocId; use rustc_middle::mir::{self, BinOp, BorrowKind, FakeReadCause, Mutability, UnOp}; use rustc_middle::ty::adjustment::PointerCoercion; -use rustc_middle::ty::subst::SubstsRef; -use rustc_middle::ty::{self, AdtDef, FnSig, List, Ty, UpvarSubsts}; +use rustc_middle::ty::GenericArgsRef; +use rustc_middle::ty::{self, AdtDef, FnSig, List, Ty, UpvarArgs}; use rustc_middle::ty::{CanonicalUserType, CanonicalUserTypeAnnotation}; use rustc_span::def_id::LocalDefId; use rustc_span::{sym, Span, Symbol, DUMMY_SP}; @@ -150,9 +150,9 @@ pub struct AdtExpr<'tcx> { pub adt_def: AdtDef<'tcx>, /// The variant of the ADT. pub variant_index: VariantIdx, - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, - /// Optional user-given substs: for something like `let x = + /// Optional user-given args: for something like `let x = /// Bar::<T> { ... }`. pub user_ty: UserTy<'tcx>, @@ -164,7 +164,7 @@ pub struct AdtExpr<'tcx> { #[derive(Clone, Debug, HashStable)] pub struct ClosureExpr<'tcx> { pub closure_id: LocalDefId, - pub substs: UpvarSubsts<'tcx>, + pub args: UpvarArgs<'tcx>, pub upvars: Box<[ExprId]>, pub movability: Option<hir::Movability>, pub fake_reads: Vec<(ExprId, FakeReadCause, hir::HirId)>, @@ -346,6 +346,7 @@ pub enum ExprKind<'tcx> { /// A `match` expression. Match { scrutinee: ExprId, + scrutinee_hir_id: hir::HirId, arms: Box<[ArmId]>, }, /// A block. @@ -418,7 +419,7 @@ pub enum ExprKind<'tcx> { /// An inline `const` block, e.g. `const {}`. ConstBlock { did: DefId, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, }, /// An array literal constructed from one repeated element, e.g. `[1; 5]`. Repeat { @@ -466,7 +467,7 @@ pub enum ExprKind<'tcx> { /// Associated constants and named constants NamedConst { def_id: DefId, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, user_ty: UserTy<'tcx>, }, ConstParam { @@ -659,7 +660,7 @@ impl<'tcx> Pat<'tcx> { impl<'tcx> IntoDiagnosticArg for Pat<'tcx> { fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { - format!("{}", self).into_diagnostic_arg() + format!("{self}").into_diagnostic_arg() } } @@ -714,7 +715,7 @@ pub enum PatKind<'tcx> { /// multiple variants. Variant { adt_def: AdtDef<'tcx>, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, variant_index: VariantIdx, subpatterns: Vec<FieldPat<'tcx>>, }, @@ -789,7 +790,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> { match self.kind { PatKind::Wild => write!(f, "_"), - PatKind::AscribeUserType { ref subpattern, .. } => write!(f, "{}: _", subpattern), + PatKind::AscribeUserType { ref subpattern, .. } => write!(f, "{subpattern}: _"), PatKind::Binding { mutability, name, mode, ref subpattern, .. } => { let is_mut = match mode { BindingMode::ByValue => mutability == Mutability::Mut, @@ -801,9 +802,9 @@ impl<'tcx> fmt::Display for Pat<'tcx> { if is_mut { write!(f, "mut ")?; } - write!(f, "{}", name)?; + write!(f, "{name}")?; if let Some(ref subpattern) = *subpattern { - write!(f, " @ {}", subpattern)?; + write!(f, " @ {subpattern}")?; } Ok(()) } @@ -833,7 +834,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> { }; if let Some((variant, name)) = &variant_and_name { - write!(f, "{}", name)?; + write!(f, "{name}")?; // Only for Adt we can have `S {...}`, // which we handle separately here. @@ -893,13 +894,13 @@ impl<'tcx> fmt::Display for Pat<'tcx> { } _ => bug!("{} is a bad Deref pattern type", self.ty), } - write!(f, "{}", subpattern) + write!(f, "{subpattern}") } - PatKind::Constant { value } => write!(f, "{}", value), + PatKind::Constant { value } => write!(f, "{value}"), PatKind::Range(box PatRange { lo, hi, end }) => { - write!(f, "{}", lo)?; - write!(f, "{}", end)?; - write!(f, "{}", hi) + write!(f, "{lo}")?; + write!(f, "{end}")?; + write!(f, "{hi}") } PatKind::Slice { ref prefix, ref slice, ref suffix } | PatKind::Array { ref prefix, ref slice, ref suffix } => { @@ -911,7 +912,7 @@ impl<'tcx> fmt::Display for Pat<'tcx> { write!(f, "{}", start_or_comma())?; match slice.kind { PatKind::Wild => {} - _ => write!(f, "{}", slice)?, + _ => write!(f, "{slice}")?, } write!(f, "..")?; } diff --git a/compiler/rustc_middle/src/thir/visit.rs b/compiler/rustc_middle/src/thir/visit.rs index 14bc1ac0c..681400dbb 100644 --- a/compiler/rustc_middle/src/thir/visit.rs +++ b/compiler/rustc_middle/src/thir/visit.rs @@ -70,7 +70,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp visitor.visit_expr(&visitor.thir()[expr]); } Loop { body } => visitor.visit_expr(&visitor.thir()[body]), - Match { scrutinee, ref arms } => { + Match { scrutinee, ref arms, .. } => { visitor.visit_expr(&visitor.thir()[scrutinee]); for &arm in &**arms { visitor.visit_arm(&visitor.thir()[arm]); @@ -101,7 +101,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp } } Become { value } => visitor.visit_expr(&visitor.thir()[value]), - ConstBlock { did: _, substs: _ } => {} + ConstBlock { did: _, args: _ } => {} Repeat { value, count: _ } => { visitor.visit_expr(&visitor.thir()[value]); } @@ -115,7 +115,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp ref base, adt_def: _, variant_index: _, - substs: _, + args: _, user_ty: _, }) => { for field in &**fields { @@ -130,7 +130,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp } Closure(box ClosureExpr { closure_id: _, - substs: _, + args: _, upvars: _, movability: _, fake_reads: _, @@ -138,7 +138,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp Literal { lit: _, neg: _ } => {} NonHirLiteral { lit: _, user_ty: _ } => {} ZstLiteral { user_ty: _ } => {} - NamedConst { def_id: _, substs: _, user_ty: _ } => {} + NamedConst { def_id: _, args: _, user_ty: _ } => {} ConstParam { param: _, def_id: _ } => {} StaticRef { alloc_id: _, ty: _, def_id: _ } => {} InlineAsm(box InlineAsmExpr { ref operands, template: _, options: _, line_spans: _ }) => { @@ -227,7 +227,7 @@ pub fn walk_pat<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, pat: &Pat<' name: _, } => visitor.visit_pat(&subpattern), Binding { .. } | Wild => {} - Variant { subpatterns, adt_def: _, substs: _, variant_index: _ } | Leaf { subpatterns } => { + Variant { subpatterns, adt_def: _, args: _, variant_index: _ } | Leaf { subpatterns } => { for subpattern in subpatterns { visitor.visit_pat(&subpattern.pattern); } diff --git a/compiler/rustc_middle/src/traits/mod.rs b/compiler/rustc_middle/src/traits/mod.rs index c7d2e4c22..3465759b9 100644 --- a/compiler/rustc_middle/src/traits/mod.rs +++ b/compiler/rustc_middle/src/traits/mod.rs @@ -12,7 +12,7 @@ pub mod util; use crate::infer::canonical::Canonical; use crate::mir::ConstraintCategory; use crate::ty::abstract_const::NotConstEvaluatable; -use crate::ty::subst::SubstsRef; +use crate::ty::GenericArgsRef; use crate::ty::{self, AdtKind, Ty, TyCtxt}; use rustc_data_structures::sync::Lrc; @@ -199,7 +199,7 @@ impl<'tcx> ObligationCause<'tcx> { pub struct UnifyReceiverContext<'tcx> { pub assoc_item: ty::AssocItem, pub param_env: ty::ParamEnv<'tcx>, - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, } #[derive(Clone, PartialEq, Eq, Lift, Default, HashStable)] @@ -402,7 +402,7 @@ pub enum ObligationCauseCode<'tcx> { OpaqueReturnType(Option<(Ty<'tcx>, Span)>), /// Block implicit return - BlockTailExpression(hir::HirId), + BlockTailExpression(hir::HirId, hir::MatchSource), /// #[feature(trivial_bounds)] is not enabled TrivialBound, @@ -543,7 +543,6 @@ pub struct MatchExpressionArmCause<'tcx> { pub scrut_span: Span, pub source: hir::MatchSource, pub prior_arms: Vec<Span>, - pub scrut_hir_id: hir::HirId, pub opt_suggest_box_span: Option<Span>, } @@ -649,43 +648,31 @@ pub enum ImplSource<'tcx, N> { /// for some type parameter. The `Vec<N>` represents the /// obligations incurred from normalizing the where-clause (if /// any). - Param(Vec<N>, ty::BoundConstness), + Param(Vec<N>), - /// Virtual calls through an object. - Object(ImplSourceObjectData<N>), - - /// Successful resolution for a builtin trait. - Builtin(Vec<N>), - - /// ImplSource for trait upcasting coercion - TraitUpcasting(ImplSourceTraitUpcastingData<N>), + /// Successful resolution for a builtin impl. + Builtin(BuiltinImplSource, Vec<N>), } impl<'tcx, N> ImplSource<'tcx, N> { pub fn nested_obligations(self) -> Vec<N> { match self { ImplSource::UserDefined(i) => i.nested, - ImplSource::Param(n, _) | ImplSource::Builtin(n) => n, - ImplSource::Object(d) => d.nested, - ImplSource::TraitUpcasting(d) => d.nested, + ImplSource::Param(n) | ImplSource::Builtin(_, n) => n, } } pub fn borrow_nested_obligations(&self) -> &[N] { match self { ImplSource::UserDefined(i) => &i.nested, - ImplSource::Param(n, _) | ImplSource::Builtin(n) => &n, - ImplSource::Object(d) => &d.nested, - ImplSource::TraitUpcasting(d) => &d.nested, + ImplSource::Param(n) | ImplSource::Builtin(_, n) => &n, } } pub fn borrow_nested_obligations_mut(&mut self) -> &mut [N] { match self { ImplSource::UserDefined(i) => &mut i.nested, - ImplSource::Param(n, _) | ImplSource::Builtin(n) => n, - ImplSource::Object(d) => &mut d.nested, - ImplSource::TraitUpcasting(d) => &mut d.nested, + ImplSource::Param(n) | ImplSource::Builtin(_, n) => n, } } @@ -696,20 +683,12 @@ impl<'tcx, N> ImplSource<'tcx, N> { match self { ImplSource::UserDefined(i) => ImplSource::UserDefined(ImplSourceUserDefinedData { impl_def_id: i.impl_def_id, - substs: i.substs, + args: i.args, nested: i.nested.into_iter().map(f).collect(), }), - ImplSource::Param(n, ct) => ImplSource::Param(n.into_iter().map(f).collect(), ct), - ImplSource::Builtin(n) => ImplSource::Builtin(n.into_iter().map(f).collect()), - ImplSource::Object(o) => ImplSource::Object(ImplSourceObjectData { - vtable_base: o.vtable_base, - nested: o.nested.into_iter().map(f).collect(), - }), - ImplSource::TraitUpcasting(d) => { - ImplSource::TraitUpcasting(ImplSourceTraitUpcastingData { - vtable_vptr_slot: d.vtable_vptr_slot, - nested: d.nested.into_iter().map(f).collect(), - }) + ImplSource::Param(n) => ImplSource::Param(n.into_iter().map(f).collect()), + ImplSource::Builtin(source, n) => { + ImplSource::Builtin(source, n.into_iter().map(f).collect()) } } } @@ -729,33 +708,35 @@ impl<'tcx, N> ImplSource<'tcx, N> { #[derive(TypeFoldable, TypeVisitable)] pub struct ImplSourceUserDefinedData<'tcx, N> { pub impl_def_id: DefId, - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, pub nested: Vec<N>, } -#[derive(Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Lift)] -#[derive(TypeFoldable, TypeVisitable)] -pub struct ImplSourceTraitUpcastingData<N> { +#[derive(Copy, Clone, PartialEq, Eq, TyEncodable, TyDecodable, HashStable, Debug)] +pub enum BuiltinImplSource { + /// Some builtin impl we don't need to differentiate. This should be used + /// unless more specific information is necessary. + Misc, + /// A builtin impl for trait objects. + /// + /// The vtable is formed by concatenating together the method lists of + /// the base object trait and all supertraits, pointers to supertrait vtable will + /// be provided when necessary; this is the start of `upcast_trait_ref`'s methods + /// in that vtable. + Object { vtable_base: usize }, /// The vtable is formed by concatenating together the method lists of /// the base object trait and all supertraits, pointers to supertrait vtable will /// be provided when necessary; this is the position of `upcast_trait_ref`'s vtable /// within that vtable. - pub vtable_vptr_slot: Option<usize>, - - pub nested: Vec<N>, + TraitUpcasting { vtable_vptr_slot: Option<usize> }, + /// Unsizing a tuple like `(A, B, ..., X)` to `(A, B, ..., Y)` if `X` unsizes to `Y`. + /// + /// This needs to be a separate variant as it is still unstable and we need to emit + /// a feature error when using it on stable. + TupleUnsizing, } -#[derive(PartialEq, Eq, Clone, TyEncodable, TyDecodable, HashStable, Lift)] -#[derive(TypeFoldable, TypeVisitable)] -pub struct ImplSourceObjectData<N> { - /// The vtable is formed by concatenating together the method lists of - /// the base object trait and all supertraits, pointers to supertrait vtable will - /// be provided when necessary; this is the start of `upcast_trait_ref`'s methods - /// in that vtable. - pub vtable_base: usize, - - pub nested: Vec<N>, -} +TrivialTypeTraversalAndLiftImpls! { BuiltinImplSource } #[derive(Clone, Debug, PartialEq, Eq, Hash, HashStable, PartialOrd, Ord)] pub enum ObjectSafetyViolation { @@ -795,49 +776,48 @@ impl ObjectSafetyViolation { "where clause cannot reference non-lifetime `for<...>` variables".into() } ObjectSafetyViolation::Method(name, MethodViolationCode::StaticMethod(_), _) => { - format!("associated function `{}` has no `self` parameter", name).into() + format!("associated function `{name}` has no `self` parameter").into() } ObjectSafetyViolation::Method( name, MethodViolationCode::ReferencesSelfInput(_), DUMMY_SP, - ) => format!("method `{}` references the `Self` type in its parameters", name).into(), + ) => format!("method `{name}` references the `Self` type in its parameters").into(), ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelfInput(_), _) => { - format!("method `{}` references the `Self` type in this parameter", name).into() + format!("method `{name}` references the `Self` type in this parameter").into() } ObjectSafetyViolation::Method(name, MethodViolationCode::ReferencesSelfOutput, _) => { - format!("method `{}` references the `Self` type in its return type", name).into() + format!("method `{name}` references the `Self` type in its return type").into() } ObjectSafetyViolation::Method( name, MethodViolationCode::ReferencesImplTraitInTrait(_), _, - ) => format!("method `{}` references an `impl Trait` type in its return type", name) - .into(), + ) => { + format!("method `{name}` references an `impl Trait` type in its return type").into() + } ObjectSafetyViolation::Method(name, MethodViolationCode::AsyncFn, _) => { - format!("method `{}` is `async`", name).into() + format!("method `{name}` is `async`").into() } ObjectSafetyViolation::Method( name, MethodViolationCode::WhereClauseReferencesSelf, _, - ) => { - format!("method `{}` references the `Self` type in its `where` clause", name).into() - } + ) => format!("method `{name}` references the `Self` type in its `where` clause").into(), ObjectSafetyViolation::Method(name, MethodViolationCode::Generic, _) => { - format!("method `{}` has generic type parameters", name).into() + format!("method `{name}` has generic type parameters").into() } ObjectSafetyViolation::Method( name, MethodViolationCode::UndispatchableReceiver(_), _, - ) => format!("method `{}`'s `self` parameter cannot be dispatched on", name).into(), + ) => format!("method `{name}`'s `self` parameter cannot be dispatched on").into(), ObjectSafetyViolation::AssocConst(name, DUMMY_SP) => { - format!("it contains associated `const` `{}`", name).into() + format!("it contains associated `const` `{name}`").into() } ObjectSafetyViolation::AssocConst(..) => "it contains this associated `const`".into(), ObjectSafetyViolation::GAT(name, _) => { - format!("it contains the generic associated type `{}`", name).into() + format!("it contains the generic associated type `{name}`").into() } } } @@ -855,8 +835,7 @@ impl ObjectSafetyViolation { err.span_suggestion( add_self_sugg.1, format!( - "consider turning `{}` into a method by giving it a `&self` argument", - name + "consider turning `{name}` into a method by giving it a `&self` argument" ), add_self_sugg.0.to_string(), Applicability::MaybeIncorrect, @@ -864,9 +843,8 @@ impl ObjectSafetyViolation { err.span_suggestion( make_sized_sugg.1, format!( - "alternatively, consider constraining `{}` so it does not apply to \ - trait objects", - name + "alternatively, consider constraining `{name}` so it does not apply to \ + trait objects" ), make_sized_sugg.0.to_string(), Applicability::MaybeIncorrect, @@ -879,7 +857,7 @@ impl ObjectSafetyViolation { ) => { err.span_suggestion( *span, - format!("consider changing method `{}`'s `self` parameter to be `&self`", name), + format!("consider changing method `{name}`'s `self` parameter to be `&self`"), "&Self", Applicability::MachineApplicable, ); @@ -887,7 +865,7 @@ impl ObjectSafetyViolation { ObjectSafetyViolation::AssocConst(name, _) | ObjectSafetyViolation::GAT(name, _) | ObjectSafetyViolation::Method(name, ..) => { - err.help(format!("consider moving `{}` to another trait", name)); + err.help(format!("consider moving `{name}` to another trait")); } } } diff --git a/compiler/rustc_middle/src/traits/query.rs b/compiler/rustc_middle/src/traits/query.rs index 60a38747f..950a59e96 100644 --- a/compiler/rustc_middle/src/traits/query.rs +++ b/compiler/rustc_middle/src/traits/query.rs @@ -8,7 +8,7 @@ use crate::error::DropCheckOverflow; use crate::infer::canonical::{Canonical, QueryResponse}; use crate::ty::error::TypeError; -use crate::ty::subst::GenericArg; +use crate::ty::GenericArg; use crate::ty::{self, Ty, TyCtxt}; use rustc_span::source_map::Span; @@ -132,7 +132,7 @@ impl<'tcx> DropckOutlivesResult<'tcx> { pub struct DropckConstraint<'tcx> { /// Types that are required to be alive in order for this /// type to be valid for destruction. - pub outlives: Vec<ty::subst::GenericArg<'tcx>>, + pub outlives: Vec<ty::GenericArg<'tcx>>, /// Types that could not be resolved: projections and params. pub dtorck_types: Vec<Ty<'tcx>>, diff --git a/compiler/rustc_middle/src/traits/select.rs b/compiler/rustc_middle/src/traits/select.rs index f2dda003b..ffae35798 100644 --- a/compiler/rustc_middle/src/traits/select.rs +++ b/compiler/rustc_middle/src/traits/select.rs @@ -127,6 +127,7 @@ pub enum SelectionCandidate<'tcx> { /// an applicable bound in the trait definition. The `usize` is an index /// into the list returned by `tcx.item_bounds`. The constness is the /// constness of the bound in the trait. + // FIXME(effects) do we need this constness ProjectionCandidate(usize, ty::BoundConstness), /// Implementation of a `Fn`-family trait by one of the anonymous types @@ -304,9 +305,7 @@ impl From<ErrorGuaranteed> for OverflowError { } } -TrivialTypeTraversalAndLiftImpls! { - OverflowError, -} +TrivialTypeTraversalAndLiftImpls! { OverflowError } impl<'tcx> From<OverflowError> for SelectionError<'tcx> { fn from(overflow_error: OverflowError) -> SelectionError<'tcx> { diff --git a/compiler/rustc_middle/src/traits/solve.rs b/compiler/rustc_middle/src/traits/solve.rs index 73b332fd8..9d63d2918 100644 --- a/compiler/rustc_middle/src/traits/solve.rs +++ b/compiler/rustc_middle/src/traits/solve.rs @@ -1,7 +1,6 @@ use std::ops::ControlFlow; use rustc_data_structures::intern::Interned; -use rustc_query_system::cache::Cache; use crate::infer::canonical::{CanonicalVarValues, QueryRegionConstraints}; use crate::traits::query::NoSolution; @@ -11,9 +10,10 @@ use crate::ty::{ TypeVisitor, }; +mod cache; pub mod inspect; -pub type EvaluationCache<'tcx> = Cache<CanonicalInput<'tcx>, QueryResult<'tcx>>; +pub use cache::{CacheData, EvaluationCache}; /// A goal is a statement, i.e. `predicate`, we want to prove /// given some assumptions, i.e. `param_env`. @@ -57,6 +57,7 @@ pub enum Certainty { impl Certainty { pub const AMBIGUOUS: Certainty = Certainty::Maybe(MaybeCause::Ambiguity); + pub const OVERFLOW: Certainty = Certainty::Maybe(MaybeCause::Overflow); /// Use this function to merge the certainty of multiple nested subgoals. /// @@ -66,7 +67,7 @@ impl Certainty { /// success, we merge these two responses. This results in ambiguity. /// /// If we unify ambiguity with overflow, we return overflow. This doesn't matter - /// inside of the solver as we distinguish ambiguity from overflow. It does + /// inside of the solver as we do not distinguish ambiguity from overflow. It does /// however matter for diagnostics. If `T: Foo` resulted in overflow and `T: Bar` /// in ambiguity without changing the inference state, we still want to tell the /// user that `T: Baz` results in overflow. @@ -146,7 +147,7 @@ impl<'tcx> std::ops::Deref for ExternalConstraints<'tcx> { } /// Additional constraints returned on success. -#[derive(Debug, PartialEq, Eq, Clone, Hash, HashStable, Default)] +#[derive(Debug, PartialEq, Eq, Clone, Hash, HashStable, Default, TypeVisitable, TypeFoldable)] pub struct ExternalConstraintsData<'tcx> { // FIXME: implement this. pub region_constraints: QueryRegionConstraints<'tcx>, diff --git a/compiler/rustc_middle/src/traits/solve/cache.rs b/compiler/rustc_middle/src/traits/solve/cache.rs new file mode 100644 index 000000000..9898b0019 --- /dev/null +++ b/compiler/rustc_middle/src/traits/solve/cache.rs @@ -0,0 +1,100 @@ +use super::{CanonicalInput, QueryResult}; +use crate::ty::TyCtxt; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; +use rustc_data_structures::sync::Lock; +use rustc_query_system::cache::WithDepNode; +use rustc_query_system::dep_graph::DepNodeIndex; +use rustc_session::Limit; +/// The trait solver cache used by `-Ztrait-solver=next`. +/// +/// FIXME(@lcnr): link to some official documentation of how +/// this works. +#[derive(Default)] +pub struct EvaluationCache<'tcx> { + map: Lock<FxHashMap<CanonicalInput<'tcx>, CacheEntry<'tcx>>>, +} + +pub struct CacheData<'tcx> { + pub result: QueryResult<'tcx>, + pub reached_depth: usize, + pub encountered_overflow: bool, +} + +impl<'tcx> EvaluationCache<'tcx> { + /// Insert a final result into the global cache. + pub fn insert( + &self, + key: CanonicalInput<'tcx>, + reached_depth: usize, + did_overflow: bool, + cycle_participants: FxHashSet<CanonicalInput<'tcx>>, + dep_node: DepNodeIndex, + result: QueryResult<'tcx>, + ) { + let mut map = self.map.borrow_mut(); + let entry = map.entry(key).or_default(); + let data = WithDepNode::new(dep_node, result); + entry.cycle_participants.extend(cycle_participants); + if did_overflow { + entry.with_overflow.insert(reached_depth, data); + } else { + entry.success = Some(Success { data, reached_depth }); + } + } + + /// Try to fetch a cached result, checking the recursion limit + /// and handling root goals of coinductive cycles. + /// + /// If this returns `Some` the cache result can be used. + pub fn get( + &self, + tcx: TyCtxt<'tcx>, + key: CanonicalInput<'tcx>, + cycle_participant_in_stack: impl FnOnce(&FxHashSet<CanonicalInput<'tcx>>) -> bool, + available_depth: Limit, + ) -> Option<CacheData<'tcx>> { + let map = self.map.borrow(); + let entry = map.get(&key)?; + + if cycle_participant_in_stack(&entry.cycle_participants) { + return None; + } + + if let Some(ref success) = entry.success { + if available_depth.value_within_limit(success.reached_depth) { + return Some(CacheData { + result: success.data.get(tcx), + reached_depth: success.reached_depth, + encountered_overflow: false, + }); + } + } + + entry.with_overflow.get(&available_depth.0).map(|e| CacheData { + result: e.get(tcx), + reached_depth: available_depth.0, + encountered_overflow: true, + }) + } +} + +struct Success<'tcx> { + data: WithDepNode<QueryResult<'tcx>>, + reached_depth: usize, +} + +/// The cache entry for a goal `CanonicalInput`. +/// +/// This contains results whose computation never hit the +/// recursion limit in `success`, and all results which hit +/// the recursion limit in `with_overflow`. +#[derive(Default)] +struct CacheEntry<'tcx> { + success: Option<Success<'tcx>>, + /// We have to be careful when caching roots of cycles. + /// + /// See the doc comment of `StackEntry::cycle_participants` for more + /// details. + cycle_participants: FxHashSet<CanonicalInput<'tcx>>, + with_overflow: FxHashMap<usize, WithDepNode<QueryResult<'tcx>>>, +} diff --git a/compiler/rustc_middle/src/traits/solve/inspect.rs b/compiler/rustc_middle/src/traits/solve/inspect.rs index 527afa005..4e2af3816 100644 --- a/compiler/rustc_middle/src/traits/solve/inspect.rs +++ b/compiler/rustc_middle/src/traits/solve/inspect.rs @@ -32,7 +32,7 @@ pub enum GoalEvaluationKind<'tcx> { } impl Debug for GoalEvaluation<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ProofTreeFormatter { f, on_newline: true }.format_goal_evaluation(self) + ProofTreeFormatter::new(f).format_goal_evaluation(self) } } @@ -43,7 +43,7 @@ pub struct AddedGoalsEvaluation<'tcx> { } impl Debug for AddedGoalsEvaluation<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ProofTreeFormatter { f, on_newline: true }.format_nested_goal_evaluation(self) + ProofTreeFormatter::new(f).format_nested_goal_evaluation(self) } } @@ -58,7 +58,7 @@ pub struct GoalEvaluationStep<'tcx> { } impl Debug for GoalEvaluationStep<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ProofTreeFormatter { f, on_newline: true }.format_evaluation_step(self) + ProofTreeFormatter::new(f).format_evaluation_step(self) } } @@ -75,9 +75,16 @@ pub enum CandidateKind<'tcx> { NormalizedSelfTyAssembly, /// A normal candidate for proving a goal Candidate { name: String, result: QueryResult<'tcx> }, + /// Used in the probe that wraps normalizing the non-self type for the unsize + /// trait, which is also structurally matched on. + UnsizeAssembly, + /// During upcasting from some source object to target object type, used to + /// do a probe to find out what projection type(s) may be used to prove that + /// the source type upholds all of the target type's object bounds. + UpcastProbe, } impl Debug for GoalCandidate<'_> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - ProofTreeFormatter { f, on_newline: true }.format_candidate(self) + ProofTreeFormatter::new(f).format_candidate(self) } } diff --git a/compiler/rustc_middle/src/traits/solve/inspect/format.rs b/compiler/rustc_middle/src/traits/solve/inspect/format.rs index 2ee625674..8759fecb0 100644 --- a/compiler/rustc_middle/src/traits/solve/inspect/format.rs +++ b/compiler/rustc_middle/src/traits/solve/inspect/format.rs @@ -1,17 +1,25 @@ use super::*; pub(super) struct ProofTreeFormatter<'a, 'b> { - pub(super) f: &'a mut (dyn Write + 'b), - pub(super) on_newline: bool, + f: &'a mut (dyn Write + 'b), } -impl Write for ProofTreeFormatter<'_, '_> { +/// A formatter which adds 4 spaces of indentation to its input before +/// passing it on to its nested formatter. +/// +/// We can use this for arbitrary levels of indentation by nesting it. +struct Indentor<'a, 'b> { + f: &'a mut (dyn Write + 'b), + on_newline: bool, +} + +impl Write for Indentor<'_, '_> { fn write_str(&mut self, s: &str) -> std::fmt::Result { - for line in s.split_inclusive("\n") { + for line in s.split_inclusive('\n') { if self.on_newline { self.f.write_str(" ")?; } - self.on_newline = line.ends_with("\n"); + self.on_newline = line.ends_with('\n'); self.f.write_str(line)?; } @@ -19,49 +27,52 @@ impl Write for ProofTreeFormatter<'_, '_> { } } -impl ProofTreeFormatter<'_, '_> { - fn nested(&mut self) -> ProofTreeFormatter<'_, '_> { - ProofTreeFormatter { f: self, on_newline: true } +impl<'a, 'b> ProofTreeFormatter<'a, 'b> { + pub(super) fn new(f: &'a mut (dyn Write + 'b)) -> Self { + ProofTreeFormatter { f } } - pub(super) fn format_goal_evaluation(&mut self, goal: &GoalEvaluation<'_>) -> std::fmt::Result { - let f = &mut *self.f; + fn nested<F, R>(&mut self, func: F) -> R + where + F: FnOnce(&mut ProofTreeFormatter<'_, '_>) -> R, + { + func(&mut ProofTreeFormatter { f: &mut Indentor { f: self.f, on_newline: true } }) + } + pub(super) fn format_goal_evaluation(&mut self, goal: &GoalEvaluation<'_>) -> std::fmt::Result { let goal_text = match goal.is_normalizes_to_hack { IsNormalizesToHack::Yes => "NORMALIZES-TO HACK GOAL", IsNormalizesToHack::No => "GOAL", }; - writeln!(f, "{}: {:?}", goal_text, goal.uncanonicalized_goal,)?; - writeln!(f, "CANONICALIZED: {:?}", goal.canonicalized_goal)?; + writeln!(self.f, "{}: {:?}", goal_text, goal.uncanonicalized_goal)?; + writeln!(self.f, "CANONICALIZED: {:?}", goal.canonicalized_goal)?; match &goal.kind { GoalEvaluationKind::CacheHit(CacheHit::Global) => { - writeln!(f, "GLOBAL CACHE HIT: {:?}", goal.result) + writeln!(self.f, "GLOBAL CACHE HIT: {:?}", goal.result) } GoalEvaluationKind::CacheHit(CacheHit::Provisional) => { - writeln!(f, "PROVISIONAL CACHE HIT: {:?}", goal.result) + writeln!(self.f, "PROVISIONAL CACHE HIT: {:?}", goal.result) } GoalEvaluationKind::Uncached { revisions } => { for (n, step) in revisions.iter().enumerate() { - let f = &mut *self.f; - writeln!(f, "REVISION {n}: {:?}", step.result)?; - let mut f = self.nested(); - f.format_evaluation_step(step)?; + writeln!(self.f, "REVISION {n}: {:?}", step.result)?; + self.nested(|this| this.format_evaluation_step(step))?; } - - let f = &mut *self.f; - writeln!(f, "RESULT: {:?}", goal.result) + writeln!(self.f, "RESULT: {:?}", goal.result) } }?; if goal.returned_goals.len() > 0 { - let f = &mut *self.f; - writeln!(f, "NESTED GOALS ADDED TO CALLER: [")?; - let mut f = self.nested(); - for goal in goal.returned_goals.iter() { - writeln!(f, "ADDED GOAL: {:?},", goal)?; - } + writeln!(self.f, "NESTED GOALS ADDED TO CALLER: [")?; + self.nested(|this| { + for goal in goal.returned_goals.iter() { + writeln!(this.f, "ADDED GOAL: {goal:?},")?; + } + Ok(()) + })?; + writeln!(self.f, "]")?; } @@ -72,58 +83,59 @@ impl ProofTreeFormatter<'_, '_> { &mut self, evaluation_step: &GoalEvaluationStep<'_>, ) -> std::fmt::Result { - let f = &mut *self.f; - writeln!(f, "INSTANTIATED: {:?}", evaluation_step.instantiated_goal)?; + writeln!(self.f, "INSTANTIATED: {:?}", evaluation_step.instantiated_goal)?; for candidate in &evaluation_step.candidates { - let mut f = self.nested(); - f.format_candidate(candidate)?; + self.nested(|this| this.format_candidate(candidate))?; } - for nested_goal_evaluation in &evaluation_step.nested_goal_evaluations { - let mut f = self.nested(); - f.format_nested_goal_evaluation(nested_goal_evaluation)?; + for nested in &evaluation_step.nested_goal_evaluations { + self.nested(|this| this.format_nested_goal_evaluation(nested))?; } Ok(()) } pub(super) fn format_candidate(&mut self, candidate: &GoalCandidate<'_>) -> std::fmt::Result { - let f = &mut *self.f; - match &candidate.kind { CandidateKind::NormalizedSelfTyAssembly => { - writeln!(f, "NORMALIZING SELF TY FOR ASSEMBLY:") + writeln!(self.f, "NORMALIZING SELF TY FOR ASSEMBLY:") + } + CandidateKind::UnsizeAssembly => { + writeln!(self.f, "ASSEMBLING CANDIDATES FOR UNSIZING:") + } + CandidateKind::UpcastProbe => { + writeln!(self.f, "PROBING FOR PROJECTION COMPATIBILITY FOR UPCASTING:") } CandidateKind::Candidate { name, result } => { - writeln!(f, "CANDIDATE {}: {:?}", name, result) + writeln!(self.f, "CANDIDATE {name}: {result:?}") } }?; - let mut f = self.nested(); - for candidate in &candidate.candidates { - f.format_candidate(candidate)?; - } - for nested_evaluations in &candidate.nested_goal_evaluations { - f.format_nested_goal_evaluation(nested_evaluations)?; - } - - Ok(()) + self.nested(|this| { + for candidate in &candidate.candidates { + this.format_candidate(candidate)?; + } + for nested in &candidate.nested_goal_evaluations { + this.format_nested_goal_evaluation(nested)?; + } + Ok(()) + }) } pub(super) fn format_nested_goal_evaluation( &mut self, nested_goal_evaluation: &AddedGoalsEvaluation<'_>, ) -> std::fmt::Result { - let f = &mut *self.f; - writeln!(f, "TRY_EVALUATE_ADDED_GOALS: {:?}", nested_goal_evaluation.result)?; + writeln!(self.f, "TRY_EVALUATE_ADDED_GOALS: {:?}", nested_goal_evaluation.result)?; for (n, revision) in nested_goal_evaluation.evaluations.iter().enumerate() { - let f = &mut *self.f; - writeln!(f, "REVISION {n}")?; - let mut f = self.nested(); - for goal_evaluation in revision { - f.format_goal_evaluation(goal_evaluation)?; - } + writeln!(self.f, "REVISION {n}")?; + self.nested(|this| { + for goal_evaluation in revision { + this.format_goal_evaluation(goal_evaluation)?; + } + Ok(()) + })?; } Ok(()) diff --git a/compiler/rustc_middle/src/traits/specialization_graph.rs b/compiler/rustc_middle/src/traits/specialization_graph.rs index dc2cd2035..e48b46d12 100644 --- a/compiler/rustc_middle/src/traits/specialization_graph.rs +++ b/compiler/rustc_middle/src/traits/specialization_graph.rs @@ -43,7 +43,7 @@ impl Graph { /// The parent of a given impl, which is the `DefId` of the trait when the /// impl is a "specialization root". pub fn parent(&self, child: DefId) -> DefId { - *self.parent.get(&child).unwrap_or_else(|| panic!("Failed to get parent for {:?}", child)) + *self.parent.get(&child).unwrap_or_else(|| panic!("Failed to get parent for {child:?}")) } } @@ -259,7 +259,9 @@ pub fn ancestors( if let Some(reported) = specialization_graph.has_errored { Err(reported) - } else if let Err(reported) = tcx.type_of(start_from_impl).subst_identity().error_reported() { + } else if let Err(reported) = + tcx.type_of(start_from_impl).instantiate_identity().error_reported() + { Err(reported) } else { Ok(Ancestors { diff --git a/compiler/rustc_middle/src/traits/structural_impls.rs b/compiler/rustc_middle/src/traits/structural_impls.rs index a703e3c95..ec450cf55 100644 --- a/compiler/rustc_middle/src/traits/structural_impls.rs +++ b/compiler/rustc_middle/src/traits/structural_impls.rs @@ -6,18 +6,16 @@ use std::fmt; impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSource<'tcx, N> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match *self { - super::ImplSource::UserDefined(ref v) => write!(f, "{:?}", v), + match self { + super::ImplSource::UserDefined(v) => write!(f, "{v:?}"), - super::ImplSource::Builtin(ref d) => write!(f, "{:?}", d), - - super::ImplSource::Object(ref d) => write!(f, "{:?}", d), - - super::ImplSource::Param(ref n, ct) => { - write!(f, "ImplSourceParamData({:?}, {:?})", n, ct) + super::ImplSource::Builtin(source, d) => { + write!(f, "Builtin({source:?}, {d:?})") } - super::ImplSource::TraitUpcasting(ref d) => write!(f, "{:?}", d), + super::ImplSource::Param(n) => { + write!(f, "ImplSourceParamData({n:?})") + } } } } @@ -26,28 +24,8 @@ impl<'tcx, N: fmt::Debug> fmt::Debug for traits::ImplSourceUserDefinedData<'tcx, fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!( f, - "ImplSourceUserDefinedData(impl_def_id={:?}, substs={:?}, nested={:?})", - self.impl_def_id, self.substs, self.nested - ) - } -} - -impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceTraitUpcastingData<N> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "ImplSourceTraitUpcastingData(vtable_vptr_slot={:?}, nested={:?})", - self.vtable_vptr_slot, self.nested - ) - } -} - -impl<N: fmt::Debug> fmt::Debug for traits::ImplSourceObjectData<N> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "ImplSourceObjectData(vtable_base={}, nested={:?})", - self.vtable_base, self.nested + "ImplSourceUserDefinedData(impl_def_id={:?}, args={:?}, nested={:?})", + self.impl_def_id, self.args, self.nested ) } } diff --git a/compiler/rustc_middle/src/ty/abstract_const.rs b/compiler/rustc_middle/src/ty/abstract_const.rs index ffee7ba28..cdd835149 100644 --- a/compiler/rustc_middle/src/ty/abstract_const.rs +++ b/compiler/rustc_middle/src/ty/abstract_const.rs @@ -27,9 +27,7 @@ impl From<ErrorGuaranteed> for NotConstEvaluatable { } } -TrivialTypeTraversalAndLiftImpls! { - NotConstEvaluatable, -} +TrivialTypeTraversalAndLiftImpls! { NotConstEvaluatable } pub type BoundAbstractConst<'tcx> = Result<Option<EarlyBinder<ty::Const<'tcx>>>, ErrorGuaranteed>; @@ -55,8 +53,8 @@ impl<'tcx> TyCtxt<'tcx> { ty::ConstKind::Unevaluated(uv) => match self.tcx.thir_abstract_const(uv.def) { Err(e) => ty::Const::new_error(self.tcx, e, c.ty()), Ok(Some(bac)) => { - let substs = self.tcx.erase_regions(uv.substs); - let bac = bac.subst(self.tcx, substs); + let args = self.tcx.erase_regions(uv.args); + let bac = bac.instantiate(self.tcx, args); return bac.fold_with(self); } Ok(None) => c, diff --git a/compiler/rustc_middle/src/ty/adt.rs b/compiler/rustc_middle/src/ty/adt.rs index e067d2a98..b4c6e0d97 100644 --- a/compiler/rustc_middle/src/ty/adt.rs +++ b/compiler/rustc_middle/src/ty/adt.rs @@ -448,7 +448,7 @@ impl<'tcx> AdtDef<'tcx> { Res::Def(DefKind::Ctor(..), cid) => self.variant_with_ctor_id(cid), Res::Def(DefKind::Struct, _) | Res::Def(DefKind::Union, _) - | Res::Def(DefKind::TyAlias, _) + | Res::Def(DefKind::TyAlias { .. }, _) | Res::Def(DefKind::AssocTy, _) | Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } @@ -562,18 +562,10 @@ impl<'tcx> AdtDef<'tcx> { tcx.adt_destructor(self.did()) } - /// Returns a list of types such that `Self: Sized` if and only - /// if that type is `Sized`, or `TyErr` if this type is recursive. - /// - /// Oddly enough, checking that the sized-constraint is `Sized` is - /// actually more expressive than checking all members: - /// the `Sized` trait is inductive, so an associated type that references - /// `Self` would prevent its containing ADT from being `Sized`. - /// - /// Due to normalization being eager, this applies even if - /// the associated type is behind a pointer (e.g., issue #31299). - pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> ty::EarlyBinder<&'tcx [Ty<'tcx>]> { - ty::EarlyBinder::bind(tcx.adt_sized_constraint(self.did())) + /// Returns a list of types such that `Self: Sized` if and only if that + /// type is `Sized`, or `ty::Error` if this type has a recursive layout. + pub fn sized_constraint(self, tcx: TyCtxt<'tcx>) -> ty::EarlyBinder<&'tcx ty::List<Ty<'tcx>>> { + tcx.adt_sized_constraint(self.did()) } } diff --git a/compiler/rustc_middle/src/ty/assoc.rs b/compiler/rustc_middle/src/ty/assoc.rs index cce609c26..f77a8c671 100644 --- a/compiler/rustc_middle/src/ty/assoc.rs +++ b/compiler/rustc_middle/src/ty/assoc.rs @@ -84,14 +84,22 @@ impl AssocItem { // late-bound regions, and we don't want method signatures to show up // `as for<'r> fn(&'r MyType)`. Pretty-printing handles late-bound // regions just fine, showing `fn(&MyType)`. - tcx.fn_sig(self.def_id).subst_identity().skip_binder().to_string() + tcx.fn_sig(self.def_id).instantiate_identity().skip_binder().to_string() } ty::AssocKind::Type => format!("type {};", self.name), ty::AssocKind::Const => { - format!("const {}: {:?};", self.name, tcx.type_of(self.def_id).subst_identity()) + format!( + "const {}: {:?};", + self.name, + tcx.type_of(self.def_id).instantiate_identity() + ) } } } + + pub fn is_impl_trait_in_trait(&self) -> bool { + self.opt_rpitit_info.is_some() + } } #[derive(Copy, Clone, PartialEq, Debug, HashStable, Eq, Hash, Encodable, Decodable)] diff --git a/compiler/rustc_middle/src/ty/binding.rs b/compiler/rustc_middle/src/ty/binding.rs index a5b05a4f9..2fec8ac90 100644 --- a/compiler/rustc_middle/src/ty/binding.rs +++ b/compiler/rustc_middle/src/ty/binding.rs @@ -6,7 +6,7 @@ pub enum BindingMode { BindByValue(Mutability), } -TrivialTypeTraversalAndLiftImpls! { BindingMode, } +TrivialTypeTraversalAndLiftImpls! { BindingMode } impl BindingMode { pub fn convert(BindingAnnotation(by_ref, mutbl): BindingAnnotation) -> BindingMode { diff --git a/compiler/rustc_middle/src/ty/closure.rs b/compiler/rustc_middle/src/ty/closure.rs index bc9273745..74bdd07a1 100644 --- a/compiler/rustc_middle/src/ty/closure.rs +++ b/compiler/rustc_middle/src/ty/closure.rs @@ -6,9 +6,11 @@ use crate::{mir, ty}; use std::fmt::Write; use crate::query::Providers; -use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; +use rustc_data_structures::fx::FxIndexMap; +use rustc_errors::{DiagnosticArgValue, IntoDiagnosticArg}; use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::{self as hir, LangItem}; +use rustc_span::def_id::LocalDefIdMap; use rustc_span::symbol::Ident; use rustc_span::{Span, Symbol}; @@ -56,12 +58,9 @@ pub enum UpvarCapture { ByRef(BorrowKind), } -pub type UpvarListMap = FxHashMap<DefId, FxIndexMap<hir::HirId, UpvarId>>; -pub type UpvarCaptureMap = FxHashMap<UpvarId, UpvarCapture>; - /// Given the closure DefId this map provides a map of root variables to minimum /// set of `CapturedPlace`s that need to be tracked to support all captures of that closure. -pub type MinCaptureInformationMap<'tcx> = FxHashMap<LocalDefId, RootVariableMinCaptureList<'tcx>>; +pub type MinCaptureInformationMap<'tcx> = LocalDefIdMap<RootVariableMinCaptureList<'tcx>>; /// Part of `MinCaptureInformationMap`; Maps a root variable to the list of `CapturedPlace`. /// Used to track the minimum set of `Place`s that need to be captured to support all @@ -91,10 +90,18 @@ pub enum ClosureKind { FnOnce, } -impl<'tcx> ClosureKind { +impl ClosureKind { /// This is the initial value used when doing upvar inference. pub const LATTICE_BOTTOM: ClosureKind = ClosureKind::Fn; + pub const fn as_str(self) -> &'static str { + match self { + ClosureKind::Fn => "Fn", + ClosureKind::FnMut => "FnMut", + ClosureKind::FnOnce => "FnOnce", + } + } + /// Returns `true` if a type that impls this closure kind /// must also implement `other`. pub fn extends(self, other: ty::ClosureKind) -> bool { @@ -117,7 +124,7 @@ impl<'tcx> ClosureKind { /// Returns the representative scalar type for this closure kind. /// See `Ty::to_opt_closure_kind` for more details. - pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { + pub fn to_ty<'tcx>(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { match self { ClosureKind::Fn => tcx.types.i8, ClosureKind::FnMut => tcx.types.i16, @@ -126,6 +133,12 @@ impl<'tcx> ClosureKind { } } +impl IntoDiagnosticArg for ClosureKind { + fn into_diagnostic_arg(self) -> DiagnosticArgValue<'static> { + DiagnosticArgValue::Str(self.as_str().into()) + } +} + /// A composite describing a `Place` that is captured by a closure. #[derive(PartialEq, Clone, Debug, TyEncodable, TyDecodable, HashStable)] #[derive(TypeFoldable, TypeVisitable)] @@ -176,6 +189,8 @@ impl<'tcx> CapturedPlace<'tcx> { // Ignore derefs for now, as they are likely caused by // autoderefs that don't appear in the original code. HirProjectionKind::Deref => {} + // Just change the type to the hidden type, so we can actually project. + HirProjectionKind::OpaqueCast => {} proj => bug!("Unexpected projection {:?} in captured place", proj), } ty = proj.ty; @@ -350,7 +365,7 @@ pub fn place_to_string_for_capture<'tcx>(tcx: TyCtxt<'tcx>, place: &HirPlace<'tc for (i, proj) in place.projections.iter().enumerate() { match proj.kind { HirProjectionKind::Deref => { - curr_string = format!("*{}", curr_string); + curr_string = format!("*{curr_string}"); } HirProjectionKind::Field(idx, variant) => match place.ty_before_projection(i).kind() { ty::Adt(def, ..) => { diff --git a/compiler/rustc_middle/src/ty/codec.rs b/compiler/rustc_middle/src/ty/codec.rs index 6adbb44a1..7c05deae9 100644 --- a/compiler/rustc_middle/src/ty/codec.rs +++ b/compiler/rustc_middle/src/ty/codec.rs @@ -13,7 +13,7 @@ use crate::mir::{ interpret::{AllocId, ConstAllocation}, }; use crate::traits; -use crate::ty::subst::SubstsRef; +use crate::ty::GenericArgsRef; use crate::ty::{self, AdtDef, Ty}; use rustc_data_structures::fx::FxHashMap; use rustc_middle::ty::TyCtxt; @@ -168,7 +168,6 @@ impl<'tcx, E: TyEncoder<I = TyCtxt<'tcx>>> Encodable<E> for ty::ParamEnv<'tcx> { fn encode(&self, e: &mut E) { self.caller_bounds().encode(e); self.reveal().encode(e); - self.constness().encode(e); } } @@ -254,12 +253,12 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Clause<'tcx> { } } -impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for SubstsRef<'tcx> { +impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for GenericArgsRef<'tcx> { fn decode(decoder: &mut D) -> Self { let len = decoder.read_usize(); let tcx = decoder.interner(); - tcx.mk_substs_from_iter( - (0..len).map::<ty::subst::GenericArg<'tcx>, _>(|_| Decodable::decode(decoder)), + tcx.mk_args_from_iter( + (0..len).map::<ty::GenericArg<'tcx>, _>(|_| Decodable::decode(decoder)), ) } } @@ -306,8 +305,7 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::ParamEnv<'tcx> { fn decode(d: &mut D) -> Self { let caller_bounds = Decodable::decode(d); let reveal = Decodable::decode(d); - let constness = Decodable::decode(d); - ty::ParamEnv::new(caller_bounds, reveal, constness) + ty::ParamEnv::new(caller_bounds, reveal) } } diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 1cbfe99f8..cce10417e 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -1,6 +1,6 @@ use crate::middle::resolve_bound_vars as rbv; use crate::mir::interpret::{AllocId, ConstValue, LitToConstInput, Scalar}; -use crate::ty::{self, InternalSubsts, ParamEnv, ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt}; +use crate::ty::{self, GenericArgs, ParamEnv, ParamEnvAnd, Ty, TyCtxt, TypeVisitableExt}; use rustc_data_structures::intern::Interned; use rustc_error_messages::MultiSpan; use rustc_hir as hir; @@ -171,7 +171,7 @@ impl<'tcx> Const<'tcx> { tcx, ty::UnevaluatedConst { def: def.to_def_id(), - substs: InternalSubsts::identity_for_item(tcx, def.to_def_id()), + args: GenericArgs::identity_for_item(tcx, def.to_def_id()), }, ty, ), @@ -212,7 +212,7 @@ impl<'tcx> Const<'tcx> { Err(e) => { tcx.sess.delay_span_bug( expr.span, - format!("Const::from_anon_const: couldn't lit_to_const {:?}", e), + format!("Const::from_anon_const: couldn't lit_to_const {e:?}"), ); } } @@ -225,7 +225,7 @@ impl<'tcx> Const<'tcx> { )) => { // Use the type from the param's definition, since we can resolve it, // not the expected parameter type from WithOptConstParam. - let param_ty = tcx.type_of(def_id).subst_identity(); + let param_ty = tcx.type_of(def_id).instantiate_identity(); match tcx.named_bound_var(expr.hir_id) { Some(rbv::ResolvedArg::EarlyBound(_)) => { // Find the name and index of the const parameter by indexing the generics of @@ -267,7 +267,7 @@ impl<'tcx> Const<'tcx> { pub fn from_bits(tcx: TyCtxt<'tcx>, bits: u128, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> Self { let size = tcx .layout_of(ty) - .unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e)) + .unwrap_or_else(|e| panic!("could not compute layout for {ty:?}: {e:?}")) .size; ty::Const::new_value( tcx, @@ -294,6 +294,14 @@ impl<'tcx> Const<'tcx> { Self::from_bits(tcx, n as u128, ParamEnv::empty().and(tcx.types.usize)) } + /// Attempts to convert to a `ValTree` + pub fn try_to_valtree(self) -> Option<ty::ValTree<'tcx>> { + match self.kind() { + ty::ConstKind::Value(valtree) => Some(valtree), + _ => None, + } + } + #[inline] /// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of /// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it @@ -406,14 +414,14 @@ impl<'tcx> Const<'tcx> { // any region variables. // HACK(eddyb) when the query key would contain inference variables, - // attempt using identity substs and `ParamEnv` instead, that will succeed + // attempt using identity args and `ParamEnv` instead, that will succeed // when the expression doesn't depend on any parameters. // FIXME(eddyb, skinny121) pass `InferCtxt` into here when it's available, so that // we can call `infcx.const_eval_resolve` which handles inference variables. let param_env_and = if (param_env, unevaluated).has_non_region_infer() { tcx.param_env(unevaluated.def).and(ty::UnevaluatedConst { def: unevaluated.def, - substs: InternalSubsts::identity_for_item(tcx, unevaluated.def), + args: GenericArgs::identity_for_item(tcx, unevaluated.def), }) } else { tcx.erase_regions(param_env) @@ -430,8 +438,8 @@ impl<'tcx> Const<'tcx> { EvalMode::Typeck => { match tcx.const_eval_resolve_for_typeck(param_env, unevaluated, None) { // NOTE(eddyb) `val` contains no lifetimes/types/consts, - // and we use the original type, so nothing from `substs` - // (which may be identity substs, see above), + // and we use the original type, so nothing from `args` + // (which may be identity args, see above), // can leak through `val` into the const we return. Ok(val) => Some(Ok(EvalResult::ValTree(val?))), Err(ErrorHandled::TooGeneric) => None, @@ -441,8 +449,8 @@ impl<'tcx> Const<'tcx> { EvalMode::Mir => { match tcx.const_eval_resolve(param_env, unevaluated.expand(), None) { // NOTE(eddyb) `val` contains no lifetimes/types/consts, - // and we use the original type, so nothing from `substs` - // (which may be identity substs, see above), + // and we use the original type, so nothing from `args` + // (which may be identity args, see above), // can leak through `val` into the const we return. Ok(val) => Some(Ok(EvalResult::ConstVal(val))), Err(ErrorHandled::TooGeneric) => None, diff --git a/compiler/rustc_middle/src/ty/consts/int.rs b/compiler/rustc_middle/src/ty/consts/int.rs index 1e43fab45..b16163edf 100644 --- a/compiler/rustc_middle/src/ty/consts/int.rs +++ b/compiler/rustc_middle/src/ty/consts/int.rs @@ -418,7 +418,7 @@ impl TryFrom<ScalarInt> for char { #[inline] fn try_from(int: ScalarInt) -> Result<Self, Self::Error> { - let Ok(bits) = int.to_bits(Size::from_bytes(std::mem::size_of::<char>())) else { + let Ok(bits) = int.to_bits(Size::from_bytes(std::mem::size_of::<char>())) else { return Err(CharTryFromScalarInt); }; match char::from_u32(bits.try_into().unwrap()) { @@ -463,7 +463,7 @@ impl TryFrom<ScalarInt> for Double { impl fmt::Debug for ScalarInt { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { // Dispatch to LowerHex below. - write!(f, "0x{:x}", self) + write!(f, "0x{self:x}") } } diff --git a/compiler/rustc_middle/src/ty/consts/kind.rs b/compiler/rustc_middle/src/ty/consts/kind.rs index a6bf74911..db4a15fbe 100644 --- a/compiler/rustc_middle/src/ty/consts/kind.rs +++ b/compiler/rustc_middle/src/ty/consts/kind.rs @@ -1,41 +1,41 @@ use super::Const; use crate::mir; use crate::ty::abstract_const::CastKind; -use crate::ty::subst::SubstsRef; +use crate::ty::GenericArgsRef; use crate::ty::{self, List, Ty}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_hir::def_id::DefId; use rustc_macros::HashStable; /// An unevaluated (potentially generic) constant used in the type-system. -#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)] +#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, TyEncodable, TyDecodable, Lift)] #[derive(Hash, HashStable, TypeFoldable, TypeVisitable)] pub struct UnevaluatedConst<'tcx> { pub def: DefId, - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, } impl rustc_errors::IntoDiagnosticArg for UnevaluatedConst<'_> { fn into_diagnostic_arg(self) -> rustc_errors::DiagnosticArgValue<'static> { - format!("{:?}", self).into_diagnostic_arg() + format!("{self:?}").into_diagnostic_arg() } } impl<'tcx> UnevaluatedConst<'tcx> { #[inline] pub fn expand(self) -> mir::UnevaluatedConst<'tcx> { - mir::UnevaluatedConst { def: self.def, substs: self.substs, promoted: None } + mir::UnevaluatedConst { def: self.def, args: self.args, promoted: None } } } impl<'tcx> UnevaluatedConst<'tcx> { #[inline] - pub fn new(def: DefId, substs: SubstsRef<'tcx>) -> UnevaluatedConst<'tcx> { - UnevaluatedConst { def, substs } + pub fn new(def: DefId, args: GenericArgsRef<'tcx>) -> UnevaluatedConst<'tcx> { + UnevaluatedConst { def, args } } } -#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Hash)] +#[derive(Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)] #[derive(HashStable, TyEncodable, TyDecodable, TypeVisitable, TypeFoldable)] pub enum Expr<'tcx> { Binop(mir::BinOp, Const<'tcx>, Const<'tcx>), diff --git a/compiler/rustc_middle/src/ty/consts/valtree.rs b/compiler/rustc_middle/src/ty/consts/valtree.rs index 8b96864dd..fb7bf78ba 100644 --- a/compiler/rustc_middle/src/ty/consts/valtree.rs +++ b/compiler/rustc_middle/src/ty/consts/valtree.rs @@ -24,7 +24,7 @@ pub enum ValTree<'tcx> { Leaf(ScalarInt), //SliceOrStr(ValSlice<'tcx>), - // dont use SliceOrStr for now + // don't use SliceOrStr for now /// The fields of any kind of aggregate. Structs, tuples and arrays are represented by /// listing their fields' values in order. /// diff --git a/compiler/rustc_middle/src/ty/context.rs b/compiler/rustc_middle/src/ty/context.rs index 035e978f6..be839e03c 100644 --- a/compiler/rustc_middle/src/ty/context.rs +++ b/compiler/rustc_middle/src/ty/context.rs @@ -30,7 +30,7 @@ use crate::ty::{ Predicate, PredicateKind, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty, TyKind, TyVid, TypeAndMut, Visibility, }; -use crate::ty::{GenericArg, InternalSubsts, SubstsRef}; +use crate::ty::{GenericArg, GenericArgs, GenericArgsRef}; use rustc_ast::{self as ast, attr}; use rustc_data_structures::fingerprint::Fingerprint; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; @@ -50,9 +50,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE}; use rustc_hir::definitions::Definitions; use rustc_hir::intravisit::Visitor; use rustc_hir::lang_items::LangItem; -use rustc_hir::{ - Constness, ExprKind, HirId, ImplItemKind, ItemKind, Node, TraitCandidate, TraitItemKind, -}; +use rustc_hir::{Constness, HirId, Node, TraitCandidate}; use rustc_index::IndexVec; use rustc_macros::HashStable; use rustc_query_system::dep_graph::DepNodeIndex; @@ -61,8 +59,7 @@ use rustc_serialize::opaque::{FileEncodeResult, FileEncoder}; use rustc_session::config::CrateType; use rustc_session::cstore::{CrateStoreDyn, Untracked}; use rustc_session::lint::Lint; -use rustc_session::Limit; -use rustc_session::Session; +use rustc_session::{Limit, MetadataKind, Session}; use rustc_span::def_id::{DefPathHash, StableCrateId}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{Span, DUMMY_SP}; @@ -84,7 +81,7 @@ use std::ops::{Bound, Deref}; #[allow(rustc::usage_of_ty_tykind)] impl<'tcx> Interner for TyCtxt<'tcx> { type AdtDef = ty::AdtDef<'tcx>; - type SubstsRef = ty::SubstsRef<'tcx>; + type GenericArgsRef = ty::GenericArgsRef<'tcx>; type DefId = DefId; type Binder<T> = Binder<'tcx, T>; type Ty = Ty<'tcx>; @@ -142,7 +139,7 @@ pub struct CtxtInterners<'tcx> { // they're accessed quite often. type_: InternedSet<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>, const_lists: InternedSet<'tcx, List<ty::Const<'tcx>>>, - substs: InternedSet<'tcx, InternalSubsts<'tcx>>, + args: InternedSet<'tcx, GenericArgs<'tcx>>, type_lists: InternedSet<'tcx, List<Ty<'tcx>>>, canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo<'tcx>>>, region: InternedSet<'tcx, RegionKind<'tcx>>, @@ -167,7 +164,7 @@ impl<'tcx> CtxtInterners<'tcx> { arena, type_: Default::default(), const_lists: Default::default(), - substs: Default::default(), + args: Default::default(), type_lists: Default::default(), region: Default::default(), poly_existential_predicates: Default::default(), @@ -529,6 +526,13 @@ pub struct GlobalCtxt<'tcx> { interners: CtxtInterners<'tcx>, pub sess: &'tcx Session, + crate_types: Vec<CrateType>, + /// The `stable_crate_id` is constructed out of the crate name and all the + /// `-C metadata` arguments passed to the compiler. Its value forms a unique + /// global identifier for the crate. It is used to allow multiple crates + /// with the same name to coexist. See the + /// `rustc_symbol_mangling` crate for more information. + stable_crate_id: StableCrateId, /// This only ever stores a `LintStore` but we don't want a dependency on that type here. /// @@ -569,6 +573,7 @@ pub struct GlobalCtxt<'tcx> { /// Caches the results of goal evaluation in the new solver. pub new_solver_evaluation_cache: solve::EvaluationCache<'tcx>, + pub new_solver_coherence_evaluation_cache: solve::EvaluationCache<'tcx>, /// Data layout specification for the current target. pub data_layout: TargetDataLayout, @@ -680,12 +685,16 @@ impl<'tcx> TyCtxt<'tcx> { value.lift_to_tcx(self) } - /// Creates a type context and call the closure with a `TyCtxt` reference - /// to the context. The closure enforces that the type context and any interned - /// value (types, substs, etc.) can only be used while `ty::tls` has a valid - /// reference to the context, to allow formatting values that need it. + /// Creates a type context. To use the context call `fn enter` which + /// provides a `TyCtxt`. + /// + /// By only providing the `TyCtxt` inside of the closure we enforce that the type + /// context and any interned alue (types, args, etc.) can only be used while `ty::tls` + /// has a valid reference to the context, to allow formatting values that need it. pub fn create_global_ctxt( s: &'tcx Session, + crate_types: Vec<CrateType>, + stable_crate_id: StableCrateId, lint_store: Lrc<dyn Any + sync::DynSend + sync::DynSync>, arena: &'tcx WorkerLocal<Arena<'tcx>>, hir_arena: &'tcx WorkerLocal<hir::Arena<'tcx>>, @@ -704,6 +713,8 @@ impl<'tcx> TyCtxt<'tcx> { GlobalCtxt { sess: s, + crate_types, + stable_crate_id, lint_store, arena, hir_arena, @@ -721,6 +732,7 @@ impl<'tcx> TyCtxt<'tcx> { selection_cache: Default::default(), evaluation_cache: Default::default(), new_solver_evaluation_cache: Default::default(), + new_solver_coherence_evaluation_cache: Default::default(), data_layout, alloc_map: Lock::new(interpret::AllocMap::new()), } @@ -799,9 +811,46 @@ impl<'tcx> TyCtxt<'tcx> { } #[inline] + pub fn crate_types(self) -> &'tcx [CrateType] { + &self.crate_types + } + + pub fn metadata_kind(self) -> MetadataKind { + self.crate_types() + .iter() + .map(|ty| match *ty { + CrateType::Executable | CrateType::Staticlib | CrateType::Cdylib => { + MetadataKind::None + } + CrateType::Rlib => MetadataKind::Uncompressed, + CrateType::Dylib | CrateType::ProcMacro => MetadataKind::Compressed, + }) + .max() + .unwrap_or(MetadataKind::None) + } + + pub fn needs_metadata(self) -> bool { + self.metadata_kind() != MetadataKind::None + } + + pub fn needs_crate_hash(self) -> bool { + // Why is the crate hash needed for these configurations? + // - debug_assertions: for the "fingerprint the result" check in + // `rustc_query_system::query::plumbing::execute_job`. + // - incremental: for query lookups. + // - needs_metadata: for putting into crate metadata. + // - instrument_coverage: for putting into coverage data (see + // `hash_mir_source`). + cfg!(debug_assertions) + || self.sess.opts.incremental.is_some() + || self.needs_metadata() + || self.sess.instrument_coverage() + } + + #[inline] pub fn stable_crate_id(self, crate_num: CrateNum) -> StableCrateId { if crate_num == LOCAL_CRATE { - self.sess.local_stable_crate_id() + self.stable_crate_id } else { self.cstore_untracked().stable_crate_id(crate_num) } @@ -811,7 +860,7 @@ impl<'tcx> TyCtxt<'tcx> { /// that the crate in question has already been loaded by the CrateStore. #[inline] pub fn stable_crate_id_to_crate_num(self, stable_crate_id: StableCrateId) -> CrateNum { - if stable_crate_id == self.sess.local_stable_crate_id() { + if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) { LOCAL_CRATE } else { self.cstore_untracked().stable_crate_id_to_crate_num(stable_crate_id) @@ -828,7 +877,7 @@ impl<'tcx> TyCtxt<'tcx> { // If this is a DefPathHash from the local crate, we can look up the // DefId in the tcx's `Definitions`. - if stable_crate_id == self.sess.local_stable_crate_id() { + if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) { self.untracked.definitions.read().local_def_path_hash_to_def_id(hash, err).to_def_id() } else { // If this is a DefPathHash from an upstream crate, let the CrateStore map @@ -845,7 +894,7 @@ impl<'tcx> TyCtxt<'tcx> { // statements within the query system and we'd run into endless // recursion otherwise. let (crate_name, stable_crate_id) = if def_id.is_local() { - (self.crate_name(LOCAL_CRATE), self.sess.local_stable_crate_id()) + (self.crate_name(LOCAL_CRATE), self.stable_crate_id(LOCAL_CRATE)) } else { let cstore = &*self.cstore_untracked(); (cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate)) @@ -984,7 +1033,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn local_crate_exports_generics(self) -> bool { debug_assert!(self.sess.opts.share_generics()); - self.sess.crate_types().iter().any(|crate_type| { + self.crate_types().iter().any(|crate_type| { match crate_type { CrateType::Executable | CrateType::Staticlib @@ -1036,7 +1085,9 @@ impl<'tcx> TyCtxt<'tcx> { scope_def_id: LocalDefId, ) -> Vec<&'tcx hir::Ty<'tcx>> { let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id); - let Some(hir::FnDecl { output: hir::FnRetTy::Return(hir_output), .. }) = self.hir().fn_decl_by_hir_id(hir_id) else { + let Some(hir::FnDecl { output: hir::FnRetTy::Return(hir_output), .. }) = + self.hir().fn_decl_by_hir_id(hir_id) + else { return vec![]; }; @@ -1058,7 +1109,7 @@ impl<'tcx> TyCtxt<'tcx> { if let Some(hir::FnDecl { output: hir::FnRetTy::Return(hir_output), .. }) = self.hir().fn_decl_by_hir_id(hir_id) && let hir::TyKind::Path(hir::QPath::Resolved( None, - hir::Path { res: hir::def::Res::Def(DefKind::TyAlias, def_id), .. }, )) = hir_output.kind + hir::Path { res: hir::def::Res::Def(DefKind::TyAlias { .. }, def_id), .. }, )) = hir_output.kind && let Some(local_id) = def_id.as_local() && let Some(alias_ty) = self.hir().get_by_def_id(local_id).alias_ty() // it is type alias && let Some(alias_generics) = self.hir().get_by_def_id(local_id).generics() @@ -1071,31 +1122,6 @@ impl<'tcx> TyCtxt<'tcx> { return None; } - pub fn return_type_impl_trait(self, scope_def_id: LocalDefId) -> Option<(Ty<'tcx>, Span)> { - // `type_of()` will fail on these (#55796, #86483), so only allow `fn`s or closures. - match self.hir().get_by_def_id(scope_def_id) { - Node::Item(&hir::Item { kind: ItemKind::Fn(..), .. }) => {} - Node::TraitItem(&hir::TraitItem { kind: TraitItemKind::Fn(..), .. }) => {} - Node::ImplItem(&hir::ImplItem { kind: ImplItemKind::Fn(..), .. }) => {} - Node::Expr(&hir::Expr { kind: ExprKind::Closure { .. }, .. }) => {} - _ => return None, - } - - let ret_ty = self.type_of(scope_def_id).subst_identity(); - match ret_ty.kind() { - ty::FnDef(_, _) => { - let sig = ret_ty.fn_sig(self); - let output = self.erase_late_bound_regions(sig.output()); - output.is_impl_trait().then(|| { - let hir_id = self.hir().local_def_id_to_hir_id(scope_def_id); - let fn_decl = self.hir().fn_decl_by_hir_id(hir_id).unwrap(); - (output, fn_decl.output.span()) - }) - } - _ => None, - } - } - /// Checks if the bound region is in Impl Item. pub fn is_bound_region_in_impl_item(self, suitable_region_binding_scope: LocalDefId) -> bool { let container_id = self.parent(suitable_region_binding_scope.to_def_id()); @@ -1123,7 +1149,7 @@ impl<'tcx> TyCtxt<'tcx> { self, self.lifetimes.re_static, self.type_of(self.require_lang_item(LangItem::PanicLocation, None)) - .subst(self, self.mk_substs(&[self.lifetimes.re_static.into()])), + .instantiate(self, self.mk_args(&[self.lifetimes.re_static.into()])), ) } @@ -1166,7 +1192,7 @@ impl<'tcx> TyCtxt<'tcx> { /// A trait implemented for all `X<'a>` types that can be safely and /// efficiently converted to `X<'tcx>` as long as they are part of the /// provided `TyCtxt<'tcx>`. -/// This can be done, for example, for `Ty<'tcx>` or `SubstsRef<'tcx>` +/// This can be done, for example, for `Ty<'tcx>` or `GenericArgsRef<'tcx>` /// by looking them up in their respective interners. /// /// However, this is still not the best implementation as it does @@ -1232,8 +1258,8 @@ nop_list_lift! {canonical_var_infos; CanonicalVarInfo<'a> => CanonicalVarInfo<'t nop_list_lift! {projs; ProjectionKind => ProjectionKind} nop_list_lift! {bound_variable_kinds; ty::BoundVariableKind => ty::BoundVariableKind} -// This is the impl for `&'a InternalSubsts<'a>`. -nop_list_lift! {substs; GenericArg<'a> => GenericArg<'tcx>} +// This is the impl for `&'a GenericArgs<'a>`. +nop_list_lift! {args; GenericArg<'a> => GenericArg<'tcx>} CloneLiftImpls! { Constness, @@ -1345,7 +1371,7 @@ impl<'tcx> TyCtxt<'tcx> { Foreign )?; - writeln!(fmt, "InternalSubsts interner: #{}", self.0.interners.substs.len())?; + writeln!(fmt, "GenericArgs interner: #{}", self.0.interners.args.len())?; writeln!(fmt, "Region interner: #{}", self.0.interners.region.len())?; writeln!( fmt, @@ -1501,7 +1527,7 @@ macro_rules! slice_interners { // should be used when possible, because it's faster. slice_interners!( const_lists: pub mk_const_list(Const<'tcx>), - substs: pub mk_substs(GenericArg<'tcx>), + args: pub mk_args(GenericArg<'tcx>), type_lists: pub mk_type_list(Ty<'tcx>), canonical_var_infos: pub mk_canonical_var_infos(CanonicalVarInfo<'tcx>), poly_existential_predicates: intern_poly_existential_predicates(PolyExistentialPredicate<'tcx>), @@ -1615,12 +1641,12 @@ impl<'tcx> TyCtxt<'tcx> { } #[inline(always)] - pub(crate) fn check_and_mk_substs( + pub(crate) fn check_and_mk_args( self, _def_id: DefId, - substs: impl IntoIterator<Item: Into<GenericArg<'tcx>>>, - ) -> SubstsRef<'tcx> { - let substs = substs.into_iter().map(Into::into); + args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>, + ) -> GenericArgsRef<'tcx> { + let args = args.into_iter().map(Into::into); #[cfg(debug_assertions)] { let generics = self.generics_of(_def_id); @@ -1636,12 +1662,12 @@ impl<'tcx> TyCtxt<'tcx> { }; assert_eq!( (n, Some(n)), - substs.size_hint(), + args.size_hint(), "wrong number of generic parameters for {_def_id:?}: {:?}", - substs.collect::<Vec<_>>(), + args.collect::<Vec<_>>(), ); } - self.mk_substs_from_iter(substs) + self.mk_args_from_iter(args) } #[inline] @@ -1799,12 +1825,12 @@ impl<'tcx> TyCtxt<'tcx> { T::collect_and_apply(iter, |xs| self.mk_type_list(xs)) } - pub fn mk_substs_from_iter<I, T>(self, iter: I) -> T::Output + pub fn mk_args_from_iter<I, T>(self, iter: I) -> T::Output where I: Iterator<Item = T>, T: CollectAndApply<GenericArg<'tcx>, &'tcx List<GenericArg<'tcx>>>, { - T::collect_and_apply(iter, |xs| self.mk_substs(xs)) + T::collect_and_apply(iter, |xs| self.mk_args(xs)) } pub fn mk_canonical_var_infos_from_iter<I, T>(self, iter: I) -> T::Output @@ -1831,21 +1857,21 @@ impl<'tcx> TyCtxt<'tcx> { T::collect_and_apply(iter, |xs| self.mk_fields(xs)) } - pub fn mk_substs_trait( + pub fn mk_args_trait( self, self_ty: Ty<'tcx>, rest: impl IntoIterator<Item = GenericArg<'tcx>>, - ) -> SubstsRef<'tcx> { - self.mk_substs_from_iter(iter::once(self_ty.into()).chain(rest)) + ) -> GenericArgsRef<'tcx> { + self.mk_args_from_iter(iter::once(self_ty.into()).chain(rest)) } pub fn mk_alias_ty( self, def_id: DefId, - substs: impl IntoIterator<Item: Into<GenericArg<'tcx>>>, + args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>, ) -> ty::AliasTy<'tcx> { - let substs = self.check_and_mk_substs(def_id, substs); - ty::AliasTy { def_id, substs, _use_mk_alias_ty_instead: () } + let args = self.check_and_mk_args(def_id, args); + ty::AliasTy { def_id, args, _use_mk_alias_ty_instead: () } } pub fn mk_bound_variable_kinds_from_iter<I, T>(self, iter: I) -> T::Output @@ -1858,6 +1884,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Emit a lint at `span` from a lint struct (some type that implements `DecorateLint`, /// typically generated by `#[derive(LintDiagnostic)]`). + #[track_caller] pub fn emit_spanned_lint( self, lint: &'static Lint, @@ -1878,6 +1905,7 @@ impl<'tcx> TyCtxt<'tcx> { /// /// [`struct_lint_level`]: rustc_middle::lint::struct_lint_level#decorate-signature #[rustc_lint_diagnostics] + #[track_caller] pub fn struct_span_lint_hir( self, lint: &'static Lint, @@ -1894,6 +1922,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Emit a lint from a lint struct (some type that implements `DecorateLint`, typically /// generated by `#[derive(LintDiagnostic)]`). + #[track_caller] pub fn emit_lint( self, lint: &'static Lint, @@ -1909,6 +1938,7 @@ impl<'tcx> TyCtxt<'tcx> { /// /// [`struct_lint_level`]: rustc_middle::lint::struct_lint_level#decorate-signature #[rustc_lint_diagnostics] + #[track_caller] pub fn struct_lint_node( self, lint: &'static Lint, @@ -1948,6 +1978,84 @@ impl<'tcx> TyCtxt<'tcx> { ) } + /// Given the def-id of an early-bound lifetime on an RPIT corresponding to + /// a duplicated captured lifetime, map it back to the early- or late-bound + /// lifetime of the function from which it originally as captured. If it is + /// a late-bound lifetime, this will represent the liberated (`ReFree`) lifetime + /// of the signature. + // FIXME(RPITIT): if we ever synthesize new lifetimes for RPITITs and not just + // re-use the generics of the opaque, this function will need to be tweaked slightly. + pub fn map_rpit_lifetime_to_fn_lifetime( + self, + mut rpit_lifetime_param_def_id: LocalDefId, + ) -> ty::Region<'tcx> { + debug_assert!( + matches!(self.def_kind(rpit_lifetime_param_def_id), DefKind::LifetimeParam), + "{rpit_lifetime_param_def_id:?} is a {}", + self.def_descr(rpit_lifetime_param_def_id.to_def_id()) + ); + + loop { + let parent = self.local_parent(rpit_lifetime_param_def_id); + let hir::OpaqueTy { lifetime_mapping, .. } = + self.hir().get_by_def_id(parent).expect_item().expect_opaque_ty(); + + let Some((lifetime, _)) = lifetime_mapping + .iter() + .find(|(_, duplicated_param)| *duplicated_param == rpit_lifetime_param_def_id) + else { + bug!("duplicated lifetime param should be present"); + }; + + match self.named_bound_var(lifetime.hir_id) { + Some(resolve_bound_vars::ResolvedArg::EarlyBound(ebv)) => { + let new_parent = self.parent(ebv); + + // If we map to another opaque, then it should be a parent + // of the opaque we mapped from. Continue mapping. + if matches!(self.def_kind(new_parent), DefKind::OpaqueTy) { + debug_assert_eq!(self.parent(parent.to_def_id()), new_parent); + rpit_lifetime_param_def_id = ebv.expect_local(); + continue; + } + + let generics = self.generics_of(new_parent); + return ty::Region::new_early_bound( + self, + ty::EarlyBoundRegion { + def_id: ebv, + index: generics + .param_def_id_to_index(self, ebv) + .expect("early-bound var should be present in fn generics"), + name: self.hir().name(self.local_def_id_to_hir_id(ebv.expect_local())), + }, + ); + } + Some(resolve_bound_vars::ResolvedArg::LateBound(_, _, lbv)) => { + let new_parent = self.parent(lbv); + return ty::Region::new_free( + self, + new_parent, + ty::BoundRegionKind::BrNamed( + lbv, + self.hir().name(self.local_def_id_to_hir_id(lbv.expect_local())), + ), + ); + } + Some(resolve_bound_vars::ResolvedArg::Error(guar)) => { + return ty::Region::new_error(self, guar); + } + _ => { + return ty::Region::new_error_with_message( + self, + lifetime.ident.span, + "cannot resolve lifetime", + ); + } + } + } + } + /// Whether the `def_id` counts as const fn in the current crate, considering all active /// feature gates pub fn is_const_fn(self, def_id: DefId) -> bool { @@ -1980,9 +2088,9 @@ impl<'tcx> TyCtxt<'tcx> { matches!( node, hir::Node::Item(hir::Item { - kind: hir::ItemKind::Impl(hir::Impl { constness: hir::Constness::Const, .. }), + kind: hir::ItemKind::Impl(hir::Impl { generics, .. }), .. - }) + }) if generics.params.iter().any(|p| self.has_attr(p.def_id, sym::rustc_host)) ) } @@ -2002,16 +2110,8 @@ impl<'tcx> TyCtxt<'tcx> { ) } - pub fn lower_impl_trait_in_trait_to_assoc_ty(self) -> bool { - self.sess.opts.unstable_opts.lower_impl_trait_in_trait_to_assoc_ty - } - pub fn is_impl_trait_in_trait(self, def_id: DefId) -> bool { - if self.lower_impl_trait_in_trait_to_assoc_ty() { - self.opt_rpitit_info(def_id).is_some() - } else { - self.def_kind(def_id) == DefKind::ImplTraitPlaceholder - } + self.opt_rpitit_info(def_id).is_some() } /// Named module children from all kinds of items, including imports. diff --git a/compiler/rustc_middle/src/ty/diagnostics.rs b/compiler/rustc_middle/src/ty/diagnostics.rs index a0b17c374..5db9b775a 100644 --- a/compiler/rustc_middle/src/ty/diagnostics.rs +++ b/compiler/rustc_middle/src/ty/diagnostics.rs @@ -1,6 +1,7 @@ //! Diagnostics related methods for `Ty`. use std::borrow::Cow; +use std::fmt::Write; use std::ops::ControlFlow; use crate::ty::{ @@ -71,7 +72,7 @@ impl<'tcx> Ty<'tcx> { /// ADTs with no type arguments. pub fn is_simple_text(self) -> bool { match self.kind() { - Adt(_, substs) => substs.non_erasable_generics().next().is_none(), + Adt(_, args) => args.non_erasable_generics().next().is_none(), Ref(_, ty, _) => ty.is_simple_text(), _ => self.is_simple_ty(), } @@ -126,7 +127,7 @@ pub fn suggest_arbitrary_trait_bound<'tcx>( if constraint.ends_with('>') { constraint = format!("{}, {} = {}>", &constraint[..constraint.len() - 1], name, term); } else { - constraint.push_str(&format!("<{} = {}>", name, term)); + constraint.push_str(&format!("<{name} = {term}>")); } } @@ -274,9 +275,9 @@ pub fn suggest_constraining_type_params<'a>( if span_to_replace.is_some() { constraint.clone() } else if bound_list_non_empty { - format!(" + {}", constraint) + format!(" + {constraint}") } else { - format!(" {}", constraint) + format!(" {constraint}") }, SuggestChangingConstraintsMessage::RestrictBoundFurther, )) @@ -335,10 +336,10 @@ pub fn suggest_constraining_type_params<'a>( // - insert: `, X: Bar` suggestions.push(( generics.tail_span_for_predicate_suggestion(), - constraints - .iter() - .map(|&(constraint, _)| format!(", {}: {}", param_name, constraint)) - .collect::<String>(), + constraints.iter().fold(String::new(), |mut string, &(constraint, _)| { + write!(string, ", {param_name}: {constraint}").unwrap(); + string + }), SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name }, )); continue; @@ -358,7 +359,7 @@ pub fn suggest_constraining_type_params<'a>( // default (`<T=Foo>`), so we suggest adding `where T: Bar`. suggestions.push(( generics.tail_span_for_predicate_suggestion(), - format!(" where {}: {}", param_name, constraint), + format!(" where {param_name}: {constraint}"), SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name }, )); continue; @@ -371,7 +372,7 @@ pub fn suggest_constraining_type_params<'a>( if let Some(colon_span) = param.colon_span { suggestions.push(( colon_span.shrink_to_hi(), - format!(" {}", constraint), + format!(" {constraint}"), SuggestChangingConstraintsMessage::RestrictType { ty: param_name }, )); continue; @@ -383,7 +384,7 @@ pub fn suggest_constraining_type_params<'a>( // - help: consider restricting this type parameter with `T: Foo` suggestions.push(( param.span.shrink_to_hi(), - format!(": {}", constraint), + format!(": {constraint}"), SuggestChangingConstraintsMessage::RestrictType { ty: param_name }, )); } @@ -401,10 +402,10 @@ pub fn suggest_constraining_type_params<'a>( Cow::from("consider further restricting this bound") } SuggestChangingConstraintsMessage::RestrictType { ty } => { - Cow::from(format!("consider restricting type parameter `{}`", ty)) + Cow::from(format!("consider restricting type parameter `{ty}`")) } SuggestChangingConstraintsMessage::RestrictTypeFurther { ty } => { - Cow::from(format!("consider further restricting type parameter `{}`", ty)) + Cow::from(format!("consider further restricting type parameter `{ty}`")) } SuggestChangingConstraintsMessage::RemoveMaybeUnsized => { Cow::from("consider removing the `?Sized` bound to make the type parameter `Sized`") @@ -491,8 +492,8 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for IsSuggestableVisitor<'tcx> { Alias(Opaque, AliasTy { def_id, .. }) => { let parent = self.tcx.parent(def_id); - let parent_ty = self.tcx.type_of(parent).subst_identity(); - if let DefKind::TyAlias | DefKind::AssocTy = self.tcx.def_kind(parent) + let parent_ty = self.tcx.type_of(parent).instantiate_identity(); + if let DefKind::TyAlias { .. } | DefKind::AssocTy = self.tcx.def_kind(parent) && let Alias(Opaque, AliasTy { def_id: parent_opaque_def_id, .. }) = *parent_ty.kind() && parent_opaque_def_id == def_id { @@ -558,8 +559,8 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for MakeSuggestableFolder<'tcx> { let t = match *t.kind() { Infer(InferTy::TyVar(_)) if self.infer_suggestable => t, - FnDef(def_id, substs) => { - Ty::new_fn_ptr(self.tcx, self.tcx.fn_sig(def_id).subst(self.tcx, substs)) + FnDef(def_id, args) => { + Ty::new_fn_ptr(self.tcx, self.tcx.fn_sig(def_id).instantiate(self.tcx, args)) } // FIXME(compiler-errors): We could replace these with infer, I guess. @@ -575,8 +576,8 @@ impl<'tcx> FallibleTypeFolder<TyCtxt<'tcx>> for MakeSuggestableFolder<'tcx> { Alias(Opaque, AliasTy { def_id, .. }) => { let parent = self.tcx.parent(def_id); - let parent_ty = self.tcx.type_of(parent).subst_identity(); - if let hir::def::DefKind::TyAlias | hir::def::DefKind::AssocTy = self.tcx.def_kind(parent) + let parent_ty = self.tcx.type_of(parent).instantiate_identity(); + if let hir::def::DefKind::TyAlias { .. } | hir::def::DefKind::AssocTy = self.tcx.def_kind(parent) && let Alias(Opaque, AliasTy { def_id: parent_opaque_def_id, .. }) = *parent_ty.kind() && parent_opaque_def_id == def_id { diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs index c794c3fad..bf6f082c2 100644 --- a/compiler/rustc_middle/src/ty/error.rs +++ b/compiler/rustc_middle/src/ty/error.rs @@ -90,9 +90,9 @@ impl<'tcx> TypeError<'tcx> { // A naive approach to making sure that we're not reporting silly errors such as: // (expected closure, found closure). if expected == found { - format!("expected {}, found a different {}", expected, found) + format!("expected {expected}, found a different {found}") } else { - format!("expected {}, found {}", expected, found) + format!("expected {expected}, found {found}") } } @@ -131,7 +131,7 @@ impl<'tcx> TypeError<'tcx> { ) .into(), ArgCount => "incorrect number of function parameters".into(), - FieldMisMatch(adt, field) => format!("field type mismatch: {}.{}", adt, field).into(), + FieldMisMatch(adt, field) => format!("field type mismatch: {adt}.{field}").into(), RegionsDoesNotOutlive(..) => "lifetime mismatch".into(), // Actually naming the region here is a bit confusing because context is lacking RegionsInsufficientlyPolymorphic(..) => { @@ -164,7 +164,7 @@ impl<'tcx> TypeError<'tcx> { ty::IntVarValue::IntType(ty) => ty.name_str(), ty::IntVarValue::UintType(ty) => ty.name_str(), }; - format!("expected `{}`, found `{}`", expected, found).into() + format!("expected `{expected}`, found `{found}`").into() } FloatMismatch(ref values) => format!( "expected `{}`, found `{}`", @@ -339,12 +339,17 @@ impl<'tcx> TyCtxt<'tcx> { } pub fn short_ty_string(self, ty: Ty<'tcx>) -> (String, Option<PathBuf>) { - let width = self.sess.diagnostic_width(); - let length_limit = width.saturating_sub(30); let regular = FmtPrinter::new(self, hir::def::Namespace::TypeNS) .pretty_print_type(ty) .expect("could not write to `String`") .into_buffer(); + + if !self.sess.opts.unstable_opts.write_long_types_to_disk { + return (regular, None); + } + + let width = self.sess.diagnostic_width(); + let length_limit = width.saturating_sub(30); if regular.len() <= width { return (regular, None); } diff --git a/compiler/rustc_middle/src/ty/fast_reject.rs b/compiler/rustc_middle/src/ty/fast_reject.rs index 76f61d9ac..668aa4521 100644 --- a/compiler/rustc_middle/src/ty/fast_reject.rs +++ b/compiler/rustc_middle/src/ty/fast_reject.rs @@ -1,40 +1,38 @@ use crate::mir::Mutability; -use crate::ty::subst::GenericArgKind; -use crate::ty::{self, SubstsRef, Ty, TyCtxt, TypeVisitableExt}; +use crate::ty::GenericArgKind; +use crate::ty::{self, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt}; use rustc_hir::def_id::DefId; use std::fmt::Debug; use std::hash::Hash; use std::iter; -use self::SimplifiedType::*; - /// See `simplify_type`. #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, HashStable)] pub enum SimplifiedType { - BoolSimplifiedType, - CharSimplifiedType, - IntSimplifiedType(ty::IntTy), - UintSimplifiedType(ty::UintTy), - FloatSimplifiedType(ty::FloatTy), - AdtSimplifiedType(DefId), - ForeignSimplifiedType(DefId), - StrSimplifiedType, - ArraySimplifiedType, - SliceSimplifiedType, - RefSimplifiedType(Mutability), - PtrSimplifiedType(Mutability), - NeverSimplifiedType, - TupleSimplifiedType(usize), + Bool, + Char, + Int(ty::IntTy), + Uint(ty::UintTy), + Float(ty::FloatTy), + Adt(DefId), + Foreign(DefId), + Str, + Array, + Slice, + Ref(Mutability), + Ptr(Mutability), + Never, + Tuple(usize), /// A trait object, all of whose components are markers /// (e.g., `dyn Send + Sync`). - MarkerTraitObjectSimplifiedType, - TraitSimplifiedType(DefId), - ClosureSimplifiedType(DefId), - GeneratorSimplifiedType(DefId), - GeneratorWitnessSimplifiedType(usize), - GeneratorWitnessMIRSimplifiedType(DefId), - FunctionSimplifiedType(usize), - PlaceholderSimplifiedType, + MarkerTraitObject, + Trait(DefId), + Closure(DefId), + Generator(DefId), + GeneratorWitness(usize), + GeneratorWitnessMIR(DefId), + Function(usize), + Placeholder, } /// Generic parameters are pretty much just bound variables, e.g. @@ -64,6 +62,9 @@ pub enum TreatParams { /// correct mode for *lookup*, as during candidate selection. /// /// N.B. during deep rejection, this acts identically to `ForLookup`. + /// + /// FIXME(-Ztrait-solver=next): Remove this variant and cleanup + /// the code. NextSolverLookup, } @@ -110,34 +111,36 @@ pub fn simplify_type<'tcx>( treat_params: TreatParams, ) -> Option<SimplifiedType> { match *ty.kind() { - ty::Bool => Some(BoolSimplifiedType), - ty::Char => Some(CharSimplifiedType), - ty::Int(int_type) => Some(IntSimplifiedType(int_type)), - ty::Uint(uint_type) => Some(UintSimplifiedType(uint_type)), - ty::Float(float_type) => Some(FloatSimplifiedType(float_type)), - ty::Adt(def, _) => Some(AdtSimplifiedType(def.did())), - ty::Str => Some(StrSimplifiedType), - ty::Array(..) => Some(ArraySimplifiedType), - ty::Slice(..) => Some(SliceSimplifiedType), - ty::RawPtr(ptr) => Some(PtrSimplifiedType(ptr.mutbl)), + ty::Bool => Some(SimplifiedType::Bool), + ty::Char => Some(SimplifiedType::Char), + ty::Int(int_type) => Some(SimplifiedType::Int(int_type)), + ty::Uint(uint_type) => Some(SimplifiedType::Uint(uint_type)), + ty::Float(float_type) => Some(SimplifiedType::Float(float_type)), + ty::Adt(def, _) => Some(SimplifiedType::Adt(def.did())), + ty::Str => Some(SimplifiedType::Str), + ty::Array(..) => Some(SimplifiedType::Array), + ty::Slice(..) => Some(SimplifiedType::Slice), + ty::RawPtr(ptr) => Some(SimplifiedType::Ptr(ptr.mutbl)), ty::Dynamic(trait_info, ..) => match trait_info.principal_def_id() { Some(principal_def_id) if !tcx.trait_is_auto(principal_def_id) => { - Some(TraitSimplifiedType(principal_def_id)) + Some(SimplifiedType::Trait(principal_def_id)) } - _ => Some(MarkerTraitObjectSimplifiedType), + _ => Some(SimplifiedType::MarkerTraitObject), }, - ty::Ref(_, _, mutbl) => Some(RefSimplifiedType(mutbl)), - ty::FnDef(def_id, _) | ty::Closure(def_id, _) => Some(ClosureSimplifiedType(def_id)), - ty::Generator(def_id, _, _) => Some(GeneratorSimplifiedType(def_id)), - ty::GeneratorWitness(tys) => Some(GeneratorWitnessSimplifiedType(tys.skip_binder().len())), - ty::GeneratorWitnessMIR(def_id, _) => Some(GeneratorWitnessMIRSimplifiedType(def_id)), - ty::Never => Some(NeverSimplifiedType), - ty::Tuple(tys) => Some(TupleSimplifiedType(tys.len())), - ty::FnPtr(f) => Some(FunctionSimplifiedType(f.skip_binder().inputs().len())), - ty::Placeholder(..) => Some(PlaceholderSimplifiedType), + ty::Ref(_, _, mutbl) => Some(SimplifiedType::Ref(mutbl)), + ty::FnDef(def_id, _) | ty::Closure(def_id, _) => Some(SimplifiedType::Closure(def_id)), + ty::Generator(def_id, _, _) => Some(SimplifiedType::Generator(def_id)), + ty::GeneratorWitness(tys) => { + Some(SimplifiedType::GeneratorWitness(tys.skip_binder().len())) + } + ty::GeneratorWitnessMIR(def_id, _) => Some(SimplifiedType::GeneratorWitnessMIR(def_id)), + ty::Never => Some(SimplifiedType::Never), + ty::Tuple(tys) => Some(SimplifiedType::Tuple(tys.len())), + ty::FnPtr(f) => Some(SimplifiedType::Function(f.skip_binder().inputs().len())), + ty::Placeholder(..) => Some(SimplifiedType::Placeholder), ty::Param(_) => match treat_params { TreatParams::ForLookup | TreatParams::NextSolverLookup => { - Some(PlaceholderSimplifiedType) + Some(SimplifiedType::Placeholder) } TreatParams::AsCandidateKey => None, }, @@ -147,11 +150,13 @@ pub fn simplify_type<'tcx>( // // We will have to be careful with lazy normalization here. // FIXME(lazy_normalization): This is probably not right... - TreatParams::ForLookup if !ty.has_non_region_infer() => Some(PlaceholderSimplifiedType), - TreatParams::NextSolverLookup => Some(PlaceholderSimplifiedType), + TreatParams::ForLookup if !ty.has_non_region_infer() => { + Some(SimplifiedType::Placeholder) + } + TreatParams::NextSolverLookup => Some(SimplifiedType::Placeholder), TreatParams::ForLookup | TreatParams::AsCandidateKey => None, }, - ty::Foreign(def_id) => Some(ForeignSimplifiedType(def_id)), + ty::Foreign(def_id) => Some(SimplifiedType::Foreign(def_id)), ty::Bound(..) | ty::Infer(_) | ty::Error(_) => None, } } @@ -159,12 +164,12 @@ pub fn simplify_type<'tcx>( impl SimplifiedType { pub fn def(self) -> Option<DefId> { match self { - AdtSimplifiedType(d) - | ForeignSimplifiedType(d) - | TraitSimplifiedType(d) - | ClosureSimplifiedType(d) - | GeneratorSimplifiedType(d) - | GeneratorWitnessMIRSimplifiedType(d) => Some(d), + SimplifiedType::Adt(d) + | SimplifiedType::Foreign(d) + | SimplifiedType::Trait(d) + | SimplifiedType::Closure(d) + | SimplifiedType::Generator(d) + | SimplifiedType::GeneratorWitnessMIR(d) => Some(d), _ => None, } } @@ -188,12 +193,12 @@ pub struct DeepRejectCtxt { } impl DeepRejectCtxt { - pub fn substs_refs_may_unify<'tcx>( + pub fn args_refs_may_unify<'tcx>( self, - obligation_substs: SubstsRef<'tcx>, - impl_substs: SubstsRef<'tcx>, + obligation_args: GenericArgsRef<'tcx>, + impl_args: GenericArgsRef<'tcx>, ) -> bool { - iter::zip(obligation_substs, impl_substs).all(|(obl, imp)| { + iter::zip(obligation_args, impl_args).all(|(obl, imp)| { match (obl.unpack(), imp.unpack()) { // We don't fast reject based on regions for now. (GenericArgKind::Lifetime(_), GenericArgKind::Lifetime(_)) => true, @@ -258,9 +263,9 @@ impl DeepRejectCtxt { } _ => false, }, - ty::Adt(obl_def, obl_substs) => match k { - &ty::Adt(impl_def, impl_substs) => { - obl_def == impl_def && self.substs_refs_may_unify(obl_substs, impl_substs) + ty::Adt(obl_def, obl_args) => match k { + &ty::Adt(impl_def, impl_args) => { + obl_def == impl_def && self.args_refs_may_unify(obl_args, impl_args) } _ => false, }, diff --git a/compiler/rustc_middle/src/ty/flags.rs b/compiler/rustc_middle/src/ty/flags.rs index ff3917947..bbd4a6233 100644 --- a/compiler/rustc_middle/src/ty/flags.rs +++ b/compiler/rustc_middle/src/ty/flags.rs @@ -1,5 +1,5 @@ -use crate::ty::subst::{GenericArg, GenericArgKind}; use crate::ty::{self, InferConst, Ty, TypeFlags}; +use crate::ty::{GenericArg, GenericArgKind}; use std::slice; #[derive(Debug)] @@ -105,48 +105,48 @@ impl FlagComputation { self.add_flags(TypeFlags::STILL_FURTHER_SPECIALIZABLE); } - ty::Generator(_, substs, _) => { - let substs = substs.as_generator(); + ty::Generator(_, args, _) => { + let args = args.as_generator(); let should_remove_further_specializable = !self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE); - self.add_substs(substs.parent_substs()); + self.add_args(args.parent_args()); if should_remove_further_specializable { self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE; } - self.add_ty(substs.resume_ty()); - self.add_ty(substs.return_ty()); - self.add_ty(substs.witness()); - self.add_ty(substs.yield_ty()); - self.add_ty(substs.tupled_upvars_ty()); + self.add_ty(args.resume_ty()); + self.add_ty(args.return_ty()); + self.add_ty(args.witness()); + self.add_ty(args.yield_ty()); + self.add_ty(args.tupled_upvars_ty()); } &ty::GeneratorWitness(ts) => { self.bound_computation(ts, |flags, ts| flags.add_tys(ts)); } - ty::GeneratorWitnessMIR(_, substs) => { + ty::GeneratorWitnessMIR(_, args) => { let should_remove_further_specializable = !self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE); - self.add_substs(substs); + self.add_args(args); if should_remove_further_specializable { self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE; } self.add_flags(TypeFlags::HAS_TY_GENERATOR); } - &ty::Closure(_, substs) => { - let substs = substs.as_closure(); + &ty::Closure(_, args) => { + let args = args.as_closure(); let should_remove_further_specializable = !self.flags.contains(TypeFlags::STILL_FURTHER_SPECIALIZABLE); - self.add_substs(substs.parent_substs()); + self.add_args(args.parent_args()); if should_remove_further_specializable { self.flags -= TypeFlags::STILL_FURTHER_SPECIALIZABLE; } - self.add_ty(substs.sig_as_fn_ptr_ty()); - self.add_ty(substs.kind_ty()); - self.add_ty(substs.tupled_upvars_ty()); + self.add_ty(args.sig_as_fn_ptr_ty()); + self.add_ty(args.kind_ty()); + self.add_ty(args.tupled_upvars_ty()); } &ty::Bound(debruijn, _) => { @@ -172,8 +172,8 @@ impl FlagComputation { } } - &ty::Adt(_, substs) => { - self.add_substs(substs); + &ty::Adt(_, args) => { + self.add_args(args); } &ty::Alias(kind, data) => { @@ -189,7 +189,7 @@ impl FlagComputation { &ty::Dynamic(obj, r, _) => { for predicate in obj.iter() { self.bound_computation(predicate, |computation, predicate| match predicate { - ty::ExistentialPredicate::Trait(tr) => computation.add_substs(tr.substs), + ty::ExistentialPredicate::Trait(tr) => computation.add_args(tr.args), ty::ExistentialPredicate::Projection(p) => { computation.add_existential_projection(&p); } @@ -220,8 +220,8 @@ impl FlagComputation { self.add_tys(types); } - &ty::FnDef(_, substs) => { - self.add_substs(substs); + &ty::FnDef(_, args) => { + self.add_args(args); } &ty::FnPtr(fn_sig) => self.bound_computation(fn_sig, |computation, fn_sig| { @@ -238,7 +238,7 @@ impl FlagComputation { fn add_predicate_atom(&mut self, atom: ty::PredicateKind<'_>) { match atom { ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) => { - self.add_substs(trait_pred.trait_ref.substs); + self.add_args(trait_pred.trait_ref.args); } ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate( a, @@ -274,11 +274,11 @@ impl FlagComputation { self.add_term(term); } ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => { - self.add_substs(slice::from_ref(&arg)); + self.add_args(slice::from_ref(&arg)); } ty::PredicateKind::ObjectSafe(_def_id) => {} - ty::PredicateKind::ClosureKind(_def_id, substs, _kind) => { - self.add_substs(substs); + ty::PredicateKind::ClosureKind(_def_id, args, _kind) => { + self.add_args(args); } ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(uv)) => { self.add_const(uv); @@ -317,7 +317,7 @@ impl FlagComputation { self.add_ty(c.ty()); match c.kind() { ty::ConstKind::Unevaluated(uv) => { - self.add_substs(uv.substs); + self.add_args(uv.args); self.add_flags(TypeFlags::HAS_CT_PROJECTION); } ty::ConstKind::Infer(infer) => { @@ -365,7 +365,7 @@ impl FlagComputation { } fn add_existential_projection(&mut self, projection: &ty::ExistentialProjection<'_>) { - self.add_substs(projection.substs); + self.add_args(projection.args); match projection.term.unpack() { ty::TermKind::Ty(ty) => self.add_ty(ty), ty::TermKind::Const(ct) => self.add_const(ct), @@ -373,11 +373,11 @@ impl FlagComputation { } fn add_alias_ty(&mut self, alias_ty: ty::AliasTy<'_>) { - self.add_substs(alias_ty.substs); + self.add_args(alias_ty.args); } - fn add_substs(&mut self, substs: &[GenericArg<'_>]) { - for kind in substs { + fn add_args(&mut self, args: &[GenericArg<'_>]) { + for kind in args { match kind.unpack() { GenericArgKind::Type(ty) => self.add_ty(ty), GenericArgKind::Lifetime(lt) => self.add_region(lt), diff --git a/compiler/rustc_middle/src/ty/subst.rs b/compiler/rustc_middle/src/ty/generic_args.rs index 4d5f5b865..97dab5cb4 100644 --- a/compiler/rustc_middle/src/ty/subst.rs +++ b/compiler/rustc_middle/src/ty/generic_args.rs @@ -1,8 +1,8 @@ -// Type substitutions. +// Generic arguments. use crate::ty::codec::{TyDecoder, TyEncoder}; use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder, TypeSuperFoldable}; -use crate::ty::sty::{ClosureSubsts, GeneratorSubsts, InlineConstSubsts}; +use crate::ty::sty::{ClosureArgs, GeneratorArgs, InlineConstArgs}; use crate::ty::visit::{TypeVisitable, TypeVisitableExt, TypeVisitor}; use crate::ty::{self, Lift, List, ParamConst, Ty, TyCtxt}; @@ -17,7 +17,6 @@ use smallvec::SmallVec; use core::intrinsics; use std::cmp::Ordering; -use std::fmt; use std::marker::PhantomData; use std::mem; use std::num::NonZeroUsize; @@ -80,16 +79,6 @@ impl<'tcx> GenericArgKind<'tcx> { } } -impl<'tcx> fmt::Debug for GenericArg<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - match self.unpack() { - GenericArgKind::Lifetime(lt) => lt.fmt(f), - GenericArgKind::Type(ty) => ty.fmt(f), - GenericArgKind::Const(ct) => ct.fmt(f), - } - } -} - impl<'tcx> Ord for GenericArg<'tcx> { fn cmp(&self, other: &GenericArg<'tcx>) -> Ordering { self.unpack().cmp(&other.unpack()) @@ -185,7 +174,7 @@ impl<'tcx> GenericArg<'tcx> { } /// Unpack the `GenericArg` as a type when it is known certainly to be a type. - /// This is true in cases where `Substs` is used in places where the kinds are known + /// This is true in cases where `GenericArgs` is used in places where the kinds are known /// to be limited (e.g. in tuples, where the only parameters are type parameters). pub fn expect_ty(self) -> Ty<'tcx> { self.as_type().unwrap_or_else(|| bug!("expected a type, but found another kind")) @@ -252,13 +241,13 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for GenericArg<'tcx> { } } -/// List of generic arguments that are gonna be used to substitute generic parameters. -pub type InternalSubsts<'tcx> = List<GenericArg<'tcx>>; +/// List of generic arguments that are gonna be used to replace generic parameters. +pub type GenericArgs<'tcx> = List<GenericArg<'tcx>>; -pub type SubstsRef<'tcx> = &'tcx InternalSubsts<'tcx>; +pub type GenericArgsRef<'tcx> = &'tcx GenericArgs<'tcx>; -impl<'tcx> InternalSubsts<'tcx> { - /// Converts substs to a type list. +impl<'tcx> GenericArgs<'tcx> { + /// Converts generic args to a type list. /// /// # Panics /// @@ -266,66 +255,71 @@ impl<'tcx> InternalSubsts<'tcx> { pub fn into_type_list(&self, tcx: TyCtxt<'tcx>) -> &'tcx List<Ty<'tcx>> { tcx.mk_type_list_from_iter(self.iter().map(|arg| match arg.unpack() { GenericArgKind::Type(ty) => ty, - _ => bug!("`into_type_list` called on substs with non-types"), + _ => bug!("`into_type_list` called on generic arg with non-types"), })) } - /// Interpret these substitutions as the substitutions of a closure type. - /// Closure substitutions have a particular structure controlled by the + /// Interpret these generic args as the args of a closure type. + /// Closure args have a particular structure controlled by the /// compiler that encodes information like the signature and closure kind; - /// see `ty::ClosureSubsts` struct for more comments. - pub fn as_closure(&'tcx self) -> ClosureSubsts<'tcx> { - ClosureSubsts { substs: self } + /// see `ty::ClosureArgs` struct for more comments. + pub fn as_closure(&'tcx self) -> ClosureArgs<'tcx> { + ClosureArgs { args: self } } - /// Interpret these substitutions as the substitutions of a generator type. - /// Generator substitutions have a particular structure controlled by the + /// Interpret these generic args as the args of a generator type. + /// Generator args have a particular structure controlled by the /// compiler that encodes information like the signature and generator kind; - /// see `ty::GeneratorSubsts` struct for more comments. - pub fn as_generator(&'tcx self) -> GeneratorSubsts<'tcx> { - GeneratorSubsts { substs: self } + /// see `ty::GeneratorArgs` struct for more comments. + pub fn as_generator(&'tcx self) -> GeneratorArgs<'tcx> { + GeneratorArgs { args: self } } - /// Interpret these substitutions as the substitutions of an inline const. - /// Inline const substitutions have a particular structure controlled by the + /// Interpret these generic args as the args of an inline const. + /// Inline const args have a particular structure controlled by the /// compiler that encodes information like the inferred type; - /// see `ty::InlineConstSubsts` struct for more comments. - pub fn as_inline_const(&'tcx self) -> InlineConstSubsts<'tcx> { - InlineConstSubsts { substs: self } + /// see `ty::InlineConstArgs` struct for more comments. + pub fn as_inline_const(&'tcx self) -> InlineConstArgs<'tcx> { + InlineConstArgs { args: self } } - /// Creates an `InternalSubsts` that maps each generic parameter to itself. - pub fn identity_for_item(tcx: TyCtxt<'tcx>, def_id: impl Into<DefId>) -> SubstsRef<'tcx> { + /// Creates an `GenericArgs` that maps each generic parameter to itself. + pub fn identity_for_item(tcx: TyCtxt<'tcx>, def_id: impl Into<DefId>) -> GenericArgsRef<'tcx> { Self::for_item(tcx, def_id.into(), |param, _| tcx.mk_param_from_def(param)) } - /// Creates an `InternalSubsts` for generic parameter definitions, + /// Creates an `GenericArgs` for generic parameter definitions, /// by calling closures to obtain each kind. - /// The closures get to observe the `InternalSubsts` as they're + /// The closures get to observe the `GenericArgs` as they're /// being built, which can be used to correctly - /// substitute defaults of generic parameters. - pub fn for_item<F>(tcx: TyCtxt<'tcx>, def_id: DefId, mut mk_kind: F) -> SubstsRef<'tcx> + /// replace defaults of generic parameters. + pub fn for_item<F>(tcx: TyCtxt<'tcx>, def_id: DefId, mut mk_kind: F) -> GenericArgsRef<'tcx> where F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>, { let defs = tcx.generics_of(def_id); let count = defs.count(); - let mut substs = SmallVec::with_capacity(count); - Self::fill_item(&mut substs, tcx, defs, &mut mk_kind); - tcx.mk_substs(&substs) + let mut args = SmallVec::with_capacity(count); + Self::fill_item(&mut args, tcx, defs, &mut mk_kind); + tcx.mk_args(&args) } - pub fn extend_to<F>(&self, tcx: TyCtxt<'tcx>, def_id: DefId, mut mk_kind: F) -> SubstsRef<'tcx> + pub fn extend_to<F>( + &self, + tcx: TyCtxt<'tcx>, + def_id: DefId, + mut mk_kind: F, + ) -> GenericArgsRef<'tcx> where F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>, { - Self::for_item(tcx, def_id, |param, substs| { - self.get(param.index as usize).cloned().unwrap_or_else(|| mk_kind(param, substs)) + Self::for_item(tcx, def_id, |param, args| { + self.get(param.index as usize).cloned().unwrap_or_else(|| mk_kind(param, args)) }) } pub fn fill_item<F>( - substs: &mut SmallVec<[GenericArg<'tcx>; 8]>, + args: &mut SmallVec<[GenericArg<'tcx>; 8]>, tcx: TyCtxt<'tcx>, defs: &ty::Generics, mk_kind: &mut F, @@ -334,38 +328,38 @@ impl<'tcx> InternalSubsts<'tcx> { { if let Some(def_id) = defs.parent { let parent_defs = tcx.generics_of(def_id); - Self::fill_item(substs, tcx, parent_defs, mk_kind); + Self::fill_item(args, tcx, parent_defs, mk_kind); } - Self::fill_single(substs, defs, mk_kind) + Self::fill_single(args, defs, mk_kind) } pub fn fill_single<F>( - substs: &mut SmallVec<[GenericArg<'tcx>; 8]>, + args: &mut SmallVec<[GenericArg<'tcx>; 8]>, defs: &ty::Generics, mk_kind: &mut F, ) where F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>, { - substs.reserve(defs.params.len()); + args.reserve(defs.params.len()); for param in &defs.params { - let kind = mk_kind(param, substs); - assert_eq!(param.index as usize, substs.len(), "{substs:#?}, {defs:#?}"); - substs.push(kind); + let kind = mk_kind(param, args); + assert_eq!(param.index as usize, args.len(), "{args:#?}, {defs:#?}"); + args.push(kind); } } - // Extend an `original_substs` list to the full number of substs expected by `def_id`, + // Extend an `original_args` list to the full number of args expected by `def_id`, // filling in the missing parameters with error ty/ct or 'static regions. pub fn extend_with_error( tcx: TyCtxt<'tcx>, def_id: DefId, - original_substs: &[GenericArg<'tcx>], - ) -> SubstsRef<'tcx> { - ty::InternalSubsts::for_item(tcx, def_id, |def, substs| { - if let Some(subst) = original_substs.get(def.index as usize) { - *subst + original_args: &[GenericArg<'tcx>], + ) -> GenericArgsRef<'tcx> { + ty::GenericArgs::for_item(tcx, def_id, |def, args| { + if let Some(arg) = original_args.get(def.index as usize) { + *arg } else { - def.to_error(tcx, substs) + def.to_error(tcx, args) } }) } @@ -421,9 +415,9 @@ impl<'tcx> InternalSubsts<'tcx> { self.type_at(def.index as usize).into() } - /// Transform from substitutions for a child of `source_ancestor` - /// (e.g., a trait or impl) to substitutions for the same child - /// in a different item, with `target_substs` as the base for + /// Transform from generic args for a child of `source_ancestor` + /// (e.g., a trait or impl) to args for the same child + /// in a different item, with `target_args` as the base for /// the target impl/trait, with the source child-specific /// parameters (e.g., method parameters) on top of that base. /// @@ -434,23 +428,23 @@ impl<'tcx> InternalSubsts<'tcx> { /// impl<U> X<U> for U { fn f<V>() {} } /// ``` /// - /// * If `self` is `[Self, S, T]`: the identity substs of `f` in the trait. + /// * If `self` is `[Self, S, T]`: the identity args of `f` in the trait. /// * If `source_ancestor` is the def_id of the trait. - /// * If `target_substs` is `[U]`, the substs for the impl. - /// * Then we will return `[U, T]`, the subst for `f` in the impl that + /// * If `target_args` is `[U]`, the args for the impl. + /// * Then we will return `[U, T]`, the arg for `f` in the impl that /// are needed for it to match the trait. pub fn rebase_onto( &self, tcx: TyCtxt<'tcx>, source_ancestor: DefId, - target_substs: SubstsRef<'tcx>, - ) -> SubstsRef<'tcx> { + target_args: GenericArgsRef<'tcx>, + ) -> GenericArgsRef<'tcx> { let defs = tcx.generics_of(source_ancestor); - tcx.mk_substs_from_iter(target_substs.iter().chain(self.iter().skip(defs.params.len()))) + tcx.mk_args_from_iter(target_args.iter().chain(self.iter().skip(defs.params.len()))) } - pub fn truncate_to(&self, tcx: TyCtxt<'tcx>, generics: &ty::Generics) -> SubstsRef<'tcx> { - tcx.mk_substs_from_iter(self.iter().take(generics.count())) + pub fn truncate_to(&self, tcx: TyCtxt<'tcx>, generics: &ty::Generics) -> GenericArgsRef<'tcx> { + tcx.mk_args_from_iter(self.iter().take(generics.count())) } pub fn host_effect_param(&'tcx self) -> Option<ty::Const<'tcx>> { @@ -458,7 +452,7 @@ impl<'tcx> InternalSubsts<'tcx> { } } -impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for SubstsRef<'tcx> { +impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for GenericArgsRef<'tcx> { fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>( self, folder: &mut F, @@ -467,16 +461,12 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for SubstsRef<'tcx> { // common length lists, to avoid the overhead of `SmallVec` creation. // The match arms are in order of frequency. The 1, 2, and 0 cases are // typically hit in 90--99.99% of cases. When folding doesn't change - // the substs, it's faster to reuse the existing substs rather than - // calling `mk_substs`. + // the args, it's faster to reuse the existing args rather than + // calling `mk_args`. match self.len() { 1 => { let param0 = self[0].try_fold_with(folder)?; - if param0 == self[0] { - Ok(self) - } else { - Ok(folder.interner().mk_substs(&[param0])) - } + if param0 == self[0] { Ok(self) } else { Ok(folder.interner().mk_args(&[param0])) } } 2 => { let param0 = self[0].try_fold_with(folder)?; @@ -484,11 +474,11 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for SubstsRef<'tcx> { if param0 == self[0] && param1 == self[1] { Ok(self) } else { - Ok(folder.interner().mk_substs(&[param0, param1])) + Ok(folder.interner().mk_args(&[param0, param1])) } } 0 => Ok(self), - _ => ty::util::fold_list(self, folder, |tcx, v| tcx.mk_substs(v)), + _ => ty::util::fold_list(self, folder, |tcx, v| tcx.mk_args(v)), } } } @@ -498,7 +488,7 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<Ty<'tcx>> { self, folder: &mut F, ) -> Result<Self, F::Error> { - // This code is fairly hot, though not as hot as `SubstsRef`. + // This code is fairly hot, though not as hot as `GenericArgsRef`. // // When compiling stage 2, I get the following results: // @@ -536,18 +526,18 @@ impl<'tcx, T: TypeVisitable<TyCtxt<'tcx>>> TypeVisitable<TyCtxt<'tcx>> for &'tcx } /// Similar to [`super::Binder`] except that it tracks early bound generics, i.e. `struct Foo<T>(T)` -/// needs `T` substituted immediately. This type primarily exists to avoid forgetting to call -/// `subst`. +/// needs `T` instantiated immediately. This type primarily exists to avoid forgetting to call +/// `instantiate`. /// -/// If you don't have anything to `subst`, you may be looking for -/// [`subst_identity`](EarlyBinder::subst_identity) or [`skip_binder`](EarlyBinder::skip_binder). +/// If you don't have anything to `instantiate`, you may be looking for +/// [`instantiate_identity`](EarlyBinder::instantiate_identity) or [`skip_binder`](EarlyBinder::skip_binder). #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] #[derive(Encodable, Decodable, HashStable)] pub struct EarlyBinder<T> { value: T, } -/// For early binders, you should first call `subst` before using any visitors. +/// For early binders, you should first call `instantiate` before using any visitors. impl<'tcx, T> !TypeFoldable<TyCtxt<'tcx>> for ty::EarlyBinder<T> {} impl<'tcx, T> !TypeVisitable<TyCtxt<'tcx>> for ty::EarlyBinder<T> {} @@ -591,7 +581,7 @@ impl<T> EarlyBinder<T> { /// This can be used to extract data that does not depend on generic parameters /// (e.g., getting the `DefId` of the inner value or getting the number of /// arguments of an `FnSig`). Otherwise, consider using - /// [`subst_identity`](EarlyBinder::subst_identity). + /// [`instantiate_identity`](EarlyBinder::instantiate_identity). /// /// To skip the binder on `x: &EarlyBinder<T>` to obtain `&T`, leverage /// [`EarlyBinder::as_ref`](EarlyBinder::as_ref): `x.as_ref().skip_binder()`. @@ -620,35 +610,35 @@ impl<'tcx, 's, I: IntoIterator> EarlyBinder<I> where I::Item: TypeFoldable<TyCtxt<'tcx>>, { - pub fn subst_iter( + pub fn iter_instantiated( self, tcx: TyCtxt<'tcx>, - substs: &'s [GenericArg<'tcx>], - ) -> SubstIter<'s, 'tcx, I> { - SubstIter { it: self.value.into_iter(), tcx, substs } + args: &'s [GenericArg<'tcx>], + ) -> IterInstantiated<'s, 'tcx, I> { + IterInstantiated { it: self.value.into_iter(), tcx, args } } - /// Similar to [`subst_identity`](EarlyBinder::subst_identity), + /// Similar to [`instantiate_identity`](EarlyBinder::instantiate_identity), /// but on an iterator of `TypeFoldable` values. - pub fn subst_identity_iter(self) -> I::IntoIter { + pub fn instantiate_identity_iter(self) -> I::IntoIter { self.value.into_iter() } } -pub struct SubstIter<'s, 'tcx, I: IntoIterator> { +pub struct IterInstantiated<'s, 'tcx, I: IntoIterator> { it: I::IntoIter, tcx: TyCtxt<'tcx>, - substs: &'s [GenericArg<'tcx>], + args: &'s [GenericArg<'tcx>], } -impl<'tcx, I: IntoIterator> Iterator for SubstIter<'_, 'tcx, I> +impl<'tcx, I: IntoIterator> Iterator for IterInstantiated<'_, 'tcx, I> where I::Item: TypeFoldable<TyCtxt<'tcx>>, { type Item = I::Item; fn next(&mut self) -> Option<Self::Item> { - Some(EarlyBinder { value: self.it.next()? }.subst(self.tcx, self.substs)) + Some(EarlyBinder { value: self.it.next()? }.instantiate(self.tcx, self.args)) } fn size_hint(&self) -> (usize, Option<usize>) { @@ -656,17 +646,17 @@ where } } -impl<'tcx, I: IntoIterator> DoubleEndedIterator for SubstIter<'_, 'tcx, I> +impl<'tcx, I: IntoIterator> DoubleEndedIterator for IterInstantiated<'_, 'tcx, I> where I::IntoIter: DoubleEndedIterator, I::Item: TypeFoldable<TyCtxt<'tcx>>, { fn next_back(&mut self) -> Option<Self::Item> { - Some(EarlyBinder { value: self.it.next_back()? }.subst(self.tcx, self.substs)) + Some(EarlyBinder { value: self.it.next_back()? }.instantiate(self.tcx, self.args)) } } -impl<'tcx, I: IntoIterator> ExactSizeIterator for SubstIter<'_, 'tcx, I> +impl<'tcx, I: IntoIterator> ExactSizeIterator for IterInstantiated<'_, 'tcx, I> where I::IntoIter: ExactSizeIterator, I::Item: TypeFoldable<TyCtxt<'tcx>>, @@ -678,28 +668,30 @@ where I::Item: Deref, <I::Item as Deref>::Target: Copy + TypeFoldable<TyCtxt<'tcx>>, { - pub fn subst_iter_copied( + pub fn iter_instantiated_copied( self, tcx: TyCtxt<'tcx>, - substs: &'s [GenericArg<'tcx>], - ) -> SubstIterCopied<'s, 'tcx, I> { - SubstIterCopied { it: self.value.into_iter(), tcx, substs } + args: &'s [GenericArg<'tcx>], + ) -> IterInstantiatedCopied<'s, 'tcx, I> { + IterInstantiatedCopied { it: self.value.into_iter(), tcx, args } } - /// Similar to [`subst_identity`](EarlyBinder::subst_identity), + /// Similar to [`instantiate_identity`](EarlyBinder::instantiate_identity), /// but on an iterator of values that deref to a `TypeFoldable`. - pub fn subst_identity_iter_copied(self) -> impl Iterator<Item = <I::Item as Deref>::Target> { + pub fn instantiate_identity_iter_copied( + self, + ) -> impl Iterator<Item = <I::Item as Deref>::Target> { self.value.into_iter().map(|v| *v) } } -pub struct SubstIterCopied<'a, 'tcx, I: IntoIterator> { +pub struct IterInstantiatedCopied<'a, 'tcx, I: IntoIterator> { it: I::IntoIter, tcx: TyCtxt<'tcx>, - substs: &'a [GenericArg<'tcx>], + args: &'a [GenericArg<'tcx>], } -impl<'tcx, I: IntoIterator> Iterator for SubstIterCopied<'_, 'tcx, I> +impl<'tcx, I: IntoIterator> Iterator for IterInstantiatedCopied<'_, 'tcx, I> where I::Item: Deref, <I::Item as Deref>::Target: Copy + TypeFoldable<TyCtxt<'tcx>>, @@ -707,7 +699,7 @@ where type Item = <I::Item as Deref>::Target; fn next(&mut self) -> Option<Self::Item> { - self.it.next().map(|value| EarlyBinder { value: *value }.subst(self.tcx, self.substs)) + self.it.next().map(|value| EarlyBinder { value: *value }.instantiate(self.tcx, self.args)) } fn size_hint(&self) -> (usize, Option<usize>) { @@ -715,18 +707,20 @@ where } } -impl<'tcx, I: IntoIterator> DoubleEndedIterator for SubstIterCopied<'_, 'tcx, I> +impl<'tcx, I: IntoIterator> DoubleEndedIterator for IterInstantiatedCopied<'_, 'tcx, I> where I::IntoIter: DoubleEndedIterator, I::Item: Deref, <I::Item as Deref>::Target: Copy + TypeFoldable<TyCtxt<'tcx>>, { fn next_back(&mut self) -> Option<Self::Item> { - self.it.next_back().map(|value| EarlyBinder { value: *value }.subst(self.tcx, self.substs)) + self.it + .next_back() + .map(|value| EarlyBinder { value: *value }.instantiate(self.tcx, self.args)) } } -impl<'tcx, I: IntoIterator> ExactSizeIterator for SubstIterCopied<'_, 'tcx, I> +impl<'tcx, I: IntoIterator> ExactSizeIterator for IterInstantiatedCopied<'_, 'tcx, I> where I::IntoIter: ExactSizeIterator, I::Item: Deref, @@ -757,20 +751,20 @@ impl<T: Iterator> Iterator for EarlyBinderIter<T> { } impl<'tcx, T: TypeFoldable<TyCtxt<'tcx>>> ty::EarlyBinder<T> { - pub fn subst(self, tcx: TyCtxt<'tcx>, substs: &[GenericArg<'tcx>]) -> T { - let mut folder = SubstFolder { tcx, substs, binders_passed: 0 }; + pub fn instantiate(self, tcx: TyCtxt<'tcx>, args: &[GenericArg<'tcx>]) -> T { + let mut folder = ArgFolder { tcx, args, binders_passed: 0 }; self.value.fold_with(&mut folder) } - /// Makes the identity substitution `T0 => T0, ..., TN => TN`. + /// Makes the identity replacement `T0 => T0, ..., TN => TN`. /// Conceptually, this converts universally bound variables into placeholders /// when inside of a given item. /// /// For example, consider `for<T> fn foo<T>(){ .. }`: /// - Outside of `foo`, `T` is bound (represented by the presence of `EarlyBinder`). /// - Inside of the body of `foo`, we treat `T` as a placeholder by calling - /// `subst_identity` to discharge the `EarlyBinder`. - pub fn subst_identity(self) -> T { + /// `instantiate_identity` to discharge the `EarlyBinder`. + pub fn instantiate_identity(self) -> T { self.value } @@ -783,15 +777,15 @@ impl<'tcx, T: TypeFoldable<TyCtxt<'tcx>>> ty::EarlyBinder<T> { /////////////////////////////////////////////////////////////////////////// // The actual substitution engine itself is a type folder. -struct SubstFolder<'a, 'tcx> { +struct ArgFolder<'a, 'tcx> { tcx: TyCtxt<'tcx>, - substs: &'a [GenericArg<'tcx>], + args: &'a [GenericArg<'tcx>], /// Number of region binders we have passed through while doing the substitution binders_passed: u32, } -impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for SubstFolder<'a, 'tcx> { +impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ArgFolder<'a, 'tcx> { #[inline] fn interner(&self) -> TyCtxt<'tcx> { self.tcx @@ -810,12 +804,12 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for SubstFolder<'a, 'tcx> { fn fold_region(&mut self, r: ty::Region<'tcx>) -> ty::Region<'tcx> { #[cold] #[inline(never)] - fn region_param_out_of_range(data: ty::EarlyBoundRegion, substs: &[GenericArg<'_>]) -> ! { + fn region_param_out_of_range(data: ty::EarlyBoundRegion, args: &[GenericArg<'_>]) -> ! { bug!( - "Region parameter out of range when substituting in region {} (index={}, substs = {:?})", + "Region parameter out of range when substituting in region {} (index={}, args = {:?})", data.name, data.index, - substs, + args, ) } @@ -837,11 +831,11 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for SubstFolder<'a, 'tcx> { // the specialized routine `ty::replace_late_regions()`. match *r { ty::ReEarlyBound(data) => { - let rk = self.substs.get(data.index as usize).map(|k| k.unpack()); + let rk = self.args.get(data.index as usize).map(|k| k.unpack()); match rk { Some(GenericArgKind::Lifetime(lt)) => self.shift_region_through_binders(lt), Some(other) => region_param_invalid(data, other), - None => region_param_out_of_range(data, self.substs), + None => region_param_out_of_range(data, self.args), } } ty::ReLateBound(..) @@ -874,10 +868,10 @@ impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for SubstFolder<'a, 'tcx> { } } -impl<'a, 'tcx> SubstFolder<'a, 'tcx> { +impl<'a, 'tcx> ArgFolder<'a, 'tcx> { fn ty_for_param(&self, p: ty::ParamTy, source_ty: Ty<'tcx>) -> Ty<'tcx> { - // Look up the type in the substitutions. It really should be in there. - let opt_ty = self.substs.get(p.index as usize).map(|k| k.unpack()); + // Look up the type in the args. It really should be in there. + let opt_ty = self.args.get(p.index as usize).map(|k| k.unpack()); let ty = match opt_ty { Some(GenericArgKind::Type(ty)) => ty, Some(kind) => self.type_param_expected(p, source_ty, kind), @@ -891,12 +885,12 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> { #[inline(never)] fn type_param_expected(&self, p: ty::ParamTy, ty: Ty<'tcx>, kind: GenericArgKind<'tcx>) -> ! { bug!( - "expected type for `{:?}` ({:?}/{}) but found {:?} when substituting, substs={:?}", + "expected type for `{:?}` ({:?}/{}) but found {:?} when substituting, args={:?}", p, ty, p.index, kind, - self.substs, + self.args, ) } @@ -904,17 +898,17 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> { #[inline(never)] fn type_param_out_of_range(&self, p: ty::ParamTy, ty: Ty<'tcx>) -> ! { bug!( - "type parameter `{:?}` ({:?}/{}) out of range when substituting, substs={:?}", + "type parameter `{:?}` ({:?}/{}) out of range when substituting, args={:?}", p, ty, p.index, - self.substs, + self.args, ) } fn const_for_param(&self, p: ParamConst, source_ct: ty::Const<'tcx>) -> ty::Const<'tcx> { - // Look up the const in the substitutions. It really should be in there. - let opt_ct = self.substs.get(p.index as usize).map(|k| k.unpack()); + // Look up the const in the args. It really should be in there. + let opt_ct = self.args.get(p.index as usize).map(|k| k.unpack()); let ct = match opt_ct { Some(GenericArgKind::Const(ct)) => ct, Some(kind) => self.const_param_expected(p, source_ct, kind), @@ -933,12 +927,12 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> { kind: GenericArgKind<'tcx>, ) -> ! { bug!( - "expected const for `{:?}` ({:?}/{}) but found {:?} when substituting substs={:?}", + "expected const for `{:?}` ({:?}/{}) but found {:?} when substituting args={:?}", p, ct, p.index, kind, - self.substs, + self.args, ) } @@ -946,11 +940,11 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> { #[inline(never)] fn const_param_out_of_range(&self, p: ty::ParamConst, ct: ty::Const<'tcx>) -> ! { bug!( - "const parameter `{:?}` ({:?}/{}) out of range when substituting substs={:?}", + "const parameter `{:?}` ({:?}/{}) out of range when substituting args={:?}", p, ct, p.index, - self.substs, + self.args, ) } @@ -1022,13 +1016,13 @@ impl<'a, 'tcx> SubstFolder<'a, 'tcx> { } } -/// Stores the user-given substs to reach some fully qualified path +/// Stores the user-given args to reach some fully qualified path /// (e.g., `<T>::Item` or `<T as Trait>::Item`). #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable)] #[derive(HashStable, TypeFoldable, TypeVisitable, Lift)] -pub struct UserSubsts<'tcx> { - /// The substitutions for the item as given by the user. - pub substs: SubstsRef<'tcx>, +pub struct UserArgs<'tcx> { + /// The args for the item as given by the user. + pub args: GenericArgsRef<'tcx>, /// The self type, in the case of a `<T>::Item` path (when applied /// to an inherent impl). See `UserSelfTy` below. @@ -1048,7 +1042,7 @@ pub struct UserSubsts<'tcx> { /// when you then have a path like `<Foo<&'static u32>>::method`, /// this struct would carry the `DefId` of the impl along with the /// self type `Foo<u32>`. Then we can instantiate the parameters of -/// the impl (with the substs from `UserSubsts`) and apply those to +/// the impl (with the args from `UserArgs`) and apply those to /// the self type, giving `Foo<?A>`. Finally, we unify that with /// the self type here, which contains `?A` to be `&'static u32` #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable)] diff --git a/compiler/rustc_middle/src/ty/generics.rs b/compiler/rustc_middle/src/ty/generics.rs index 6c7125c4c..70a35f137 100644 --- a/compiler/rustc_middle/src/ty/generics.rs +++ b/compiler/rustc_middle/src/ty/generics.rs @@ -1,5 +1,5 @@ use crate::ty; -use crate::ty::{EarlyBinder, SubstsRef}; +use crate::ty::{EarlyBinder, GenericArgsRef}; use rustc_ast as ast; use rustc_data_structures::fx::FxHashMap; use rustc_hir::def_id::DefId; @@ -97,14 +97,14 @@ impl GenericParamDef { pub fn to_error<'tcx>( &self, tcx: TyCtxt<'tcx>, - preceding_substs: &[ty::GenericArg<'tcx>], + preceding_args: &[ty::GenericArg<'tcx>], ) -> ty::GenericArg<'tcx> { match &self.kind { ty::GenericParamDefKind::Lifetime => ty::Region::new_error_misc(tcx).into(), ty::GenericParamDefKind::Type { .. } => Ty::new_misc_error(tcx).into(), ty::GenericParamDefKind::Const { .. } => ty::Const::new_misc_error( tcx, - tcx.type_of(self.def_id).subst(tcx, preceding_substs), + tcx.type_of(self.def_id).instantiate(tcx, preceding_args), ) .into(), } @@ -136,7 +136,7 @@ pub struct Generics { pub has_self: bool, pub has_late_bound_regions: Option<Span>, - // The index of the host effect when substituted. (i.e. might be index to parent substs) + // The index of the host effect when substituted. (i.e. might be index to parent args) pub host_effect_index: Option<usize>, } @@ -278,14 +278,14 @@ impl<'tcx> Generics { }) } - /// Returns the substs corresponding to the generic parameters + /// Returns the args corresponding to the generic parameters /// of this item, excluding `Self`. /// /// **This should only be used for diagnostics purposes.** - pub fn own_substs_no_defaults( + pub fn own_args_no_defaults( &'tcx self, tcx: TyCtxt<'tcx>, - substs: &'tcx [ty::GenericArg<'tcx>], + args: &'tcx [ty::GenericArg<'tcx>], ) -> &'tcx [ty::GenericArg<'tcx>] { let mut own_params = self.parent_count..self.count(); if self.has_self && self.parent.is_none() { @@ -304,22 +304,22 @@ impl<'tcx> Generics { .rev() .take_while(|param| { param.default_value(tcx).is_some_and(|default| { - default.subst(tcx, substs) == substs[param.index as usize] + default.instantiate(tcx, args) == args[param.index as usize] }) }) .count(); - &substs[own_params] + &args[own_params] } - /// Returns the substs corresponding to the generic parameters of this item, excluding `Self`. + /// Returns the args corresponding to the generic parameters of this item, excluding `Self`. /// /// **This should only be used for diagnostics purposes.** - pub fn own_substs( + pub fn own_args( &'tcx self, - substs: &'tcx [ty::GenericArg<'tcx>], + args: &'tcx [ty::GenericArg<'tcx>], ) -> &'tcx [ty::GenericArg<'tcx>] { - let own = &substs[self.parent_count..][..self.params.len()]; + let own = &args[self.parent_count..][..self.params.len()]; if self.has_self && self.parent.is_none() { &own[1..] } else { &own } } } @@ -335,19 +335,19 @@ impl<'tcx> GenericPredicates<'tcx> { pub fn instantiate( &self, tcx: TyCtxt<'tcx>, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) -> InstantiatedPredicates<'tcx> { let mut instantiated = InstantiatedPredicates::empty(); - self.instantiate_into(tcx, &mut instantiated, substs); + self.instantiate_into(tcx, &mut instantiated, args); instantiated } pub fn instantiate_own( &self, tcx: TyCtxt<'tcx>, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) -> impl Iterator<Item = (Clause<'tcx>, Span)> + DoubleEndedIterator + ExactSizeIterator { - EarlyBinder::bind(self.predicates).subst_iter_copied(tcx, substs) + EarlyBinder::bind(self.predicates).iter_instantiated_copied(tcx, args) } #[instrument(level = "debug", skip(self, tcx))] @@ -355,14 +355,14 @@ impl<'tcx> GenericPredicates<'tcx> { &self, tcx: TyCtxt<'tcx>, instantiated: &mut InstantiatedPredicates<'tcx>, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) { if let Some(def_id) = self.parent { - tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, substs); + tcx.predicates_of(def_id).instantiate_into(tcx, instantiated, args); } - instantiated - .predicates - .extend(self.predicates.iter().map(|(p, _)| EarlyBinder::bind(*p).subst(tcx, substs))); + instantiated.predicates.extend( + self.predicates.iter().map(|(p, _)| EarlyBinder::bind(*p).instantiate(tcx, args)), + ); instantiated.spans.extend(self.predicates.iter().map(|(_, sp)| *sp)); } diff --git a/compiler/rustc_middle/src/ty/impls_ty.rs b/compiler/rustc_middle/src/ty/impls_ty.rs index 02baa395c..b03874a90 100644 --- a/compiler/rustc_middle/src/ty/impls_ty.rs +++ b/compiler/rustc_middle/src/ty/impls_ty.rs @@ -67,7 +67,7 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for SimplifiedType { } } -impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::subst::GenericArg<'tcx> { +impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::GenericArg<'tcx> { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { self.unpack().hash_stable(hcx, hasher); } diff --git a/compiler/rustc_middle/src/ty/inhabitedness/inhabited_predicate.rs b/compiler/rustc_middle/src/ty/inhabitedness/inhabited_predicate.rs index 295cb1464..f278cace9 100644 --- a/compiler/rustc_middle/src/ty/inhabitedness/inhabited_predicate.rs +++ b/compiler/rustc_middle/src/ty/inhabitedness/inhabited_predicate.rs @@ -19,7 +19,7 @@ pub enum InhabitedPredicate<'tcx> { /// type has restricted visibility. NotInModule(DefId), /// Inhabited if some generic type is inhabited. - /// These are replaced by calling [`Self::subst`]. + /// These are replaced by calling [`Self::instantiate`]. GenericType(Ty<'tcx>), /// A AND B And(&'tcx [InhabitedPredicate<'tcx>; 2]), @@ -162,14 +162,14 @@ impl<'tcx> InhabitedPredicate<'tcx> { } /// Replaces generic types with its corresponding predicate - pub fn subst(self, tcx: TyCtxt<'tcx>, substs: ty::SubstsRef<'tcx>) -> Self { - self.subst_opt(tcx, substs).unwrap_or(self) + pub fn instantiate(self, tcx: TyCtxt<'tcx>, args: ty::GenericArgsRef<'tcx>) -> Self { + self.instantiate_opt(tcx, args).unwrap_or(self) } - fn subst_opt(self, tcx: TyCtxt<'tcx>, substs: ty::SubstsRef<'tcx>) -> Option<Self> { + fn instantiate_opt(self, tcx: TyCtxt<'tcx>, args: ty::GenericArgsRef<'tcx>) -> Option<Self> { match self { Self::ConstIsZero(c) => { - let c = ty::EarlyBinder::bind(c).subst(tcx, substs); + let c = ty::EarlyBinder::bind(c).instantiate(tcx, args); let pred = match c.try_to_target_usize(tcx) { Some(0) => Self::True, Some(1..) => Self::False, @@ -178,17 +178,17 @@ impl<'tcx> InhabitedPredicate<'tcx> { Some(pred) } Self::GenericType(t) => { - Some(ty::EarlyBinder::bind(t).subst(tcx, substs).inhabited_predicate(tcx)) + Some(ty::EarlyBinder::bind(t).instantiate(tcx, args).inhabited_predicate(tcx)) } - Self::And(&[a, b]) => match a.subst_opt(tcx, substs) { - None => b.subst_opt(tcx, substs).map(|b| a.and(tcx, b)), + Self::And(&[a, b]) => match a.instantiate_opt(tcx, args) { + None => b.instantiate_opt(tcx, args).map(|b| a.and(tcx, b)), Some(InhabitedPredicate::False) => Some(InhabitedPredicate::False), - Some(a) => Some(a.and(tcx, b.subst_opt(tcx, substs).unwrap_or(b))), + Some(a) => Some(a.and(tcx, b.instantiate_opt(tcx, args).unwrap_or(b))), }, - Self::Or(&[a, b]) => match a.subst_opt(tcx, substs) { - None => b.subst_opt(tcx, substs).map(|b| a.or(tcx, b)), + Self::Or(&[a, b]) => match a.instantiate_opt(tcx, args) { + None => b.instantiate_opt(tcx, args).map(|b| a.or(tcx, b)), Some(InhabitedPredicate::True) => Some(InhabitedPredicate::True), - Some(a) => Some(a.or(tcx, b.subst_opt(tcx, substs).unwrap_or(b))), + Some(a) => Some(a.or(tcx, b.instantiate_opt(tcx, args).unwrap_or(b))), }, _ => None, } diff --git a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs index b92d84152..4dac6891b 100644 --- a/compiler/rustc_middle/src/ty/inhabitedness/mod.rs +++ b/compiler/rustc_middle/src/ty/inhabitedness/mod.rs @@ -58,7 +58,7 @@ pub(crate) fn provide(providers: &mut Providers) { } /// Returns an `InhabitedPredicate` that is generic over type parameters and -/// requires calling [`InhabitedPredicate::subst`] +/// requires calling [`InhabitedPredicate::instantiate`] fn inhabited_predicate_adt(tcx: TyCtxt<'_>, def_id: DefId) -> InhabitedPredicate<'_> { if let Some(def_id) = def_id.as_local() { if matches!(tcx.representability(def_id), ty::Representability::Infinite) { @@ -87,7 +87,7 @@ impl<'tcx> VariantDef { InhabitedPredicate::all( tcx, self.fields.iter().map(|field| { - let pred = tcx.type_of(field.did).subst_identity().inhabited_predicate(tcx); + let pred = tcx.type_of(field.did).instantiate_identity().inhabited_predicate(tcx); if adt.is_enum() { return pred; } @@ -114,8 +114,8 @@ impl<'tcx> Ty<'tcx> { Never => InhabitedPredicate::False, Param(_) | Alias(ty::Projection, _) => InhabitedPredicate::GenericType(self), // FIXME(inherent_associated_types): Most likely we can just map to `GenericType` like above. - // However it's unclear if the substs passed to `InhabitedPredicate::subst` are of the correct - // format, i.e. don't contain parent substs. If you hit this case, please verify this beforehand. + // However it's unclear if the args passed to `InhabitedPredicate::instantiate` are of the correct + // format, i.e. don't contain parent args. If you hit this case, please verify this beforehand. Alias(ty::Inherent, _) => { bug!("unimplemented: inhabitedness checking for inherent projections") } @@ -189,7 +189,7 @@ impl<'tcx> Ty<'tcx> { /// N.B. this query should only be called through `Ty::inhabited_predicate` fn inhabited_predicate_type<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> InhabitedPredicate<'tcx> { match *ty.kind() { - Adt(adt, substs) => tcx.inhabited_predicate_adt(adt.did()).subst(tcx, substs), + Adt(adt, args) => tcx.inhabited_predicate_adt(adt.did()).instantiate(tcx, args), Tuple(tys) => { InhabitedPredicate::all(tcx, tys.iter().map(|ty| ty.inhabited_predicate(tcx))) diff --git a/compiler/rustc_middle/src/ty/instance.rs b/compiler/rustc_middle/src/ty/instance.rs index ae57e954f..8913bf76d 100644 --- a/compiler/rustc_middle/src/ty/instance.rs +++ b/compiler/rustc_middle/src/ty/instance.rs @@ -1,7 +1,7 @@ use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags; use crate::ty::print::{FmtPrinter, Printer}; use crate::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable}; -use crate::ty::{EarlyBinder, InternalSubsts, SubstsRef, TypeVisitableExt}; +use crate::ty::{EarlyBinder, GenericArgs, GenericArgsRef, TypeVisitableExt}; use rustc_errors::ErrorGuaranteed; use rustc_hir::def::Namespace; use rustc_hir::def_id::{CrateNum, DefId}; @@ -16,13 +16,13 @@ use std::fmt; /// A monomorphized `InstanceDef`. /// /// Monomorphization happens on-the-fly and no monomorphized MIR is ever created. Instead, this type -/// simply couples a potentially generic `InstanceDef` with some substs, and codegen and const eval +/// simply couples a potentially generic `InstanceDef` with some args, and codegen and const eval /// will do all required substitution as they run. #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)] #[derive(HashStable, Lift, TypeFoldable, TypeVisitable)] pub struct Instance<'tcx> { pub def: InstanceDef<'tcx>, - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, } #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] @@ -115,7 +115,7 @@ impl<'tcx> Instance<'tcx> { /// lifetimes erased, allowing a `ParamEnv` to be specified for use during normalization. pub fn ty(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Ty<'tcx> { let ty = tcx.type_of(self.def.def_id()); - tcx.subst_and_normalize_erasing_regions(self.substs, param_env, ty) + tcx.subst_and_normalize_erasing_regions(self.args, param_env, ty) } /// Finds a crate that contains a monomorphization of this instance that @@ -139,13 +139,13 @@ impl<'tcx> Instance<'tcx> { } // If this a non-generic instance, it cannot be a shared monomorphization. - self.substs.non_erasable_generics().next()?; + self.args.non_erasable_generics().next()?; match self.def { InstanceDef::Item(def) => tcx .upstream_monomorphizations_for(def) - .and_then(|monos| monos.get(&self.substs).cloned()), - InstanceDef::DropGlue(_, Some(_)) => tcx.upstream_drop_glue_for(self.substs), + .and_then(|monos| monos.get(&self.args).cloned()), + InstanceDef::DropGlue(_, Some(_)) => tcx.upstream_drop_glue_for(self.args), _ => None, } } @@ -265,8 +265,8 @@ impl<'tcx> InstanceDef<'tcx> { } /// Returns `true` when the MIR body associated with this instance should be monomorphized - /// by its users (e.g. codegen or miri) by substituting the `substs` from `Instance` (see - /// `Instance::substs_for_mir_body`). + /// by its users (e.g. codegen or miri) by substituting the `args` from `Instance` (see + /// `Instance::args_for_mir_body`). /// /// Otherwise, returns `false` only for some kinds of shims where the construction of the MIR /// body should perform necessary substitutions. @@ -294,10 +294,10 @@ fn fmt_instance( type_length: rustc_session::Limit, ) -> fmt::Result { ty::tls::with(|tcx| { - let substs = tcx.lift(instance.substs).expect("could not lift for printing"); + let args = tcx.lift(instance.args).expect("could not lift for printing"); let s = FmtPrinter::new_with_limit(tcx, Namespace::ValueNS, type_length) - .print_def_path(instance.def_id(), substs)? + .print_def_path(instance.def_id(), args)? .into_buffer(); f.write_str(&s) })?; @@ -308,13 +308,13 @@ fn fmt_instance( InstanceDef::ReifyShim(_) => write!(f, " - shim(reify)"), InstanceDef::ThreadLocalShim(_) => write!(f, " - shim(tls)"), InstanceDef::Intrinsic(_) => write!(f, " - intrinsic"), - InstanceDef::Virtual(_, num) => write!(f, " - virtual#{}", num), - InstanceDef::FnPtrShim(_, ty) => write!(f, " - shim({})", ty), + InstanceDef::Virtual(_, num) => write!(f, " - virtual#{num}"), + InstanceDef::FnPtrShim(_, ty) => write!(f, " - shim({ty})"), InstanceDef::ClosureOnceShim { .. } => write!(f, " - shim"), InstanceDef::DropGlue(_, None) => write!(f, " - shim(None)"), - InstanceDef::DropGlue(_, Some(ty)) => write!(f, " - shim(Some({}))", ty), - InstanceDef::CloneShim(_, ty) => write!(f, " - shim({})", ty), - InstanceDef::FnPtrAddrShim(_, ty) => write!(f, " - shim({})", ty), + InstanceDef::DropGlue(_, Some(ty)) => write!(f, " - shim(Some({ty}))"), + InstanceDef::CloneShim(_, ty) => write!(f, " - shim({ty})"), + InstanceDef::FnPtrAddrShim(_, ty) => write!(f, " - shim({ty})"), } } @@ -333,18 +333,16 @@ impl<'tcx> fmt::Display for Instance<'tcx> { } impl<'tcx> Instance<'tcx> { - pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> Instance<'tcx> { + pub fn new(def_id: DefId, args: GenericArgsRef<'tcx>) -> Instance<'tcx> { assert!( - !substs.has_escaping_bound_vars(), - "substs of instance {:?} not normalized for codegen: {:?}", - def_id, - substs + !args.has_escaping_bound_vars(), + "args of instance {def_id:?} not normalized for codegen: {args:?}" ); - Instance { def: InstanceDef::Item(def_id), substs } + Instance { def: InstanceDef::Item(def_id), args } } pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> { - let substs = InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind { + let args = GenericArgs::for_item(tcx, def_id, |param, _| match param.kind { ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(), ty::GenericParamDefKind::Type { .. } => { bug!("Instance::mono: {:?} has type parameters", def_id) @@ -354,7 +352,7 @@ impl<'tcx> Instance<'tcx> { } }); - Instance::new(def_id, substs) + Instance::new(def_id, args) } #[inline] @@ -362,7 +360,7 @@ impl<'tcx> Instance<'tcx> { self.def.def_id() } - /// Resolves a `(def_id, substs)` pair to an (optional) instance -- most commonly, + /// Resolves a `(def_id, args)` pair to an (optional) instance -- most commonly, /// this is used to find the precise code that will run for a trait method invocation, /// if known. /// @@ -390,29 +388,29 @@ impl<'tcx> Instance<'tcx> { tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, def_id: DefId, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> { // All regions in the result of this query are erased, so it's // fine to erase all of the input regions. - // HACK(eddyb) erase regions in `substs` first, so that `param_env.and(...)` + // HACK(eddyb) erase regions in `args` first, so that `param_env.and(...)` // below is more likely to ignore the bounds in scope (e.g. if the only - // generic parameters mentioned by `substs` were lifetime ones). - let substs = tcx.erase_regions(substs); - tcx.resolve_instance(tcx.erase_regions(param_env.and((def_id, substs)))) + // generic parameters mentioned by `args` were lifetime ones). + let args = tcx.erase_regions(args); + tcx.resolve_instance(tcx.erase_regions(param_env.and((def_id, args)))) } pub fn expect_resolve( tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, def_id: DefId, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) -> Instance<'tcx> { - match ty::Instance::resolve(tcx, param_env, def_id, substs) { + match ty::Instance::resolve(tcx, param_env, def_id, args) { Ok(Some(instance)) => instance, instance => bug!( "failed to resolve instance for {}: {instance:#?}", - tcx.def_path_str_with_substs(def_id, substs) + tcx.def_path_str_with_args(def_id, args) ), } } @@ -421,12 +419,12 @@ impl<'tcx> Instance<'tcx> { tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, def_id: DefId, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) -> Option<Instance<'tcx>> { - debug!("resolve(def_id={:?}, substs={:?})", def_id, substs); + debug!("resolve(def_id={:?}, args={:?})", def_id, args); // Use either `resolve_closure` or `resolve_for_vtable` - assert!(!tcx.is_closure(def_id), "Called `resolve_for_fn_ptr` on closure: {:?}", def_id); - Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| { + assert!(!tcx.is_closure(def_id), "Called `resolve_for_fn_ptr` on closure: {def_id:?}"); + Instance::resolve(tcx, param_env, def_id, args).ok().flatten().map(|mut resolved| { match resolved.def { InstanceDef::Item(def) if resolved.def.requires_caller_location(tcx) => { debug!(" => fn pointer created for function with #[track_caller]"); @@ -447,18 +445,18 @@ impl<'tcx> Instance<'tcx> { tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, def_id: DefId, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) -> Option<Instance<'tcx>> { - debug!("resolve_for_vtable(def_id={:?}, substs={:?})", def_id, substs); - let fn_sig = tcx.fn_sig(def_id).subst_identity(); + debug!("resolve_for_vtable(def_id={:?}, args={:?})", def_id, args); + let fn_sig = tcx.fn_sig(def_id).instantiate_identity(); let is_vtable_shim = !fn_sig.inputs().skip_binder().is_empty() && fn_sig.input(0).skip_binder().is_param(0) && tcx.generics_of(def_id).has_self; if is_vtable_shim { debug!(" => associated item with unsizeable self: Self"); - Some(Instance { def: InstanceDef::VTableShim(def_id), substs }) + Some(Instance { def: InstanceDef::VTableShim(def_id), args }) } else { - Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| { + Instance::resolve(tcx, param_env, def_id, args).ok().flatten().map(|mut resolved| { match resolved.def { InstanceDef::Item(def) => { // We need to generate a shim when we cannot guarantee that @@ -489,12 +487,12 @@ impl<'tcx> Instance<'tcx> { { if tcx.is_closure(def) { debug!(" => vtable fn pointer created for closure with #[track_caller]: {:?} for method {:?} {:?}", - def, def_id, substs); + def, def_id, args); // Create a shim for the `FnOnce/FnMut/Fn` method we are calling // - unlike functions, invoking a closure always goes through a // trait. - resolved = Instance { def: InstanceDef::ReifyShim(def_id), substs }; + resolved = Instance { def: InstanceDef::ReifyShim(def_id), args }; } else { debug!( " => vtable fn pointer created for function with #[track_caller]: {:?}", def @@ -518,28 +516,28 @@ impl<'tcx> Instance<'tcx> { pub fn resolve_closure( tcx: TyCtxt<'tcx>, def_id: DefId, - substs: ty::SubstsRef<'tcx>, + args: ty::GenericArgsRef<'tcx>, requested_kind: ty::ClosureKind, ) -> Option<Instance<'tcx>> { - let actual_kind = substs.as_closure().kind(); + let actual_kind = args.as_closure().kind(); match needs_fn_once_adapter_shim(actual_kind, requested_kind) { - Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs), - _ => Some(Instance::new(def_id, substs)), + Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, args), + _ => Some(Instance::new(def_id, args)), } } pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> { let def_id = tcx.require_lang_item(LangItem::DropInPlace, None); - let substs = tcx.mk_substs(&[ty.into()]); - Instance::expect_resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs) + let args = tcx.mk_args(&[ty.into()]); + Instance::expect_resolve(tcx, ty::ParamEnv::reveal_all(), def_id, args) } #[instrument(level = "debug", skip(tcx), ret)] pub fn fn_once_adapter_instance( tcx: TyCtxt<'tcx>, closure_did: DefId, - substs: ty::SubstsRef<'tcx>, + args: ty::GenericArgsRef<'tcx>, ) -> Option<Instance<'tcx>> { let fn_once = tcx.require_lang_item(LangItem::FnOnce, None); let call_once = tcx @@ -552,30 +550,30 @@ impl<'tcx> Instance<'tcx> { tcx.codegen_fn_attrs(closure_did).flags.contains(CodegenFnAttrFlags::TRACK_CALLER); let def = ty::InstanceDef::ClosureOnceShim { call_once, track_caller }; - let self_ty = Ty::new_closure(tcx, closure_did, substs); + let self_ty = Ty::new_closure(tcx, closure_did, args); - let sig = substs.as_closure().sig(); + let sig = args.as_closure().sig(); let sig = tcx.try_normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), sig).ok()?; assert_eq!(sig.inputs().len(), 1); - let substs = tcx.mk_substs_trait(self_ty, [sig.inputs()[0].into()]); + let args = tcx.mk_args_trait(self_ty, [sig.inputs()[0].into()]); debug!(?self_ty, ?sig); - Some(Instance { def, substs }) + Some(Instance { def, args }) } /// Depending on the kind of `InstanceDef`, the MIR body associated with an /// instance is expressed in terms of the generic parameters of `self.def_id()`, and in other /// cases the MIR body is expressed in terms of the types found in the substitution array. /// In the former case, we want to substitute those generic types and replace them with the - /// values from the substs when monomorphizing the function body. But in the latter case, we + /// values from the args when monomorphizing the function body. But in the latter case, we /// don't want to do that substitution, since it has already been done effectively. /// - /// This function returns `Some(substs)` in the former case and `None` otherwise -- i.e., if + /// This function returns `Some(args)` in the former case and `None` otherwise -- i.e., if /// this function returns `None`, then the MIR body does not require substitution during /// codegen. - fn substs_for_mir_body(&self) -> Option<SubstsRef<'tcx>> { - self.def.has_polymorphic_mir_body().then_some(self.substs) + fn args_for_mir_body(&self) -> Option<GenericArgsRef<'tcx>> { + self.def.has_polymorphic_mir_body().then_some(self.args) } pub fn subst_mir<T>(&self, tcx: TyCtxt<'tcx>, v: EarlyBinder<&T>) -> T @@ -583,10 +581,10 @@ impl<'tcx> Instance<'tcx> { T: TypeFoldable<TyCtxt<'tcx>> + Copy, { let v = v.map_bound(|v| *v); - if let Some(substs) = self.substs_for_mir_body() { - v.subst(tcx, substs) + if let Some(args) = self.args_for_mir_body() { + v.instantiate(tcx, args) } else { - v.subst_identity() + v.instantiate_identity() } } @@ -600,8 +598,8 @@ impl<'tcx> Instance<'tcx> { where T: TypeFoldable<TyCtxt<'tcx>> + Clone, { - if let Some(substs) = self.substs_for_mir_body() { - tcx.subst_and_normalize_erasing_regions(substs, param_env, v) + if let Some(args) = self.args_for_mir_body() { + tcx.subst_and_normalize_erasing_regions(args, param_env, v) } else { tcx.normalize_erasing_regions(param_env, v.skip_binder()) } @@ -617,14 +615,14 @@ impl<'tcx> Instance<'tcx> { where T: TypeFoldable<TyCtxt<'tcx>> + Clone, { - if let Some(substs) = self.substs_for_mir_body() { - tcx.try_subst_and_normalize_erasing_regions(substs, param_env, v) + if let Some(args) = self.args_for_mir_body() { + tcx.try_subst_and_normalize_erasing_regions(args, param_env, v) } else { tcx.try_normalize_erasing_regions(param_env, v.skip_binder()) } } - /// Returns a new `Instance` where generic parameters in `instance.substs` are replaced by + /// Returns a new `Instance` where generic parameters in `instance.args` are replaced by /// identity parameters if they are determined to be unused in `instance.def`. pub fn polymorphize(self, tcx: TyCtxt<'tcx>) -> Self { debug!("polymorphize: running polymorphization analysis"); @@ -632,18 +630,18 @@ impl<'tcx> Instance<'tcx> { return self; } - let polymorphized_substs = polymorphize(tcx, self.def, self.substs); - debug!("polymorphize: self={:?} polymorphized_substs={:?}", self, polymorphized_substs); - Self { def: self.def, substs: polymorphized_substs } + let polymorphized_args = polymorphize(tcx, self.def, self.args); + debug!("polymorphize: self={:?} polymorphized_args={:?}", self, polymorphized_args); + Self { def: self.def, args: polymorphized_args } } } fn polymorphize<'tcx>( tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>, - substs: SubstsRef<'tcx>, -) -> SubstsRef<'tcx> { - debug!("polymorphize({:?}, {:?})", instance, substs); + args: GenericArgsRef<'tcx>, +) -> GenericArgsRef<'tcx> { + debug!("polymorphize({:?}, {:?})", instance, args); let unused = tcx.unused_generic_params(instance); debug!("polymorphize: unused={:?}", unused); @@ -653,9 +651,9 @@ fn polymorphize<'tcx>( // multiple mono items (and eventually symbol clashes). let def_id = instance.def_id(); let upvars_ty = if tcx.is_closure(def_id) { - Some(substs.as_closure().tupled_upvars_ty()) + Some(args.as_closure().tupled_upvars_ty()) } else if tcx.type_of(def_id).skip_binder().is_generator() { - Some(substs.as_generator().tupled_upvars_ty()) + Some(args.as_generator().tupled_upvars_ty()) } else { None }; @@ -674,22 +672,22 @@ fn polymorphize<'tcx>( fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { debug!("fold_ty: ty={:?}", ty); match *ty.kind() { - ty::Closure(def_id, substs) => { - let polymorphized_substs = - polymorphize(self.tcx, ty::InstanceDef::Item(def_id), substs); - if substs == polymorphized_substs { + ty::Closure(def_id, args) => { + let polymorphized_args = + polymorphize(self.tcx, ty::InstanceDef::Item(def_id), args); + if args == polymorphized_args { ty } else { - Ty::new_closure(self.tcx, def_id, polymorphized_substs) + Ty::new_closure(self.tcx, def_id, polymorphized_args) } } - ty::Generator(def_id, substs, movability) => { - let polymorphized_substs = - polymorphize(self.tcx, ty::InstanceDef::Item(def_id), substs); - if substs == polymorphized_substs { + ty::Generator(def_id, args, movability) => { + let polymorphized_args = + polymorphize(self.tcx, ty::InstanceDef::Item(def_id), args); + if args == polymorphized_args { ty } else { - Ty::new_generator(self.tcx, def_id, polymorphized_substs, movability) + Ty::new_generator(self.tcx, def_id, polymorphized_args, movability) } } _ => ty.super_fold_with(self), @@ -697,7 +695,7 @@ fn polymorphize<'tcx>( } } - InternalSubsts::for_item(tcx, def_id, |param, _| { + GenericArgs::for_item(tcx, def_id, |param, _| { let is_unused = unused.is_unused(param.index); debug!("polymorphize: param={:?} is_unused={:?}", param, is_unused); match param.kind { @@ -706,7 +704,7 @@ fn polymorphize<'tcx>( // ..and has upvars.. has_upvars && // ..and this param has the same type as the tupled upvars.. - upvars_ty == Some(substs[param.index as usize].expect_ty()) => { + upvars_ty == Some(args[param.index as usize].expect_ty()) => { // ..then double-check that polymorphization marked it used.. debug_assert!(!is_unused); // ..and polymorphize any closures/generators captured as upvars. @@ -725,7 +723,7 @@ fn polymorphize<'tcx>( tcx.mk_param_from_def(param), // Otherwise, use the parameter as before. - _ => substs[param.index as usize], + _ => args[param.index as usize], } }) } diff --git a/compiler/rustc_middle/src/ty/layout.rs b/compiler/rustc_middle/src/ty/layout.rs index d95b05ef7..e362b3477 100644 --- a/compiler/rustc_middle/src/ty/layout.rs +++ b/compiler/rustc_middle/src/ty/layout.rs @@ -10,7 +10,7 @@ use rustc_hir::def_id::DefId; use rustc_index::IndexVec; use rustc_session::config::OptLevel; use rustc_span::symbol::{sym, Symbol}; -use rustc_span::{Span, DUMMY_SP}; +use rustc_span::{ErrorGuaranteed, Span, DUMMY_SP}; use rustc_target::abi::call::FnAbi; use rustc_target::abi::*; use rustc_target::spec::{abi::Abi as SpecAbi, HasTargetSpec, PanicStrategy, Target}; @@ -212,6 +212,7 @@ pub enum LayoutError<'tcx> { Unknown(Ty<'tcx>), SizeOverflow(Ty<'tcx>), NormalizationFailure(Ty<'tcx>, NormalizationError<'tcx>), + ReferencesError(ErrorGuaranteed), Cycle, } @@ -224,6 +225,7 @@ impl<'tcx> LayoutError<'tcx> { SizeOverflow(_) => middle_values_too_big, NormalizationFailure(_, _) => middle_cannot_be_normalized, Cycle => middle_cycle, + ReferencesError(_) => middle_layout_references_error, } } @@ -237,6 +239,7 @@ impl<'tcx> LayoutError<'tcx> { E::NormalizationFailure { ty, failure_ty: e.get_type_for_failure() } } Cycle => E::Cycle, + ReferencesError(_) => E::ReferencesError, } } } @@ -246,9 +249,9 @@ impl<'tcx> LayoutError<'tcx> { impl<'tcx> fmt::Display for LayoutError<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - LayoutError::Unknown(ty) => write!(f, "the type `{}` has an unknown layout", ty), + LayoutError::Unknown(ty) => write!(f, "the type `{ty}` has an unknown layout"), LayoutError::SizeOverflow(ty) => { - write!(f, "values of the type `{}` are too big for the current architecture", ty) + write!(f, "values of the type `{ty}` are too big for the current architecture") } LayoutError::NormalizationFailure(t, e) => write!( f, @@ -257,6 +260,7 @@ impl<'tcx> fmt::Display for LayoutError<'tcx> { e.get_type_for_failure() ), LayoutError::Cycle => write!(f, "a cycle occurred during layout computation"), + LayoutError::ReferencesError(_) => write!(f, "the type has an unknown layout"), } } } @@ -323,7 +327,8 @@ impl<'tcx> SizeSkeleton<'tcx> { Err( e @ LayoutError::Cycle | e @ LayoutError::SizeOverflow(_) - | e @ LayoutError::NormalizationFailure(..), + | e @ LayoutError::NormalizationFailure(..) + | e @ LayoutError::ReferencesError(_), ) => return Err(e), }; @@ -374,7 +379,7 @@ impl<'tcx> SizeSkeleton<'tcx> { } } - ty::Adt(def, substs) => { + ty::Adt(def, args) => { // Only newtypes and enums w/ nullable pointer optimization. if def.is_union() || def.variants().is_empty() || def.variants().len() > 2 { return Err(err); @@ -385,7 +390,7 @@ impl<'tcx> SizeSkeleton<'tcx> { let i = VariantIdx::from_usize(i); let fields = def.variant(i).fields.iter().map(|field| { - SizeSkeleton::compute(field.ty(tcx, substs), tcx, param_env) + SizeSkeleton::compute(field.ty(tcx, args), tcx, param_env) }); let mut ptr = None; for field in fields { @@ -741,9 +746,9 @@ where let fields = match this.ty.kind() { ty::Adt(def, _) if def.variants().is_empty() => - bug!("for_variant called on zero-variant enum"), + bug!("for_variant called on zero-variant enum {}", this.ty), ty::Adt(def, _) => def.variant(variant_index).fields.len(), - _ => bug!(), + _ => bug!("`ty_and_layout_for_variant` on unexpected type {}", this.ty), }; tcx.mk_layout(LayoutS { variants: Variants::Single { index: variant_index }, @@ -755,6 +760,8 @@ where largest_niche: None, align: tcx.data_layout.i8_align, size: Size::ZERO, + max_repr_align: None, + unadjusted_abi_align: tcx.data_layout.i8_align.abi, }) } @@ -861,9 +868,9 @@ where // offers better information than `std::ptr::metadata::VTable`, // and we rely on this layout information to trigger a panic in // `std::mem::uninitialized::<&dyn Trait>()`, for example. - if let ty::Adt(def, substs) = metadata.kind() + if let ty::Adt(def, args) = metadata.kind() && Some(def.did()) == tcx.lang_items().dyn_metadata() - && substs.type_at(0).is_trait() + && args.type_at(0).is_trait() { mk_dyn_vtable() } else { @@ -885,16 +892,15 @@ where ty::Str => TyMaybeWithLayout::Ty(tcx.types.u8), // Tuples, generators and closures. - ty::Closure(_, ref substs) => field_ty_or_layout( - TyAndLayout { ty: substs.as_closure().tupled_upvars_ty(), ..this }, + ty::Closure(_, ref args) => field_ty_or_layout( + TyAndLayout { ty: args.as_closure().tupled_upvars_ty(), ..this }, cx, i, ), - ty::Generator(def_id, ref substs, _) => match this.variants { + ty::Generator(def_id, ref args, _) => match this.variants { Variants::Single { index } => TyMaybeWithLayout::Ty( - substs - .as_generator() + args.as_generator() .state_tys(def_id, tcx) .nth(index.as_usize()) .unwrap() @@ -905,18 +911,18 @@ where if i == tag_field { return TyMaybeWithLayout::TyAndLayout(tag_layout(tag)); } - TyMaybeWithLayout::Ty(substs.as_generator().prefix_tys().nth(i).unwrap()) + TyMaybeWithLayout::Ty(args.as_generator().prefix_tys()[i]) } }, ty::Tuple(tys) => TyMaybeWithLayout::Ty(tys[i]), // ADTs. - ty::Adt(def, substs) => { + ty::Adt(def, args) => { match this.variants { Variants::Single { index } => { let field = &def.variant(index).fields[FieldIdx::from_usize(i)]; - TyMaybeWithLayout::Ty(field.ty(tcx, substs)) + TyMaybeWithLayout::Ty(field.ty(tcx, args)) } // Discriminant field for enums (where applicable). @@ -1233,6 +1239,8 @@ pub fn fn_can_unwind(tcx: TyCtxt<'_>, fn_def_id: Option<DefId>, abi: SpecAbi) -> | EfiApi | AvrInterrupt | AvrNonBlockingInterrupt + | RiscvInterruptM + | RiscvInterruptS | CCmseNonSecureCall | Wasm | PlatformIntrinsic diff --git a/compiler/rustc_middle/src/ty/list.rs b/compiler/rustc_middle/src/ty/list.rs index 71911a5a6..7a32cfb10 100644 --- a/compiler/rustc_middle/src/ty/list.rs +++ b/compiler/rustc_middle/src/ty/list.rs @@ -1,6 +1,7 @@ use crate::arena::Arena; use rustc_data_structures::aligned::{align_of, Aligned}; use rustc_serialize::{Encodable, Encoder}; +use rustc_type_ir::{InferCtxtLike, OptWithInfcx}; use std::alloc::Layout; use std::cmp::Ordering; use std::fmt; @@ -119,6 +120,14 @@ impl<T: fmt::Debug> fmt::Debug for List<T> { (**self).fmt(f) } } +impl<'tcx, T: super::DebugWithInfcx<TyCtxt<'tcx>>> super::DebugWithInfcx<TyCtxt<'tcx>> for List<T> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + fmt::Debug::fmt(&this.map(|this| this.as_slice()), f) + } +} impl<S: Encoder, T: Encodable<S>> Encodable<S> for List<T> { #[inline] @@ -202,6 +211,8 @@ unsafe impl<T: Sync> Sync for List<T> {} // We need this since `List` uses extern type `OpaqueListContents`. #[cfg(parallel_compiler)] use rustc_data_structures::sync::DynSync; + +use super::TyCtxt; #[cfg(parallel_compiler)] unsafe impl<T: DynSync> DynSync for List<T> {} diff --git a/compiler/rustc_middle/src/ty/mod.rs b/compiler/rustc_middle/src/ty/mod.rs index aa8bfd317..1274f427e 100644 --- a/compiler/rustc_middle/src/ty/mod.rs +++ b/compiler/rustc_middle/src/ty/mod.rs @@ -28,6 +28,7 @@ use crate::ty::fast_reject::SimplifiedType; use crate::ty::util::Discr; pub use adt::*; pub use assoc::*; +pub use generic_args::*; pub use generics::*; use rustc_ast as ast; use rustc_ast::node_id::NodeMap; @@ -53,7 +54,7 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol}; use rustc_span::{ExpnId, ExpnKind, Span}; use rustc_target::abi::{Align, FieldIdx, Integer, IntegerType, VariantIdx}; pub use rustc_target::abi::{ReprFlags, ReprOptions}; -pub use subst::*; +pub use rustc_type_ir::{DebugWithInfcx, InferCtxtLike, OptWithInfcx}; pub use vtable::*; use std::fmt::Debug; @@ -81,8 +82,7 @@ pub use self::binding::BindingMode::*; pub use self::closure::{ is_ancestor_or_same_capture, place_to_string_for_capture, BorrowKind, CaptureInfo, CapturedPlace, ClosureKind, ClosureTypeInfo, MinCaptureInformationMap, MinCaptureList, - RootVariableMinCaptureList, UpvarCapture, UpvarCaptureMap, UpvarId, UpvarListMap, UpvarPath, - CAPTURE_STRUCT_LOCAL, + RootVariableMinCaptureList, UpvarCapture, UpvarId, UpvarPath, CAPTURE_STRUCT_LOCAL, }; pub use self::consts::{ Const, ConstData, ConstInt, Expr, InferConst, ScalarInt, UnevaluatedConst, ValTree, @@ -97,12 +97,12 @@ pub use self::rvalue_scopes::RvalueScopes; pub use self::sty::BoundRegionKind::*; pub use self::sty::{ AliasTy, Article, Binder, BoundRegion, BoundRegionKind, BoundTy, BoundTyKind, BoundVar, - BoundVariableKind, CanonicalPolyFnSig, ClosureSubsts, ClosureSubstsParts, ConstKind, ConstVid, + BoundVariableKind, CanonicalPolyFnSig, ClosureArgs, ClosureArgsParts, ConstKind, ConstVid, EarlyBoundRegion, ExistentialPredicate, ExistentialProjection, ExistentialTraitRef, FnSig, - FreeRegion, GenSig, GeneratorSubsts, GeneratorSubstsParts, InlineConstSubsts, - InlineConstSubstsParts, ParamConst, ParamTy, PolyExistentialPredicate, - PolyExistentialProjection, PolyExistentialTraitRef, PolyFnSig, PolyGenSig, PolyTraitRef, - Region, RegionKind, RegionVid, TraitRef, TyKind, TypeAndMut, UpvarSubsts, VarianceDiagInfo, + FreeRegion, GenSig, GeneratorArgs, GeneratorArgsParts, InlineConstArgs, InlineConstArgsParts, + ParamConst, ParamTy, PolyExistentialPredicate, PolyExistentialProjection, + PolyExistentialTraitRef, PolyFnSig, PolyGenSig, PolyTraitRef, Region, RegionKind, RegionVid, + TraitRef, TyKind, TypeAndMut, UpvarArgs, VarianceDiagInfo, }; pub use self::trait_def::TraitDef; pub use self::typeck_results::{ @@ -126,7 +126,6 @@ pub mod layout; pub mod normalize_erasing_regions; pub mod print; pub mod relate; -pub mod subst; pub mod trait_def; pub mod util; pub mod visit; @@ -140,6 +139,7 @@ mod consts; mod context; mod diagnostics; mod erase_regions; +mod generic_args; mod generics; mod impls_ty; mod instance; @@ -148,7 +148,7 @@ mod opaque_types; mod parameterized; mod rvalue_scopes; mod structural_impls; -#[cfg_attr(not(bootstrap), allow(hidden_glob_reexports))] +#[allow(hidden_glob_reexports)] mod sty; mod typeck_results; @@ -238,7 +238,7 @@ pub struct ImplHeader<'tcx> { pub impl_def_id: DefId, pub self_ty: Ty<'tcx>, pub trait_ref: Option<TraitRef<'tcx>>, - pub predicates: Vec<Predicate<'tcx>>, + pub predicates: Vec<(Predicate<'tcx>, Span)>, } #[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable)] @@ -355,8 +355,8 @@ impl TyCtxt<'_> { #[inline] #[track_caller] - pub fn local_parent(self, id: LocalDefId) -> LocalDefId { - self.parent(id.to_def_id()).expect_local() + pub fn local_parent(self, id: impl Into<LocalDefId>) -> LocalDefId { + self.parent(id.into().to_def_id()).expect_local() } pub fn is_descendant_of(self, mut descendant: DefId, ancestor: DefId) -> bool { @@ -498,11 +498,9 @@ impl<'tcx> Predicate<'tcx> { .map_bound(|kind| match kind { PredicateKind::Clause(ClauseKind::Trait(TraitPredicate { trait_ref, - constness, polarity, })) => Some(PredicateKind::Clause(ClauseKind::Trait(TraitPredicate { trait_ref, - constness, polarity: polarity.flip()?, }))), @@ -513,19 +511,6 @@ impl<'tcx> Predicate<'tcx> { Some(tcx.mk_predicate(kind)) } - pub fn without_const(mut self, tcx: TyCtxt<'tcx>) -> Self { - if let PredicateKind::Clause(ClauseKind::Trait(TraitPredicate { trait_ref, constness, polarity })) = self.kind().skip_binder() - && constness != BoundConstness::NotConst - { - self = tcx.mk_predicate(self.kind().rebind(PredicateKind::Clause(ClauseKind::Trait(TraitPredicate { - trait_ref, - constness: BoundConstness::NotConst, - polarity, - })))); - } - self - } - #[instrument(level = "debug", skip(tcx), ret)] pub fn is_coinductive(self, tcx: TyCtxt<'tcx>) -> bool { match self.kind().skip_binder() { @@ -629,10 +614,6 @@ impl<'tcx> Clause<'tcx> { None } } - - pub fn without_const(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { - self.as_predicate().without_const(tcx).expect_clause() - } } #[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)] @@ -676,9 +657,9 @@ pub enum PredicateKind<'tcx> { ObjectSafe(DefId), /// No direct syntax. May be thought of as `where T: FnFoo<...>` - /// for some substitutions `...` and `T` being a closure type. + /// for some generic args `...` and `T` being a closure type. /// Satisfied (or refuted) once we know the closure's kind. - ClosureKind(DefId, SubstsRef<'tcx>, ClosureKind), + ClosureKind(DefId, GenericArgsRef<'tcx>, ClosureKind), /// `T1 <: T2` /// @@ -813,15 +794,15 @@ impl<'tcx> Clause<'tcx> { // this trick achieves that). // Working through the second example: - // trait_ref: for<'x> T: Foo1<'^0.0>; substs: [T, '^0.0] - // predicate: for<'b> Self: Bar1<'a, '^0.0>; substs: [Self, 'a, '^0.0] + // trait_ref: for<'x> T: Foo1<'^0.0>; args: [T, '^0.0] + // predicate: for<'b> Self: Bar1<'a, '^0.0>; args: [Self, 'a, '^0.0] // We want to end up with: // for<'x, 'b> T: Bar1<'^0.0, '^0.1> // To do this: // 1) We must shift all bound vars in predicate by the length // of trait ref's bound vars. So, we would end up with predicate like // Self: Bar1<'a, '^0.1> - // 2) We can then apply the trait substs to this, ending up with + // 2) We can then apply the trait args to this, ending up with // T: Bar1<'^0.0, '^0.1> // 3) Finally, to create the final bound vars, we concatenate the bound // vars of the trait ref with those of the predicate: @@ -833,7 +814,7 @@ impl<'tcx> Clause<'tcx> { let shifted_pred = tcx.shift_bound_var_indices(trait_bound_vars.len(), bound_pred.skip_binder()); // 2) Self: Bar1<'a, '^0.1> -> T: Bar1<'^0.0, '^0.1> - let new = EarlyBinder::bind(shifted_pred).subst(tcx, trait_ref.skip_binder().substs); + let new = EarlyBinder::bind(shifted_pred).instantiate(tcx, trait_ref.skip_binder().args); // 3) ['x] + ['b] -> ['x, 'b] let bound_vars = tcx.mk_bound_variable_kinds_from_iter(trait_bound_vars.iter().chain(pred_bound_vars)); @@ -852,8 +833,6 @@ impl<'tcx> Clause<'tcx> { pub struct TraitPredicate<'tcx> { pub trait_ref: TraitRef<'tcx>, - pub constness: BoundConstness, - /// If polarity is Positive: we are proving that the trait is implemented. /// /// If polarity is Negative: we are proving that a negative impl of this trait @@ -867,20 +846,6 @@ pub struct TraitPredicate<'tcx> { pub type PolyTraitPredicate<'tcx> = ty::Binder<'tcx, TraitPredicate<'tcx>>; impl<'tcx> TraitPredicate<'tcx> { - pub fn remap_constness(&mut self, param_env: &mut ParamEnv<'tcx>) { - *param_env = param_env.with_constness(self.constness.and(param_env.constness())) - } - - /// Remap the constness of this predicate before emitting it for diagnostics. - pub fn remap_constness_diag(&mut self, param_env: ParamEnv<'tcx>) { - // this is different to `remap_constness` that callees want to print this predicate - // in case of selection errors. `T: ~const Drop` bounds cannot end up here when the - // param_env is not const because it is always satisfied in non-const contexts. - if let hir::Constness::NotConst = param_env.constness() { - self.constness = ty::BoundConstness::NotConst; - } - } - pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self { Self { trait_ref: self.trait_ref.with_self_ty(tcx, self_ty), ..self } } @@ -892,24 +857,6 @@ impl<'tcx> TraitPredicate<'tcx> { pub fn self_ty(self) -> Ty<'tcx> { self.trait_ref.self_ty() } - - #[inline] - pub fn is_const_if_const(self) -> bool { - self.constness == BoundConstness::ConstIfConst - } - - pub fn is_constness_satisfied_by(self, constness: hir::Constness) -> bool { - match (self.constness, constness) { - (BoundConstness::NotConst, _) - | (BoundConstness::ConstIfConst, hir::Constness::Const) => true, - (BoundConstness::ConstIfConst, hir::Constness::NotConst) => false, - } - } - - pub fn without_const(mut self) -> Self { - self.constness = BoundConstness::NotConst; - self - } } impl<'tcx> PolyTraitPredicate<'tcx> { @@ -922,19 +869,6 @@ impl<'tcx> PolyTraitPredicate<'tcx> { self.map_bound(|trait_ref| trait_ref.self_ty()) } - /// Remap the constness of this predicate before emitting it for diagnostics. - pub fn remap_constness_diag(&mut self, param_env: ParamEnv<'tcx>) { - *self = self.map_bound(|mut p| { - p.remap_constness_diag(param_env); - p - }); - } - - #[inline] - pub fn is_const_if_const(self) -> bool { - self.skip_binder().is_const_if_const() - } - #[inline] pub fn polarity(self) -> ImplPolarity { self.skip_binder().polarity @@ -980,9 +914,9 @@ pub struct Term<'tcx> { impl Debug for Term<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let data = if let Some(ty) = self.ty() { - format!("Term::Ty({:?})", ty) + format!("Term::Ty({ty:?})") } else if let Some(ct) = self.ct() { - format!("Term::Ct({:?})", ct) + format!("Term::Ct({ct:?})") } else { unreachable!() }; @@ -1079,7 +1013,7 @@ impl<'tcx> Term<'tcx> { _ => None, }, TermKind::Const(ct) => match ct.kind() { - ConstKind::Unevaluated(uv) => Some(tcx.mk_alias_ty(uv.def, uv.substs)), + ConstKind::Unevaluated(uv) => Some(tcx.mk_alias_ty(uv.def, uv.args)), _ => None, }, } @@ -1309,7 +1243,7 @@ impl<'tcx> ToPredicate<'tcx> for TraitRef<'tcx> { impl<'tcx> ToPredicate<'tcx, TraitPredicate<'tcx>> for TraitRef<'tcx> { #[inline(always)] fn to_predicate(self, _tcx: TyCtxt<'tcx>) -> TraitPredicate<'tcx> { - self.without_const() + TraitPredicate { trait_ref: self, polarity: ImplPolarity::Positive } } } @@ -1350,7 +1284,6 @@ impl<'tcx> ToPredicate<'tcx, PolyTraitPredicate<'tcx>> for Binder<'tcx, TraitRef fn to_predicate(self, _: TyCtxt<'tcx>) -> PolyTraitPredicate<'tcx> { self.map_bound(|trait_ref| TraitPredicate { trait_ref, - constness: ty::BoundConstness::NotConst, polarity: ty::ImplPolarity::Positive, }) } @@ -1381,24 +1314,49 @@ impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for PolyTraitPredicate<'tcx> { } } +impl<'tcx> ToPredicate<'tcx> for OutlivesPredicate<ty::Region<'tcx>, ty::Region<'tcx>> { + fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + ty::Binder::dummy(PredicateKind::Clause(ClauseKind::RegionOutlives(self))).to_predicate(tcx) + } +} + impl<'tcx> ToPredicate<'tcx> for PolyRegionOutlivesPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { self.map_bound(|p| PredicateKind::Clause(ClauseKind::RegionOutlives(p))).to_predicate(tcx) } } +impl<'tcx> ToPredicate<'tcx> for OutlivesPredicate<Ty<'tcx>, ty::Region<'tcx>> { + fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + ty::Binder::dummy(PredicateKind::Clause(ClauseKind::TypeOutlives(self))).to_predicate(tcx) + } +} + impl<'tcx> ToPredicate<'tcx> for PolyTypeOutlivesPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { self.map_bound(|p| PredicateKind::Clause(ClauseKind::TypeOutlives(p))).to_predicate(tcx) } } +impl<'tcx> ToPredicate<'tcx> for ProjectionPredicate<'tcx> { + fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { + ty::Binder::dummy(PredicateKind::Clause(ClauseKind::Projection(self))).to_predicate(tcx) + } +} + impl<'tcx> ToPredicate<'tcx> for PolyProjectionPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Predicate<'tcx> { self.map_bound(|p| PredicateKind::Clause(ClauseKind::Projection(p))).to_predicate(tcx) } } +impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for ProjectionPredicate<'tcx> { + fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { + let p: Predicate<'tcx> = self.to_predicate(tcx); + p.expect_clause() + } +} + impl<'tcx> ToPredicate<'tcx, Clause<'tcx>> for PolyProjectionPredicate<'tcx> { fn to_predicate(self, tcx: TyCtxt<'tcx>) -> Clause<'tcx> { let p: Predicate<'tcx> = self.to_predicate(tcx); @@ -1558,7 +1516,7 @@ impl<'a, 'tcx> IntoIterator for &'a InstantiatedPredicates<'tcx> { #[derive(TypeFoldable, TypeVisitable)] pub struct OpaqueTypeKey<'tcx> { pub def_id: LocalDefId, - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, } #[derive(Copy, Clone, Debug, TypeFoldable, TypeVisitable, HashStable, TyEncodable, TyDecodable)] @@ -1629,21 +1587,21 @@ impl<'tcx> OpaqueHiddenType<'tcx> { // typeck errors have subpar spans for opaque types, so delay error reporting until borrowck. ignore_errors: bool, ) -> Self { - let OpaqueTypeKey { def_id, substs } = opaque_type_key; + let OpaqueTypeKey { def_id, args } = opaque_type_key; - // Use substs to build up a reverse map from regions to their + // Use args to build up a reverse map from regions to their // identity mappings. This is necessary because of `impl // Trait` lifetimes are computed by replacing existing // lifetimes with 'static and remapping only those used in the // `impl Trait` return type, resulting in the parameters // shifting. - let id_substs = InternalSubsts::identity_for_item(tcx, def_id); - debug!(?id_substs); + let id_args = GenericArgs::identity_for_item(tcx, def_id); + debug!(?id_args); - // This zip may have several times the same lifetime in `substs` paired with a different - // lifetime from `id_substs`. Simply `collect`ing the iterator is the correct behaviour: + // This zip may have several times the same lifetime in `args` paired with a different + // lifetime from `id_args`. Simply `collect`ing the iterator is the correct behaviour: // it will pick the last one, which is the one we introduced in the impl-trait desugaring. - let map = substs.iter().zip(id_substs).collect(); + let map = args.iter().zip(id_args).collect(); debug!("map = {:#?}", map); // Convert the type from the function into a type valid outside @@ -1700,15 +1658,12 @@ pub struct ParamEnv<'tcx> { #[derive(Copy, Clone)] struct ParamTag { reveal: traits::Reveal, - constness: hir::Constness, } impl_tag! { impl Tag for ParamTag; - ParamTag { reveal: traits::Reveal::UserFacing, constness: hir::Constness::NotConst }, - ParamTag { reveal: traits::Reveal::All, constness: hir::Constness::NotConst }, - ParamTag { reveal: traits::Reveal::UserFacing, constness: hir::Constness::Const }, - ParamTag { reveal: traits::Reveal::All, constness: hir::Constness::Const }, + ParamTag { reveal: traits::Reveal::UserFacing }, + ParamTag { reveal: traits::Reveal::All }, } impl<'tcx> fmt::Debug for ParamEnv<'tcx> { @@ -1716,7 +1671,6 @@ impl<'tcx> fmt::Debug for ParamEnv<'tcx> { f.debug_struct("ParamEnv") .field("caller_bounds", &self.caller_bounds()) .field("reveal", &self.reveal()) - .field("constness", &self.constness()) .finish() } } @@ -1725,7 +1679,6 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ParamEnv<'tcx> { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { self.caller_bounds().hash_stable(hcx, hasher); self.reveal().hash_stable(hcx, hasher); - self.constness().hash_stable(hcx, hasher); } } @@ -1737,7 +1690,6 @@ impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for ParamEnv<'tcx> { Ok(ParamEnv::new( self.caller_bounds().try_fold_with(folder)?, self.reveal().try_fold_with(folder)?, - self.constness(), )) } } @@ -1756,7 +1708,7 @@ impl<'tcx> ParamEnv<'tcx> { /// type-checking. #[inline] pub fn empty() -> Self { - Self::new(List::empty(), Reveal::UserFacing, hir::Constness::NotConst) + Self::new(List::empty(), Reveal::UserFacing) } #[inline] @@ -1769,16 +1721,6 @@ impl<'tcx> ParamEnv<'tcx> { self.packed.tag().reveal } - #[inline] - pub fn constness(self) -> hir::Constness { - self.packed.tag().constness - } - - #[inline] - pub fn is_const(self) -> bool { - self.packed.tag().constness == hir::Constness::Const - } - /// Construct a trait environment with no where-clauses in scope /// where the values of all `impl Trait` and other hidden types /// are revealed. This is suitable for monomorphized, post-typeck @@ -1788,17 +1730,13 @@ impl<'tcx> ParamEnv<'tcx> { /// or invoke `param_env.with_reveal_all()`. #[inline] pub fn reveal_all() -> Self { - Self::new(List::empty(), Reveal::All, hir::Constness::NotConst) + Self::new(List::empty(), Reveal::All) } /// Construct a trait environment with the given set of predicates. #[inline] - pub fn new( - caller_bounds: &'tcx List<Clause<'tcx>>, - reveal: Reveal, - constness: hir::Constness, - ) -> Self { - ty::ParamEnv { packed: CopyTaggedPtr::new(caller_bounds, ParamTag { reveal, constness }) } + pub fn new(caller_bounds: &'tcx List<Clause<'tcx>>, reveal: Reveal) -> Self { + ty::ParamEnv { packed: CopyTaggedPtr::new(caller_bounds, ParamTag { reveal }) } } pub fn with_user_facing(mut self) -> Self { @@ -1806,29 +1744,6 @@ impl<'tcx> ParamEnv<'tcx> { self } - #[inline] - pub fn with_constness(mut self, constness: hir::Constness) -> Self { - self.packed.set_tag(ParamTag { constness, ..self.packed.tag() }); - self - } - - #[inline] - pub fn with_const(mut self) -> Self { - self.packed.set_tag(ParamTag { constness: hir::Constness::Const, ..self.packed.tag() }); - self - } - - #[inline] - pub fn without_const(mut self) -> Self { - self.packed.set_tag(ParamTag { constness: hir::Constness::NotConst, ..self.packed.tag() }); - self - } - - #[inline] - pub fn remap_constness_with(&mut self, mut constness: ty::BoundConstness) { - *self = self.with_constness(constness.and(self.constness())) - } - /// Returns a new parameter environment with the same clauses, but /// which "reveals" the true results of projections in all cases /// (even for associated types that are specializable). This is @@ -1843,17 +1758,13 @@ impl<'tcx> ParamEnv<'tcx> { return self; } - ParamEnv::new( - tcx.reveal_opaque_types_in_bounds(self.caller_bounds()), - Reveal::All, - self.constness(), - ) + ParamEnv::new(tcx.reveal_opaque_types_in_bounds(self.caller_bounds()), Reveal::All) } /// Returns this same environment but with no caller bounds. #[inline] pub fn without_caller_bounds(self) -> Self { - Self::new(List::empty(), self.reveal(), self.constness()) + Self::new(List::empty(), self.reveal()) } /// Creates a suitable environment in which to perform trait @@ -1883,24 +1794,6 @@ impl<'tcx> ParamEnv<'tcx> { } } -// FIXME(ecstaticmorse): Audit all occurrences of `without_const().to_predicate(tcx)` to ensure that -// the constness of trait bounds is being propagated correctly. -impl<'tcx> PolyTraitRef<'tcx> { - #[inline] - pub fn with_constness(self, constness: BoundConstness) -> PolyTraitPredicate<'tcx> { - self.map_bound(|trait_ref| ty::TraitPredicate { - trait_ref, - constness, - polarity: ty::ImplPolarity::Positive, - }) - } - - #[inline] - pub fn without_const(self) -> PolyTraitPredicate<'tcx> { - self.with_constness(BoundConstness::NotConst) - } -} - #[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, TypeFoldable, TypeVisitable)] #[derive(HashStable, Lift)] pub struct ParamEnvAnd<'tcx, T> { @@ -2164,10 +2057,10 @@ impl Hash for FieldDef { } impl<'tcx> FieldDef { - /// Returns the type of this field. The resulting type is not normalized. The `subst` is + /// Returns the type of this field. The resulting type is not normalized. The `arg` is /// typically obtained via the second field of [`TyKind::Adt`]. - pub fn ty(&self, tcx: TyCtxt<'tcx>, subst: SubstsRef<'tcx>) -> Ty<'tcx> { - tcx.type_of(self.did).subst(tcx, subst) + pub fn ty(&self, tcx: TyCtxt<'tcx>, arg: GenericArgsRef<'tcx>) -> Ty<'tcx> { + tcx.type_of(self.did).instantiate(tcx, arg) } /// Computes the `Ident` of this variant by looking up the `Span` @@ -2391,8 +2284,8 @@ impl<'tcx> TyCtxt<'tcx> { let impl_trait_ref2 = self.impl_trait_ref(def_id2); // If either trait impl references an error, they're allowed to overlap, // as one of them essentially doesn't exist. - if impl_trait_ref1.is_some_and(|tr| tr.subst_identity().references_error()) - || impl_trait_ref2.is_some_and(|tr| tr.subst_identity().references_error()) + if impl_trait_ref1.is_some_and(|tr| tr.instantiate_identity().references_error()) + || impl_trait_ref2.is_some_and(|tr| tr.instantiate_identity().references_error()) { return Some(ImplOverlapKind::Permitted { marker: false }); } @@ -2682,20 +2575,6 @@ impl<'tcx> TyCtxt<'tcx> { matches!(self.trait_of_item(def_id), Some(trait_id) if self.has_attr(trait_id, sym::const_trait)) } - pub fn impl_trait_in_trait_parent_fn(self, mut def_id: DefId) -> DefId { - match self.opt_rpitit_info(def_id) { - Some(ImplTraitInTraitData::Trait { fn_def_id, .. }) - | Some(ImplTraitInTraitData::Impl { fn_def_id, .. }) => fn_def_id, - None => { - while let def_kind = self.def_kind(def_id) && def_kind != DefKind::AssocFn { - debug_assert_eq!(def_kind, DefKind::ImplTraitPlaceholder); - def_id = self.parent(def_id); - } - def_id - } - } - } - /// Returns the `DefId` of the item within which the `impl Trait` is declared. /// For type-alias-impl-trait this is the `type` alias. /// For impl-trait-in-assoc-type this is the assoc type. @@ -2713,33 +2592,20 @@ impl<'tcx> TyCtxt<'tcx> { return false; } - let Some(item) = self.opt_associated_item(def_id) else { return false; }; + let Some(item) = self.opt_associated_item(def_id) else { + return false; + }; if item.container != ty::AssocItemContainer::ImplContainer { return false; } - let Some(trait_item_def_id) = item.trait_item_def_id else { return false; }; - - if self.lower_impl_trait_in_trait_to_assoc_ty() { - return !self - .associated_types_for_impl_traits_in_associated_fn(trait_item_def_id) - .is_empty(); - } + let Some(trait_item_def_id) = item.trait_item_def_id else { + return false; + }; - // FIXME(RPITIT): This does a somewhat manual walk through the signature - // of the trait fn to look for any RPITITs, but that's kinda doing a lot - // of work. We can probably remove this when we refactor RPITITs to be - // associated types. - self.fn_sig(trait_item_def_id).subst_identity().skip_binder().output().walk().any(|arg| { - if let ty::GenericArgKind::Type(ty) = arg.unpack() - && let ty::Alias(ty::Projection, data) = ty.kind() - && self.def_kind(data.def_id) == DefKind::ImplTraitPlaceholder - { - true - } else { - false - } - }) + return !self + .associated_types_for_impl_traits_in_associated_fn(trait_item_def_id) + .is_empty(); } } diff --git a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs index a0c8d299f..2415d50b2 100644 --- a/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs +++ b/compiler/rustc_middle/src/ty/normalize_erasing_regions.rs @@ -9,7 +9,7 @@ use crate::traits::query::NoSolution; use crate::ty::fold::{FallibleTypeFolder, TypeFoldable, TypeFolder}; -use crate::ty::{self, EarlyBinder, SubstsRef, Ty, TyCtxt, TypeVisitableExt}; +use crate::ty::{self, EarlyBinder, GenericArgsRef, Ty, TyCtxt, TypeVisitableExt}; #[derive(Debug, Copy, Clone, HashStable, TyEncodable, TyDecodable)] pub enum NormalizationError<'tcx> { @@ -20,8 +20,8 @@ pub enum NormalizationError<'tcx> { impl<'tcx> NormalizationError<'tcx> { pub fn get_type_for_failure(&self) -> String { match self { - NormalizationError::Type(t) => format!("{}", t), - NormalizationError::Const(c) => format!("{}", c), + NormalizationError::Type(t) => format!("{t}"), + NormalizationError::Const(c) => format!("{c}"), } } } @@ -137,7 +137,7 @@ impl<'tcx> TyCtxt<'tcx> { /// use `try_subst_and_normalize_erasing_regions` instead. pub fn subst_and_normalize_erasing_regions<T>( self, - param_substs: SubstsRef<'tcx>, + param_args: GenericArgsRef<'tcx>, param_env: ty::ParamEnv<'tcx>, value: EarlyBinder<T>, ) -> T @@ -146,12 +146,12 @@ impl<'tcx> TyCtxt<'tcx> { { debug!( "subst_and_normalize_erasing_regions(\ - param_substs={:?}, \ + param_args={:?}, \ value={:?}, \ param_env={:?})", - param_substs, value, param_env, + param_args, value, param_env, ); - let substituted = value.subst(self, param_substs); + let substituted = value.instantiate(self, param_args); self.normalize_erasing_regions(param_env, substituted) } @@ -161,7 +161,7 @@ impl<'tcx> TyCtxt<'tcx> { /// not assume that normalization succeeds. pub fn try_subst_and_normalize_erasing_regions<T>( self, - param_substs: SubstsRef<'tcx>, + param_args: GenericArgsRef<'tcx>, param_env: ty::ParamEnv<'tcx>, value: EarlyBinder<T>, ) -> Result<T, NormalizationError<'tcx>> @@ -170,12 +170,12 @@ impl<'tcx> TyCtxt<'tcx> { { debug!( "subst_and_normalize_erasing_regions(\ - param_substs={:?}, \ + param_args={:?}, \ value={:?}, \ param_env={:?})", - param_substs, value, param_env, + param_args, value, param_env, ); - let substituted = value.subst(self, param_substs); + let substituted = value.instantiate(self, param_args); self.try_normalize_erasing_regions(param_env, substituted) } } diff --git a/compiler/rustc_middle/src/ty/opaque_types.rs b/compiler/rustc_middle/src/ty/opaque_types.rs index b10921eff..0ff5ac903 100644 --- a/compiler/rustc_middle/src/ty/opaque_types.rs +++ b/compiler/rustc_middle/src/ty/opaque_types.rs @@ -1,7 +1,7 @@ use crate::error::ConstNotUsedTraitAlias; use crate::ty::fold::{TypeFolder, TypeSuperFoldable}; -use crate::ty::subst::{GenericArg, GenericArgKind}; use crate::ty::{self, Ty, TyCtxt, TypeFoldable}; +use crate::ty::{GenericArg, GenericArgKind}; use rustc_data_structures::fx::FxHashMap; use rustc_span::def_id::DefId; use rustc_span::Span; @@ -49,11 +49,11 @@ impl<'tcx> ReverseMapper<'tcx> { kind.fold_with(self) } - fn fold_closure_substs( + fn fold_closure_args( &mut self, def_id: DefId, - substs: ty::SubstsRef<'tcx>, - ) -> ty::SubstsRef<'tcx> { + args: ty::GenericArgsRef<'tcx>, + ) -> ty::GenericArgsRef<'tcx> { // I am a horrible monster and I pray for death. When // we encounter a closure here, it is always a closure // from within the function that we are currently @@ -79,7 +79,7 @@ impl<'tcx> ReverseMapper<'tcx> { // during codegen. let generics = self.tcx.generics_of(def_id); - self.tcx.mk_substs_from_iter(substs.iter().enumerate().map(|(index, kind)| { + self.tcx.mk_args_from_iter(args.iter().enumerate().map(|(index, kind)| { if index < generics.parent_count { // Accommodate missing regions in the parent kinds... self.fold_kind_no_missing_regions_error(kind) @@ -124,7 +124,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReverseMapper<'tcx> { match self.map.get(&r.into()).map(|k| k.unpack()) { Some(GenericArgKind::Lifetime(r1)) => r1, - Some(u) => panic!("region mapped to unexpected kind: {:?}", u), + Some(u) => panic!("region mapped to unexpected kind: {u:?}"), None if self.do_not_error => self.tcx.lifetimes.re_static, None => { let e = self @@ -134,9 +134,8 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReverseMapper<'tcx> { .span_label( self.span, format!( - "lifetime `{}` is part of concrete type but not used in \ - parameter list of the `impl Trait` type alias", - r + "lifetime `{r}` is part of concrete type but not used in \ + parameter list of the `impl Trait` type alias" ), ) .emit(); @@ -148,19 +147,19 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReverseMapper<'tcx> { fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { match *ty.kind() { - ty::Closure(def_id, substs) => { - let substs = self.fold_closure_substs(def_id, substs); - Ty::new_closure(self.tcx, def_id, substs) + ty::Closure(def_id, args) => { + let args = self.fold_closure_args(def_id, args); + Ty::new_closure(self.tcx, def_id, args) } - ty::Generator(def_id, substs, movability) => { - let substs = self.fold_closure_substs(def_id, substs); - Ty::new_generator(self.tcx, def_id, substs, movability) + ty::Generator(def_id, args, movability) => { + let args = self.fold_closure_args(def_id, args); + Ty::new_generator(self.tcx, def_id, args, movability) } - ty::GeneratorWitnessMIR(def_id, substs) => { - let substs = self.fold_closure_substs(def_id, substs); - Ty::new_generator_witness_mir(self.tcx, def_id, substs) + ty::GeneratorWitnessMIR(def_id, args) => { + let args = self.fold_closure_args(def_id, args); + Ty::new_generator_witness_mir(self.tcx, def_id, args) } ty::Param(param) => { @@ -169,7 +168,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReverseMapper<'tcx> { // Found it in the substitution list; replace with the parameter from the // opaque type. Some(GenericArgKind::Type(t1)) => t1, - Some(u) => panic!("type mapped to unexpected kind: {:?}", u), + Some(u) => panic!("type mapped to unexpected kind: {u:?}"), None => { debug!(?param, ?self.map); if !self.ignore_errors { @@ -178,9 +177,8 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReverseMapper<'tcx> { .struct_span_err( self.span, format!( - "type parameter `{}` is part of concrete type but not \ - used in parameter list for the `impl Trait` type alias", - ty + "type parameter `{ty}` is part of concrete type but not \ + used in parameter list for the `impl Trait` type alias" ), ) .emit(); @@ -205,7 +203,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReverseMapper<'tcx> { // Found it in the substitution list, replace with the parameter from the // opaque type. Some(GenericArgKind::Const(c1)) => c1, - Some(u) => panic!("const mapped to unexpected kind: {:?}", u), + Some(u) => panic!("const mapped to unexpected kind: {u:?}"), None => { let guar = self .tcx diff --git a/compiler/rustc_middle/src/ty/parameterized.rs b/compiler/rustc_middle/src/ty/parameterized.rs index cc2b26a5e..f1c389842 100644 --- a/compiler/rustc_middle/src/ty/parameterized.rs +++ b/compiler/rustc_middle/src/ty/parameterized.rs @@ -52,6 +52,7 @@ trivially_parameterized_over_tcx! { usize, (), u32, + u64, bool, std::string::String, crate::metadata::ModChild, diff --git a/compiler/rustc_middle/src/ty/print/mod.rs b/compiler/rustc_middle/src/ty/print/mod.rs index 2de0a3f75..05871d0bc 100644 --- a/compiler/rustc_middle/src/ty/print/mod.rs +++ b/compiler/rustc_middle/src/ty/print/mod.rs @@ -42,19 +42,19 @@ pub trait Printer<'tcx>: Sized { fn print_def_path( self, def_id: DefId, - substs: &'tcx [GenericArg<'tcx>], + args: &'tcx [GenericArg<'tcx>], ) -> Result<Self::Path, Self::Error> { - self.default_print_def_path(def_id, substs) + self.default_print_def_path(def_id, args) } fn print_impl_path( self, impl_def_id: DefId, - substs: &'tcx [GenericArg<'tcx>], + args: &'tcx [GenericArg<'tcx>], self_ty: Ty<'tcx>, trait_ref: Option<ty::TraitRef<'tcx>>, ) -> Result<Self::Path, Self::Error> { - self.default_print_impl_path(impl_def_id, substs, self_ty, trait_ref) + self.default_print_impl_path(impl_def_id, args, self_ty, trait_ref) } fn print_region(self, region: ty::Region<'tcx>) -> Result<Self::Region, Self::Error>; @@ -102,7 +102,7 @@ pub trait Printer<'tcx>: Sized { fn default_print_def_path( self, def_id: DefId, - substs: &'tcx [GenericArg<'tcx>], + args: &'tcx [GenericArg<'tcx>], ) -> Result<Self::Path, Self::Error> { let key = self.tcx().def_key(def_id); debug!(?key); @@ -117,25 +117,28 @@ pub trait Printer<'tcx>: Sized { let generics = self.tcx().generics_of(def_id); let self_ty = self.tcx().type_of(def_id); let impl_trait_ref = self.tcx().impl_trait_ref(def_id); - let (self_ty, impl_trait_ref) = if substs.len() >= generics.count() { + let (self_ty, impl_trait_ref) = if args.len() >= generics.count() { ( - self_ty.subst(self.tcx(), substs), - impl_trait_ref.map(|i| i.subst(self.tcx(), substs)), + self_ty.instantiate(self.tcx(), args), + impl_trait_ref.map(|i| i.instantiate(self.tcx(), args)), ) } else { - (self_ty.subst_identity(), impl_trait_ref.map(|i| i.subst_identity())) + ( + self_ty.instantiate_identity(), + impl_trait_ref.map(|i| i.instantiate_identity()), + ) }; - self.print_impl_path(def_id, substs, self_ty, impl_trait_ref) + self.print_impl_path(def_id, args, self_ty, impl_trait_ref) } _ => { let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id }; - let mut parent_substs = substs; + let mut parent_args = args; let mut trait_qualify_parent = false; - if !substs.is_empty() { + if !args.is_empty() { let generics = self.tcx().generics_of(def_id); - parent_substs = &substs[..generics.parent_count.min(substs.len())]; + parent_args = &args[..generics.parent_count.min(args.len())]; match key.disambiguated_data.data { // Closures' own generics are only captures, don't print them. @@ -148,10 +151,10 @@ pub trait Printer<'tcx>: Sized { // If we have any generic arguments to print, we do that // on top of the same path, but without its own generics. _ => { - if !generics.params.is_empty() && substs.len() >= generics.count() { - let args = generics.own_substs_no_defaults(self.tcx(), substs); + if !generics.params.is_empty() && args.len() >= generics.count() { + let args = generics.own_args_no_defaults(self.tcx(), args); return self.path_generic_args( - |cx| cx.print_def_path(def_id, parent_substs), + |cx| cx.print_def_path(def_id, parent_args), args, ); } @@ -162,7 +165,7 @@ pub trait Printer<'tcx>: Sized { // logic, instead of doing it when printing the child. trait_qualify_parent = generics.has_self && generics.parent == Some(parent_def_id) - && parent_substs.len() == generics.parent_count + && parent_args.len() == generics.parent_count && self.tcx().generics_of(parent_def_id).parent_count == 0; } @@ -172,11 +175,11 @@ pub trait Printer<'tcx>: Sized { let trait_ref = ty::TraitRef::new( cx.tcx(), parent_def_id, - parent_substs.iter().copied(), + parent_args.iter().copied(), ); cx.path_qualified(trait_ref.self_ty(), Some(trait_ref)) } else { - cx.print_def_path(parent_def_id, parent_substs) + cx.print_def_path(parent_def_id, parent_args) } }, &key.disambiguated_data, @@ -188,7 +191,7 @@ pub trait Printer<'tcx>: Sized { fn default_print_impl_path( self, impl_def_id: DefId, - _substs: &'tcx [GenericArg<'tcx>], + _args: &'tcx [GenericArg<'tcx>], self_ty: Ty<'tcx>, impl_trait_ref: Option<ty::TraitRef<'tcx>>, ) -> Result<Self::Path, Self::Error> { @@ -326,7 +329,8 @@ impl<'tcx, P: Printer<'tcx>> Print<'tcx, P> for ty::Const<'tcx> { } // This is only used by query descriptions -pub fn describe_as_module(def_id: LocalDefId, tcx: TyCtxt<'_>) -> String { +pub fn describe_as_module(def_id: impl Into<LocalDefId>, tcx: TyCtxt<'_>) -> String { + let def_id = def_id.into(); if def_id.is_top_level_module() { "top-level module".to_string() } else { diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs index 96cf36eb9..ac0c88468 100644 --- a/compiler/rustc_middle/src/ty/print/pretty.rs +++ b/compiler/rustc_middle/src/ty/print/pretty.rs @@ -11,12 +11,13 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; use rustc_data_structures::sso::SsoHashSet; use rustc_hir as hir; use rustc_hir::def::{self, CtorKind, DefKind, Namespace}; -use rustc_hir::def_id::{DefId, DefIdSet, CRATE_DEF_ID, LOCAL_CRATE}; +use rustc_hir::def_id::{DefId, DefIdSet, ModDefId, CRATE_DEF_ID, LOCAL_CRATE}; use rustc_hir::definitions::{DefKey, DefPathData, DefPathDataName, DisambiguatedDefPathData}; use rustc_hir::LangItem; use rustc_session::config::TrimmedDefPaths; use rustc_session::cstore::{ExternCrate, ExternCrateSource}; use rustc_session::Limit; +use rustc_span::sym; use rustc_span::symbol::{kw, Ident, Symbol}; use rustc_span::FileNameDisplayPreference; use rustc_target::abi::Size; @@ -224,9 +225,9 @@ pub trait PrettyPrinter<'tcx>: fn print_value_path( self, def_id: DefId, - substs: &'tcx [GenericArg<'tcx>], + args: &'tcx [GenericArg<'tcx>], ) -> Result<Self::Path, Self::Error> { - self.print_def_path(def_id, substs) + self.print_def_path(def_id, args) } fn in_binder<T>(self, value: &ty::Binder<'tcx, T>) -> Result<Self, Self::Error> @@ -325,7 +326,8 @@ pub trait PrettyPrinter<'tcx>: { this .tcx() - .module_children(visible_parent) + // FIXME(typed_def_id): Further propagate ModDefId + .module_children(ModDefId::new_unchecked(*visible_parent)) .iter() .filter(|child| child.res.opt_def_id() == Some(def_id)) .find(|child| child.vis.is_public() && child.ident.name != kw::Underscore) @@ -363,7 +365,7 @@ pub trait PrettyPrinter<'tcx>: self.write_str(get_local_name(&self, symbol, parent, parent_key).as_str())?; self.write_str("::")?; } else if let DefKind::Struct | DefKind::Union | DefKind::Enum | DefKind::Trait - | DefKind::TyAlias | DefKind::Fn | DefKind::Const | DefKind::Static(_) = kind + | DefKind::TyAlias { .. } | DefKind::Fn | DefKind::Const | DefKind::Static(_) = kind { } else { // If not covered above, like for example items out of `impl` blocks, fallback. @@ -550,7 +552,8 @@ pub trait PrettyPrinter<'tcx>: // that's public and whose identifier isn't `_`. let reexport = self .tcx() - .module_children(visible_parent) + // FIXME(typed_def_id): Further propagate ModDefId + .module_children(ModDefId::new_unchecked(visible_parent)) .iter() .filter(|child| child.res.opt_def_id() == Some(def_id)) .find(|child| child.vis.is_public() && child.ident.name != kw::Underscore) @@ -679,12 +682,12 @@ pub trait PrettyPrinter<'tcx>: } p!(")") } - ty::FnDef(def_id, substs) => { + ty::FnDef(def_id, args) => { if with_no_queries() { - p!(print_def_path(def_id, substs)); + p!(print_def_path(def_id, args)); } else { - let sig = self.tcx().fn_sig(def_id).subst(self.tcx(), substs); - p!(print(sig), " {{", print_value_path(def_id, substs), "}}"); + let sig = self.tcx().fn_sig(def_id).instantiate(self.tcx(), args); + p!(print(sig), " {{", print_value_path(def_id, args), "}}"); } } ty::FnPtr(ref bare_fn) => p!(print(bare_fn)), @@ -715,8 +718,8 @@ pub trait PrettyPrinter<'tcx>: false => p!(write("{s}")), }, }, - ty::Adt(def, substs) => { - p!(print_def_path(def.did(), substs)); + ty::Adt(def, args) => { + p!(print_def_path(def.did(), args)); } ty::Dynamic(data, r, repr) => { let print_r = self.should_print_region(r); @@ -739,7 +742,7 @@ pub trait PrettyPrinter<'tcx>: if !(self.should_print_verbose() || with_no_queries()) && self.tcx().is_impl_trait_in_trait(data.def_id) { - return self.pretty_print_opaque_impl_type(data.def_id, data.substs); + return self.pretty_print_opaque_impl_type(data.def_id, data.args); } else { p!(print(data)) } @@ -751,7 +754,7 @@ pub trait PrettyPrinter<'tcx>: false => p!(write("{name}")), }, }, - ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => { + ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) => { // We use verbose printing in 'NO_QUERIES' mode, to // avoid needing to call `predicates_of`. This should // only affect certain debug messages (e.g. messages printed @@ -759,27 +762,27 @@ pub trait PrettyPrinter<'tcx>: // and should have no effect on any compiler output. if self.should_print_verbose() { // FIXME(eddyb) print this with `print_def_path`. - p!(write("Opaque({:?}, {:?})", def_id, substs)); + p!(write("Opaque({:?}, {:?})", def_id, args)); return Ok(self); } let parent = self.tcx().parent(def_id); match self.tcx().def_kind(parent) { - DefKind::TyAlias | DefKind::AssocTy => { + DefKind::TyAlias { .. } | DefKind::AssocTy => { // NOTE: I know we should check for NO_QUERIES here, but it's alright. // `type_of` on a type alias or assoc type should never cause a cycle. if let ty::Alias(ty::Opaque, ty::AliasTy { def_id: d, .. }) = - *self.tcx().type_of(parent).subst_identity().kind() + *self.tcx().type_of(parent).instantiate_identity().kind() { if d == def_id { // If the type alias directly starts with the `impl` of the // opaque type we're printing, then skip the `::{opaque#1}`. - p!(print_def_path(parent, substs)); + p!(print_def_path(parent, args)); return Ok(self); } } // Complex opaque type, e.g. `type Foo = (i32, impl Debug);` - p!(print_def_path(def_id, substs)); + p!(print_def_path(def_id, args)); return Ok(self); } _ => { @@ -787,13 +790,13 @@ pub trait PrettyPrinter<'tcx>: p!(print_def_path(def_id, &[])); return Ok(self); } else { - return self.pretty_print_opaque_impl_type(def_id, substs); + return self.pretty_print_opaque_impl_type(def_id, args); } } } } ty::Str => p!("str"), - ty::Generator(did, substs, movability) => { + ty::Generator(did, args, movability) => { p!(write("[")); let generator_kind = self.tcx().generator_kind(did).unwrap(); let should_print_movability = @@ -818,20 +821,20 @@ pub trait PrettyPrinter<'tcx>: self.tcx().sess.source_map().span_to_embeddable_string(span) )); } else { - p!(write("@"), print_def_path(did, substs)); + p!(write("@"), print_def_path(did, args)); } } else { - p!(print_def_path(did, substs)); + p!(print_def_path(did, args)); p!(" upvar_tys=("); - if !substs.as_generator().is_valid() { + if !args.as_generator().is_valid() { p!("unavailable"); } else { - self = self.comma_sep(substs.as_generator().upvar_tys())?; + self = self.comma_sep(args.as_generator().upvar_tys().iter())?; } p!(")"); - if substs.as_generator().is_valid() { - p!(" ", print(substs.as_generator().witness())); + if args.as_generator().is_valid() { + p!(" ", print(args.as_generator().witness())); } } @@ -840,7 +843,7 @@ pub trait PrettyPrinter<'tcx>: ty::GeneratorWitness(types) => { p!(in_binder(&types)); } - ty::GeneratorWitnessMIR(did, substs) => { + ty::GeneratorWitnessMIR(did, args) => { p!(write("[")); if !self.tcx().sess.verbose() { p!("generator witness"); @@ -854,22 +857,22 @@ pub trait PrettyPrinter<'tcx>: self.tcx().sess.source_map().span_to_embeddable_string(span) )); } else { - p!(write("@"), print_def_path(did, substs)); + p!(write("@"), print_def_path(did, args)); } } else { - p!(print_def_path(did, substs)); + p!(print_def_path(did, args)); } p!("]") } - ty::Closure(did, substs) => { + ty::Closure(did, args) => { p!(write("[")); if !self.should_print_verbose() { p!(write("closure")); // FIXME(eddyb) should use `def_span`. if let Some(did) = did.as_local() { if self.tcx().sess.opts.unstable_opts.span_free_formats { - p!("@", print_def_path(did.to_def_id(), substs)); + p!("@", print_def_path(did.to_def_id(), args)); } else { let span = self.tcx().def_span(did); let preference = if FORCE_TRIMMED_PATH.with(|flag| flag.get()) { @@ -885,21 +888,21 @@ pub trait PrettyPrinter<'tcx>: )); } } else { - p!(write("@"), print_def_path(did, substs)); + p!(write("@"), print_def_path(did, args)); } } else { - p!(print_def_path(did, substs)); - if !substs.as_closure().is_valid() { - p!(" closure_substs=(unavailable)"); - p!(write(" substs={:?}", substs)); + p!(print_def_path(did, args)); + if !args.as_closure().is_valid() { + p!(" closure_args=(unavailable)"); + p!(write(" args={:?}", args)); } else { - p!(" closure_kind_ty=", print(substs.as_closure().kind_ty())); + p!(" closure_kind_ty=", print(args.as_closure().kind_ty())); p!( " closure_sig_as_fn_ptr_ty=", - print(substs.as_closure().sig_as_fn_ptr_ty()) + print(args.as_closure().sig_as_fn_ptr_ty()) ); p!(" upvar_tys=("); - self = self.comma_sep(substs.as_closure().upvar_tys())?; + self = self.comma_sep(args.as_closure().upvar_tys().iter())?; p!(")"); } } @@ -915,7 +918,7 @@ pub trait PrettyPrinter<'tcx>: fn pretty_print_opaque_impl_type( mut self, def_id: DefId, - substs: &'tcx ty::List<ty::GenericArg<'tcx>>, + args: &'tcx ty::List<ty::GenericArg<'tcx>>, ) -> Result<Self::Type, Self::Error> { let tcx = self.tcx(); @@ -928,7 +931,7 @@ pub trait PrettyPrinter<'tcx>: let mut is_sized = false; let mut lifetimes = SmallVec::<[ty::Region<'tcx>; 1]>::new(); - for (predicate, _) in bounds.subst_iter_copied(tcx, substs) { + for (predicate, _) in bounds.iter_instantiated_copied(tcx, args) { let bound_predicate = predicate.kind(); match bound_predicate.skip_binder() { @@ -978,9 +981,9 @@ pub trait PrettyPrinter<'tcx>: define_scoped_cx!(cx); // Get the (single) generic ty (the args) of this FnOnce trait ref. let generics = tcx.generics_of(trait_ref.def_id); - let args = generics.own_substs_no_defaults(tcx, trait_ref.substs); + let own_args = generics.own_args_no_defaults(tcx, trait_ref.args); - match (entry.return_ty, args[0].expect_ty()) { + match (entry.return_ty, own_args[0].expect_ty()) { // We can only print `impl Fn() -> ()` if we have a tuple of args and we recorded // a return type. (Some(return_ty), arg_tys) if matches!(arg_tys.kind(), ty::Tuple(_)) => { @@ -1044,12 +1047,12 @@ pub trait PrettyPrinter<'tcx>: p!(print(trait_ref.print_only_trait_name())); let generics = tcx.generics_of(trait_ref.def_id); - let args = generics.own_substs_no_defaults(tcx, trait_ref.substs); + let own_args = generics.own_args_no_defaults(tcx, trait_ref.args); - if !args.is_empty() || !assoc_items.is_empty() { + if !own_args.is_empty() || !assoc_items.is_empty() { let mut first = true; - for ty in args { + for ty in own_args { if first { p!("<"); first = false; @@ -1068,8 +1071,8 @@ pub trait PrettyPrinter<'tcx>: && assoc.trait_container(tcx) == tcx.lang_items().gen_trait() && assoc.name == rustc_span::sym::Return { - if let ty::Generator(_, substs, _) = substs.type_at(0).kind() { - let return_ty = substs.as_generator().return_ty(); + if let ty::Generator(_, args, _) = args.type_at(0).kind() { + let return_ty = args.as_generator().return_ty(); if !return_ty.is_ty_var() { return_ty.into() } else { @@ -1182,7 +1185,7 @@ pub trait PrettyPrinter<'tcx>: &def_key.disambiguated_data, ) }, - &alias_ty.substs[1..], + &alias_ty.args[1..], ) } @@ -1211,7 +1214,7 @@ pub trait PrettyPrinter<'tcx>: // Special-case `Fn(...) -> ...` and re-sugar it. let fn_trait_kind = cx.tcx().fn_trait_kind_from_def_id(principal.def_id); if !cx.should_print_verbose() && fn_trait_kind.is_some() { - if let ty::Tuple(tys) = principal.substs.type_at(0).kind() { + if let ty::Tuple(tys) = principal.args.type_at(0).kind() { let mut projections = predicates.projection_bounds(); if let (Some(proj), None) = (projections.next(), projections.next()) { p!(pretty_fn_sig( @@ -1234,7 +1237,7 @@ pub trait PrettyPrinter<'tcx>: let args = cx .tcx() .generics_of(principal.def_id) - .own_substs_no_defaults(cx.tcx(), principal.substs); + .own_args_no_defaults(cx.tcx(), principal.args); let mut projections = predicates.projection_bounds(); @@ -1341,10 +1344,10 @@ pub trait PrettyPrinter<'tcx>: } match ct.kind() { - ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }) => { + ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, args }) => { match self.tcx().def_kind(def) { DefKind::Const | DefKind::AssocConst => { - p!(print_value_path(def, substs)) + p!(print_value_path(def, args)) } DefKind::AnonConst => { if def.is_local() @@ -1449,7 +1452,7 @@ pub trait PrettyPrinter<'tcx>: self.tcx().try_get_global_alloc(alloc_id) { self = self.typed_value( - |this| this.print_value_path(instance.def_id(), instance.substs), + |this| this.print_value_path(instance.def_id(), instance.args), |this| this.print_type(ty), " as ", )?; @@ -1497,7 +1500,7 @@ pub trait PrettyPrinter<'tcx>: let data = int.assert_bits(self.tcx().data_layout.pointer_size); self = self.typed_value( |mut this| { - write!(this, "0x{:x}", data)?; + write!(this, "0x{data:x}")?; Ok(this) }, |this| this.print_type(ty), @@ -1510,7 +1513,7 @@ pub trait PrettyPrinter<'tcx>: if int.size() == Size::ZERO { write!(this, "transmute(())")?; } else { - write!(this, "transmute(0x{:x})", int)?; + write!(this, "transmute(0x{int:x})")?; } Ok(this) }; @@ -1619,11 +1622,11 @@ pub trait PrettyPrinter<'tcx>: ": ", )?; } - ty::Adt(def, substs) => { + ty::Adt(def, args) => { let variant_idx = contents.variant.expect("destructed const of adt without variant idx"); let variant_def = &def.variant(variant_idx); - p!(print_value_path(variant_def.def_id, substs)); + p!(print_value_path(variant_def.def_id, args)); match variant_def.ctor_kind() { Some(CtorKind::Const) => {} Some(CtorKind::Fn) => { @@ -1673,7 +1676,7 @@ pub trait PrettyPrinter<'tcx>: fn pretty_closure_as_impl( mut self, - closure: ty::ClosureSubsts<'tcx>, + closure: ty::ClosureArgs<'tcx>, ) -> Result<Self::Const, Self::Error> { let sig = closure.sig(); let kind = closure.kind_ty().to_opt_closure_kind().unwrap_or(ty::ClosureKind::Fn); @@ -1798,29 +1801,29 @@ impl<'t> TyCtxt<'t> { /// Returns a string identifying this `DefId`. This string is /// suitable for user output. pub fn def_path_str(self, def_id: impl IntoQueryParam<DefId>) -> String { - self.def_path_str_with_substs(def_id, &[]) + self.def_path_str_with_args(def_id, &[]) } - pub fn def_path_str_with_substs( + pub fn def_path_str_with_args( self, def_id: impl IntoQueryParam<DefId>, - substs: &'t [GenericArg<'t>], + args: &'t [GenericArg<'t>], ) -> String { let def_id = def_id.into_query_param(); let ns = guess_def_namespace(self, def_id); debug!("def_path_str: def_id={:?}, ns={:?}", def_id, ns); - FmtPrinter::new(self, ns).print_def_path(def_id, substs).unwrap().into_buffer() + FmtPrinter::new(self, ns).print_def_path(def_id, args).unwrap().into_buffer() } - pub fn value_path_str_with_substs( + pub fn value_path_str_with_args( self, def_id: impl IntoQueryParam<DefId>, - substs: &'t [GenericArg<'t>], + args: &'t [GenericArg<'t>], ) -> String { let def_id = def_id.into_query_param(); let ns = guess_def_namespace(self, def_id); debug!("value_path_str: def_id={:?}, ns={:?}", def_id, ns); - FmtPrinter::new(self, ns).print_value_path(def_id, substs).unwrap().into_buffer() + FmtPrinter::new(self, ns).print_value_path(def_id, args).unwrap().into_buffer() } } @@ -1847,11 +1850,11 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { fn print_def_path( mut self, def_id: DefId, - substs: &'tcx [GenericArg<'tcx>], + args: &'tcx [GenericArg<'tcx>], ) -> Result<Self::Path, Self::Error> { define_scoped_cx!(self); - if substs.is_empty() { + if args.is_empty() { match self.try_print_trimmed_def_path(def_id)? { (cx, true) => return Ok(cx), (cx, false) => self = cx, @@ -1900,7 +1903,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { } } - self.default_print_def_path(def_id, substs) + self.default_print_def_path(def_id, args) } fn print_region(self, region: ty::Region<'tcx>) -> Result<Self::Region, Self::Error> { @@ -1932,7 +1935,7 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { fn path_crate(mut self, cnum: CrateNum) -> Result<Self::Path, Self::Error> { self.empty_path = true; if cnum == LOCAL_CRATE { - if self.tcx.sess.rust_2018() { + if self.tcx.sess.at_least_rust_2018() { // We add the `crate::` keyword on Rust 2018, only when desired. if SHOULD_PREFIX_WITH_CRATE.with(|flag| flag.get()) { write!(self, "{}", kw::Crate)?; @@ -2017,11 +2020,37 @@ impl<'tcx> Printer<'tcx> for FmtPrinter<'_, 'tcx> { ) -> Result<Self::Path, Self::Error> { self = print_prefix(self)?; - if args.first().is_some() { + let tcx = self.tcx; + + let args = args.iter().copied(); + + let args: Vec<_> = if !tcx.sess.verbose() { + // skip host param as those are printed as `~const` + args.filter(|arg| match arg.unpack() { + // FIXME(effects) there should be a better way than just matching the name + GenericArgKind::Const(c) + if tcx.features().effects + && matches!( + c.kind(), + ty::ConstKind::Param(ty::ParamConst { name: sym::host, .. }) + ) => + { + false + } + _ => true, + }) + .collect() + } else { + // If -Zverbose is passed, we should print the host parameter instead + // of eating it. + args.collect() + }; + + if !args.is_empty() { if self.in_value { write!(self, "::")?; } - self.generic_delimiters(|cx| cx.comma_sep(args.iter().cloned())) + self.generic_delimiters(|cx| cx.comma_sep(args.into_iter())) } else { Ok(self) } @@ -2044,10 +2073,10 @@ impl<'tcx> PrettyPrinter<'tcx> for FmtPrinter<'_, 'tcx> { fn print_value_path( mut self, def_id: DefId, - substs: &'tcx [GenericArg<'tcx>], + args: &'tcx [GenericArg<'tcx>], ) -> Result<Self::Path, Self::Error> { let was_in_value = std::mem::replace(&mut self.in_value, true); - self = self.print_def_path(def_id, substs)?; + self = self.print_def_path(def_id, args)?; self.in_value = was_in_value; Ok(self) @@ -2348,10 +2377,10 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { } else { cont }; - let _ = write!(cx, "{}", w); + let _ = write!(cx, "{w}"); }; let do_continue = |cx: &mut Self, cont: Symbol| { - let _ = write!(cx, "{}", cont); + let _ = write!(cx, "{cont}"); }; define_scoped_cx!(self); @@ -2387,7 +2416,7 @@ impl<'tcx> FmtPrinter<'_, 'tcx> { let (new_value, map) = if self.should_print_verbose() { for var in value.bound_vars().iter() { start_or_continue(&mut self, "for<", ", "); - write!(self, "{:?}", var)?; + write!(self, "{var:?}")?; } start_or_continue(&mut self, "", "> "); (value.clone().skip_binder(), BTreeMap::default()) @@ -2695,7 +2724,7 @@ impl<'tcx> ty::PolyTraitPredicate<'tcx> { #[derive(Debug, Copy, Clone, Lift)] pub struct PrintClosureAsImpl<'tcx> { - pub closure: ty::ClosureSubsts<'tcx>, + pub closure: ty::ClosureArgs<'tcx>, } forward_display_to_print! { @@ -2707,8 +2736,9 @@ forward_display_to_print! { // HACK(eddyb) these are exhaustive instead of generic, // because `for<'tcx>` isn't possible yet. ty::PolyExistentialPredicate<'tcx>, + ty::PolyExistentialProjection<'tcx>, + ty::PolyExistentialTraitRef<'tcx>, ty::Binder<'tcx, ty::TraitRef<'tcx>>, - ty::Binder<'tcx, ty::ExistentialTraitRef<'tcx>>, ty::Binder<'tcx, TraitRefPrintOnlyTraitPath<'tcx>>, ty::Binder<'tcx, TraitRefPrintOnlyTraitName<'tcx>>, ty::Binder<'tcx, ty::FnSig<'tcx>>, @@ -2771,7 +2801,7 @@ define_print_and_forward_display! { } TraitRefPrintOnlyTraitPath<'tcx> { - p!(print_def_path(self.0.def_id, self.0.substs)); + p!(print_def_path(self.0.def_id, self.0.args)); } TraitRefPrintOnlyTraitName<'tcx> { @@ -2779,10 +2809,7 @@ define_print_and_forward_display! { } TraitPredPrintModifiersAndPath<'tcx> { - if let ty::BoundConstness::ConstIfConst = self.0.constness { - p!("~const ") - } - + // FIXME(effects) print `~const` here if let ty::ImplPolarity::Negative = self.0.polarity { p!("!") } @@ -2816,9 +2843,12 @@ define_print_and_forward_display! { ty::TraitPredicate<'tcx> { p!(print(self.trait_ref.self_ty()), ": "); - if let ty::BoundConstness::ConstIfConst = self.constness && cx.tcx().features().const_trait_impl { - p!("~const "); + if let Some(idx) = cx.tcx().generics_of(self.trait_ref.def_id).host_effect_index { + if self.trait_ref.args.const_at(idx) != cx.tcx().consts.true_ { + p!("~const "); + } } + // FIXME(effects) print `~const` here if let ty::ImplPolarity::Negative = self.polarity { p!("!"); } @@ -2842,16 +2872,12 @@ define_print_and_forward_display! { if let DefKind::Impl { of_trait: false } = cx.tcx().def_kind(cx.tcx().parent(self.def_id)) { p!(pretty_print_inherent_projection(self)) } else { - p!(print_def_path(self.def_id, self.substs)); + p!(print_def_path(self.def_id, self.args)); } } ty::ClosureKind { - match *self { - ty::ClosureKind::Fn => p!("Fn"), - ty::ClosureKind::FnMut => p!("FnMut"), - ty::ClosureKind::FnOnce => p!("FnOnce"), - } + p!(write("{}", self.as_str())) } ty::Predicate<'tcx> { @@ -2891,7 +2917,7 @@ define_print_and_forward_display! { ty::PredicateKind::ObjectSafe(trait_def_id) => { p!("the trait `", print_def_path(trait_def_id, &[]), "` is object-safe") } - ty::PredicateKind::ClosureKind(closure_def_id, _closure_substs, kind) => p!( + ty::PredicateKind::ClosureKind(closure_def_id, _closure_args, kind) => p!( "the closure `", print_value_path(closure_def_id, &[]), write("` implements the trait `{}`", kind) @@ -2960,7 +2986,7 @@ fn for_each_def(tcx: TyCtxt<'_>, mut collect_fn: impl for<'b> FnMut(&'b Ident, N match child.res { def::Res::Def(DefKind::AssocTy, _) => {} - def::Res::Def(DefKind::TyAlias, _) => {} + def::Res::Def(DefKind::TyAlias { .. }, _) => {} def::Res::Def(defkind, def_id) => { if let Some(ns) = defkind.ns() { collect_fn(&child.ident, ns, def_id); diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index 5741832c9..47512d350 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -6,7 +6,7 @@ use crate::ty::error::{ExpectedFound, TypeError}; use crate::ty::{self, Expr, ImplSubject, Term, TermKind, Ty, TyCtxt, TypeFoldable}; -use crate::ty::{GenericArg, GenericArgKind, SubstsRef}; +use crate::ty::{GenericArg, GenericArgKind, GenericArgsRef}; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_target::spec::abi; @@ -43,23 +43,23 @@ pub trait TypeRelation<'tcx>: Sized { Relate::relate(self, a, b) } - /// Relate the two substitutions for the given item. The default + /// Relate the two args for the given item. The default /// is to look up the variance for the item and proceed /// accordingly. - fn relate_item_substs( + fn relate_item_args( &mut self, item_def_id: DefId, - a_subst: SubstsRef<'tcx>, - b_subst: SubstsRef<'tcx>, - ) -> RelateResult<'tcx, SubstsRef<'tcx>> { + a_arg: GenericArgsRef<'tcx>, + b_arg: GenericArgsRef<'tcx>, + ) -> RelateResult<'tcx, GenericArgsRef<'tcx>> { debug!( - "relate_item_substs(item_def_id={:?}, a_subst={:?}, b_subst={:?})", - item_def_id, a_subst, b_subst + "relate_item_args(item_def_id={:?}, a_arg={:?}, b_arg={:?})", + item_def_id, a_arg, b_arg ); let tcx = self.tcx(); let opt_variances = tcx.variances_of(item_def_id); - relate_substs_with_variances(self, item_def_id, opt_variances, a_subst, b_subst, true) + relate_args_with_variances(self, item_def_id, opt_variances, a_arg, b_arg, true) } /// Switch variance for the purpose of relating `a` and `b`. @@ -134,31 +134,32 @@ pub fn relate_type_and_mut<'tcx, R: TypeRelation<'tcx>>( } #[inline] -pub fn relate_substs<'tcx, R: TypeRelation<'tcx>>( +pub fn relate_args<'tcx, R: TypeRelation<'tcx>>( relation: &mut R, - a_subst: SubstsRef<'tcx>, - b_subst: SubstsRef<'tcx>, -) -> RelateResult<'tcx, SubstsRef<'tcx>> { - relation.tcx().mk_substs_from_iter(iter::zip(a_subst, b_subst).map(|(a, b)| { + a_arg: GenericArgsRef<'tcx>, + b_arg: GenericArgsRef<'tcx>, +) -> RelateResult<'tcx, GenericArgsRef<'tcx>> { + relation.tcx().mk_args_from_iter(iter::zip(a_arg, b_arg).map(|(a, b)| { relation.relate_with_variance(ty::Invariant, ty::VarianceDiagInfo::default(), a, b) })) } -pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>( +pub fn relate_args_with_variances<'tcx, R: TypeRelation<'tcx>>( relation: &mut R, ty_def_id: DefId, variances: &[ty::Variance], - a_subst: SubstsRef<'tcx>, - b_subst: SubstsRef<'tcx>, + a_arg: GenericArgsRef<'tcx>, + b_arg: GenericArgsRef<'tcx>, fetch_ty_for_diag: bool, -) -> RelateResult<'tcx, SubstsRef<'tcx>> { +) -> RelateResult<'tcx, GenericArgsRef<'tcx>> { let tcx = relation.tcx(); let mut cached_ty = None; - let params = iter::zip(a_subst, b_subst).enumerate().map(|(i, (a, b))| { + let params = iter::zip(a_arg, b_arg).enumerate().map(|(i, (a, b))| { let variance = variances[i]; let variance_info = if variance == ty::Invariant && fetch_ty_for_diag { - let ty = *cached_ty.get_or_insert_with(|| tcx.type_of(ty_def_id).subst(tcx, a_subst)); + let ty = + *cached_ty.get_or_insert_with(|| tcx.type_of(ty_def_id).instantiate(tcx, a_arg)); ty::VarianceDiagInfo::Invariant { ty, param_index: i.try_into().unwrap() } } else { ty::VarianceDiagInfo::default() @@ -166,7 +167,7 @@ pub fn relate_substs_with_variances<'tcx, R: TypeRelation<'tcx>>( relation.relate_with_variance(variance, variance_info, a, b) }); - tcx.mk_substs_from_iter(params) + tcx.mk_args_from_iter(params) } impl<'tcx> Relate<'tcx> for ty::FnSig<'tcx> { @@ -272,8 +273,8 @@ impl<'tcx> Relate<'tcx> for ty::AliasTy<'tcx> { if a.def_id != b.def_id { Err(TypeError::ProjectionMismatched(expected_found(relation, a.def_id, b.def_id))) } else { - let substs = relation.relate(a.substs, b.substs)?; - Ok(relation.tcx().mk_alias_ty(a.def_id, substs)) + let args = relation.relate(a.args, b.args)?; + Ok(relation.tcx().mk_alias_ty(a.def_id, args)) } } } @@ -293,13 +294,13 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialProjection<'tcx> { a.term, b.term, )?; - let substs = relation.relate_with_variance( + let args = relation.relate_with_variance( ty::Invariant, ty::VarianceDiagInfo::default(), - a.substs, - b.substs, + a.args, + b.args, )?; - Ok(ty::ExistentialProjection { def_id: a.def_id, substs, term }) + Ok(ty::ExistentialProjection { def_id: a.def_id, args, term }) } } } @@ -314,8 +315,8 @@ impl<'tcx> Relate<'tcx> for ty::TraitRef<'tcx> { if a.def_id != b.def_id { Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id))) } else { - let substs = relate_substs(relation, a.substs, b.substs)?; - Ok(ty::TraitRef::new(relation.tcx(), a.def_id, substs)) + let args = relate_args(relation, a.args, b.args)?; + Ok(ty::TraitRef::new(relation.tcx(), a.def_id, args)) } } } @@ -330,8 +331,8 @@ impl<'tcx> Relate<'tcx> for ty::ExistentialTraitRef<'tcx> { if a.def_id != b.def_id { Err(TypeError::Traits(expected_found(relation, a.def_id, b.def_id))) } else { - let substs = relate_substs(relation, a.substs, b.substs)?; - Ok(ty::ExistentialTraitRef { def_id: a.def_id, substs }) + let args = relate_args(relation, a.args, b.args)?; + Ok(ty::ExistentialTraitRef { def_id: a.def_id, args }) } } } @@ -426,9 +427,9 @@ pub fn structurally_relate_tys<'tcx, R: TypeRelation<'tcx>>( (ty::Placeholder(p1), ty::Placeholder(p2)) if p1 == p2 => Ok(a), - (&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs)) if a_def == b_def => { - let substs = relation.relate_item_substs(a_def.did(), a_substs, b_substs)?; - Ok(Ty::new_adt(tcx, a_def, substs)) + (&ty::Adt(a_def, a_args), &ty::Adt(b_def, b_args)) if a_def == b_def => { + let args = relation.relate_item_args(a_def.did(), a_args, b_args)?; + Ok(Ty::new_adt(tcx, a_def, args)) } (&ty::Foreign(a_id), &ty::Foreign(b_id)) if a_id == b_id => Ok(Ty::new_foreign(tcx, a_id)), @@ -442,14 +443,14 @@ pub fn structurally_relate_tys<'tcx, R: TypeRelation<'tcx>>( Ok(Ty::new_dynamic(tcx, relation.relate(a_obj, b_obj)?, region_bound, a_repr)) } - (&ty::Generator(a_id, a_substs, movability), &ty::Generator(b_id, b_substs, _)) + (&ty::Generator(a_id, a_args, movability), &ty::Generator(b_id, b_args, _)) if a_id == b_id => { // All Generator types with the same id represent // the (anonymous) type of the same generator expression. So // all of their regions should be equated. - let substs = relation.relate(a_substs, b_substs)?; - Ok(Ty::new_generator(tcx, a_id, substs, movability)) + let args = relation.relate(a_args, b_args)?; + Ok(Ty::new_generator(tcx, a_id, args, movability)) } (&ty::GeneratorWitness(a_types), &ty::GeneratorWitness(b_types)) => { @@ -462,22 +463,22 @@ pub fn structurally_relate_tys<'tcx, R: TypeRelation<'tcx>>( Ok(Ty::new_generator_witness(tcx, types)) } - (&ty::GeneratorWitnessMIR(a_id, a_substs), &ty::GeneratorWitnessMIR(b_id, b_substs)) + (&ty::GeneratorWitnessMIR(a_id, a_args), &ty::GeneratorWitnessMIR(b_id, b_args)) if a_id == b_id => { // All GeneratorWitness types with the same id represent // the (anonymous) type of the same generator expression. So // all of their regions should be equated. - let substs = relation.relate(a_substs, b_substs)?; - Ok(Ty::new_generator_witness_mir(tcx, a_id, substs)) + let args = relation.relate(a_args, b_args)?; + Ok(Ty::new_generator_witness_mir(tcx, a_id, args)) } - (&ty::Closure(a_id, a_substs), &ty::Closure(b_id, b_substs)) if a_id == b_id => { + (&ty::Closure(a_id, a_args), &ty::Closure(b_id, b_args)) if a_id == b_id => { // All Closure types with the same id represent // the (anonymous) type of the same closure expression. So // all of their regions should be equated. - let substs = relation.relate(a_substs, b_substs)?; - Ok(Ty::new_closure(tcx, a_id, &substs)) + let args = relation.relate(a_args, b_args)?; + Ok(Ty::new_closure(tcx, a_id, &args)) } (&ty::RawPtr(a_mt), &ty::RawPtr(b_mt)) => { @@ -535,11 +536,9 @@ pub fn structurally_relate_tys<'tcx, R: TypeRelation<'tcx>>( } } - (&ty::FnDef(a_def_id, a_substs), &ty::FnDef(b_def_id, b_substs)) - if a_def_id == b_def_id => - { - let substs = relation.relate_item_substs(a_def_id, a_substs, b_substs)?; - Ok(Ty::new_fn_def(tcx, a_def_id, substs)) + (&ty::FnDef(a_def_id, a_args), &ty::FnDef(b_def_id, b_args)) if a_def_id == b_def_id => { + let args = relation.relate_item_args(a_def_id, a_args, b_args)?; + Ok(Ty::new_fn_def(tcx, a_def_id, args)) } (&ty::FnPtr(a_fty), &ty::FnPtr(b_fty)) => { @@ -547,35 +546,29 @@ pub fn structurally_relate_tys<'tcx, R: TypeRelation<'tcx>>( Ok(Ty::new_fn_ptr(tcx, fty)) } - // The substs of opaque types may not all be invariant, so we have + // The args of opaque types may not all be invariant, so we have // to treat them separately from other aliases. ( - &ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, substs: a_substs, .. }), - &ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, substs: b_substs, .. }), + &ty::Alias(ty::Opaque, ty::AliasTy { def_id: a_def_id, args: a_args, .. }), + &ty::Alias(ty::Opaque, ty::AliasTy { def_id: b_def_id, args: b_args, .. }), ) if a_def_id == b_def_id => { let opt_variances = tcx.variances_of(a_def_id); - let substs = relate_substs_with_variances( + let args = relate_args_with_variances( relation, a_def_id, opt_variances, - a_substs, - b_substs, + a_args, + b_args, false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle )?; - Ok(Ty::new_opaque(tcx, a_def_id, substs)) + Ok(Ty::new_opaque(tcx, a_def_id, args)) } // Alias tend to mostly already be handled downstream due to normalization. (&ty::Alias(a_kind, a_data), &ty::Alias(b_kind, b_data)) => { - // FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty): This if can be removed - // and the assert uncommented once the new desugaring is stable. - if a_kind == b_kind { - let alias_ty = relation.relate(a_data, b_data)?; - // assert_eq!(a_kind, b_kind); - Ok(Ty::new_alias(tcx, a_kind, alias_ty)) - } else { - Err(TypeError::Sorts(expected_found(relation, a, b))) - } + let alias_ty = relation.relate(a_data, b_data)?; + assert_eq!(a_kind, b_kind); + Ok(Ty::new_alias(tcx, a_kind, alias_ty)) } _ => Err(TypeError::Sorts(expected_found(relation, a, b))), @@ -624,15 +617,15 @@ pub fn structurally_relate_consts<'tcx, R: TypeRelation<'tcx>>( // be stabilized. (ty::ConstKind::Unevaluated(au), ty::ConstKind::Unevaluated(bu)) if au.def == bu.def => { assert_eq!(a.ty(), b.ty()); - let substs = relation.relate_with_variance( + let args = relation.relate_with_variance( ty::Variance::Invariant, ty::VarianceDiagInfo::default(), - au.substs, - bu.substs, + au.args, + bu.args, )?; return Ok(ty::Const::new_unevaluated( tcx, - ty::UnevaluatedConst { def: au.def, substs }, + ty::UnevaluatedConst { def: au.def, args }, a.ty(), )); } @@ -646,7 +639,7 @@ pub fn structurally_relate_consts<'tcx, R: TypeRelation<'tcx>>( // FIXME(generic_const_exprs): relating the `ty()`s is a little weird since it is supposed to // ICE If they mismatch. Unfortunately `ConstKind::Expr` is a little special and can be thought // of as being generic over the argument types, however this is implicit so these types don't get - // related when we relate the substs of the item this const arg is for. + // related when we relate the args of the item this const arg is for. let expr = match (ae, be) { (Expr::Binop(a_op, al, ar), Expr::Binop(b_op, bl, br)) if a_op == b_op => { r.relate(al.ty(), bl.ty())?; @@ -720,35 +713,35 @@ impl<'tcx> Relate<'tcx> for &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>> { } } -impl<'tcx> Relate<'tcx> for ty::ClosureSubsts<'tcx> { +impl<'tcx> Relate<'tcx> for ty::ClosureArgs<'tcx> { fn relate<R: TypeRelation<'tcx>>( relation: &mut R, - a: ty::ClosureSubsts<'tcx>, - b: ty::ClosureSubsts<'tcx>, - ) -> RelateResult<'tcx, ty::ClosureSubsts<'tcx>> { - let substs = relate_substs(relation, a.substs, b.substs)?; - Ok(ty::ClosureSubsts { substs }) + a: ty::ClosureArgs<'tcx>, + b: ty::ClosureArgs<'tcx>, + ) -> RelateResult<'tcx, ty::ClosureArgs<'tcx>> { + let args = relate_args(relation, a.args, b.args)?; + Ok(ty::ClosureArgs { args }) } } -impl<'tcx> Relate<'tcx> for ty::GeneratorSubsts<'tcx> { +impl<'tcx> Relate<'tcx> for ty::GeneratorArgs<'tcx> { fn relate<R: TypeRelation<'tcx>>( relation: &mut R, - a: ty::GeneratorSubsts<'tcx>, - b: ty::GeneratorSubsts<'tcx>, - ) -> RelateResult<'tcx, ty::GeneratorSubsts<'tcx>> { - let substs = relate_substs(relation, a.substs, b.substs)?; - Ok(ty::GeneratorSubsts { substs }) + a: ty::GeneratorArgs<'tcx>, + b: ty::GeneratorArgs<'tcx>, + ) -> RelateResult<'tcx, ty::GeneratorArgs<'tcx>> { + let args = relate_args(relation, a.args, b.args)?; + Ok(ty::GeneratorArgs { args }) } } -impl<'tcx> Relate<'tcx> for SubstsRef<'tcx> { +impl<'tcx> Relate<'tcx> for GenericArgsRef<'tcx> { fn relate<R: TypeRelation<'tcx>>( relation: &mut R, - a: SubstsRef<'tcx>, - b: SubstsRef<'tcx>, - ) -> RelateResult<'tcx, SubstsRef<'tcx>> { - relate_substs(relation, a, b) + a: GenericArgsRef<'tcx>, + b: GenericArgsRef<'tcx>, + ) -> RelateResult<'tcx, GenericArgsRef<'tcx>> { + relate_args(relation, a, b) } } @@ -833,7 +826,6 @@ impl<'tcx> Relate<'tcx> for ty::TraitPredicate<'tcx> { ) -> RelateResult<'tcx, ty::TraitPredicate<'tcx>> { Ok(ty::TraitPredicate { trait_ref: relation.relate(a.trait_ref, b.trait_ref)?, - constness: relation.relate(a.constness, b.constness)?, polarity: relation.relate(a.polarity, b.polarity)?, }) } diff --git a/compiler/rustc_middle/src/ty/rvalue_scopes.rs b/compiler/rustc_middle/src/ty/rvalue_scopes.rs index e79b79a25..17eabec25 100644 --- a/compiler/rustc_middle/src/ty/rvalue_scopes.rs +++ b/compiler/rustc_middle/src/ty/rvalue_scopes.rs @@ -1,12 +1,12 @@ use crate::middle::region::{Scope, ScopeData, ScopeTree}; -use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; +use rustc_hir::ItemLocalMap; /// `RvalueScopes` is a mapping from sub-expressions to _extended_ lifetime as determined by /// rules laid out in `rustc_hir_analysis::check::rvalue_scopes`. #[derive(TyEncodable, TyDecodable, Clone, Debug, Default, Eq, PartialEq, HashStable)] pub struct RvalueScopes { - map: FxHashMap<hir::ItemLocalId, Option<Scope>>, + map: ItemLocalMap<Option<Scope>>, } impl RvalueScopes { diff --git a/compiler/rustc_middle/src/ty/structural_impls.rs b/compiler/rustc_middle/src/ty/structural_impls.rs index 7220d133f..f979ddd00 100644 --- a/compiler/rustc_middle/src/ty/structural_impls.rs +++ b/compiler/rustc_middle/src/ty/structural_impls.rs @@ -11,13 +11,15 @@ use crate::ty::{self, AliasTy, InferConst, Lift, Term, TermKind, Ty, TyCtxt}; use rustc_hir::def::Namespace; use rustc_index::{Idx, IndexVec}; use rustc_target::abi::TyAndLayout; -use rustc_type_ir::ConstKind; +use rustc_type_ir::{ConstKind, DebugWithInfcx, InferCtxtLike, OptWithInfcx}; -use std::fmt; +use std::fmt::{self, Debug}; use std::ops::ControlFlow; use std::rc::Rc; use std::sync::Arc; +use super::{GenericArg, GenericArgKind, Region}; + impl fmt::Debug for ty::TraitDef { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { ty::tls::with(|tcx| { @@ -71,9 +73,9 @@ impl fmt::Debug for ty::BoundRegionKind { ty::BrAnon(span) => write!(f, "BrAnon({span:?})"), ty::BrNamed(did, name) => { if did.is_crate_root() { - write!(f, "BrNamed({})", name) + write!(f, "BrNamed({name})") } else { - write!(f, "BrNamed({:?}, {})", did, name) + write!(f, "BrNamed({did:?}, {name})") } } ty::BrEnv => write!(f, "BrEnv"), @@ -89,7 +91,16 @@ impl fmt::Debug for ty::FreeRegion { impl<'tcx> fmt::Debug for ty::FnSig<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let ty::FnSig { inputs_and_output: _, c_variadic, unsafety, abi } = self; + OptWithInfcx::new_no_ctx(self).fmt(f) + } +} +impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::FnSig<'tcx> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + let sig = this.data; + let ty::FnSig { inputs_and_output: _, c_variadic, unsafety, abi } = sig; write!(f, "{}", unsafety.prefix_str())?; match abi { @@ -98,15 +109,15 @@ impl<'tcx> fmt::Debug for ty::FnSig<'tcx> { }; write!(f, "fn(")?; - let inputs = self.inputs(); + let inputs = sig.inputs(); match inputs.len() { 0 if *c_variadic => write!(f, "...)")?, 0 => write!(f, ")")?, _ => { - for ty in &self.inputs()[0..(self.inputs().len() - 1)] { - write!(f, "{ty:?}, ")?; + for ty in &sig.inputs()[0..(sig.inputs().len() - 1)] { + write!(f, "{:?}, ", &this.wrap(ty))?; } - write!(f, "{:?}", self.inputs().last().unwrap())?; + write!(f, "{:?}", &this.wrap(sig.inputs().last().unwrap()))?; if *c_variadic { write!(f, "...")?; } @@ -114,9 +125,9 @@ impl<'tcx> fmt::Debug for ty::FnSig<'tcx> { } } - match self.output().kind() { + match sig.output().kind() { ty::Tuple(list) if list.is_empty() => Ok(()), - _ => write!(f, " -> {:?}", self.output()), + _ => write!(f, " -> {:?}", &this.wrap(sig.output())), } } } @@ -133,6 +144,14 @@ impl<'tcx> fmt::Debug for ty::TraitRef<'tcx> { } } +impl<'tcx> ty::DebugWithInfcx<TyCtxt<'tcx>> for Ty<'tcx> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + this.data.fmt(f) + } +} impl<'tcx> fmt::Debug for Ty<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { with_no_trimmed_paths!(fmt::Display::fmt(self, f)) @@ -153,9 +172,7 @@ impl fmt::Debug for ty::ParamConst { impl<'tcx> fmt::Debug for ty::TraitPredicate<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - if let ty::BoundConstness::ConstIfConst = self.constness { - write!(f, "~const ")?; - } + // FIXME(effects) printing? write!(f, "TraitPredicate({:?}, polarity:{:?})", self.trait_ref, self.polarity) } } @@ -186,7 +203,7 @@ impl<'tcx> fmt::Debug for ty::ClauseKind<'tcx> { ty::ClauseKind::RegionOutlives(ref pair) => pair.fmt(f), ty::ClauseKind::TypeOutlives(ref pair) => pair.fmt(f), ty::ClauseKind::Projection(ref pair) => pair.fmt(f), - ty::ClauseKind::WellFormed(ref data) => write!(f, "WellFormed({:?})", data), + ty::ClauseKind::WellFormed(ref data) => write!(f, "WellFormed({data:?})"), ty::ClauseKind::ConstEvaluatable(ct) => { write!(f, "ConstEvaluatable({ct:?})") } @@ -201,12 +218,12 @@ impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> { ty::PredicateKind::Subtype(ref pair) => pair.fmt(f), ty::PredicateKind::Coerce(ref pair) => pair.fmt(f), ty::PredicateKind::ObjectSafe(trait_def_id) => { - write!(f, "ObjectSafe({:?})", trait_def_id) + write!(f, "ObjectSafe({trait_def_id:?})") } - ty::PredicateKind::ClosureKind(closure_def_id, closure_substs, kind) => { - write!(f, "ClosureKind({:?}, {:?}, {:?})", closure_def_id, closure_substs, kind) + ty::PredicateKind::ClosureKind(closure_def_id, closure_args, kind) => { + write!(f, "ClosureKind({closure_def_id:?}, {closure_args:?}, {kind:?})") } - ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({:?}, {:?})", c1, c2), + ty::PredicateKind::ConstEquate(c1, c2) => write!(f, "ConstEquate({c1:?}, {c2:?})"), ty::PredicateKind::Ambiguous => write!(f, "Ambiguous"), ty::PredicateKind::AliasRelate(t1, t2, dir) => { write!(f, "AliasRelate({t1:?}, {dir:?}, {t2:?})") @@ -217,9 +234,17 @@ impl<'tcx> fmt::Debug for ty::PredicateKind<'tcx> { impl<'tcx> fmt::Debug for AliasTy<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + OptWithInfcx::new_no_ctx(self).fmt(f) + } +} +impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for AliasTy<'tcx> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { f.debug_struct("AliasTy") - .field("substs", &self.substs) - .field("def_id", &self.def_id) + .field("args", &this.map(|data| data.args)) + .field("def_id", &this.data.def_id) .finish() } } @@ -232,13 +257,93 @@ impl<'tcx> fmt::Debug for ty::InferConst<'tcx> { } } } +impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::InferConst<'tcx> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + use ty::InferConst::*; + match this.infcx.and_then(|infcx| infcx.universe_of_ct(*this.data)) { + None => write!(f, "{:?}", this.data), + Some(universe) => match *this.data { + Var(vid) => write!(f, "?{}_{}c", vid.index, universe.index()), + Fresh(_) => { + unreachable!() + } + }, + } + } +} + +impl<'tcx> fmt::Debug for ty::consts::Expr<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + OptWithInfcx::new_no_ctx(self).fmt(f) + } +} +impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::consts::Expr<'tcx> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + match this.data { + ty::Expr::Binop(op, lhs, rhs) => { + write!(f, "({op:?}: {:?}, {:?})", &this.wrap(lhs), &this.wrap(rhs)) + } + ty::Expr::UnOp(op, rhs) => write!(f, "({op:?}: {:?})", &this.wrap(rhs)), + ty::Expr::FunctionCall(func, args) => { + write!(f, "{:?}(", &this.wrap(func))?; + for arg in args.as_slice().iter().rev().skip(1).rev() { + write!(f, "{:?}, ", &this.wrap(arg))?; + } + if let Some(arg) = args.last() { + write!(f, "{:?}", &this.wrap(arg))?; + } + + write!(f, ")") + } + ty::Expr::Cast(cast_kind, lhs, rhs) => { + write!(f, "({cast_kind:?}: {:?}, {:?})", &this.wrap(lhs), &this.wrap(rhs)) + } + } + } +} + +impl<'tcx> fmt::Debug for ty::UnevaluatedConst<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + OptWithInfcx::new_no_ctx(self).fmt(f) + } +} +impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::UnevaluatedConst<'tcx> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + f.debug_struct("UnevaluatedConst") + .field("def", &this.data.def) + .field("args", &this.wrap(this.data.args)) + .finish() + } +} impl<'tcx> fmt::Debug for ty::Const<'tcx> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + OptWithInfcx::new_no_ctx(self).fmt(f) + } +} +impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::Const<'tcx> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { // This reflects what `Const` looked liked before `Interned` was // introduced. We print it like this to avoid having to update expected // output in a lot of tests. - write!(f, "Const {{ ty: {:?}, kind: {:?} }}", self.ty(), self.kind()) + write!( + f, + "Const {{ ty: {:?}, kind: {:?} }}", + &this.map(|data| data.ty()), + &this.map(|data| data.kind()) + ) } } @@ -261,6 +366,66 @@ impl<T: fmt::Debug> fmt::Debug for ty::Placeholder<T> { } } +impl<'tcx> fmt::Debug for GenericArg<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.unpack() { + GenericArgKind::Lifetime(lt) => lt.fmt(f), + GenericArgKind::Type(ty) => ty.fmt(f), + GenericArgKind::Const(ct) => ct.fmt(f), + } + } +} +impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for GenericArg<'tcx> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + match this.data.unpack() { + GenericArgKind::Lifetime(lt) => write!(f, "{:?}", &this.wrap(lt)), + GenericArgKind::Const(ct) => write!(f, "{:?}", &this.wrap(ct)), + GenericArgKind::Type(ty) => write!(f, "{:?}", &this.wrap(ty)), + } + } +} + +impl<'tcx> fmt::Debug for Region<'tcx> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{:?}", self.kind()) + } +} +impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for Region<'tcx> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + write!(f, "{:?}", &this.map(|data| data.kind())) + } +} + +impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ty::RegionVid { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + match this.infcx.and_then(|infcx| infcx.universe_of_lt(*this.data)) { + Some(universe) => write!(f, "'?{}_{}", this.data.index(), universe.index()), + None => write!(f, "{:?}", this.data), + } + } +} + +impl<'tcx, T: DebugWithInfcx<TyCtxt<'tcx>>> DebugWithInfcx<TyCtxt<'tcx>> for ty::Binder<'tcx, T> { + fn fmt<InfCtx: InferCtxtLike<TyCtxt<'tcx>>>( + this: OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + f.debug_tuple("Binder") + .field(&this.map(|data| data.as_ref().skip_binder())) + .field(&this.data.bound_vars()) + .finish() + } +} + /////////////////////////////////////////////////////////////////////////// // Atomic structs // @@ -273,7 +438,6 @@ CloneLiftImpls! { (), bool, usize, - u8, u16, u32, u64, @@ -303,10 +467,8 @@ TrivialTypeTraversalAndLiftImpls! { ::rustc_hir::Unsafety, ::rustc_target::asm::InlineAsmRegOrRegClass, ::rustc_target::spec::abi::Abi, - crate::mir::coverage::ExpressionOperandId, - crate::mir::coverage::CounterValueReference, - crate::mir::coverage::InjectedExpressionId, - crate::mir::coverage::InjectedExpressionIndex, + crate::mir::coverage::CounterId, + crate::mir::coverage::ExpressionId, crate::mir::coverage::MappedExpressionIndex, crate::mir::Local, crate::mir::Promoted, @@ -435,7 +597,7 @@ impl<'a, 'tcx> Lift<'tcx> for ty::ParamEnv<'a> { type Lifted = ty::ParamEnv<'tcx>; fn lift_to_tcx(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> { tcx.lift(self.caller_bounds()) - .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal(), self.constness())) + .map(|caller_bounds| ty::ParamEnv::new(caller_bounds, self.reveal())) } } @@ -528,26 +690,26 @@ impl<'tcx> TypeSuperFoldable<TyCtxt<'tcx>> for Ty<'tcx> { ty::RawPtr(tm) => ty::RawPtr(tm.try_fold_with(folder)?), ty::Array(typ, sz) => ty::Array(typ.try_fold_with(folder)?, sz.try_fold_with(folder)?), ty::Slice(typ) => ty::Slice(typ.try_fold_with(folder)?), - ty::Adt(tid, substs) => ty::Adt(tid, substs.try_fold_with(folder)?), + ty::Adt(tid, args) => ty::Adt(tid, args.try_fold_with(folder)?), ty::Dynamic(trait_ty, region, representation) => ty::Dynamic( trait_ty.try_fold_with(folder)?, region.try_fold_with(folder)?, representation, ), ty::Tuple(ts) => ty::Tuple(ts.try_fold_with(folder)?), - ty::FnDef(def_id, substs) => ty::FnDef(def_id, substs.try_fold_with(folder)?), + ty::FnDef(def_id, args) => ty::FnDef(def_id, args.try_fold_with(folder)?), ty::FnPtr(f) => ty::FnPtr(f.try_fold_with(folder)?), ty::Ref(r, ty, mutbl) => { ty::Ref(r.try_fold_with(folder)?, ty.try_fold_with(folder)?, mutbl) } - ty::Generator(did, substs, movability) => { - ty::Generator(did, substs.try_fold_with(folder)?, movability) + ty::Generator(did, args, movability) => { + ty::Generator(did, args.try_fold_with(folder)?, movability) } ty::GeneratorWitness(types) => ty::GeneratorWitness(types.try_fold_with(folder)?), - ty::GeneratorWitnessMIR(did, substs) => { - ty::GeneratorWitnessMIR(did, substs.try_fold_with(folder)?) + ty::GeneratorWitnessMIR(did, args) => { + ty::GeneratorWitnessMIR(did, args.try_fold_with(folder)?) } - ty::Closure(did, substs) => ty::Closure(did, substs.try_fold_with(folder)?), + ty::Closure(did, args) => ty::Closure(did, args.try_fold_with(folder)?), ty::Alias(kind, data) => ty::Alias(kind, data.try_fold_with(folder)?), ty::Bool @@ -581,22 +743,22 @@ impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for Ty<'tcx> { sz.visit_with(visitor) } ty::Slice(typ) => typ.visit_with(visitor), - ty::Adt(_, substs) => substs.visit_with(visitor), + ty::Adt(_, args) => args.visit_with(visitor), ty::Dynamic(ref trait_ty, ref reg, _) => { trait_ty.visit_with(visitor)?; reg.visit_with(visitor) } ty::Tuple(ts) => ts.visit_with(visitor), - ty::FnDef(_, substs) => substs.visit_with(visitor), + ty::FnDef(_, args) => args.visit_with(visitor), ty::FnPtr(ref f) => f.visit_with(visitor), ty::Ref(r, ty, _) => { r.visit_with(visitor)?; ty.visit_with(visitor) } - ty::Generator(_did, ref substs, _) => substs.visit_with(visitor), + ty::Generator(_did, ref args, _) => args.visit_with(visitor), ty::GeneratorWitness(ref types) => types.visit_with(visitor), - ty::GeneratorWitnessMIR(_did, ref substs) => substs.visit_with(visitor), - ty::Closure(_did, ref substs) => substs.visit_with(visitor), + ty::GeneratorWitnessMIR(_did, ref args) => args.visit_with(visitor), + ty::Closure(_did, ref args) => args.visit_with(visitor), ty::Alias(_, ref data) => data.visit_with(visitor), ty::Bool @@ -776,7 +938,7 @@ impl<'tcx> TypeSuperVisitable<TyCtxt<'tcx>> for ty::UnevaluatedConst<'tcx> { &self, visitor: &mut V, ) -> ControlFlow<V::BreakTy> { - self.substs.visit_with(visitor) + self.args.visit_with(visitor) } } diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index 94746fbdc..0291cdd6c 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -3,19 +3,18 @@ #![allow(rustc::usage_of_ty_tykind)] use crate::infer::canonical::Canonical; -use crate::ty::subst::{GenericArg, InternalSubsts, SubstsRef}; use crate::ty::visit::ValidateBoundVars; use crate::ty::InferTy::*; use crate::ty::{ self, AdtDef, Discr, Term, Ty, TyCtxt, TypeFlags, TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeVisitor, }; +use crate::ty::{GenericArg, GenericArgs, GenericArgsRef}; use crate::ty::{List, ParamEnv}; use hir::def::DefKind; use polonius_engine::Atom; use rustc_data_structures::captures::Captures; use rustc_data_structures::intern::Interned; -use rustc_error_messages::DiagnosticMessage; use rustc_errors::{DiagnosticArgValue, ErrorGuaranteed, IntoDiagnosticArg, MultiSpan}; use rustc_hir as hir; use rustc_hir::def_id::DefId; @@ -35,9 +34,12 @@ use std::ops::{ControlFlow, Deref, Range}; use ty::util::IntTypeExt; use rustc_type_ir::sty::TyKind::*; +use rustc_type_ir::CollectAndApply; +use rustc_type_ir::ConstKind as IrConstKind; +use rustc_type_ir::DebugWithInfcx; +use rustc_type_ir::DynKind; +use rustc_type_ir::RegionKind as IrRegionKind; use rustc_type_ir::TyKind as IrTyKind; -use rustc_type_ir::{CollectAndApply, ConstKind as IrConstKind}; -use rustc_type_ir::{DynKind, RegionKind as IrRegionKind}; use super::GenericParamDefKind; @@ -215,7 +217,7 @@ impl<'tcx> Article for TyKind<'tcx> { /// /// ## Generators /// -/// Generators are handled similarly in `GeneratorSubsts`. The set of +/// Generators are handled similarly in `GeneratorArgs`. The set of /// type parameters is similar, but `CK` and `CS` are replaced by the /// following type parameters: /// @@ -228,33 +230,30 @@ impl<'tcx> Article for TyKind<'tcx> { /// completion of the generator. /// * `GW`: The "generator witness". #[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable, Lift)] -pub struct ClosureSubsts<'tcx> { +pub struct ClosureArgs<'tcx> { /// Lifetime and type parameters from the enclosing function, /// concatenated with a tuple containing the types of the upvars. /// /// These are separated out because codegen wants to pass them around /// when monomorphizing. - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, } /// Struct returned by `split()`. -pub struct ClosureSubstsParts<'tcx, T> { - pub parent_substs: &'tcx [GenericArg<'tcx>], +pub struct ClosureArgsParts<'tcx, T> { + pub parent_args: &'tcx [GenericArg<'tcx>], pub closure_kind_ty: T, pub closure_sig_as_fn_ptr_ty: T, pub tupled_upvars_ty: T, } -impl<'tcx> ClosureSubsts<'tcx> { - /// Construct `ClosureSubsts` from `ClosureSubstsParts`, containing `Substs` +impl<'tcx> ClosureArgs<'tcx> { + /// Construct `ClosureArgs` from `ClosureArgsParts`, containing `Args` /// for the closure parent, alongside additional closure-specific components. - pub fn new( - tcx: TyCtxt<'tcx>, - parts: ClosureSubstsParts<'tcx, Ty<'tcx>>, - ) -> ClosureSubsts<'tcx> { - ClosureSubsts { - substs: tcx.mk_substs_from_iter( - parts.parent_substs.iter().copied().chain( + pub fn new(tcx: TyCtxt<'tcx>, parts: ClosureArgsParts<'tcx, Ty<'tcx>>) -> ClosureArgs<'tcx> { + ClosureArgs { + args: tcx.mk_args_from_iter( + parts.parent_args.iter().copied().chain( [parts.closure_kind_ty, parts.closure_sig_as_fn_ptr_ty, parts.tupled_upvars_ty] .iter() .map(|&ty| ty.into()), @@ -263,53 +262,47 @@ impl<'tcx> ClosureSubsts<'tcx> { } } - /// Divides the closure substs into their respective components. - /// The ordering assumed here must match that used by `ClosureSubsts::new` above. - fn split(self) -> ClosureSubstsParts<'tcx, GenericArg<'tcx>> { - match self.substs[..] { - [ - ref parent_substs @ .., - closure_kind_ty, - closure_sig_as_fn_ptr_ty, - tupled_upvars_ty, - ] => ClosureSubstsParts { - parent_substs, - closure_kind_ty, - closure_sig_as_fn_ptr_ty, - tupled_upvars_ty, - }, - _ => bug!("closure substs missing synthetics"), + /// Divides the closure args into their respective components. + /// The ordering assumed here must match that used by `ClosureArgs::new` above. + fn split(self) -> ClosureArgsParts<'tcx, GenericArg<'tcx>> { + match self.args[..] { + [ref parent_args @ .., closure_kind_ty, closure_sig_as_fn_ptr_ty, tupled_upvars_ty] => { + ClosureArgsParts { + parent_args, + closure_kind_ty, + closure_sig_as_fn_ptr_ty, + tupled_upvars_ty, + } + } + _ => bug!("closure args missing synthetics"), } } /// Returns `true` only if enough of the synthetic types are known to - /// allow using all of the methods on `ClosureSubsts` without panicking. + /// allow using all of the methods on `ClosureArgs` without panicking. /// /// Used primarily by `ty::print::pretty` to be able to handle closure /// types that haven't had their synthetic types substituted in. pub fn is_valid(self) -> bool { - self.substs.len() >= 3 - && matches!(self.split().tupled_upvars_ty.expect_ty().kind(), Tuple(_)) + self.args.len() >= 3 && matches!(self.split().tupled_upvars_ty.expect_ty().kind(), Tuple(_)) } /// Returns the substitutions of the closure's parent. - pub fn parent_substs(self) -> &'tcx [GenericArg<'tcx>] { - self.split().parent_substs + pub fn parent_args(self) -> &'tcx [GenericArg<'tcx>] { + self.split().parent_args } /// Returns an iterator over the list of types of captured paths by the closure. /// In case there was a type error in figuring out the types of the captured path, an /// empty iterator is returned. #[inline] - pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx { + pub fn upvar_tys(self) -> &'tcx List<Ty<'tcx>> { match self.tupled_upvars_ty().kind() { - TyKind::Error(_) => None, - TyKind::Tuple(..) => Some(self.tupled_upvars_ty().tuple_fields()), + TyKind::Error(_) => ty::List::empty(), + TyKind::Tuple(..) => self.tupled_upvars_ty().tuple_fields(), TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"), ty => bug!("Unexpected representation of upvar types tuple {:?}", ty), } - .into_iter() - .flatten() } /// Returns the tuple type representing the upvars for this closure. @@ -320,7 +313,7 @@ impl<'tcx> ClosureSubsts<'tcx> { /// Returns the closure kind for this closure; may return a type /// variable during inference. To get the closure kind during - /// inference, use `infcx.closure_kind(substs)`. + /// inference, use `infcx.closure_kind(args)`. pub fn kind_ty(self) -> Ty<'tcx> { self.split().closure_kind_ty.expect_ty() } @@ -328,7 +321,7 @@ impl<'tcx> ClosureSubsts<'tcx> { /// Returns the `fn` pointer type representing the closure signature for this /// closure. // FIXME(eddyb) this should be unnecessary, as the shallowly resolved - // type is known at the time of the creation of `ClosureSubsts`, + // type is known at the time of the creation of `ClosureArgs`, // see `rustc_hir_analysis::check::closure`. pub fn sig_as_fn_ptr_ty(self) -> Ty<'tcx> { self.split().closure_sig_as_fn_ptr_ty.expect_ty() @@ -357,14 +350,14 @@ impl<'tcx> ClosureSubsts<'tcx> { } } -/// Similar to `ClosureSubsts`; see the above documentation for more. +/// Similar to `ClosureArgs`; see the above documentation for more. #[derive(Copy, Clone, PartialEq, Eq, Debug, TypeFoldable, TypeVisitable, Lift)] -pub struct GeneratorSubsts<'tcx> { - pub substs: SubstsRef<'tcx>, +pub struct GeneratorArgs<'tcx> { + pub args: GenericArgsRef<'tcx>, } -pub struct GeneratorSubstsParts<'tcx, T> { - pub parent_substs: &'tcx [GenericArg<'tcx>], +pub struct GeneratorArgsParts<'tcx, T> { + pub parent_args: &'tcx [GenericArg<'tcx>], pub resume_ty: T, pub yield_ty: T, pub return_ty: T, @@ -372,16 +365,16 @@ pub struct GeneratorSubstsParts<'tcx, T> { pub tupled_upvars_ty: T, } -impl<'tcx> GeneratorSubsts<'tcx> { - /// Construct `GeneratorSubsts` from `GeneratorSubstsParts`, containing `Substs` +impl<'tcx> GeneratorArgs<'tcx> { + /// Construct `GeneratorArgs` from `GeneratorArgsParts`, containing `Args` /// for the generator parent, alongside additional generator-specific components. pub fn new( tcx: TyCtxt<'tcx>, - parts: GeneratorSubstsParts<'tcx, Ty<'tcx>>, - ) -> GeneratorSubsts<'tcx> { - GeneratorSubsts { - substs: tcx.mk_substs_from_iter( - parts.parent_substs.iter().copied().chain( + parts: GeneratorArgsParts<'tcx, Ty<'tcx>>, + ) -> GeneratorArgs<'tcx> { + GeneratorArgs { + args: tcx.mk_args_from_iter( + parts.parent_args.iter().copied().chain( [ parts.resume_ty, parts.yield_ty, @@ -396,13 +389,13 @@ impl<'tcx> GeneratorSubsts<'tcx> { } } - /// Divides the generator substs into their respective components. - /// The ordering assumed here must match that used by `GeneratorSubsts::new` above. - fn split(self) -> GeneratorSubstsParts<'tcx, GenericArg<'tcx>> { - match self.substs[..] { - [ref parent_substs @ .., resume_ty, yield_ty, return_ty, witness, tupled_upvars_ty] => { - GeneratorSubstsParts { - parent_substs, + /// Divides the generator args into their respective components. + /// The ordering assumed here must match that used by `GeneratorArgs::new` above. + fn split(self) -> GeneratorArgsParts<'tcx, GenericArg<'tcx>> { + match self.args[..] { + [ref parent_args @ .., resume_ty, yield_ty, return_ty, witness, tupled_upvars_ty] => { + GeneratorArgsParts { + parent_args, resume_ty, yield_ty, return_ty, @@ -410,23 +403,22 @@ impl<'tcx> GeneratorSubsts<'tcx> { tupled_upvars_ty, } } - _ => bug!("generator substs missing synthetics"), + _ => bug!("generator args missing synthetics"), } } /// Returns `true` only if enough of the synthetic types are known to - /// allow using all of the methods on `GeneratorSubsts` without panicking. + /// allow using all of the methods on `GeneratorArgs` without panicking. /// /// Used primarily by `ty::print::pretty` to be able to handle generator /// types that haven't had their synthetic types substituted in. pub fn is_valid(self) -> bool { - self.substs.len() >= 5 - && matches!(self.split().tupled_upvars_ty.expect_ty().kind(), Tuple(_)) + self.args.len() >= 5 && matches!(self.split().tupled_upvars_ty.expect_ty().kind(), Tuple(_)) } /// Returns the substitutions of the generator's parent. - pub fn parent_substs(self) -> &'tcx [GenericArg<'tcx>] { - self.split().parent_substs + pub fn parent_args(self) -> &'tcx [GenericArg<'tcx>] { + self.split().parent_args } /// This describes the types that can be contained in a generator. @@ -442,15 +434,13 @@ impl<'tcx> GeneratorSubsts<'tcx> { /// In case there was a type error in figuring out the types of the captured path, an /// empty iterator is returned. #[inline] - pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx { + pub fn upvar_tys(self) -> &'tcx List<Ty<'tcx>> { match self.tupled_upvars_ty().kind() { - TyKind::Error(_) => None, - TyKind::Tuple(..) => Some(self.tupled_upvars_ty().tuple_fields()), + TyKind::Error(_) => ty::List::empty(), + TyKind::Tuple(..) => self.tupled_upvars_ty().tuple_fields(), TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"), ty => bug!("Unexpected representation of upvar types tuple {:?}", ty), } - .into_iter() - .flatten() } /// Returns the tuple type representing the upvars for this generator. @@ -495,7 +485,7 @@ impl<'tcx> GeneratorSubsts<'tcx> { } } -impl<'tcx> GeneratorSubsts<'tcx> { +impl<'tcx> GeneratorArgs<'tcx> { /// Generator has not been resumed yet. pub const UNRESUMED: usize = 0; /// Generator has returned or is completed. @@ -574,7 +564,7 @@ impl<'tcx> GeneratorSubsts<'tcx> { let layout = tcx.generator_layout(def_id).unwrap(); layout.variant_fields.iter().map(move |variant| { variant.iter().map(move |field| { - ty::EarlyBinder::bind(layout.field_tys[*field].ty).subst(tcx, self.substs) + ty::EarlyBinder::bind(layout.field_tys[*field].ty).instantiate(tcx, self.args) }) }) } @@ -582,43 +572,41 @@ impl<'tcx> GeneratorSubsts<'tcx> { /// This is the types of the fields of a generator which are not stored in a /// variant. #[inline] - pub fn prefix_tys(self) -> impl Iterator<Item = Ty<'tcx>> { + pub fn prefix_tys(self) -> &'tcx List<Ty<'tcx>> { self.upvar_tys() } } #[derive(Debug, Copy, Clone, HashStable)] -pub enum UpvarSubsts<'tcx> { - Closure(SubstsRef<'tcx>), - Generator(SubstsRef<'tcx>), +pub enum UpvarArgs<'tcx> { + Closure(GenericArgsRef<'tcx>), + Generator(GenericArgsRef<'tcx>), } -impl<'tcx> UpvarSubsts<'tcx> { +impl<'tcx> UpvarArgs<'tcx> { /// Returns an iterator over the list of types of captured paths by the closure/generator. /// In case there was a type error in figuring out the types of the captured path, an /// empty iterator is returned. #[inline] - pub fn upvar_tys(self) -> impl Iterator<Item = Ty<'tcx>> + 'tcx { + pub fn upvar_tys(self) -> &'tcx List<Ty<'tcx>> { let tupled_tys = match self { - UpvarSubsts::Closure(substs) => substs.as_closure().tupled_upvars_ty(), - UpvarSubsts::Generator(substs) => substs.as_generator().tupled_upvars_ty(), + UpvarArgs::Closure(args) => args.as_closure().tupled_upvars_ty(), + UpvarArgs::Generator(args) => args.as_generator().tupled_upvars_ty(), }; match tupled_tys.kind() { - TyKind::Error(_) => None, - TyKind::Tuple(..) => Some(self.tupled_upvars_ty().tuple_fields()), + TyKind::Error(_) => ty::List::empty(), + TyKind::Tuple(..) => self.tupled_upvars_ty().tuple_fields(), TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"), ty => bug!("Unexpected representation of upvar types tuple {:?}", ty), } - .into_iter() - .flatten() } #[inline] pub fn tupled_upvars_ty(self) -> Ty<'tcx> { match self { - UpvarSubsts::Closure(substs) => substs.as_closure().tupled_upvars_ty(), - UpvarSubsts::Generator(substs) => substs.as_generator().tupled_upvars_ty(), + UpvarArgs::Closure(args) => args.as_closure().tupled_upvars_ty(), + UpvarArgs::Generator(args) => args.as_generator().tupled_upvars_ty(), } } } @@ -635,46 +623,46 @@ impl<'tcx> UpvarSubsts<'tcx> { /// /// When the inline const is instantiated, `R` is substituted as the actual inferred /// type of the constant. The reason that `R` is represented as an extra type parameter -/// is the same reason that [`ClosureSubsts`] have `CS` and `U` as type parameters: +/// is the same reason that [`ClosureArgs`] have `CS` and `U` as type parameters: /// inline const can reference lifetimes that are internal to the creating function. #[derive(Copy, Clone, Debug)] -pub struct InlineConstSubsts<'tcx> { +pub struct InlineConstArgs<'tcx> { /// Generic parameters from the enclosing item, /// concatenated with the inferred type of the constant. - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, } /// Struct returned by `split()`. -pub struct InlineConstSubstsParts<'tcx, T> { - pub parent_substs: &'tcx [GenericArg<'tcx>], +pub struct InlineConstArgsParts<'tcx, T> { + pub parent_args: &'tcx [GenericArg<'tcx>], pub ty: T, } -impl<'tcx> InlineConstSubsts<'tcx> { - /// Construct `InlineConstSubsts` from `InlineConstSubstsParts`. +impl<'tcx> InlineConstArgs<'tcx> { + /// Construct `InlineConstArgs` from `InlineConstArgsParts`. pub fn new( tcx: TyCtxt<'tcx>, - parts: InlineConstSubstsParts<'tcx, Ty<'tcx>>, - ) -> InlineConstSubsts<'tcx> { - InlineConstSubsts { - substs: tcx.mk_substs_from_iter( - parts.parent_substs.iter().copied().chain(std::iter::once(parts.ty.into())), + parts: InlineConstArgsParts<'tcx, Ty<'tcx>>, + ) -> InlineConstArgs<'tcx> { + InlineConstArgs { + args: tcx.mk_args_from_iter( + parts.parent_args.iter().copied().chain(std::iter::once(parts.ty.into())), ), } } - /// Divides the inline const substs into their respective components. - /// The ordering assumed here must match that used by `InlineConstSubsts::new` above. - fn split(self) -> InlineConstSubstsParts<'tcx, GenericArg<'tcx>> { - match self.substs[..] { - [ref parent_substs @ .., ty] => InlineConstSubstsParts { parent_substs, ty }, - _ => bug!("inline const substs missing synthetics"), + /// Divides the inline const args into their respective components. + /// The ordering assumed here must match that used by `InlineConstArgs::new` above. + fn split(self) -> InlineConstArgsParts<'tcx, GenericArg<'tcx>> { + match self.args[..] { + [ref parent_args @ .., ty] => InlineConstArgsParts { parent_args, ty }, + _ => bug!("inline const args missing synthetics"), } } /// Returns the substitutions of the inline const's parent. - pub fn parent_substs(self) -> &'tcx [GenericArg<'tcx>] { - self.split().parent_substs + pub fn parent_args(self) -> &'tcx [GenericArg<'tcx>] { + self.split().parent_args } /// Returns the type of this inline const. @@ -694,6 +682,15 @@ pub enum ExistentialPredicate<'tcx> { AutoTrait(DefId), } +impl<'tcx> DebugWithInfcx<TyCtxt<'tcx>> for ExistentialPredicate<'tcx> { + fn fmt<InfCtx: rustc_type_ir::InferCtxtLike<TyCtxt<'tcx>>>( + this: rustc_type_ir::OptWithInfcx<'_, TyCtxt<'tcx>, InfCtx, &Self>, + f: &mut core::fmt::Formatter<'_>, + ) -> core::fmt::Result { + fmt::Debug::fmt(&this.data, f) + } +} + impl<'tcx> ExistentialPredicate<'tcx> { /// Compares via an ordering that will not change if modules are reordered or other changes are /// made to the tree. In particular, this ordering is preserved across incremental compilations. @@ -725,7 +722,7 @@ impl<'tcx> PolyExistentialPredicate<'tcx> { use crate::ty::ToPredicate; match self.skip_binder() { ExistentialPredicate::Trait(tr) => { - self.rebind(tr).with_self_ty(tcx, self_ty).without_const().to_predicate(tcx) + self.rebind(tr).with_self_ty(tcx, self_ty).to_predicate(tcx) } ExistentialPredicate::Projection(p) => { self.rebind(p.with_self_ty(tcx, self_ty)).to_predicate(tcx) @@ -736,12 +733,11 @@ impl<'tcx> PolyExistentialPredicate<'tcx> { ty::TraitRef::new(tcx, did, [self_ty]) } else { // If this is an ill-formed auto trait, then synthesize - // new error substs for the missing generics. - let err_substs = - ty::InternalSubsts::extend_with_error(tcx, did, &[self_ty.into()]); - ty::TraitRef::new(tcx, did, err_substs) + // new error args for the missing generics. + let err_args = ty::GenericArgs::extend_with_error(tcx, did, &[self_ty.into()]); + ty::TraitRef::new(tcx, did, err_args) }; - self.rebind(trait_ref).without_const().to_predicate(tcx) + self.rebind(trait_ref).to_predicate(tcx) } } } @@ -815,7 +811,7 @@ impl<'tcx> List<ty::PolyExistentialPredicate<'tcx>> { /// T: Foo<U> /// ``` /// This would be represented by a trait-reference where the `DefId` is the -/// `DefId` for the trait `Foo` and the substs define `T` as parameter 0, +/// `DefId` for the trait `Foo` and the args define `T` as parameter 0, /// and `U` as parameter 1. /// /// Trait references also appear in object types like `Foo<U>`, but in @@ -824,7 +820,7 @@ impl<'tcx> List<ty::PolyExistentialPredicate<'tcx>> { #[derive(HashStable, TypeFoldable, TypeVisitable, Lift)] pub struct TraitRef<'tcx> { pub def_id: DefId, - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, /// This field exists to prevent the creation of `TraitRef` without /// calling [`TraitRef::new`]. pub(super) _use_trait_ref_new_instead: (), @@ -834,60 +830,48 @@ impl<'tcx> TraitRef<'tcx> { pub fn new( tcx: TyCtxt<'tcx>, trait_def_id: DefId, - substs: impl IntoIterator<Item: Into<GenericArg<'tcx>>>, + args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>, ) -> Self { - let substs = tcx.check_and_mk_substs(trait_def_id, substs); - Self { def_id: trait_def_id, substs, _use_trait_ref_new_instead: () } + let args = tcx.check_and_mk_args(trait_def_id, args); + Self { def_id: trait_def_id, args, _use_trait_ref_new_instead: () } } pub fn from_lang_item( tcx: TyCtxt<'tcx>, trait_lang_item: LangItem, span: Span, - substs: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>, + args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>, ) -> Self { let trait_def_id = tcx.require_lang_item(trait_lang_item, Some(span)); - Self::new(tcx, trait_def_id, substs) + Self::new(tcx, trait_def_id, args) } pub fn from_method( tcx: TyCtxt<'tcx>, trait_id: DefId, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) -> ty::TraitRef<'tcx> { let defs = tcx.generics_of(trait_id); - ty::TraitRef::new(tcx, trait_id, tcx.mk_substs(&substs[..defs.params.len()])) + ty::TraitRef::new(tcx, trait_id, tcx.mk_args(&args[..defs.params.len()])) } /// Returns a `TraitRef` of the form `P0: Foo<P1..Pn>` where `Pi` /// are the parameters defined on trait. pub fn identity(tcx: TyCtxt<'tcx>, def_id: DefId) -> TraitRef<'tcx> { - ty::TraitRef::new(tcx, def_id, InternalSubsts::identity_for_item(tcx, def_id)) + ty::TraitRef::new(tcx, def_id, GenericArgs::identity_for_item(tcx, def_id)) } pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self { ty::TraitRef::new( tcx, self.def_id, - [self_ty.into()].into_iter().chain(self.substs.iter().skip(1)), + [self_ty.into()].into_iter().chain(self.args.iter().skip(1)), ) } - /// Converts this trait ref to a trait predicate with a given `constness` and a positive polarity. - #[inline] - pub fn with_constness(self, constness: ty::BoundConstness) -> ty::TraitPredicate<'tcx> { - ty::TraitPredicate { trait_ref: self, constness, polarity: ty::ImplPolarity::Positive } - } - - /// Converts this trait ref to a trait predicate without `const` and a positive polarity. - #[inline] - pub fn without_const(self) -> ty::TraitPredicate<'tcx> { - self.with_constness(ty::BoundConstness::NotConst) - } - #[inline] pub fn self_ty(&self) -> Ty<'tcx> { - self.substs.type_at(0) + self.args.type_at(0) } } @@ -920,7 +904,7 @@ impl<'tcx> IntoDiagnosticArg for TraitRef<'tcx> { #[derive(HashStable, TypeFoldable, TypeVisitable, Lift)] pub struct ExistentialTraitRef<'tcx> { pub def_id: DefId, - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, } impl<'tcx> ExistentialTraitRef<'tcx> { @@ -929,11 +913,11 @@ impl<'tcx> ExistentialTraitRef<'tcx> { trait_ref: ty::TraitRef<'tcx>, ) -> ty::ExistentialTraitRef<'tcx> { // Assert there is a Self. - trait_ref.substs.type_at(0); + trait_ref.args.type_at(0); ty::ExistentialTraitRef { def_id: trait_ref.def_id, - substs: tcx.mk_substs(&trait_ref.substs[1..]), + args: tcx.mk_args(&trait_ref.args[1..]), } } @@ -945,7 +929,7 @@ impl<'tcx> ExistentialTraitRef<'tcx> { // otherwise the escaping vars would be captured by the binder // debug_assert!(!self_ty.has_escaping_bound_vars()); - ty::TraitRef::new(tcx, self.def_id, [self_ty.into()].into_iter().chain(self.substs.iter())) + ty::TraitRef::new(tcx, self.def_id, [self_ty.into()].into_iter().chain(self.args.iter())) } } @@ -1214,7 +1198,7 @@ pub struct AliasTy<'tcx> { /// /// For RPIT the substitutions are for the generics of the function, /// while for TAIT it is used for the generic parameters of the alias. - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, /// The `DefId` of the `TraitItem` or `ImplItem` for the associated type `N` depending on whether /// this is a projection or an inherent projection or the `DefId` of the `OpaqueType` item if @@ -1237,9 +1221,9 @@ impl<'tcx> AliasTy<'tcx> { pub fn kind(self, tcx: TyCtxt<'tcx>) -> ty::AliasKind { match tcx.def_kind(self.def_id) { DefKind::AssocTy if let DefKind::Impl { of_trait: false } = tcx.def_kind(tcx.parent(self.def_id)) => ty::Inherent, - DefKind::AssocTy | DefKind::ImplTraitPlaceholder => ty::Projection, + DefKind::AssocTy => ty::Projection, DefKind::OpaqueTy => ty::Opaque, - DefKind::TyAlias => ty::Weak, + DefKind::TyAlias { .. } => ty::Weak, kind => bug!("unexpected DefKind in AliasTy: {kind:?}"), } } @@ -1252,11 +1236,11 @@ impl<'tcx> AliasTy<'tcx> { /// The following methods work only with associated type projections. impl<'tcx> AliasTy<'tcx> { pub fn self_ty(self) -> Ty<'tcx> { - self.substs.type_at(0) + self.args.type_at(0) } pub fn with_self_ty(self, tcx: TyCtxt<'tcx>, self_ty: Ty<'tcx>) -> Self { - tcx.mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.substs.iter().skip(1))) + tcx.mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.args.iter().skip(1))) } } @@ -1265,17 +1249,14 @@ impl<'tcx> AliasTy<'tcx> { pub fn trait_def_id(self, tcx: TyCtxt<'tcx>) -> DefId { match tcx.def_kind(self.def_id) { DefKind::AssocTy | DefKind::AssocConst => tcx.parent(self.def_id), - DefKind::ImplTraitPlaceholder => { - tcx.parent(tcx.impl_trait_in_trait_parent_fn(self.def_id)) - } kind => bug!("expected a projection AliasTy; found {kind:?}"), } } - /// Extracts the underlying trait reference and own substs from this projection. + /// Extracts the underlying trait reference and own args from this projection. /// For example, if this is a projection of `<T as StreamingIterator>::Item<'a>`, - /// then this function would return a `T: StreamingIterator` trait reference and `['a]` as the own substs - pub fn trait_ref_and_own_substs( + /// then this function would return a `T: StreamingIterator` trait reference and `['a]` as the own args + pub fn trait_ref_and_own_args( self, tcx: TyCtxt<'tcx>, ) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) { @@ -1283,8 +1264,8 @@ impl<'tcx> AliasTy<'tcx> { let trait_def_id = self.trait_def_id(tcx); let trait_generics = tcx.generics_of(trait_def_id); ( - ty::TraitRef::new(tcx, trait_def_id, self.substs.truncate_to(tcx, trait_generics)), - &self.substs[trait_generics.count()..], + ty::TraitRef::new(tcx, trait_def_id, self.args.truncate_to(tcx, trait_generics)), + &self.args[trait_generics.count()..], ) } @@ -1292,18 +1273,18 @@ impl<'tcx> AliasTy<'tcx> { /// For example, if this is a projection of `<T as Iterator>::Item`, /// then this function would return a `T: Iterator` trait reference. /// - /// WARNING: This will drop the substs for generic associated types - /// consider calling [Self::trait_ref_and_own_substs] to get those + /// WARNING: This will drop the args for generic associated types + /// consider calling [Self::trait_ref_and_own_args] to get those /// as well. pub fn trait_ref(self, tcx: TyCtxt<'tcx>) -> ty::TraitRef<'tcx> { let def_id = self.trait_def_id(tcx); - ty::TraitRef::new(tcx, def_id, self.substs.truncate_to(tcx, tcx.generics_of(def_id))) + ty::TraitRef::new(tcx, def_id, self.args.truncate_to(tcx, tcx.generics_of(def_id))) } } /// The following methods work only with inherent associated type projections. impl<'tcx> AliasTy<'tcx> { - /// Transform the substitutions to have the given `impl` substs as the base and the GAT substs on top of that. + /// Transform the substitutions to have the given `impl` args as the base and the GAT args on top of that. /// /// Does the following transformation: /// @@ -1313,14 +1294,14 @@ impl<'tcx> AliasTy<'tcx> { /// I_i impl subst /// P_j GAT subst /// ``` - pub fn rebase_substs_onto_impl( + pub fn rebase_inherent_args_onto_impl( self, - impl_substs: ty::SubstsRef<'tcx>, + impl_args: ty::GenericArgsRef<'tcx>, tcx: TyCtxt<'tcx>, - ) -> ty::SubstsRef<'tcx> { + ) -> ty::GenericArgsRef<'tcx> { debug_assert_eq!(self.kind(tcx), ty::Inherent); - tcx.mk_substs_from_iter(impl_substs.into_iter().chain(self.substs.into_iter().skip(1))) + tcx.mk_args_from_iter(impl_args.into_iter().chain(self.args.into_iter().skip(1))) } } @@ -1574,12 +1555,6 @@ impl<'tcx> Deref for Region<'tcx> { } } -impl<'tcx> fmt::Debug for Region<'tcx> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}", self.kind()) - } -} - #[derive(Copy, Clone, PartialEq, Eq, Hash, TyEncodable, TyDecodable, PartialOrd, Ord)] #[derive(HashStable)] pub struct EarlyBoundRegion { @@ -1646,7 +1621,7 @@ impl From<BoundVar> for BoundTy { #[derive(HashStable, TypeFoldable, TypeVisitable, Lift)] pub struct ExistentialProjection<'tcx> { pub def_id: DefId, - pub substs: SubstsRef<'tcx>, + pub args: GenericArgsRef<'tcx>, pub term: Term<'tcx>, } @@ -1660,8 +1635,8 @@ impl<'tcx> ExistentialProjection<'tcx> { pub fn trait_ref(&self, tcx: TyCtxt<'tcx>) -> ty::ExistentialTraitRef<'tcx> { let def_id = tcx.parent(self.def_id); let subst_count = tcx.generics_of(def_id).count() - 1; - let substs = tcx.mk_substs(&self.substs[..subst_count]); - ty::ExistentialTraitRef { def_id, substs } + let args = tcx.mk_args(&self.args[..subst_count]); + ty::ExistentialTraitRef { def_id, args } } pub fn with_self_ty( @@ -1674,7 +1649,7 @@ impl<'tcx> ExistentialProjection<'tcx> { ty::ProjectionPredicate { projection_ty: tcx - .mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.substs)), + .mk_alias_ty(self.def_id, [self_ty.into()].into_iter().chain(self.args)), term: self.term, } } @@ -1684,11 +1659,11 @@ impl<'tcx> ExistentialProjection<'tcx> { projection_predicate: ty::ProjectionPredicate<'tcx>, ) -> Self { // Assert there is a Self. - projection_predicate.projection_ty.substs.type_at(0); + projection_predicate.projection_ty.args.type_at(0); Self { def_id: projection_predicate.projection_ty.def_id, - substs: tcx.mk_substs(&projection_predicate.projection_ty.substs[1..]), + args: tcx.mk_args(&projection_predicate.projection_ty.args[1..]), term: projection_predicate.term, } } @@ -1970,15 +1945,14 @@ impl<'tcx> Ty<'tcx> { (kind, tcx.def_kind(alias_ty.def_id)), (ty::Opaque, DefKind::OpaqueTy) | (ty::Projection | ty::Inherent, DefKind::AssocTy) - | (ty::Opaque | ty::Projection, DefKind::ImplTraitPlaceholder) - | (ty::Weak, DefKind::TyAlias) + | (ty::Weak, DefKind::TyAlias { .. }) ); Ty::new(tcx, Alias(kind, alias_ty)) } #[inline] - pub fn new_opaque(tcx: TyCtxt<'tcx>, def_id: DefId, substs: SubstsRef<'tcx>) -> Ty<'tcx> { - Ty::new_alias(tcx, ty::Opaque, tcx.mk_alias_ty(def_id, substs)) + pub fn new_opaque(tcx: TyCtxt<'tcx>, def_id: DefId, args: GenericArgsRef<'tcx>) -> Ty<'tcx> { + Ty::new_alias(tcx, ty::Opaque, tcx.mk_alias_ty(def_id, args)) } /// Constructs a `TyKind::Error` type with current `ErrorGuaranteed` @@ -1998,7 +1972,7 @@ impl<'tcx> Ty<'tcx> { pub fn new_error_with_message<S: Into<MultiSpan>>( tcx: TyCtxt<'tcx>, span: S, - msg: impl Into<DiagnosticMessage>, + msg: impl Into<String>, ) -> Ty<'tcx> { let reported = tcx.sess.delay_span_bug(span, msg); Ty::new(tcx, Error(reported)) @@ -2070,8 +2044,8 @@ impl<'tcx> Ty<'tcx> { } #[inline] - pub fn new_adt(tcx: TyCtxt<'tcx>, def: AdtDef<'tcx>, substs: SubstsRef<'tcx>) -> Ty<'tcx> { - Ty::new(tcx, Adt(def, substs)) + pub fn new_adt(tcx: TyCtxt<'tcx>, def: AdtDef<'tcx>, args: GenericArgsRef<'tcx>) -> Ty<'tcx> { + Ty::new(tcx, Adt(def, args)) } #[inline] @@ -2115,10 +2089,10 @@ impl<'tcx> Ty<'tcx> { pub fn new_fn_def( tcx: TyCtxt<'tcx>, def_id: DefId, - substs: impl IntoIterator<Item: Into<GenericArg<'tcx>>>, + args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>, ) -> Ty<'tcx> { - let substs = tcx.check_and_mk_substs(def_id, substs); - Ty::new(tcx, FnDef(def_id, substs)) + let args = tcx.check_and_mk_args(def_id, args); + Ty::new(tcx, FnDef(def_id, args)) } #[inline] @@ -2140,38 +2114,38 @@ impl<'tcx> Ty<'tcx> { pub fn new_projection( tcx: TyCtxt<'tcx>, item_def_id: DefId, - substs: impl IntoIterator<Item: Into<GenericArg<'tcx>>>, + args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>, ) -> Ty<'tcx> { - Ty::new_alias(tcx, ty::Projection, tcx.mk_alias_ty(item_def_id, substs)) + Ty::new_alias(tcx, ty::Projection, tcx.mk_alias_ty(item_def_id, args)) } #[inline] pub fn new_closure( tcx: TyCtxt<'tcx>, def_id: DefId, - closure_substs: SubstsRef<'tcx>, + closure_args: GenericArgsRef<'tcx>, ) -> Ty<'tcx> { debug_assert_eq!( - closure_substs.len(), + closure_args.len(), tcx.generics_of(tcx.typeck_root_def_id(def_id)).count() + 3, "closure constructed with incorrect substitutions" ); - Ty::new(tcx, Closure(def_id, closure_substs)) + Ty::new(tcx, Closure(def_id, closure_args)) } #[inline] pub fn new_generator( tcx: TyCtxt<'tcx>, def_id: DefId, - generator_substs: SubstsRef<'tcx>, + generator_args: GenericArgsRef<'tcx>, movability: hir::Movability, ) -> Ty<'tcx> { debug_assert_eq!( - generator_substs.len(), + generator_args.len(), tcx.generics_of(tcx.typeck_root_def_id(def_id)).count() + 5, "generator constructed with incorrect number of substitutions" ); - Ty::new(tcx, Generator(def_id, generator_substs, movability)) + Ty::new(tcx, Generator(def_id, generator_args, movability)) } #[inline] @@ -2186,9 +2160,9 @@ impl<'tcx> Ty<'tcx> { pub fn new_generator_witness_mir( tcx: TyCtxt<'tcx>, id: DefId, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) -> Ty<'tcx> { - Ty::new(tcx, GeneratorWitnessMIR(id, substs)) + Ty::new(tcx, GeneratorWitnessMIR(id, args)) } // misc @@ -2212,19 +2186,18 @@ impl<'tcx> Ty<'tcx> { fn new_generic_adt(tcx: TyCtxt<'tcx>, wrapper_def_id: DefId, ty_param: Ty<'tcx>) -> Ty<'tcx> { let adt_def = tcx.adt_def(wrapper_def_id); - let substs = - InternalSubsts::for_item(tcx, wrapper_def_id, |param, substs| match param.kind { - GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => bug!(), - GenericParamDefKind::Type { has_default, .. } => { - if param.index == 0 { - ty_param.into() - } else { - assert!(has_default); - tcx.type_of(param.def_id).subst(tcx, substs).into() - } + let args = GenericArgs::for_item(tcx, wrapper_def_id, |param, args| match param.kind { + GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => bug!(), + GenericParamDefKind::Type { has_default, .. } => { + if param.index == 0 { + ty_param.into() + } else { + assert!(has_default); + tcx.type_of(param.def_id).instantiate(tcx, args).into() } - }); - Ty::new(tcx, Adt(adt_def, substs)) + } + }); + Ty::new(tcx, Adt(adt_def, args)) } #[inline] @@ -2255,8 +2228,8 @@ impl<'tcx> Ty<'tcx> { pub fn new_task_context(tcx: TyCtxt<'tcx>) -> Ty<'tcx> { let context_did = tcx.require_lang_item(LangItem::Context, None); let context_adt_ref = tcx.adt_def(context_did); - let context_substs = tcx.mk_substs(&[tcx.lifetimes.re_erased.into()]); - let context_ty = Ty::new_adt(tcx, context_adt_ref, context_substs); + let context_args = tcx.mk_args(&[tcx.lifetimes.re_erased.into()]); + let context_ty = Ty::new_adt(tcx, context_adt_ref, context_args); Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, context_ty) } } @@ -2380,10 +2353,10 @@ impl<'tcx> Ty<'tcx> { pub fn simd_size_and_type(self, tcx: TyCtxt<'tcx>) -> (u64, Ty<'tcx>) { match self.kind() { - Adt(def, substs) => { + Adt(def, args) => { assert!(def.repr().simd(), "`simd_size_and_type` called on non-SIMD type"); let variant = def.non_enum_variant(); - let f0_ty = variant.fields[FieldIdx::from_u32(0)].ty(tcx, substs); + let f0_ty = variant.fields[FieldIdx::from_u32(0)].ty(tcx, args); match f0_ty.kind() { // If the first field is an array, we assume it is the only field and its @@ -2444,7 +2417,7 @@ impl<'tcx> Ty<'tcx> { /// Panics if called on any type other than `Box<T>`. pub fn boxed_ty(self) -> Ty<'tcx> { match self.kind() { - Adt(def, substs) if def.is_box() => substs.type_at(0), + Adt(def, args) if def.is_box() => args.type_at(0), _ => bug!("`boxed_ty` is called on non-box type {:?}", self), } } @@ -2608,14 +2581,14 @@ impl<'tcx> Ty<'tcx> { pub fn fn_sig(self, tcx: TyCtxt<'tcx>) -> PolyFnSig<'tcx> { match self.kind() { - FnDef(def_id, substs) => tcx.fn_sig(*def_id).subst(tcx, substs), + FnDef(def_id, args) => tcx.fn_sig(*def_id).instantiate(tcx, args), FnPtr(f) => *f, Error(_) => { // ignore errors (#54954) ty::Binder::dummy(FnSig::fake()) } Closure(..) => bug!( - "to get the signature of a closure, use `substs.as_closure().sig()` not `fn_sig()`", + "to get the signature of a closure, use `args.as_closure().sig()` not `fn_sig()`", ), _ => bug!("Ty::fn_sig() called on non-fn type: {:?}", self), } @@ -2649,7 +2622,7 @@ impl<'tcx> Ty<'tcx> { #[inline] pub fn tuple_fields(self) -> &'tcx List<Ty<'tcx>> { match self.kind() { - Tuple(substs) => substs, + Tuple(args) => args, _ => bug!("tuple_fields called on non-tuple"), } } @@ -2661,8 +2634,8 @@ impl<'tcx> Ty<'tcx> { pub fn variant_range(self, tcx: TyCtxt<'tcx>) -> Option<Range<VariantIdx>> { match self.kind() { TyKind::Adt(adt, _) => Some(adt.variant_range()), - TyKind::Generator(def_id, substs, _) => { - Some(substs.as_generator().variant_range(*def_id, tcx)) + TyKind::Generator(def_id, args, _) => { + Some(args.as_generator().variant_range(*def_id, tcx)) } _ => None, } @@ -2679,16 +2652,11 @@ impl<'tcx> Ty<'tcx> { variant_index: VariantIdx, ) -> Option<Discr<'tcx>> { match self.kind() { - TyKind::Adt(adt, _) if adt.variants().is_empty() => { - // This can actually happen during CTFE, see - // https://github.com/rust-lang/rust/issues/89765. - None - } TyKind::Adt(adt, _) if adt.is_enum() => { Some(adt.discriminant_for_variant(tcx, variant_index)) } - TyKind::Generator(def_id, substs, _) => { - Some(substs.as_generator().discriminant_for_variant(*def_id, tcx, variant_index)) + TyKind::Generator(def_id, args, _) => { + Some(args.as_generator().discriminant_for_variant(*def_id, tcx, variant_index)) } _ => None, } @@ -2698,13 +2666,13 @@ impl<'tcx> Ty<'tcx> { pub fn discriminant_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> { match self.kind() { ty::Adt(adt, _) if adt.is_enum() => adt.repr().discr_type().to_ty(tcx), - ty::Generator(_, substs, _) => substs.as_generator().discr_ty(tcx), + ty::Generator(_, args, _) => args.as_generator().discr_ty(tcx), ty::Param(_) | ty::Alias(..) | ty::Infer(ty::TyVar(_)) => { let assoc_items = tcx.associated_item_def_ids( tcx.require_lang_item(hir::LangItem::DiscriminantKind, None), ); - Ty::new_projection(tcx, assoc_items[0], tcx.mk_substs(&[self.into()])) + Ty::new_projection(tcx, assoc_items[0], tcx.mk_args(&[self.into()])) } ty::Bool @@ -2777,7 +2745,7 @@ impl<'tcx> Ty<'tcx> { ty::Str | ty::Slice(_) => (tcx.types.usize, false), ty::Dynamic(..) => { let dyn_metadata = tcx.require_lang_item(LangItem::DynMetadata, None); - (tcx.type_of(dyn_metadata).subst(tcx, &[tail.into()]), false) + (tcx.type_of(dyn_metadata).instantiate(tcx, &[tail.into()]), false) }, // type parameters only have unit metadata if they're sized, so return true @@ -2794,7 +2762,7 @@ impl<'tcx> Ty<'tcx> { } /// When we create a closure, we record its kind (i.e., what trait - /// it implements) into its `ClosureSubsts` using a type + /// it implements) into its `ClosureArgs` using a type /// parameter. This is kind of a phantom type, except that the /// most convenient thing for us to are the integral types. This /// function converts such a special type into the closure @@ -2857,13 +2825,13 @@ impl<'tcx> Ty<'tcx> { ty::Tuple(tys) => tys.iter().all(|ty| ty.is_trivially_sized(tcx)), - ty::Adt(def, _substs) => def.sized_constraint(tcx).skip_binder().is_empty(), + ty::Adt(def, _args) => def.sized_constraint(tcx).skip_binder().is_empty(), - ty::Alias(..) | ty::Param(_) | ty::Placeholder(..) => false, + ty::Alias(..) | ty::Param(_) | ty::Placeholder(..) | ty::Bound(..) => false, ty::Infer(ty::TyVar(_)) => false, - ty::Bound(..) | ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => { + ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => { bug!("`is_trivially_sized` applied to unexpected type: {:?}", self) } } diff --git a/compiler/rustc_middle/src/ty/trait_def.rs b/compiler/rustc_middle/src/ty/trait_def.rs index 98c70e330..6e55e7915 100644 --- a/compiler/rustc_middle/src/ty/trait_def.rs +++ b/compiler/rustc_middle/src/ty/trait_def.rs @@ -236,7 +236,7 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait for &impl_def_id in tcx.hir().trait_impls(trait_id) { let impl_def_id = impl_def_id.to_def_id(); - let impl_self_ty = tcx.type_of(impl_def_id).subst_identity(); + let impl_self_ty = tcx.type_of(impl_def_id).instantiate_identity(); if impl_self_ty.references_error() { continue; } diff --git a/compiler/rustc_middle/src/ty/typeck_results.rs b/compiler/rustc_middle/src/ty/typeck_results.rs index 8cbffa148..327cd0a5d 100644 --- a/compiler/rustc_middle/src/ty/typeck_results.rs +++ b/compiler/rustc_middle/src/ty/typeck_results.rs @@ -4,13 +4,12 @@ use crate::{ traits::ObligationCause, ty::{ self, tls, BindingMode, BoundVar, CanonicalPolyFnSig, ClosureSizeProfileData, - GenericArgKind, InternalSubsts, SubstsRef, Ty, UserSubsts, + GenericArgKind, GenericArgs, GenericArgsRef, Ty, UserArgs, }, }; use rustc_data_structures::{ - fx::{FxHashMap, FxIndexMap}, - sync::Lrc, - unord::{UnordItems, UnordSet}, + fx::FxIndexMap, + unord::{ExtendUnord, UnordItems, UnordSet}, }; use rustc_errors::ErrorGuaranteed; use rustc_hir as hir; @@ -54,7 +53,7 @@ pub struct TypeckResults<'tcx> { /// of this node. This only applies to nodes that refer to entities /// parameterized by type parameters, such as generic fns, types, or /// other items. - node_substs: ItemLocalMap<SubstsRef<'tcx>>, + node_args: ItemLocalMap<GenericArgsRef<'tcx>>, /// This will either store the canonicalized types provided by the user /// or the substitutions that the user explicitly gave (if any) attached @@ -145,7 +144,7 @@ pub struct TypeckResults<'tcx> { /// This is used for warning unused imports. During type /// checking, this `Lrc` should not be cloned: it must have a ref-count /// of 1 so that we can insert things into the set mutably. - pub used_trait_imports: Lrc<UnordSet<LocalDefId>>, + pub used_trait_imports: UnordSet<LocalDefId>, /// If any errors occurred while type-checking this body, /// this field will be set to `Some(ErrorGuaranteed)`. @@ -183,7 +182,7 @@ pub struct TypeckResults<'tcx> { /// we never capture `t`. This becomes an issue when we build MIR as we require /// information on `t` in order to create place `t.0` and `t.1`. We can solve this /// issue by fake reading `t`. - pub closure_fake_reads: FxHashMap<LocalDefId, Vec<(HirPlace<'tcx>, FakeReadCause, hir::HirId)>>, + pub closure_fake_reads: LocalDefIdMap<Vec<(HirPlace<'tcx>, FakeReadCause, hir::HirId)>>, /// Tracks the rvalue scoping rules which defines finer scoping for rvalue expressions /// by applying extended parameter rules. @@ -197,7 +196,7 @@ pub struct TypeckResults<'tcx> { /// Stores the predicates that apply on generator witness types. /// formatting modified file tests/ui/generator/retain-resume-ref.rs pub generator_interior_predicates: - FxHashMap<LocalDefId, Vec<(ty::Predicate<'tcx>, ObligationCause<'tcx>)>>, + LocalDefIdMap<Vec<(ty::Predicate<'tcx>, ObligationCause<'tcx>)>>, /// We sometimes treat byte string literals (which are of type `&[u8; N]`) /// as `&[u8]`, depending on the pattern in which they are used. @@ -207,7 +206,7 @@ pub struct TypeckResults<'tcx> { /// Contains the data for evaluating the effect of feature `capture_disjoint_fields` /// on closure size. - pub closure_size_eval: FxHashMap<LocalDefId, ClosureSizeProfileData<'tcx>>, + pub closure_size_eval: LocalDefIdMap<ClosureSizeProfileData<'tcx>>, /// Container types and field indices of `offset_of!` expressions offset_of_data: ItemLocalMap<(Ty<'tcx>, Vec<FieldIdx>)>, @@ -265,7 +264,7 @@ impl<'tcx> TypeckResults<'tcx> { user_provided_types: Default::default(), user_provided_sigs: Default::default(), node_types: Default::default(), - node_substs: Default::default(), + node_args: Default::default(), adjustments: Default::default(), pat_binding_modes: Default::default(), pat_adjustments: Default::default(), @@ -273,7 +272,7 @@ impl<'tcx> TypeckResults<'tcx> { liberated_fn_sigs: Default::default(), fru_field_types: Default::default(), coercion_casts: Default::default(), - used_trait_imports: Lrc::new(Default::default()), + used_trait_imports: Default::default(), tainted_by_errors: None, concrete_opaque_types: Default::default(), closure_min_captures: Default::default(), @@ -385,18 +384,18 @@ impl<'tcx> TypeckResults<'tcx> { self.node_types.get(&id.local_id).cloned() } - pub fn node_substs_mut(&mut self) -> LocalTableInContextMut<'_, SubstsRef<'tcx>> { - LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.node_substs } + pub fn node_args_mut(&mut self) -> LocalTableInContextMut<'_, GenericArgsRef<'tcx>> { + LocalTableInContextMut { hir_owner: self.hir_owner, data: &mut self.node_args } } - pub fn node_substs(&self, id: hir::HirId) -> SubstsRef<'tcx> { + pub fn node_args(&self, id: hir::HirId) -> GenericArgsRef<'tcx> { validate_hir_id_for_typeck_results(self.hir_owner, id); - self.node_substs.get(&id.local_id).cloned().unwrap_or_else(|| InternalSubsts::empty()) + self.node_args.get(&id.local_id).cloned().unwrap_or_else(|| GenericArgs::empty()) } - pub fn node_substs_opt(&self, id: hir::HirId) -> Option<SubstsRef<'tcx>> { + pub fn node_args_opt(&self, id: hir::HirId) -> Option<GenericArgsRef<'tcx>> { validate_hir_id_for_typeck_results(self.hir_owner, id); - self.node_substs.get(&id.local_id).cloned() + self.node_args.get(&id.local_id).cloned() } /// Returns the type of a pattern as a monotype. Like [`expr_ty`], this function @@ -636,7 +635,7 @@ impl<'a, V> LocalTableInContextMut<'a, V> { &mut self, items: UnordItems<(hir::HirId, V), impl Iterator<Item = (hir::HirId, V)>>, ) { - self.data.extend(items.map(|(id, value)| { + self.data.extend_unord(items.map(|(id, value)| { validate_hir_id_for_typeck_results(self.hir_owner, id); (id.local_id, value) })) @@ -671,12 +670,12 @@ impl<'tcx> CanonicalUserType<'tcx> { pub fn is_identity(&self) -> bool { match self.value { UserType::Ty(_) => false, - UserType::TypeOf(_, user_substs) => { - if user_substs.user_self_ty.is_some() { + UserType::TypeOf(_, user_args) => { + if user_args.user_self_ty.is_some() { return false; } - iter::zip(user_substs.substs, BoundVar::new(0)..).all(|(kind, cvar)| { + iter::zip(user_args.args, BoundVar::new(0)..).all(|(kind, cvar)| { match kind.unpack() { GenericArgKind::Type(ty) => match ty.kind() { ty::Bound(debruijn, b) => { @@ -721,5 +720,5 @@ pub enum UserType<'tcx> { /// The canonical type is the result of `type_of(def_id)` with the /// given substitutions applied. - TypeOf(DefId, UserSubsts<'tcx>), + TypeOf(DefId, UserArgs<'tcx>), } diff --git a/compiler/rustc_middle/src/ty/util.rs b/compiler/rustc_middle/src/ty/util.rs index e2e4a2dbd..564f982f8 100644 --- a/compiler/rustc_middle/src/ty/util.rs +++ b/compiler/rustc_middle/src/ty/util.rs @@ -7,7 +7,7 @@ use crate::ty::{ self, FallibleTypeFolder, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, }; -use crate::ty::{GenericArgKind, SubstsRef}; +use crate::ty::{GenericArgKind, GenericArgsRef}; use rustc_apfloat::Float as _; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher}; @@ -19,7 +19,7 @@ use rustc_index::bit_set::GrowableBitSet; use rustc_macros::HashStable; use rustc_session::Limit; use rustc_span::sym; -use rustc_target::abi::{Integer, IntegerType, Size, TargetDataLayout}; +use rustc_target::abi::{Integer, IntegerType, Size}; use rustc_target::spec::abi::Abi; use smallvec::SmallVec; use std::{fmt, iter}; @@ -57,7 +57,7 @@ impl<'tcx> fmt::Display for Discr<'tcx> { let x = self.val; // sign extend the raw representation to be an i128 let x = size.sign_extend(x) as i128; - write!(fmt, "{}", x) + write!(fmt, "{x}") } _ => write!(fmt, "{}", self.val), } @@ -156,7 +156,7 @@ impl<'tcx> TyCtxt<'tcx> { | DefKind::Enum | DefKind::Trait | DefKind::OpaqueTy - | DefKind::TyAlias + | DefKind::TyAlias { .. } | DefKind::ForeignTy | DefKind::TraitAlias | DefKind::AssocTy @@ -226,14 +226,14 @@ impl<'tcx> TyCtxt<'tcx> { return Ty::new_error(self, reported); } match *ty.kind() { - ty::Adt(def, substs) => { + ty::Adt(def, args) => { if !def.is_struct() { break; } match def.non_enum_variant().tail_opt() { Some(field) => { f(); - ty = field.ty(self, substs); + ty = field.ty(self, args); } None => break, } @@ -301,12 +301,12 @@ impl<'tcx> TyCtxt<'tcx> { let (mut a, mut b) = (source, target); loop { match (&a.kind(), &b.kind()) { - (&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs)) + (&ty::Adt(a_def, a_args), &ty::Adt(b_def, b_args)) if a_def == b_def && a_def.is_struct() => { if let Some(f) = a_def.non_enum_variant().tail_opt() { - a = f.ty(self, a_substs); - b = f.ty(self, b_substs); + a = f.ty(self, a_args); + b = f.ty(self, b_args); } else { break; } @@ -349,7 +349,7 @@ impl<'tcx> TyCtxt<'tcx> { let drop_trait = self.lang_items().drop_trait()?; self.ensure().coherent_trait(drop_trait); - let ty = self.type_of(adt_did).subst_identity(); + let ty = self.type_of(adt_did).instantiate_identity(); let mut dtor_candidate = None; self.for_each_relevant_impl(drop_trait, ty, |impl_did| { if validate(self, impl_did).is_err() { @@ -358,7 +358,8 @@ impl<'tcx> TyCtxt<'tcx> { } let Some(item_id) = self.associated_item_def_ids(impl_did).first() else { - self.sess.delay_span_bug(self.def_span(impl_did), "Drop impl without drop function"); + self.sess + .delay_span_bug(self.def_span(impl_did), "Drop impl without drop function"); return; }; @@ -383,7 +384,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Note that this returns only the constraints for the /// destructor of `def` itself. For the destructors of the /// contents, you need `adt_dtorck_constraint`. - pub fn destructor_constraints(self, def: ty::AdtDef<'tcx>) -> Vec<ty::subst::GenericArg<'tcx>> { + pub fn destructor_constraints(self, def: ty::AdtDef<'tcx>) -> Vec<ty::GenericArg<'tcx>> { let dtor = match def.destructor(self) { None => { debug!("destructor_constraints({:?}) - no dtor", def.did()); @@ -400,7 +401,7 @@ impl<'tcx> TyCtxt<'tcx> { // must be live. // We need to return the list of parameters from the ADTs - // generics/substs that correspond to impure parameters on the + // generics/args that correspond to impure parameters on the // impl's generics. This is a bit ugly, but conceptually simple: // // Suppose our ADT looks like the following @@ -412,21 +413,21 @@ impl<'tcx> TyCtxt<'tcx> { // impl<#[may_dangle] P0, P1, P2> Drop for S<P1, P2, P0> // // We want to return the parameters (X, Y). For that, we match - // up the item-substs <X, Y, Z> with the substs on the impl ADT, - // <P1, P2, P0>, and then look up which of the impl substs refer to + // up the item-args <X, Y, Z> with the args on the impl ADT, + // <P1, P2, P0>, and then look up which of the impl args refer to // parameters marked as pure. - let impl_substs = match *self.type_of(impl_def_id).subst_identity().kind() { - ty::Adt(def_, substs) if def_ == def => substs, + let impl_args = match *self.type_of(impl_def_id).instantiate_identity().kind() { + ty::Adt(def_, args) if def_ == def => args, _ => bug!(), }; - let item_substs = match *self.type_of(def.did()).subst_identity().kind() { - ty::Adt(def_, substs) if def_ == def => substs, + let item_args = match *self.type_of(def.did()).instantiate_identity().kind() { + ty::Adt(def_, args) if def_ == def => args, _ => bug!(), }; - let result = iter::zip(item_substs, impl_substs) + let result = iter::zip(item_args, impl_args) .filter(|&(_, k)| { match k.unpack() { GenericArgKind::Lifetime(region) => match region.kind() { @@ -459,12 +460,12 @@ impl<'tcx> TyCtxt<'tcx> { /// Checks whether each generic argument is simply a unique generic parameter. pub fn uses_unique_generic_params( self, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ignore_regions: CheckRegions, ) -> Result<(), NotUniqueParam<'tcx>> { let mut seen = GrowableBitSet::default(); let mut seen_late = FxHashSet::default(); - for arg in substs { + for arg in args { match arg.unpack() { GenericArgKind::Lifetime(lt) => match (ignore_regions, lt.kind()) { (CheckRegions::Bound, ty::ReLateBound(di, reg)) => { @@ -510,10 +511,10 @@ impl<'tcx> TyCtxt<'tcx> { /// for better caching. pub fn uses_unique_placeholders_ignoring_regions( self, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) -> Result<(), NotUniqueParam<'tcx>> { let mut seen = GrowableBitSet::default(); - for arg in substs { + for arg in args { match arg.unpack() { // Ignore regions, since we can't resolve those in a canonicalized // query in the trait solver. @@ -594,7 +595,7 @@ impl<'tcx> TyCtxt<'tcx> { def_id } - /// Given the `DefId` and substs a closure, creates the type of + /// Given the `DefId` and args a closure, creates the type of /// `self` argument that the closure expects. For example, for a /// `Fn` closure, this would return a reference type `&T` where /// `T = closure_ty`. @@ -607,11 +608,11 @@ impl<'tcx> TyCtxt<'tcx> { pub fn closure_env_ty( self, closure_def_id: DefId, - closure_substs: SubstsRef<'tcx>, + closure_args: GenericArgsRef<'tcx>, env_region: ty::Region<'tcx>, ) -> Option<Ty<'tcx>> { - let closure_ty = Ty::new_closure(self, closure_def_id, closure_substs); - let closure_kind_ty = closure_substs.as_closure().kind_ty(); + let closure_ty = Ty::new_closure(self, closure_def_id, closure_args); + let closure_kind_ty = closure_args.as_closure().kind_ty(); let closure_kind = closure_kind_ty.to_opt_closure_kind()?; let env_ty = match closure_kind { ty::ClosureKind::Fn => Ty::new_imm_ref(self, env_region, closure_ty), @@ -654,7 +655,7 @@ impl<'tcx> TyCtxt<'tcx> { /// Returns the type a reference to the thread local takes in MIR. pub fn thread_local_ptr_ty(self, def_id: DefId) -> Ty<'tcx> { - let static_ty = self.type_of(def_id).subst_identity(); + let static_ty = self.type_of(def_id).instantiate_identity(); if self.is_mutable_static(def_id) { Ty::new_mut_ptr(self, static_ty) } else if self.is_foreign_item(def_id) { @@ -670,7 +671,7 @@ impl<'tcx> TyCtxt<'tcx> { // Make sure that any constants in the static's type are evaluated. let static_ty = self.normalize_erasing_regions( ty::ParamEnv::empty(), - self.type_of(def_id).subst_identity(), + self.type_of(def_id).instantiate_identity(), ); // Make sure that accesses to unsafe statics end up using raw pointers. @@ -719,7 +720,7 @@ impl<'tcx> TyCtxt<'tcx> { pub fn try_expand_impl_trait_type( self, def_id: DefId, - substs: SubstsRef<'tcx>, + args: GenericArgsRef<'tcx>, ) -> Result<Ty<'tcx>, Ty<'tcx>> { let mut visitor = OpaqueTypeExpander { seen_opaque_tys: FxHashSet::default(), @@ -732,7 +733,7 @@ impl<'tcx> TyCtxt<'tcx> { tcx: self, }; - let expanded_type = visitor.expand_opaque_ty(def_id, substs).unwrap(); + let expanded_type = visitor.expand_opaque_ty(def_id, args).unwrap(); if visitor.found_recursion { Err(expanded_type) } else { Ok(expanded_type) } } @@ -799,7 +800,7 @@ struct OpaqueTypeExpander<'tcx> { seen_opaque_tys: FxHashSet<DefId>, // Cache of all expansions we've seen so far. This is a critical // optimization for some large types produced by async fn trees. - expanded_cache: FxHashMap<(DefId, SubstsRef<'tcx>), Ty<'tcx>>, + expanded_cache: FxHashMap<(DefId, GenericArgsRef<'tcx>), Ty<'tcx>>, primary_def_id: Option<DefId>, found_recursion: bool, found_any_recursion: bool, @@ -812,19 +813,19 @@ struct OpaqueTypeExpander<'tcx> { } impl<'tcx> OpaqueTypeExpander<'tcx> { - fn expand_opaque_ty(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) -> Option<Ty<'tcx>> { + fn expand_opaque_ty(&mut self, def_id: DefId, args: GenericArgsRef<'tcx>) -> Option<Ty<'tcx>> { if self.found_any_recursion { return None; } - let substs = substs.fold_with(self); + let args = args.fold_with(self); if !self.check_recursion || self.seen_opaque_tys.insert(def_id) { - let expanded_ty = match self.expanded_cache.get(&(def_id, substs)) { + let expanded_ty = match self.expanded_cache.get(&(def_id, args)) { Some(expanded_ty) => *expanded_ty, None => { let generic_ty = self.tcx.type_of(def_id); - let concrete_ty = generic_ty.subst(self.tcx, substs); + let concrete_ty = generic_ty.instantiate(self.tcx, args); let expanded_ty = self.fold_ty(concrete_ty); - self.expanded_cache.insert((def_id, substs), expanded_ty); + self.expanded_cache.insert((def_id, args), expanded_ty); expanded_ty } }; @@ -841,21 +842,21 @@ impl<'tcx> OpaqueTypeExpander<'tcx> { } } - fn expand_generator(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) -> Option<Ty<'tcx>> { + fn expand_generator(&mut self, def_id: DefId, args: GenericArgsRef<'tcx>) -> Option<Ty<'tcx>> { if self.found_any_recursion { return None; } - let substs = substs.fold_with(self); + let args = args.fold_with(self); if !self.check_recursion || self.seen_opaque_tys.insert(def_id) { - let expanded_ty = match self.expanded_cache.get(&(def_id, substs)) { + let expanded_ty = match self.expanded_cache.get(&(def_id, args)) { Some(expanded_ty) => *expanded_ty, None => { for bty in self.tcx.generator_hidden_types(def_id) { - let hidden_ty = bty.subst(self.tcx, substs); + let hidden_ty = bty.instantiate(self.tcx, args); self.fold_ty(hidden_ty); } - let expanded_ty = Ty::new_generator_witness_mir(self.tcx, def_id, substs); - self.expanded_cache.insert((def_id, substs), expanded_ty); + let expanded_ty = Ty::new_generator_witness_mir(self.tcx, def_id, args); + self.expanded_cache.insert((def_id, args), expanded_ty); expanded_ty } }; @@ -879,16 +880,16 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for OpaqueTypeExpander<'tcx> { } fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { - let mut t = if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) = *t.kind() { - self.expand_opaque_ty(def_id, substs).unwrap_or(t) + let mut t = if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) = *t.kind() { + self.expand_opaque_ty(def_id, args).unwrap_or(t) } else if t.has_opaque_types() || t.has_generators() { t.super_fold_with(self) } else { t }; if self.expand_generators { - if let ty::GeneratorWitnessMIR(def_id, substs) = *t.kind() { - t = self.expand_generator(def_id, substs).unwrap_or(t); + if let ty::GeneratorWitnessMIR(def_id, args) = *t.kind() { + t = self.expand_generator(def_id, args).unwrap_or(t); } } t @@ -1084,7 +1085,7 @@ impl<'tcx> Ty<'tcx> { #[inline] pub fn needs_drop(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool { // Avoid querying in simple cases. - match needs_drop_components(self, &tcx.data_layout) { + match needs_drop_components(tcx, self) { Err(AlwaysRequiresDrop) => true, Ok(components) => { let query_ty = match *components { @@ -1117,7 +1118,7 @@ impl<'tcx> Ty<'tcx> { #[inline] pub fn has_significant_drop(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool { // Avoid querying in simple cases. - match needs_drop_components(self, &tcx.data_layout) { + match needs_drop_components(tcx, self) { Err(AlwaysRequiresDrop) => true, Ok(components) => { let query_ty = match *components { @@ -1277,10 +1278,10 @@ impl<'tcx> ExplicitSelf<'tcx> { /// *any* of the returned types need drop. Returns `Err(AlwaysRequiresDrop)` if /// this type always needs drop. pub fn needs_drop_components<'tcx>( + tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, - target_layout: &TargetDataLayout, ) -> Result<SmallVec<[Ty<'tcx>; 2]>, AlwaysRequiresDrop> { - match ty.kind() { + match *ty.kind() { ty::Infer(ty::FreshIntTy(_)) | ty::Infer(ty::FreshFloatTy(_)) | ty::Bool @@ -1302,11 +1303,11 @@ pub fn needs_drop_components<'tcx>( ty::Dynamic(..) | ty::Error(_) => Err(AlwaysRequiresDrop), - ty::Slice(ty) => needs_drop_components(*ty, target_layout), + ty::Slice(ty) => needs_drop_components(tcx, ty), ty::Array(elem_ty, size) => { - match needs_drop_components(*elem_ty, target_layout) { + match needs_drop_components(tcx, elem_ty) { Ok(v) if v.is_empty() => Ok(v), - res => match size.try_to_bits(target_layout.pointer_size) { + res => match size.try_to_target_usize(tcx) { // Arrays of size zero don't need drop, even if their element // type does. Some(0) => Ok(SmallVec::new()), @@ -1320,7 +1321,7 @@ pub fn needs_drop_components<'tcx>( } // If any field needs drop, then the whole tuple does. ty::Tuple(fields) => fields.iter().try_fold(SmallVec::new(), move |mut acc, elem| { - acc.extend(needs_drop_components(elem, target_layout)?); + acc.extend(needs_drop_components(tcx, elem)?); Ok(acc) }), diff --git a/compiler/rustc_middle/src/ty/visit.rs b/compiler/rustc_middle/src/ty/visit.rs index 520bb55e0..156eda477 100644 --- a/compiler/rustc_middle/src/ty/visit.rs +++ b/compiler/rustc_middle/src/ty/visit.rs @@ -88,14 +88,10 @@ pub trait TypeVisitableExt<'tcx>: TypeVisitable<TyCtxt<'tcx>> { self.has_type_flags(TypeFlags::HAS_INFER) } fn has_placeholders(&self) -> bool { - self.has_type_flags( - TypeFlags::HAS_RE_PLACEHOLDER - | TypeFlags::HAS_TY_PLACEHOLDER - | TypeFlags::HAS_CT_PLACEHOLDER, - ) + self.has_type_flags(TypeFlags::HAS_PLACEHOLDER) } fn has_non_region_placeholders(&self) -> bool { - self.has_type_flags(TypeFlags::HAS_TY_PLACEHOLDER | TypeFlags::HAS_CT_PLACEHOLDER) + self.has_type_flags(TypeFlags::HAS_PLACEHOLDER - TypeFlags::HAS_RE_PLACEHOLDER) } fn has_param(&self) -> bool { self.has_type_flags(TypeFlags::HAS_PARAM) diff --git a/compiler/rustc_middle/src/ty/vtable.rs b/compiler/rustc_middle/src/ty/vtable.rs index 443791d0a..97402caa0 100644 --- a/compiler/rustc_middle/src/ty/vtable.rs +++ b/compiler/rustc_middle/src/ty/vtable.rs @@ -29,8 +29,8 @@ impl<'tcx> fmt::Debug for VtblEntry<'tcx> { VtblEntry::MetadataSize => write!(f, "MetadataSize"), VtblEntry::MetadataAlign => write!(f, "MetadataAlign"), VtblEntry::Vacant => write!(f, "Vacant"), - VtblEntry::Method(instance) => write!(f, "Method({})", instance), - VtblEntry::TraitVPtr(trait_ref) => write!(f, "TraitVPtr({})", trait_ref), + VtblEntry::Method(instance) => write!(f, "Method({instance})"), + VtblEntry::TraitVPtr(trait_ref) => write!(f, "TraitVPtr({trait_ref})"), } } } diff --git a/compiler/rustc_middle/src/ty/walk.rs b/compiler/rustc_middle/src/ty/walk.rs index 04a635a68..7c3d9ed39 100644 --- a/compiler/rustc_middle/src/ty/walk.rs +++ b/compiler/rustc_middle/src/ty/walk.rs @@ -1,8 +1,8 @@ //! An iterator over the type substructure. //! WARNING: this does not keep track of the region depth. -use crate::ty::subst::{GenericArg, GenericArgKind}; use crate::ty::{self, Ty}; +use crate::ty::{GenericArg, GenericArgKind}; use rustc_data_structures::sso::SsoHashSet; use smallvec::SmallVec; @@ -166,33 +166,33 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>) stack.push(lt.into()); } ty::Alias(_, data) => { - stack.extend(data.substs.iter().rev()); + stack.extend(data.args.iter().rev()); } ty::Dynamic(obj, lt, _) => { stack.push(lt.into()); stack.extend(obj.iter().rev().flat_map(|predicate| { - let (substs, opt_ty) = match predicate.skip_binder() { - ty::ExistentialPredicate::Trait(tr) => (tr.substs, None), - ty::ExistentialPredicate::Projection(p) => (p.substs, Some(p.term)), + let (args, opt_ty) = match predicate.skip_binder() { + ty::ExistentialPredicate::Trait(tr) => (tr.args, None), + ty::ExistentialPredicate::Projection(p) => (p.args, Some(p.term)), ty::ExistentialPredicate::AutoTrait(_) => // Empty iterator { - (ty::InternalSubsts::empty(), None) + (ty::GenericArgs::empty(), None) } }; - substs.iter().rev().chain(opt_ty.map(|term| match term.unpack() { + args.iter().rev().chain(opt_ty.map(|term| match term.unpack() { ty::TermKind::Ty(ty) => ty.into(), ty::TermKind::Const(ct) => ct.into(), })) })); } - ty::Adt(_, substs) - | ty::Closure(_, substs) - | ty::Generator(_, substs, _) - | ty::GeneratorWitnessMIR(_, substs) - | ty::FnDef(_, substs) => { - stack.extend(substs.iter().rev()); + ty::Adt(_, args) + | ty::Closure(_, args) + | ty::Generator(_, args, _) + | ty::GeneratorWitnessMIR(_, args) + | ty::FnDef(_, args) => { + stack.extend(args.iter().rev()); } ty::Tuple(ts) => stack.extend(ts.iter().rev().map(GenericArg::from)), ty::GeneratorWitness(ts) => { @@ -233,7 +233,7 @@ fn push_inner<'tcx>(stack: &mut TypeWalkerStack<'tcx>, parent: GenericArg<'tcx>) }, ty::ConstKind::Unevaluated(ct) => { - stack.extend(ct.substs.iter().rev()); + stack.extend(ct.args.iter().rev()); } } } diff --git a/compiler/rustc_middle/src/util/bug.rs b/compiler/rustc_middle/src/util/bug.rs index 43ee0343f..634ed5ec5 100644 --- a/compiler/rustc_middle/src/util/bug.rs +++ b/compiler/rustc_middle/src/util/bug.rs @@ -29,7 +29,7 @@ fn opt_span_bug_fmt<S: Into<MultiSpan>>( location: &Location<'_>, ) -> ! { tls::with_opt(move |tcx| { - let msg = format!("{}: {}", location, args); + let msg = format!("{location}: {args}"); match (tcx, span) { (Some(tcx), Some(span)) => tcx.sess.diagnostic().span_bug(span, msg), (Some(tcx), None) => tcx.sess.diagnostic().bug(msg), diff --git a/compiler/rustc_middle/src/util/call_kind.rs b/compiler/rustc_middle/src/util/call_kind.rs index 98d55ea6d..4e2a2c6ae 100644 --- a/compiler/rustc_middle/src/util/call_kind.rs +++ b/compiler/rustc_middle/src/util/call_kind.rs @@ -2,7 +2,7 @@ //! as well as errors when attempting to call a non-const function in a const //! context. -use crate::ty::subst::SubstsRef; +use crate::ty::GenericArgsRef; use crate::ty::{AssocItemContainer, Instance, ParamEnv, Ty, TyCtxt}; use rustc_hir::def_id::DefId; use rustc_hir::{lang_items, LangItem}; @@ -43,7 +43,7 @@ pub enum CallKind<'tcx> { self_arg: Option<Ident>, desugaring: Option<(CallDesugaringKind, Ty<'tcx>)>, method_did: DefId, - method_substs: SubstsRef<'tcx>, + method_args: GenericArgsRef<'tcx>, }, /// A call to `Fn(..)::call(..)`, desugared from `my_closure(a, b, c)` FnCall { fn_trait_id: DefId, self_ty: Ty<'tcx> }, @@ -63,7 +63,7 @@ pub fn call_kind<'tcx>( tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, method_did: DefId, - method_substs: SubstsRef<'tcx>, + method_args: GenericArgsRef<'tcx>, fn_call_span: Span, from_hir_call: bool, self_arg: Option<Ident>, @@ -92,19 +92,19 @@ pub fn call_kind<'tcx>( // an FnOnce call, an operator (e.g. `<<`), or a // deref coercion. let kind = if let Some(trait_id) = fn_call { - Some(CallKind::FnCall { fn_trait_id: trait_id, self_ty: method_substs.type_at(0) }) + Some(CallKind::FnCall { fn_trait_id: trait_id, self_ty: method_args.type_at(0) }) } else if let Some(trait_id) = operator { - Some(CallKind::Operator { self_arg, trait_id, self_ty: method_substs.type_at(0) }) + Some(CallKind::Operator { self_arg, trait_id, self_ty: method_args.type_at(0) }) } else if is_deref { let deref_target = tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| { - Instance::resolve(tcx, param_env, deref_target, method_substs).transpose() + Instance::resolve(tcx, param_env, deref_target, method_args).transpose() }); if let Some(Ok(instance)) = deref_target { let deref_target_ty = instance.ty(tcx, param_env); Some(CallKind::DerefCoercion { deref_target: tcx.def_span(instance.def_id()), deref_target_ty, - self_ty: method_substs.type_at(0), + self_ty: method_args.type_at(0), }) } else { None @@ -119,24 +119,24 @@ pub fn call_kind<'tcx>( let desugaring = if Some(method_did) == tcx.lang_items().into_iter_fn() && fn_call_span.desugaring_kind() == Some(DesugaringKind::ForLoop) { - Some((CallDesugaringKind::ForLoopIntoIter, method_substs.type_at(0))) + Some((CallDesugaringKind::ForLoopIntoIter, method_args.type_at(0))) } else if fn_call_span.desugaring_kind() == Some(DesugaringKind::QuestionMark) { if Some(method_did) == tcx.lang_items().branch_fn() { - Some((CallDesugaringKind::QuestionBranch, method_substs.type_at(0))) + Some((CallDesugaringKind::QuestionBranch, method_args.type_at(0))) } else if Some(method_did) == tcx.lang_items().from_residual_fn() { - Some((CallDesugaringKind::QuestionFromResidual, method_substs.type_at(0))) + Some((CallDesugaringKind::QuestionFromResidual, method_args.type_at(0))) } else { None } } else if Some(method_did) == tcx.lang_items().from_output_fn() && fn_call_span.desugaring_kind() == Some(DesugaringKind::TryBlock) { - Some((CallDesugaringKind::TryBlockFromOutput, method_substs.type_at(0))) + Some((CallDesugaringKind::TryBlockFromOutput, method_args.type_at(0))) } else if fn_call_span.is_desugaring(DesugaringKind::Await) { - Some((CallDesugaringKind::Await, method_substs.type_at(0))) + Some((CallDesugaringKind::Await, method_args.type_at(0))) } else { None }; - CallKind::Normal { self_arg, desugaring, method_did, method_substs } + CallKind::Normal { self_arg, desugaring, method_did, method_args } }) } diff --git a/compiler/rustc_middle/src/util/common.rs b/compiler/rustc_middle/src/util/common.rs index 08977049d..df101a2f6 100644 --- a/compiler/rustc_middle/src/util/common.rs +++ b/compiler/rustc_middle/src/util/common.rs @@ -17,7 +17,7 @@ pub fn to_readable_str(mut val: usize) -> String { groups.push(group.to_string()); break; } else { - groups.push(format!("{:03}", group)); + groups.push(format!("{group:03}")); } } diff --git a/compiler/rustc_middle/src/util/find_self_call.rs b/compiler/rustc_middle/src/util/find_self_call.rs index 0eab0adf0..1b845334c 100644 --- a/compiler/rustc_middle/src/util/find_self_call.rs +++ b/compiler/rustc_middle/src/util/find_self_call.rs @@ -1,5 +1,5 @@ use crate::mir::*; -use crate::ty::subst::SubstsRef; +use crate::ty::GenericArgsRef; use crate::ty::{self, TyCtxt}; use rustc_span::def_id::DefId; @@ -11,21 +11,21 @@ pub fn find_self_call<'tcx>( body: &Body<'tcx>, local: Local, block: BasicBlock, -) -> Option<(DefId, SubstsRef<'tcx>)> { +) -> Option<(DefId, GenericArgsRef<'tcx>)> { debug!("find_self_call(local={:?}): terminator={:?}", local, &body[block].terminator); if let Some(Terminator { kind: TerminatorKind::Call { func, args, .. }, .. }) = &body[block].terminator { debug!("find_self_call: func={:?}", func); if let Operand::Constant(box Constant { literal, .. }) = func { - if let ty::FnDef(def_id, substs) = *literal.ty().kind() { + if let ty::FnDef(def_id, fn_args) = *literal.ty().kind() { if let Some(ty::AssocItem { fn_has_self_parameter: true, .. }) = tcx.opt_associated_item(def_id) { - debug!("find_self_call: args={:?}", args); + debug!("find_self_call: args={:?}", fn_args); if let [Operand::Move(self_place) | Operand::Copy(self_place), ..] = **args { if self_place.as_local() == Some(local) { - return Some((def_id, substs)); + return Some((def_id, fn_args)); } } } diff --git a/compiler/rustc_middle/src/values.rs b/compiler/rustc_middle/src/values.rs index b0961d917..384a36843 100644 --- a/compiler/rustc_middle/src/values.rs +++ b/compiler/rustc_middle/src/values.rs @@ -209,7 +209,7 @@ fn find_item_ty_spans( match ty.kind { hir::TyKind::Path(hir::QPath::Resolved(_, path)) => { if let Res::Def(kind, def_id) = path.res - && kind != DefKind::TyAlias { + && !matches!(kind, DefKind::TyAlias { .. }) { let check_params = def_id.as_local().map_or(true, |def_id| { if def_id == needle { spans.push(ty.span); |