2019-02-08 14:53:55 +01:00
|
|
|
//! Type context book-keeping.
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2022-11-25 16:55:25 +00:00
|
|
|
#![allow(rustc::usage_of_ty_tykind)]
|
|
|
|
|
2022-12-31 11:00:41 +01:00
|
|
|
pub mod tls;
|
|
|
|
|
2024-05-10 14:27:48 -04:00
|
|
|
use std::assert_matches::assert_matches;
|
2015-09-06 21:51:58 +03:00
|
|
|
use std::borrow::Borrow;
|
2017-04-27 13:04:57 -04:00
|
|
|
use std::cmp::Ordering;
|
2019-12-24 17:38:22 -05:00
|
|
|
use std::hash::{Hash, Hasher};
|
2024-05-10 14:27:48 -04:00
|
|
|
use std::marker::PhantomData;
|
2019-12-24 17:38:22 -05:00
|
|
|
use std::ops::{Bound, Deref};
|
2023-08-08 18:28:20 +08:00
|
|
|
use std::{fmt, iter, mem};
|
2024-05-10 14:27:48 -04:00
|
|
|
|
2024-11-02 19:33:00 -07:00
|
|
|
use rustc_abi::{ExternAbi, FieldIdx, Layout, LayoutData, TargetDataLayout, VariantIdx};
|
2023-03-19 21:32:34 +04:00
|
|
|
use rustc_ast::{self as ast, attr};
|
2023-08-25 17:26:24 +02:00
|
|
|
use rustc_data_structures::defer;
|
2022-02-23 18:02:06 +00:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
2024-07-06 12:33:03 -04:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2022-11-25 16:38:16 +00:00
|
|
|
use rustc_data_structures::intern::Interned;
|
2019-11-11 17:15:36 -05:00
|
|
|
use rustc_data_structures::profiling::SelfProfilerRef;
|
2020-01-02 00:44:34 +01:00
|
|
|
use rustc_data_structures::sharded::{IntoPointer, ShardedHashMap};
|
2021-05-11 10:38:54 +02:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2020-11-14 01:29:30 +01:00
|
|
|
use rustc_data_structures::steal::Steal;
|
2024-10-28 18:51:12 +01:00
|
|
|
use rustc_data_structures::sync::{
|
|
|
|
self, DynSend, DynSync, FreezeReadGuard, Lock, Lrc, RwLock, WorkerLocal,
|
|
|
|
};
|
2023-02-21 15:18:10 +01:00
|
|
|
use rustc_data_structures::unord::UnordSet;
|
2024-06-18 10:35:56 +00:00
|
|
|
use rustc_errors::{
|
|
|
|
Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, LintDiagnostic, MultiSpan,
|
|
|
|
};
|
2020-01-11 15:03:15 +01:00
|
|
|
use rustc_hir as hir;
|
2022-12-15 20:40:16 +01:00
|
|
|
use rustc_hir::def::DefKind;
|
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE, LocalDefId};
|
2021-07-12 22:19:25 +02:00
|
|
|
use rustc_hir::definitions::Definitions;
|
2020-06-09 13:57:33 -07:00
|
|
|
use rustc_hir::intravisit::Visitor;
|
2020-08-18 11:47:27 +01:00
|
|
|
use rustc_hir::lang_items::LangItem;
|
2023-09-14 12:05:05 +10:00
|
|
|
use rustc_hir::{HirId, Node, TraitCandidate};
|
2023-04-19 10:57:17 +00:00
|
|
|
use rustc_index::IndexVec;
|
2024-04-29 11:14:55 +10:00
|
|
|
use rustc_macros::{HashStable, TyDecodable, TyEncodable};
|
2024-07-11 23:13:12 +02:00
|
|
|
use rustc_query_system::cache::WithDepNode;
|
2022-11-26 13:54:45 +00:00
|
|
|
use rustc_query_system::dep_graph::DepNodeIndex;
|
2022-04-04 22:19:25 +02:00
|
|
|
use rustc_query_system::ich::StableHashingContext;
|
2020-12-06 17:30:55 -08:00
|
|
|
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
|
2022-12-05 17:00:26 +00:00
|
|
|
use rustc_session::config::CrateType;
|
2022-12-07 14:31:50 +00:00
|
|
|
use rustc_session::cstore::{CrateStoreDyn, Untracked};
|
2022-09-24 16:11:18 +02:00
|
|
|
use rustc_session::lint::Lint;
|
2023-08-08 18:28:20 +08:00
|
|
|
use rustc_session::{Limit, MetadataKind, Session};
|
2024-02-14 15:44:01 +00:00
|
|
|
use rustc_span::def_id::{CRATE_DEF_ID, DefPathHash, StableCrateId};
|
2020-12-03 20:10:55 -03:00
|
|
|
use rustc_span::symbol::{Ident, Symbol, kw, sym};
|
2020-05-05 23:02:09 -05:00
|
|
|
use rustc_span::{DUMMY_SP, Span};
|
2023-10-19 18:10:19 +00:00
|
|
|
use rustc_type_ir::TyKind::*;
|
2024-06-04 16:02:36 -04:00
|
|
|
use rustc_type_ir::fold::TypeFoldable;
|
2024-06-12 17:48:37 -04:00
|
|
|
use rustc_type_ir::lang_items::TraitSolverLangItem;
|
2024-05-10 14:27:48 -04:00
|
|
|
pub use rustc_type_ir::lift::Lift;
|
2024-07-11 23:13:12 +02:00
|
|
|
use rustc_type_ir::{CollectAndApply, Interner, TypeFlags, WithCachedTypeInfo, search_graph};
|
2024-10-03 00:44:14 -04:00
|
|
|
use tracing::{debug, instrument};
|
2020-01-11 15:03:15 +01:00
|
|
|
|
2024-05-13 12:40:08 -04:00
|
|
|
use crate::arena::Arena;
|
2019-12-24 17:38:22 -05:00
|
|
|
use crate::dep_graph::{DepGraph, DepKindStruct};
|
2024-02-14 15:13:09 +00:00
|
|
|
use crate::infer::canonical::{CanonicalParamEnvCache, CanonicalVarInfo, CanonicalVarInfos};
|
|
|
|
use crate::lint::lint_level;
|
2019-12-24 17:38:22 -05:00
|
|
|
use crate::metadata::ModChild;
|
|
|
|
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
|
|
|
|
use crate::middle::{resolve_bound_vars, stability};
|
2022-02-16 10:56:01 +01:00
|
|
|
use crate::mir::interpret::{self, Allocation, ConstAllocation};
|
2019-12-24 17:38:22 -05:00
|
|
|
use crate::mir::{Body, Local, Place, PlaceElem, ProjectionKind, Promoted};
|
2023-05-16 01:53:21 +02:00
|
|
|
use crate::query::plumbing::QuerySystem;
|
|
|
|
use crate::query::{IntoQueryParam, LocalCrate, Providers, TyCtxtAt};
|
2021-04-04 18:42:17 +02:00
|
|
|
use crate::thir::Thir;
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::traits;
|
2023-02-24 02:35:15 +00:00
|
|
|
use crate::traits::solve;
|
2023-05-16 23:51:23 +00:00
|
|
|
use crate::traits::solve::{
|
2019-12-24 17:38:22 -05:00
|
|
|
ExternalConstraints, ExternalConstraintsData, PredefinedOpaques, PredefinedOpaquesData,
|
2024-07-29 08:13:50 +10:00
|
|
|
};
|
2024-05-11 18:14:44 -04:00
|
|
|
use crate::ty::predicate::ExistentialPredicateStableCmpExt as _;
|
2020-05-26 13:13:19 -07:00
|
|
|
use crate::ty::{
|
2024-10-29 23:42:59 +00:00
|
|
|
self, AdtDef, AdtDefData, AdtKind, Binder, BoundConstness, Clause, Clauses, Const, GenericArg,
|
|
|
|
GenericArgs, GenericArgsRef, GenericParamDefKind, ImplPolarity, List, ListWithCachedTypeInfo,
|
2024-10-20 19:49:11 +00:00
|
|
|
ParamConst, ParamTy, Pattern, PatternKind, PolyExistentialPredicate, PolyFnSig, Predicate,
|
|
|
|
PredicateKind, PredicatePolarity, Region, RegionKind, ReprOptions, TraitObjectVisitor, Ty,
|
|
|
|
TyKind, TyVid, Visibility,
|
2024-07-29 08:13:50 +10:00
|
|
|
};
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2021-01-31 10:32:34 +01:00
|
|
|
#[allow(rustc::usage_of_ty_tykind)]
|
2022-05-27 20:03:57 -07:00
|
|
|
impl<'tcx> Interner for TyCtxt<'tcx> {
|
2023-10-18 23:29:28 +00:00
|
|
|
type DefId = DefId;
|
2024-06-14 15:16:47 -04:00
|
|
|
type LocalDefId = LocalDefId;
|
2024-06-12 14:58:10 -04:00
|
|
|
type Span = Span;
|
|
|
|
|
2023-10-18 23:29:28 +00:00
|
|
|
type GenericArgs = ty::GenericArgsRef<'tcx>;
|
2024-06-17 17:59:08 -04:00
|
|
|
|
2024-05-31 14:13:46 -04:00
|
|
|
type GenericArgsSlice = &'tcx [ty::GenericArg<'tcx>];
|
2023-09-15 16:32:27 +01:00
|
|
|
type GenericArg = ty::GenericArg<'tcx>;
|
2024-05-13 12:40:08 -04:00
|
|
|
type Term = ty::Term<'tcx>;
|
2024-05-20 12:57:07 -04:00
|
|
|
type BoundVarKinds = &'tcx List<ty::BoundVariableKind>;
|
2023-10-20 20:43:33 +00:00
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
type BoundVarKind = ty::BoundVariableKind;
|
2024-05-17 12:16:36 -04:00
|
|
|
type PredefinedOpaques = solve::PredefinedOpaques<'tcx>;
|
2024-06-17 17:59:08 -04:00
|
|
|
|
|
|
|
fn mk_predefined_opaques_in_body(
|
|
|
|
self,
|
|
|
|
data: PredefinedOpaquesData<Self>,
|
|
|
|
) -> Self::PredefinedOpaques {
|
|
|
|
self.mk_predefined_opaques_in_body(data)
|
|
|
|
}
|
2024-05-17 12:16:36 -04:00
|
|
|
type DefiningOpaqueTypes = &'tcx ty::List<LocalDefId>;
|
2024-06-17 17:59:08 -04:00
|
|
|
type CanonicalVars = CanonicalVarInfos<'tcx>;
|
|
|
|
fn mk_canonical_var_infos(self, infos: &[ty::CanonicalVarInfo<Self>]) -> Self::CanonicalVars {
|
|
|
|
self.mk_canonical_var_infos(infos)
|
|
|
|
}
|
2024-05-17 12:16:36 -04:00
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
type ExternalConstraints = ExternalConstraints<'tcx>;
|
|
|
|
fn mk_external_constraints(
|
|
|
|
self,
|
|
|
|
data: ExternalConstraintsData<Self>,
|
|
|
|
) -> ExternalConstraints<'tcx> {
|
|
|
|
self.mk_external_constraints(data)
|
|
|
|
}
|
|
|
|
type DepNodeIndex = DepNodeIndex;
|
|
|
|
fn with_cached_task<T>(self, task: impl FnOnce() -> T) -> (T, DepNodeIndex) {
|
|
|
|
self.dep_graph.with_anon_task(self, crate::dep_graph::dep_kinds::TraitSelect, task)
|
|
|
|
}
|
2023-10-18 23:29:28 +00:00
|
|
|
type Ty = Ty<'tcx>;
|
|
|
|
type Tys = &'tcx List<Ty<'tcx>>;
|
2024-06-17 17:59:08 -04:00
|
|
|
|
2024-05-15 13:54:37 -04:00
|
|
|
type FnInputTys = &'tcx [Ty<'tcx>];
|
2021-01-31 10:32:34 +01:00
|
|
|
type ParamTy = ParamTy;
|
|
|
|
type BoundTy = ty::BoundTy;
|
2024-05-11 12:46:11 -04:00
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
type PlaceholderTy = ty::PlaceholderType;
|
2024-05-13 12:40:08 -04:00
|
|
|
type ErrorGuaranteed = ErrorGuaranteed;
|
2023-10-18 23:29:28 +00:00
|
|
|
type BoundExistentialPredicates = &'tcx List<PolyExistentialPredicate<'tcx>>;
|
2023-10-20 20:43:33 +00:00
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
type AllocId = crate::mir::interpret::AllocId;
|
2024-05-13 12:40:08 -04:00
|
|
|
type Pat = Pattern<'tcx>;
|
2024-05-17 14:17:48 -03:00
|
|
|
type Safety = hir::Safety;
|
2024-11-02 19:33:00 -07:00
|
|
|
type Abi = ExternAbi;
|
2023-10-18 23:29:28 +00:00
|
|
|
type Const = ty::Const<'tcx>;
|
2023-11-04 17:19:16 +00:00
|
|
|
type PlaceholderConst = ty::PlaceholderConst;
|
2024-06-17 17:59:08 -04:00
|
|
|
|
2023-07-04 11:46:01 +01:00
|
|
|
type ParamConst = ty::ParamConst;
|
|
|
|
type BoundConst = ty::BoundVar;
|
|
|
|
type ValueConst = ty::ValTree<'tcx>;
|
2024-05-13 12:40:08 -04:00
|
|
|
type ExprConst = ty::Expr<'tcx>;
|
2023-10-18 23:29:28 +00:00
|
|
|
type Region = Region<'tcx>;
|
2024-06-17 17:59:08 -04:00
|
|
|
|
2023-11-14 13:13:27 +00:00
|
|
|
type EarlyParamRegion = ty::EarlyParamRegion;
|
|
|
|
type LateParamRegion = ty::LateParamRegion;
|
2024-05-11 12:46:11 -04:00
|
|
|
type BoundRegion = ty::BoundRegion;
|
2024-05-13 12:40:08 -04:00
|
|
|
type PlaceholderRegion = ty::PlaceholderRegion;
|
2024-05-15 13:54:37 -04:00
|
|
|
|
2024-05-15 21:57:41 -04:00
|
|
|
type ParamEnv = ty::ParamEnv<'tcx>;
|
2023-10-20 20:43:33 +00:00
|
|
|
type Predicate = Predicate<'tcx>;
|
2024-06-17 17:59:08 -04:00
|
|
|
|
2024-05-20 12:57:07 -04:00
|
|
|
type Clause = Clause<'tcx>;
|
2024-05-13 12:40:08 -04:00
|
|
|
type Clauses = ty::Clauses<'tcx>;
|
2024-05-15 13:54:37 -04:00
|
|
|
|
2024-07-11 23:13:12 +02:00
|
|
|
type Tracked<T: fmt::Debug + Clone> = WithDepNode<T>;
|
|
|
|
fn mk_tracked<T: fmt::Debug + Clone>(
|
|
|
|
self,
|
|
|
|
data: T,
|
|
|
|
dep_node: DepNodeIndex,
|
|
|
|
) -> Self::Tracked<T> {
|
|
|
|
WithDepNode::new(dep_node, data)
|
|
|
|
}
|
|
|
|
fn get_tracked<T: fmt::Debug + Clone>(self, tracked: &Self::Tracked<T>) -> T {
|
|
|
|
tracked.get(self)
|
|
|
|
}
|
2024-06-17 17:59:08 -04:00
|
|
|
|
2024-10-18 00:28:43 +02:00
|
|
|
fn with_global_cache<R>(self, f: impl FnOnce(&mut search_graph::GlobalCache<Self>) -> R) -> R {
|
|
|
|
f(&mut *self.new_solver_evaluation_cache.lock())
|
2024-06-04 16:02:36 -04:00
|
|
|
}
|
|
|
|
|
2024-09-07 18:29:08 -07:00
|
|
|
fn evaluation_is_concurrent(&self) -> bool {
|
|
|
|
self.sess.threads() > 1
|
|
|
|
}
|
|
|
|
|
2024-06-04 16:02:36 -04:00
|
|
|
fn expand_abstract_consts<T: TypeFoldable<TyCtxt<'tcx>>>(self, t: T) -> T {
|
|
|
|
self.expand_abstract_consts(t)
|
2023-11-22 23:44:58 +00:00
|
|
|
}
|
2024-05-10 14:59:56 -04:00
|
|
|
|
|
|
|
type GenericsOf = &'tcx ty::Generics;
|
2024-05-13 12:40:08 -04:00
|
|
|
|
2024-05-10 14:59:56 -04:00
|
|
|
fn generics_of(self, def_id: DefId) -> &'tcx ty::Generics {
|
|
|
|
self.generics_of(def_id)
|
|
|
|
}
|
|
|
|
|
2024-06-04 16:02:36 -04:00
|
|
|
type VariancesOf = &'tcx [ty::Variance];
|
|
|
|
|
2024-05-29 20:15:56 -04:00
|
|
|
fn variances_of(self, def_id: DefId) -> Self::VariancesOf {
|
2024-06-04 16:02:36 -04:00
|
|
|
self.variances_of(def_id)
|
|
|
|
}
|
|
|
|
|
2024-05-26 20:53:00 -04:00
|
|
|
fn type_of(self, def_id: DefId) -> ty::EarlyBinder<'tcx, Ty<'tcx>> {
|
|
|
|
self.type_of(def_id)
|
2024-05-13 12:40:08 -04:00
|
|
|
}
|
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
type AdtDef = ty::AdtDef<'tcx>;
|
|
|
|
fn adt_def(self, adt_def_id: DefId) -> Self::AdtDef {
|
|
|
|
self.adt_def(adt_def_id)
|
|
|
|
}
|
|
|
|
|
2024-05-13 12:40:08 -04:00
|
|
|
fn alias_ty_kind(self, alias: ty::AliasTy<'tcx>) -> ty::AliasTyKind {
|
|
|
|
match self.def_kind(alias.def_id) {
|
|
|
|
DefKind::AssocTy => {
|
|
|
|
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id))
|
|
|
|
{
|
|
|
|
ty::Inherent
|
|
|
|
} else {
|
|
|
|
ty::Projection
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DefKind::OpaqueTy => ty::Opaque,
|
|
|
|
DefKind::TyAlias => ty::Weak,
|
|
|
|
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn alias_term_kind(self, alias: ty::AliasTerm<'tcx>) -> ty::AliasTermKind {
|
|
|
|
match self.def_kind(alias.def_id) {
|
|
|
|
DefKind::AssocTy => {
|
|
|
|
if let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(alias.def_id))
|
|
|
|
{
|
|
|
|
ty::AliasTermKind::InherentTy
|
|
|
|
} else {
|
|
|
|
ty::AliasTermKind::ProjectionTy
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy,
|
|
|
|
DefKind::TyAlias => ty::AliasTermKind::WeakTy,
|
|
|
|
DefKind::AssocConst => ty::AliasTermKind::ProjectionConst,
|
|
|
|
DefKind::AnonConst => ty::AliasTermKind::UnevaluatedConst,
|
|
|
|
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trait_ref_and_own_args_for_alias(
|
|
|
|
self,
|
2024-05-29 20:15:56 -04:00
|
|
|
def_id: DefId,
|
2024-06-17 17:59:08 -04:00
|
|
|
args: ty::GenericArgsRef<'tcx>,
|
|
|
|
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
|
2024-05-13 12:40:08 -04:00
|
|
|
assert_matches!(self.def_kind(def_id), DefKind::AssocTy | DefKind::AssocConst);
|
|
|
|
let trait_def_id = self.parent(def_id);
|
|
|
|
assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
|
|
|
|
let trait_generics = self.generics_of(trait_def_id);
|
|
|
|
(
|
2024-06-21 13:33:08 -04:00
|
|
|
ty::TraitRef::new_from_args(self, trait_def_id, args.truncate_to(self, trait_generics)),
|
2024-05-13 12:40:08 -04:00
|
|
|
&args[trait_generics.count()..],
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
fn mk_args(self, args: &[Self::GenericArg]) -> ty::GenericArgsRef<'tcx> {
|
2024-05-11 12:46:11 -04:00
|
|
|
self.mk_args(args)
|
|
|
|
}
|
2024-05-11 13:51:25 -04:00
|
|
|
|
2024-06-04 16:02:36 -04:00
|
|
|
fn mk_args_from_iter<I, T>(self, args: I) -> T::Output
|
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
2024-06-17 17:59:08 -04:00
|
|
|
T: CollectAndApply<Self::GenericArg, ty::GenericArgsRef<'tcx>>,
|
2024-06-04 16:02:36 -04:00
|
|
|
{
|
2024-05-13 12:40:08 -04:00
|
|
|
self.mk_args_from_iter(args)
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
fn check_args_compatible(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) -> bool {
|
|
|
|
self.check_args_compatible(def_id, args)
|
|
|
|
}
|
|
|
|
|
2024-06-21 13:33:08 -04:00
|
|
|
fn debug_assert_args_compatible(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) {
|
|
|
|
self.debug_assert_args_compatible(def_id, args);
|
2024-05-10 14:59:56 -04:00
|
|
|
}
|
2024-05-11 13:51:25 -04:00
|
|
|
|
2024-09-30 01:00:38 -04:00
|
|
|
/// Assert that the args from an `ExistentialTraitRef` or `ExistentialProjection`
|
|
|
|
/// are compatible with the `DefId`. Since we're missing a `Self` type, stick on
|
|
|
|
/// a dummy self type and forward to `debug_assert_args_compatible`.
|
|
|
|
fn debug_assert_existential_args_compatible(
|
|
|
|
self,
|
|
|
|
def_id: Self::DefId,
|
|
|
|
args: Self::GenericArgs,
|
|
|
|
) {
|
|
|
|
// FIXME: We could perhaps add a `skip: usize` to `debug_assert_args_compatible`
|
|
|
|
// to avoid needing to reintern the set of args...
|
|
|
|
if cfg!(debug_assertions) {
|
|
|
|
self.debug_assert_args_compatible(
|
|
|
|
def_id,
|
|
|
|
self.mk_args_from_iter(
|
|
|
|
[self.types.trait_object_dummy_self.into()].into_iter().chain(args.iter()),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 16:02:36 -04:00
|
|
|
fn mk_type_list_from_iter<I, T>(self, args: I) -> T::Output
|
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
2024-06-17 17:59:08 -04:00
|
|
|
T: CollectAndApply<Ty<'tcx>, &'tcx List<Ty<'tcx>>>,
|
2024-06-04 16:02:36 -04:00
|
|
|
{
|
|
|
|
self.mk_type_list_from_iter(args)
|
|
|
|
}
|
|
|
|
|
2024-05-29 20:15:56 -04:00
|
|
|
fn parent(self, def_id: DefId) -> DefId {
|
2024-05-11 12:46:11 -04:00
|
|
|
self.parent(def_id)
|
|
|
|
}
|
2024-05-18 10:03:53 -04:00
|
|
|
|
|
|
|
fn recursion_limit(self) -> usize {
|
|
|
|
self.recursion_limit().0
|
|
|
|
}
|
2024-06-04 16:02:36 -04:00
|
|
|
|
|
|
|
type Features = &'tcx rustc_feature::Features;
|
|
|
|
|
|
|
|
fn features(self) -> Self::Features {
|
|
|
|
self.features()
|
|
|
|
}
|
2024-05-29 20:15:56 -04:00
|
|
|
|
|
|
|
fn bound_coroutine_hidden_types(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
2024-06-13 09:47:42 -04:00
|
|
|
) -> impl IntoIterator<Item = ty::EarlyBinder<'tcx, ty::Binder<'tcx, Ty<'tcx>>>> {
|
2024-05-29 20:15:56 -04:00
|
|
|
self.bound_coroutine_hidden_types(def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fn_sig(self, def_id: DefId) -> ty::EarlyBinder<'tcx, ty::PolyFnSig<'tcx>> {
|
|
|
|
self.fn_sig(def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn coroutine_movability(self, def_id: DefId) -> rustc_ast::Movability {
|
|
|
|
self.coroutine_movability(def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn coroutine_for_closure(self, def_id: DefId) -> DefId {
|
|
|
|
self.coroutine_for_closure(def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generics_require_sized_self(self, def_id: DefId) -> bool {
|
|
|
|
self.generics_require_sized_self(def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn item_bounds(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
2024-06-13 09:47:42 -04:00
|
|
|
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
|
2024-05-29 20:15:56 -04:00
|
|
|
self.item_bounds(def_id).map_bound(IntoIterator::into_iter)
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
fn predicates_of(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
|
|
|
|
ty::EarlyBinder::bind(
|
|
|
|
self.predicates_of(def_id).instantiate_identity(self).predicates.into_iter(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn own_predicates_of(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Clause<'tcx>>> {
|
|
|
|
ty::EarlyBinder::bind(
|
|
|
|
self.predicates_of(def_id).instantiate_own_identity().map(|(clause, _)| clause),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-06-27 12:01:49 -04:00
|
|
|
fn explicit_super_predicates_of(
|
2024-05-29 20:15:56 -04:00
|
|
|
self,
|
|
|
|
def_id: DefId,
|
2024-07-06 12:21:00 -04:00
|
|
|
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>> {
|
2024-08-29 00:17:40 -04:00
|
|
|
self.explicit_super_predicates_of(def_id).map_bound(|preds| preds.into_iter().copied())
|
2024-07-06 12:21:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn explicit_implied_predicates_of(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>> {
|
2024-08-29 00:17:40 -04:00
|
|
|
self.explicit_implied_predicates_of(def_id).map_bound(|preds| preds.into_iter().copied())
|
2024-05-29 20:15:56 -04:00
|
|
|
}
|
|
|
|
|
2024-10-20 19:49:11 +00:00
|
|
|
fn is_const_impl(self, def_id: DefId) -> bool {
|
2024-10-23 16:53:59 +00:00
|
|
|
self.is_conditionally_const(def_id)
|
2024-10-20 19:49:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn const_conditions(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Binder<'tcx, ty::TraitRef<'tcx>>>> {
|
|
|
|
ty::EarlyBinder::bind(
|
|
|
|
self.const_conditions(def_id).instantiate_identity(self).into_iter().map(|(c, _)| c),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn implied_const_bounds(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> ty::EarlyBinder<'tcx, impl IntoIterator<Item = ty::Binder<'tcx, ty::TraitRef<'tcx>>>> {
|
|
|
|
ty::EarlyBinder::bind(
|
|
|
|
self.implied_const_bounds(def_id).iter_identity_copied().map(|(c, _)| c),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-29 20:15:56 -04:00
|
|
|
fn has_target_features(self, def_id: DefId) -> bool {
|
|
|
|
!self.codegen_fn_attrs(def_id).target_features.is_empty()
|
|
|
|
}
|
2024-06-12 17:48:37 -04:00
|
|
|
|
|
|
|
fn require_lang_item(self, lang_item: TraitSolverLangItem) -> DefId {
|
2024-06-17 17:59:08 -04:00
|
|
|
self.require_lang_item(trait_lang_item_to_lang_item(lang_item), None)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_lang_item(self, def_id: DefId, lang_item: TraitSolverLangItem) -> bool {
|
|
|
|
self.is_lang_item(def_id, trait_lang_item_to_lang_item(lang_item))
|
2024-06-12 17:48:37 -04:00
|
|
|
}
|
|
|
|
|
2024-06-29 23:01:24 -04:00
|
|
|
fn as_lang_item(self, def_id: DefId) -> Option<TraitSolverLangItem> {
|
|
|
|
lang_item_to_trait_lang_item(self.lang_items().from_def_id(def_id)?)
|
|
|
|
}
|
|
|
|
|
2024-06-13 09:47:42 -04:00
|
|
|
fn associated_type_def_ids(self, def_id: DefId) -> impl IntoIterator<Item = DefId> {
|
2024-06-12 17:48:37 -04:00
|
|
|
self.associated_items(def_id)
|
|
|
|
.in_definition_order()
|
|
|
|
.filter(|assoc_item| matches!(assoc_item.kind, ty::AssocKind::Type))
|
|
|
|
.map(|assoc_item| assoc_item.def_id)
|
|
|
|
}
|
2024-06-17 17:59:08 -04:00
|
|
|
|
|
|
|
// This implementation is a bit different from `TyCtxt::for_each_relevant_impl`,
|
|
|
|
// since we want to skip over blanket impls for non-rigid aliases, and also we
|
|
|
|
// only want to consider types that *actually* unify with float/int vars.
|
|
|
|
fn for_each_relevant_impl(
|
|
|
|
self,
|
|
|
|
trait_def_id: DefId,
|
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
mut f: impl FnMut(DefId),
|
|
|
|
) {
|
|
|
|
let tcx = self;
|
|
|
|
let trait_impls = tcx.trait_impls_of(trait_def_id);
|
|
|
|
let mut consider_impls_for_simplified_type = |simp| {
|
|
|
|
if let Some(impls_for_type) = trait_impls.non_blanket_impls().get(&simp) {
|
|
|
|
for &impl_def_id in impls_for_type {
|
|
|
|
f(impl_def_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match self_ty.kind() {
|
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Adt(_, _)
|
|
|
|
| ty::Foreign(_)
|
|
|
|
| ty::Str
|
|
|
|
| ty::Array(_, _)
|
|
|
|
| ty::Pat(_, _)
|
|
|
|
| ty::Slice(_)
|
|
|
|
| ty::RawPtr(_, _)
|
|
|
|
| ty::Ref(_, _, _)
|
|
|
|
| ty::FnDef(_, _)
|
2024-08-08 17:18:20 +10:00
|
|
|
| ty::FnPtr(..)
|
2024-06-17 17:59:08 -04:00
|
|
|
| ty::Dynamic(_, _, _)
|
|
|
|
| ty::Closure(..)
|
|
|
|
| ty::CoroutineClosure(..)
|
|
|
|
| ty::Coroutine(_, _)
|
|
|
|
| ty::Never
|
|
|
|
| ty::Tuple(_) => {
|
|
|
|
let simp = ty::fast_reject::simplify_type(
|
|
|
|
tcx,
|
|
|
|
self_ty,
|
2024-08-04 19:52:15 +03:00
|
|
|
ty::fast_reject::TreatParams::AsRigid,
|
2024-06-17 17:59:08 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
consider_impls_for_simplified_type(simp);
|
|
|
|
}
|
|
|
|
|
|
|
|
// HACK: For integer and float variables we have to manually look at all impls
|
|
|
|
// which have some integer or float as a self type.
|
|
|
|
ty::Infer(ty::IntVar(_)) => {
|
|
|
|
use ty::IntTy::*;
|
|
|
|
use ty::UintTy::*;
|
|
|
|
// This causes a compiler error if any new integer kinds are added.
|
|
|
|
let (I8 | I16 | I32 | I64 | I128 | Isize): ty::IntTy;
|
|
|
|
let (U8 | U16 | U32 | U64 | U128 | Usize): ty::UintTy;
|
|
|
|
let possible_integers = [
|
|
|
|
// signed integers
|
|
|
|
ty::SimplifiedType::Int(I8),
|
|
|
|
ty::SimplifiedType::Int(I16),
|
|
|
|
ty::SimplifiedType::Int(I32),
|
|
|
|
ty::SimplifiedType::Int(I64),
|
|
|
|
ty::SimplifiedType::Int(I128),
|
|
|
|
ty::SimplifiedType::Int(Isize),
|
|
|
|
// unsigned integers
|
|
|
|
ty::SimplifiedType::Uint(U8),
|
|
|
|
ty::SimplifiedType::Uint(U16),
|
|
|
|
ty::SimplifiedType::Uint(U32),
|
|
|
|
ty::SimplifiedType::Uint(U64),
|
|
|
|
ty::SimplifiedType::Uint(U128),
|
|
|
|
ty::SimplifiedType::Uint(Usize),
|
|
|
|
];
|
|
|
|
for simp in possible_integers {
|
|
|
|
consider_impls_for_simplified_type(simp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ty::Infer(ty::FloatVar(_)) => {
|
|
|
|
// This causes a compiler error if any new float kinds are added.
|
|
|
|
let (ty::FloatTy::F16 | ty::FloatTy::F32 | ty::FloatTy::F64 | ty::FloatTy::F128);
|
|
|
|
let possible_floats = [
|
|
|
|
ty::SimplifiedType::Float(ty::FloatTy::F16),
|
|
|
|
ty::SimplifiedType::Float(ty::FloatTy::F32),
|
|
|
|
ty::SimplifiedType::Float(ty::FloatTy::F64),
|
|
|
|
ty::SimplifiedType::Float(ty::FloatTy::F128),
|
|
|
|
];
|
|
|
|
|
|
|
|
for simp in possible_floats {
|
|
|
|
consider_impls_for_simplified_type(simp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The only traits applying to aliases and placeholders are blanket impls.
|
|
|
|
//
|
|
|
|
// Impls which apply to an alias after normalization are handled by
|
|
|
|
// `assemble_candidates_after_normalizing_self_ty`.
|
|
|
|
ty::Alias(_, _) | ty::Placeholder(..) | ty::Error(_) => (),
|
|
|
|
|
|
|
|
// FIXME: These should ideally not exist as a self type. It would be nice for
|
|
|
|
// the builtin auto trait impls of coroutines to instead directly recurse
|
|
|
|
// into the witness.
|
|
|
|
ty::CoroutineWitness(..) => (),
|
|
|
|
|
|
|
|
// These variants should not exist as a self type.
|
|
|
|
ty::Infer(ty::TyVar(_) | ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_))
|
|
|
|
| ty::Param(_)
|
|
|
|
| ty::Bound(_, _) => bug!("unexpected self type: {self_ty}"),
|
|
|
|
}
|
|
|
|
|
|
|
|
let trait_impls = tcx.trait_impls_of(trait_def_id);
|
|
|
|
for &impl_def_id in trait_impls.blanket_impls() {
|
|
|
|
f(impl_def_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_item_definition(self, def_id: DefId) -> bool {
|
|
|
|
self.defaultness(def_id).has_value()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impl_is_default(self, impl_def_id: DefId) -> bool {
|
|
|
|
self.defaultness(impl_def_id).is_default()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impl_trait_ref(self, impl_def_id: DefId) -> ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>> {
|
|
|
|
self.impl_trait_ref(impl_def_id).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn impl_polarity(self, impl_def_id: DefId) -> ty::ImplPolarity {
|
|
|
|
self.impl_polarity(impl_def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trait_is_auto(self, trait_def_id: DefId) -> bool {
|
|
|
|
self.trait_is_auto(trait_def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn trait_is_alias(self, trait_def_id: DefId) -> bool {
|
|
|
|
self.trait_is_alias(trait_def_id)
|
|
|
|
}
|
|
|
|
|
2024-09-25 10:38:40 +02:00
|
|
|
fn trait_is_dyn_compatible(self, trait_def_id: DefId) -> bool {
|
|
|
|
self.is_dyn_compatible(trait_def_id)
|
2024-06-17 17:59:08 -04:00
|
|
|
}
|
|
|
|
|
2024-07-06 18:24:51 -04:00
|
|
|
fn trait_is_fundamental(self, def_id: DefId) -> bool {
|
|
|
|
self.trait_def(def_id).is_fundamental
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
fn trait_may_be_implemented_via_object(self, trait_def_id: DefId) -> bool {
|
|
|
|
self.trait_def(trait_def_id).implement_via_object
|
|
|
|
}
|
|
|
|
|
2024-09-29 13:41:13 -04:00
|
|
|
fn is_impl_trait_in_trait(self, def_id: DefId) -> bool {
|
|
|
|
self.is_impl_trait_in_trait(def_id)
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
fn delay_bug(self, msg: impl ToString) -> ErrorGuaranteed {
|
|
|
|
self.dcx().span_delayed_bug(DUMMY_SP, msg.to_string())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_general_coroutine(self, coroutine_def_id: DefId) -> bool {
|
|
|
|
self.is_general_coroutine(coroutine_def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn coroutine_is_async(self, coroutine_def_id: DefId) -> bool {
|
|
|
|
self.coroutine_is_async(coroutine_def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn coroutine_is_gen(self, coroutine_def_id: DefId) -> bool {
|
|
|
|
self.coroutine_is_gen(coroutine_def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn coroutine_is_async_gen(self, coroutine_def_id: DefId) -> bool {
|
|
|
|
self.coroutine_is_async_gen(coroutine_def_id)
|
|
|
|
}
|
|
|
|
|
2024-11-15 13:53:31 +01:00
|
|
|
// We don't use `TypingEnv` here as it's only defined in `rustc_middle` and
|
|
|
|
// `rustc_next_trait_solver` shouldn't have to know about it.
|
|
|
|
fn layout_is_pointer_like(
|
|
|
|
self,
|
|
|
|
typing_mode: ty::TypingMode<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> bool {
|
|
|
|
let typing_env = ty::TypingEnv { typing_mode, param_env };
|
|
|
|
self.layout_of(self.erase_regions(typing_env).as_query_input(self.erase_regions(ty)))
|
2024-06-17 17:59:08 -04:00
|
|
|
.is_ok_and(|layout| layout.layout.is_pointer_like(&self.data_layout))
|
|
|
|
}
|
|
|
|
|
|
|
|
type UnsizingParams = &'tcx rustc_index::bit_set::BitSet<u32>;
|
|
|
|
fn unsizing_params_for_adt(self, adt_def_id: DefId) -> Self::UnsizingParams {
|
|
|
|
self.unsizing_params_for_adt(adt_def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_const_ty_from_env(
|
|
|
|
self,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
placeholder: Self::PlaceholderConst,
|
|
|
|
) -> Ty<'tcx> {
|
|
|
|
placeholder.find_const_ty_from_env(param_env)
|
|
|
|
}
|
2024-07-06 12:21:00 -04:00
|
|
|
|
|
|
|
fn anonymize_bound_vars<T: TypeFoldable<TyCtxt<'tcx>>>(
|
|
|
|
self,
|
|
|
|
binder: ty::Binder<'tcx, T>,
|
|
|
|
) -> ty::Binder<'tcx, T> {
|
|
|
|
self.anonymize_bound_vars(binder)
|
|
|
|
}
|
2024-10-18 00:28:43 +02:00
|
|
|
|
|
|
|
fn opaque_types_defined_by(self, defining_anchor: LocalDefId) -> Self::DefiningOpaqueTypes {
|
|
|
|
self.opaque_types_defined_by(defining_anchor)
|
|
|
|
}
|
2024-06-17 17:59:08 -04:00
|
|
|
}
|
|
|
|
|
2024-06-29 23:01:24 -04:00
|
|
|
macro_rules! bidirectional_lang_item_map {
|
|
|
|
($($name:ident),+ $(,)?) => {
|
|
|
|
fn trait_lang_item_to_lang_item(lang_item: TraitSolverLangItem) -> LangItem {
|
|
|
|
match lang_item {
|
|
|
|
$(TraitSolverLangItem::$name => LangItem::$name,)+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lang_item_to_trait_lang_item(lang_item: LangItem) -> Option<TraitSolverLangItem> {
|
|
|
|
Some(match lang_item {
|
|
|
|
$(LangItem::$name => TraitSolverLangItem::$name,)+
|
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
2024-06-17 17:59:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-29 23:01:24 -04:00
|
|
|
bidirectional_lang_item_map! {
|
|
|
|
// tidy-alphabetical-start
|
|
|
|
AsyncDestruct,
|
2024-06-29 23:05:35 -04:00
|
|
|
AsyncFn,
|
2024-06-29 23:01:24 -04:00
|
|
|
AsyncFnKindHelper,
|
|
|
|
AsyncFnKindUpvars,
|
2024-06-29 23:05:35 -04:00
|
|
|
AsyncFnMut,
|
|
|
|
AsyncFnOnce,
|
2024-06-29 23:01:24 -04:00
|
|
|
AsyncFnOnceOutput,
|
|
|
|
AsyncIterator,
|
|
|
|
CallOnceFuture,
|
|
|
|
CallRefFuture,
|
|
|
|
Clone,
|
|
|
|
Copy,
|
|
|
|
Coroutine,
|
|
|
|
CoroutineReturn,
|
|
|
|
CoroutineYield,
|
|
|
|
Destruct,
|
|
|
|
DiscriminantKind,
|
|
|
|
DynMetadata,
|
2024-06-29 23:05:35 -04:00
|
|
|
Fn,
|
|
|
|
FnMut,
|
|
|
|
FnOnce,
|
2024-06-29 23:01:24 -04:00
|
|
|
FnPtrTrait,
|
|
|
|
FusedIterator,
|
|
|
|
Future,
|
|
|
|
FutureOutput,
|
|
|
|
Iterator,
|
|
|
|
Metadata,
|
|
|
|
Option,
|
|
|
|
PointeeTrait,
|
|
|
|
PointerLike,
|
|
|
|
Poll,
|
|
|
|
Sized,
|
|
|
|
TransmuteTrait,
|
|
|
|
Tuple,
|
|
|
|
Unpin,
|
|
|
|
Unsize,
|
|
|
|
// tidy-alphabetical-end
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
impl<'tcx> rustc_type_ir::inherent::DefId<TyCtxt<'tcx>> for DefId {
|
2024-07-06 18:24:51 -04:00
|
|
|
fn is_local(self) -> bool {
|
|
|
|
self.is_local()
|
|
|
|
}
|
|
|
|
|
2024-06-17 17:59:08 -04:00
|
|
|
fn as_local(self) -> Option<LocalDefId> {
|
|
|
|
self.as_local()
|
|
|
|
}
|
2021-01-31 10:32:34 +01:00
|
|
|
}
|
|
|
|
|
2024-11-02 19:33:00 -07:00
|
|
|
impl<'tcx> rustc_type_ir::inherent::Abi<TyCtxt<'tcx>> for ExternAbi {
|
2024-06-12 18:35:51 -04:00
|
|
|
fn rust() -> Self {
|
2024-11-02 19:33:00 -07:00
|
|
|
ExternAbi::Rust
|
2024-06-12 18:35:51 -04:00
|
|
|
}
|
|
|
|
|
2024-05-15 13:54:37 -04:00
|
|
|
fn is_rust(self) -> bool {
|
2024-11-02 19:33:00 -07:00
|
|
|
matches!(self, ExternAbi::Rust)
|
2024-05-15 13:54:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-17 14:17:48 -03:00
|
|
|
impl<'tcx> rustc_type_ir::inherent::Safety<TyCtxt<'tcx>> for hir::Safety {
|
2024-06-12 18:35:51 -04:00
|
|
|
fn safe() -> Self {
|
|
|
|
hir::Safety::Safe
|
|
|
|
}
|
|
|
|
|
2024-05-20 12:57:07 -04:00
|
|
|
fn is_safe(self) -> bool {
|
|
|
|
matches!(self, hir::Safety::Safe)
|
|
|
|
}
|
|
|
|
|
2024-05-15 13:54:37 -04:00
|
|
|
fn prefix_str(self) -> &'static str {
|
|
|
|
self.prefix_str()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-04 16:02:36 -04:00
|
|
|
impl<'tcx> rustc_type_ir::inherent::Features<TyCtxt<'tcx>> for &'tcx rustc_feature::Features {
|
|
|
|
fn generic_const_exprs(self) -> bool {
|
2024-10-09 09:01:57 +02:00
|
|
|
self.generic_const_exprs()
|
2024-06-04 16:02:36 -04:00
|
|
|
}
|
2024-05-29 20:15:56 -04:00
|
|
|
|
|
|
|
fn coroutine_clone(self) -> bool {
|
2024-10-09 09:01:57 +02:00
|
|
|
self.coroutine_clone()
|
2024-05-29 20:15:56 -04:00
|
|
|
}
|
2024-06-17 17:59:08 -04:00
|
|
|
|
|
|
|
fn associated_const_equality(self) -> bool {
|
2024-10-09 09:01:57 +02:00
|
|
|
self.associated_const_equality()
|
2024-06-17 17:59:08 -04:00
|
|
|
}
|
2024-06-04 16:02:36 -04:00
|
|
|
}
|
|
|
|
|
2024-10-04 12:58:20 -04:00
|
|
|
impl<'tcx> rustc_type_ir::inherent::Span<TyCtxt<'tcx>> for Span {
|
|
|
|
fn dummy() -> Self {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 14:27:17 +11:00
|
|
|
type InternedSet<'tcx, T> = ShardedHashMap<InternedInSet<'tcx, T>, ()>;
|
2018-04-09 16:38:00 +09:00
|
|
|
|
2016-05-11 04:14:41 +03:00
|
|
|
pub struct CtxtInterners<'tcx> {
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The arena that types, regions, etc. are allocated from.
|
2020-01-02 01:26:18 +01:00
|
|
|
arena: &'tcx WorkerLocal<Arena<'tcx>>,
|
2016-04-28 03:13:17 +03:00
|
|
|
|
2021-08-26 19:47:07 +03:00
|
|
|
// Specifically use a speedy hash algorithm for these hash sets, since
|
|
|
|
// they're accessed quite often.
|
2022-11-25 16:55:25 +00:00
|
|
|
type_: InternedSet<'tcx, WithCachedTypeInfo<TyKind<'tcx>>>,
|
2022-11-23 09:25:01 +00:00
|
|
|
const_lists: InternedSet<'tcx, List<ty::Const<'tcx>>>,
|
2023-07-11 22:35:29 +01:00
|
|
|
args: InternedSet<'tcx, GenericArgs<'tcx>>,
|
2023-04-18 17:42:30 +00:00
|
|
|
type_lists: InternedSet<'tcx, List<Ty<'tcx>>>,
|
2020-10-28 01:11:03 +00:00
|
|
|
canonical_var_infos: InternedSet<'tcx, List<CanonicalVarInfo<'tcx>>>,
|
2022-06-19 00:20:27 -04:00
|
|
|
region: InternedSet<'tcx, RegionKind<'tcx>>,
|
2022-11-19 03:28:56 +00:00
|
|
|
poly_existential_predicates: InternedSet<'tcx, List<PolyExistentialPredicate<'tcx>>>,
|
2022-11-30 14:47:40 +00:00
|
|
|
predicate: InternedSet<'tcx, WithCachedTypeInfo<ty::Binder<'tcx, PredicateKind<'tcx>>>>,
|
2024-03-24 22:49:31 +01:00
|
|
|
clauses: InternedSet<'tcx, ListWithCachedTypeInfo<Clause<'tcx>>>,
|
2019-03-28 18:00:17 -07:00
|
|
|
projs: InternedSet<'tcx, List<ProjectionKind>>,
|
2019-10-11 16:19:59 -03:00
|
|
|
place_elems: InternedSet<'tcx, List<PlaceElem<'tcx>>>,
|
2024-06-03 01:10:24 +01:00
|
|
|
const_: InternedSet<'tcx, WithCachedTypeInfo<ty::ConstKind<'tcx>>>,
|
2023-02-02 13:57:36 +00:00
|
|
|
pat: InternedSet<'tcx, PatternKind<'tcx>>,
|
2021-08-26 19:47:07 +03:00
|
|
|
const_allocation: InternedSet<'tcx, Allocation>,
|
2020-10-05 20:41:46 -04:00
|
|
|
bound_variable_kinds: InternedSet<'tcx, List<ty::BoundVariableKind>>,
|
2024-10-27 22:31:09 -07:00
|
|
|
layout: InternedSet<'tcx, LayoutData<FieldIdx, VariantIdx>>,
|
2022-03-05 07:28:41 +11:00
|
|
|
adt_def: InternedSet<'tcx, AdtDefData>,
|
2024-06-14 15:16:47 -04:00
|
|
|
external_constraints: InternedSet<'tcx, ExternalConstraintsData<TyCtxt<'tcx>>>,
|
2024-06-17 11:03:06 -04:00
|
|
|
predefined_opaques_in_body: InternedSet<'tcx, PredefinedOpaquesData<TyCtxt<'tcx>>>,
|
2023-03-11 16:01:25 -08:00
|
|
|
fields: InternedSet<'tcx, List<FieldIdx>>,
|
2023-10-18 19:28:46 +00:00
|
|
|
local_def_ids: InternedSet<'tcx, List<LocalDefId>>,
|
2024-03-07 12:27:58 +00:00
|
|
|
captures: InternedSet<'tcx, List<&'tcx ty::CapturedPlace<'tcx>>>,
|
2023-08-15 22:32:55 +01:00
|
|
|
offset_of: InternedSet<'tcx, List<(VariantIdx, FieldIdx)>>,
|
2016-04-28 03:13:17 +03:00
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> CtxtInterners<'tcx> {
|
2020-01-02 01:26:18 +01:00
|
|
|
fn new(arena: &'tcx WorkerLocal<Arena<'tcx>>) -> CtxtInterners<'tcx> {
|
2016-04-28 03:13:17 +03:00
|
|
|
CtxtInterners {
|
2018-04-09 16:38:00 +09:00
|
|
|
arena,
|
|
|
|
type_: Default::default(),
|
2022-11-23 09:25:01 +00:00
|
|
|
const_lists: Default::default(),
|
2023-07-11 22:35:29 +01:00
|
|
|
args: Default::default(),
|
2023-04-18 17:42:30 +00:00
|
|
|
type_lists: Default::default(),
|
2018-04-09 16:38:00 +09:00
|
|
|
region: Default::default(),
|
2020-12-11 15:02:46 -05:00
|
|
|
poly_existential_predicates: Default::default(),
|
2018-04-09 16:38:00 +09:00
|
|
|
canonical_var_infos: Default::default(),
|
2020-06-10 09:30:39 +01:00
|
|
|
predicate: Default::default(),
|
2023-06-19 20:46:46 +00:00
|
|
|
clauses: Default::default(),
|
2018-10-26 11:28:40 +02:00
|
|
|
projs: Default::default(),
|
2019-10-11 16:19:59 -03:00
|
|
|
place_elems: Default::default(),
|
2019-03-14 10:19:31 +01:00
|
|
|
const_: Default::default(),
|
2023-02-02 13:57:36 +00:00
|
|
|
pat: Default::default(),
|
2021-08-26 19:47:07 +03:00
|
|
|
const_allocation: Default::default(),
|
2020-10-05 20:41:46 -04:00
|
|
|
bound_variable_kinds: Default::default(),
|
2021-08-26 19:47:07 +03:00
|
|
|
layout: Default::default(),
|
2021-12-14 12:37:04 -05:00
|
|
|
adt_def: Default::default(),
|
2023-02-03 02:29:52 +00:00
|
|
|
external_constraints: Default::default(),
|
2023-05-16 23:51:23 +00:00
|
|
|
predefined_opaques_in_body: Default::default(),
|
2023-03-11 16:01:25 -08:00
|
|
|
fields: Default::default(),
|
2023-10-18 19:28:46 +00:00
|
|
|
local_def_ids: Default::default(),
|
2024-03-07 12:27:58 +00:00
|
|
|
captures: Default::default(),
|
2023-08-15 20:10:45 +01:00
|
|
|
offset_of: Default::default(),
|
2016-04-28 03:13:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
/// Interns a type. (Use `mk_*` functions instead, where possible.)
|
2019-08-11 12:55:14 -04:00
|
|
|
#[allow(rustc::usage_of_ty_tykind)]
|
2018-11-29 21:13:04 +01:00
|
|
|
#[inline(never)]
|
2022-12-08 08:52:07 +00:00
|
|
|
fn intern_ty(&self, kind: TyKind<'tcx>, sess: &Session, untracked: &Untracked) -> Ty<'tcx> {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
Ty(Interned::new_unchecked(
|
|
|
|
self.type_
|
|
|
|
.intern(kind, |kind| {
|
|
|
|
let flags = super::flags::FlagComputation::for_kind(&kind);
|
2022-12-08 08:52:07 +00:00
|
|
|
let stable_hash = self.stable_hash(&flags, sess, untracked, &kind);
|
2022-02-23 18:02:06 +00:00
|
|
|
|
2022-11-25 16:55:25 +00:00
|
|
|
InternedInSet(self.arena.alloc(WithCachedTypeInfo {
|
|
|
|
internee: kind,
|
|
|
|
stable_hash,
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
flags: flags.flags,
|
|
|
|
outer_exclusive_binder: flags.outer_exclusive_binder,
|
2022-11-25 16:55:25 +00:00
|
|
|
}))
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
})
|
|
|
|
.0,
|
|
|
|
))
|
2018-04-30 08:59:23 +02:00
|
|
|
}
|
2020-06-10 09:30:39 +01:00
|
|
|
|
2023-11-22 23:28:28 +00:00
|
|
|
/// Interns a const. (Use `mk_*` functions instead, where possible.)
|
|
|
|
#[allow(rustc::usage_of_ty_tykind)]
|
|
|
|
#[inline(never)]
|
|
|
|
fn intern_const(
|
|
|
|
&self,
|
2024-06-03 01:10:24 +01:00
|
|
|
kind: ty::ConstKind<'tcx>,
|
2023-11-22 23:28:28 +00:00
|
|
|
sess: &Session,
|
|
|
|
untracked: &Untracked,
|
|
|
|
) -> Const<'tcx> {
|
|
|
|
Const(Interned::new_unchecked(
|
|
|
|
self.const_
|
2024-06-03 01:10:24 +01:00
|
|
|
.intern(kind, |kind: ty::ConstKind<'_>| {
|
|
|
|
let flags = super::flags::FlagComputation::for_const_kind(&kind);
|
|
|
|
let stable_hash = self.stable_hash(&flags, sess, untracked, &kind);
|
2023-11-22 23:28:28 +00:00
|
|
|
|
|
|
|
InternedInSet(self.arena.alloc(WithCachedTypeInfo {
|
2024-06-03 01:10:24 +01:00
|
|
|
internee: kind,
|
2023-11-22 23:28:28 +00:00
|
|
|
stable_hash,
|
|
|
|
flags: flags.flags,
|
|
|
|
outer_exclusive_binder: flags.outer_exclusive_binder,
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
.0,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-03-14 16:54:31 +00:00
|
|
|
fn stable_hash<'a, T: HashStable<StableHashingContext<'a>>>(
|
|
|
|
&self,
|
|
|
|
flags: &ty::flags::FlagComputation,
|
|
|
|
sess: &'a Session,
|
2022-12-07 14:31:50 +00:00
|
|
|
untracked: &'a Untracked,
|
2022-03-14 16:54:31 +00:00
|
|
|
val: &T,
|
|
|
|
) -> Fingerprint {
|
|
|
|
// It's impossible to hash inference variables (and will ICE), so we don't need to try to cache them.
|
|
|
|
// Without incremental, we rarely stable-hash types, so let's not do it proactively.
|
2023-04-27 08:34:11 +01:00
|
|
|
if flags.flags.intersects(TypeFlags::HAS_INFER) || sess.opts.incremental.is_none() {
|
2022-03-14 16:54:31 +00:00
|
|
|
Fingerprint::ZERO
|
|
|
|
} else {
|
|
|
|
let mut hasher = StableHasher::new();
|
2022-12-08 08:52:07 +00:00
|
|
|
let mut hcx = StableHashingContext::new(sess, untracked);
|
2022-03-14 16:54:31 +00:00
|
|
|
val.hash_stable(&mut hcx, &mut hasher);
|
|
|
|
hasher.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
/// Interns a predicate. (Use `mk_predicate` instead, where possible.)
|
2020-06-10 09:30:39 +01:00
|
|
|
#[inline(never)]
|
2022-03-14 16:54:31 +00:00
|
|
|
fn intern_predicate(
|
|
|
|
&self,
|
|
|
|
kind: Binder<'tcx, PredicateKind<'tcx>>,
|
|
|
|
sess: &Session,
|
2022-12-07 14:31:50 +00:00
|
|
|
untracked: &Untracked,
|
2022-03-14 16:54:31 +00:00
|
|
|
) -> Predicate<'tcx> {
|
2022-01-27 17:00:16 +11:00
|
|
|
Predicate(Interned::new_unchecked(
|
|
|
|
self.predicate
|
|
|
|
.intern(kind, |kind| {
|
|
|
|
let flags = super::flags::FlagComputation::for_predicate(kind);
|
2020-06-10 09:30:39 +01:00
|
|
|
|
2022-12-08 08:52:07 +00:00
|
|
|
let stable_hash = self.stable_hash(&flags, sess, untracked, &kind);
|
2022-03-14 16:54:31 +00:00
|
|
|
|
2022-11-30 14:47:40 +00:00
|
|
|
InternedInSet(self.arena.alloc(WithCachedTypeInfo {
|
|
|
|
internee: kind,
|
|
|
|
stable_hash,
|
2022-01-27 17:00:16 +11:00
|
|
|
flags: flags.flags,
|
|
|
|
outer_exclusive_binder: flags.outer_exclusive_binder,
|
2022-11-30 14:47:40 +00:00
|
|
|
}))
|
2022-01-27 17:00:16 +11:00
|
|
|
})
|
|
|
|
.0,
|
|
|
|
))
|
2020-06-10 09:30:39 +01:00
|
|
|
}
|
2024-03-24 22:49:31 +01:00
|
|
|
|
|
|
|
fn intern_clauses(&self, clauses: &[Clause<'tcx>]) -> Clauses<'tcx> {
|
|
|
|
if clauses.is_empty() {
|
|
|
|
ListWithCachedTypeInfo::empty()
|
|
|
|
} else {
|
|
|
|
self.clauses
|
|
|
|
.intern_ref(clauses, || {
|
|
|
|
let flags = super::flags::FlagComputation::for_clauses(clauses);
|
|
|
|
|
|
|
|
InternedInSet(ListWithCachedTypeInfo::from_arena(
|
|
|
|
&*self.arena,
|
|
|
|
flags.into(),
|
|
|
|
clauses,
|
|
|
|
))
|
|
|
|
})
|
|
|
|
.0
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 03:13:17 +03:00
|
|
|
}
|
|
|
|
|
2023-02-13 15:37:44 +11:00
|
|
|
// For these preinterned values, an alternative would be to have
|
|
|
|
// variable-length vectors that grow as needed. But that turned out to be
|
|
|
|
// slightly more complex and no faster.
|
|
|
|
|
2023-02-08 11:52:19 +11:00
|
|
|
const NUM_PREINTERNED_TY_VARS: u32 = 100;
|
|
|
|
const NUM_PREINTERNED_FRESH_TYS: u32 = 20;
|
|
|
|
const NUM_PREINTERNED_FRESH_INT_TYS: u32 = 3;
|
|
|
|
const NUM_PREINTERNED_FRESH_FLOAT_TYS: u32 = 3;
|
|
|
|
|
2023-02-13 15:37:44 +11:00
|
|
|
// This number may seem high, but it is reached in all but the smallest crates.
|
|
|
|
const NUM_PREINTERNED_RE_VARS: u32 = 500;
|
|
|
|
const NUM_PREINTERNED_RE_LATE_BOUNDS_I: u32 = 2;
|
|
|
|
const NUM_PREINTERNED_RE_LATE_BOUNDS_V: u32 = 20;
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
pub struct CommonTypes<'tcx> {
|
2018-11-29 21:13:04 +01:00
|
|
|
pub unit: Ty<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
pub bool: Ty<'tcx>,
|
|
|
|
pub char: Ty<'tcx>,
|
|
|
|
pub isize: Ty<'tcx>,
|
|
|
|
pub i8: Ty<'tcx>,
|
|
|
|
pub i16: Ty<'tcx>,
|
|
|
|
pub i32: Ty<'tcx>,
|
|
|
|
pub i64: Ty<'tcx>,
|
2016-08-23 03:56:52 +03:00
|
|
|
pub i128: Ty<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
pub usize: Ty<'tcx>,
|
|
|
|
pub u8: Ty<'tcx>,
|
|
|
|
pub u16: Ty<'tcx>,
|
|
|
|
pub u32: Ty<'tcx>,
|
|
|
|
pub u64: Ty<'tcx>,
|
2016-08-23 03:56:52 +03:00
|
|
|
pub u128: Ty<'tcx>,
|
2024-02-28 03:44:23 -05:00
|
|
|
pub f16: Ty<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
pub f32: Ty<'tcx>,
|
|
|
|
pub f64: Ty<'tcx>,
|
2024-02-28 03:44:23 -05:00
|
|
|
pub f128: Ty<'tcx>,
|
2020-05-28 13:02:02 +02:00
|
|
|
pub str_: Ty<'tcx>,
|
2016-08-02 15:56:20 +08:00
|
|
|
pub never: Ty<'tcx>,
|
2019-08-12 22:15:12 +01:00
|
|
|
pub self_param: Ty<'tcx>,
|
2017-04-20 01:58:12 +03:00
|
|
|
|
2019-04-03 11:46:40 +02:00
|
|
|
/// Dummy type used for the `Self` of a `TraitRef` created for converting
|
|
|
|
/// a trait object, and which gets removed in `ExistentialTraitRef`.
|
|
|
|
/// This type must not appear anywhere in other converted types.
|
2023-02-08 11:52:19 +11:00
|
|
|
/// `Infer(ty::FreshTy(0))` does the job.
|
2019-04-03 11:46:40 +02:00
|
|
|
pub trait_object_dummy_self: Ty<'tcx>,
|
2023-02-08 11:52:19 +11:00
|
|
|
|
|
|
|
/// Pre-interned `Infer(ty::TyVar(n))` for small values of `n`.
|
|
|
|
pub ty_vars: Vec<Ty<'tcx>>,
|
|
|
|
|
|
|
|
/// Pre-interned `Infer(ty::FreshTy(n))` for small values of `n`.
|
|
|
|
pub fresh_tys: Vec<Ty<'tcx>>,
|
|
|
|
|
|
|
|
/// Pre-interned `Infer(ty::FreshIntTy(n))` for small values of `n`.
|
|
|
|
pub fresh_int_tys: Vec<Ty<'tcx>>,
|
|
|
|
|
|
|
|
/// Pre-interned `Infer(ty::FreshFloatTy(n))` for small values of `n`.
|
|
|
|
pub fresh_float_tys: Vec<Ty<'tcx>>,
|
2019-04-25 22:04:51 +01:00
|
|
|
}
|
2019-04-03 11:46:40 +02:00
|
|
|
|
2019-04-25 22:04:51 +01:00
|
|
|
pub struct CommonLifetimes<'tcx> {
|
2020-01-18 05:47:28 -05:00
|
|
|
/// `ReStatic`
|
2017-04-20 04:45:53 -04:00
|
|
|
pub re_static: Region<'tcx>,
|
2019-10-08 19:26:57 -04:00
|
|
|
|
2021-10-25 15:43:07 -05:00
|
|
|
/// Erased region, used outside of type inference.
|
2017-04-20 04:45:53 -04:00
|
|
|
pub re_erased: Region<'tcx>,
|
2023-02-13 15:37:44 +11:00
|
|
|
|
|
|
|
/// Pre-interned `ReVar(ty::RegionVar(n))` for small values of `n`.
|
|
|
|
pub re_vars: Vec<Region<'tcx>>,
|
|
|
|
|
|
|
|
/// Pre-interned values of the form:
|
2023-11-13 14:00:05 +00:00
|
|
|
/// `ReBound(DebruijnIndex(i), BoundRegion { var: v, kind: BrAnon })`
|
2023-02-13 15:37:44 +11:00
|
|
|
/// for small values of `i` and `v`.
|
|
|
|
pub re_late_bounds: Vec<Vec<Region<'tcx>>>,
|
2019-05-01 23:09:53 +01:00
|
|
|
}
|
2019-03-08 01:15:37 +00:00
|
|
|
|
2019-05-01 23:09:53 +01:00
|
|
|
pub struct CommonConsts<'tcx> {
|
2022-02-02 14:24:45 +11:00
|
|
|
pub unit: Const<'tcx>,
|
2023-06-30 15:21:35 +00:00
|
|
|
pub true_: Const<'tcx>,
|
|
|
|
pub false_: Const<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> CommonTypes<'tcx> {
|
2022-02-23 18:02:06 +00:00
|
|
|
fn new(
|
|
|
|
interners: &CtxtInterners<'tcx>,
|
|
|
|
sess: &Session,
|
2022-12-07 14:31:50 +00:00
|
|
|
untracked: &Untracked,
|
2022-02-23 18:02:06 +00:00
|
|
|
) -> CommonTypes<'tcx> {
|
2022-12-08 08:52:07 +00:00
|
|
|
let mk = |ty| interners.intern_ty(ty, sess, untracked);
|
2018-11-29 21:13:04 +01:00
|
|
|
|
2023-02-08 11:52:19 +11:00
|
|
|
let ty_vars =
|
|
|
|
(0..NUM_PREINTERNED_TY_VARS).map(|n| mk(Infer(ty::TyVar(TyVid::from(n))))).collect();
|
|
|
|
let fresh_tys: Vec<_> =
|
|
|
|
(0..NUM_PREINTERNED_FRESH_TYS).map(|n| mk(Infer(ty::FreshTy(n)))).collect();
|
|
|
|
let fresh_int_tys: Vec<_> =
|
|
|
|
(0..NUM_PREINTERNED_FRESH_INT_TYS).map(|n| mk(Infer(ty::FreshIntTy(n)))).collect();
|
|
|
|
let fresh_float_tys: Vec<_> =
|
|
|
|
(0..NUM_PREINTERNED_FRESH_FLOAT_TYS).map(|n| mk(Infer(ty::FreshFloatTy(n)))).collect();
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
CommonTypes {
|
2018-11-29 21:13:04 +01:00
|
|
|
unit: mk(Tuple(List::empty())),
|
2018-08-22 01:35:55 +01:00
|
|
|
bool: mk(Bool),
|
|
|
|
char: mk(Char),
|
2018-08-22 01:35:02 +01:00
|
|
|
never: mk(Never),
|
2020-12-12 15:28:49 +01:00
|
|
|
isize: mk(Int(ty::IntTy::Isize)),
|
|
|
|
i8: mk(Int(ty::IntTy::I8)),
|
|
|
|
i16: mk(Int(ty::IntTy::I16)),
|
|
|
|
i32: mk(Int(ty::IntTy::I32)),
|
|
|
|
i64: mk(Int(ty::IntTy::I64)),
|
|
|
|
i128: mk(Int(ty::IntTy::I128)),
|
|
|
|
usize: mk(Uint(ty::UintTy::Usize)),
|
|
|
|
u8: mk(Uint(ty::UintTy::U8)),
|
|
|
|
u16: mk(Uint(ty::UintTy::U16)),
|
|
|
|
u32: mk(Uint(ty::UintTy::U32)),
|
|
|
|
u64: mk(Uint(ty::UintTy::U64)),
|
|
|
|
u128: mk(Uint(ty::UintTy::U128)),
|
2024-02-28 03:44:23 -05:00
|
|
|
f16: mk(Float(ty::FloatTy::F16)),
|
2020-12-12 15:28:49 +01:00
|
|
|
f32: mk(Float(ty::FloatTy::F32)),
|
|
|
|
f64: mk(Float(ty::FloatTy::F64)),
|
2024-02-28 03:44:23 -05:00
|
|
|
f128: mk(Float(ty::FloatTy::F128)),
|
2020-05-28 13:02:02 +02:00
|
|
|
str_: mk(Str),
|
2019-12-24 17:38:22 -05:00
|
|
|
self_param: mk(ty::Param(ty::ParamTy { index: 0, name: kw::SelfUpper })),
|
2017-04-20 01:58:12 +03:00
|
|
|
|
2023-02-08 11:52:19 +11:00
|
|
|
trait_object_dummy_self: fresh_tys[0],
|
|
|
|
|
|
|
|
ty_vars,
|
|
|
|
fresh_tys,
|
|
|
|
fresh_int_tys,
|
|
|
|
fresh_float_tys,
|
2019-04-25 22:04:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-03 11:46:40 +02:00
|
|
|
|
2019-04-25 22:04:51 +01:00
|
|
|
impl<'tcx> CommonLifetimes<'tcx> {
|
|
|
|
fn new(interners: &CtxtInterners<'tcx>) -> CommonLifetimes<'tcx> {
|
2022-01-28 11:25:15 +11:00
|
|
|
let mk = |r| {
|
|
|
|
Region(Interned::new_unchecked(
|
|
|
|
interners.region.intern(r, |r| InternedInSet(interners.arena.alloc(r))).0,
|
|
|
|
))
|
|
|
|
};
|
2019-04-25 22:04:51 +01:00
|
|
|
|
2023-02-13 15:37:44 +11:00
|
|
|
let re_vars =
|
|
|
|
(0..NUM_PREINTERNED_RE_VARS).map(|n| mk(ty::ReVar(ty::RegionVid::from(n)))).collect();
|
|
|
|
|
|
|
|
let re_late_bounds = (0..NUM_PREINTERNED_RE_LATE_BOUNDS_I)
|
|
|
|
.map(|i| {
|
|
|
|
(0..NUM_PREINTERNED_RE_LATE_BOUNDS_V)
|
|
|
|
.map(|v| {
|
2023-08-03 15:56:56 +00:00
|
|
|
mk(ty::ReBound(ty::DebruijnIndex::from(i), ty::BoundRegion {
|
|
|
|
var: ty::BoundVar::from(v),
|
2024-11-03 22:06:03 +00:00
|
|
|
kind: ty::BoundRegionKind::Anon,
|
2023-02-13 15:37:44 +11:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
CommonLifetimes {
|
|
|
|
re_static: mk(ty::ReStatic),
|
|
|
|
re_erased: mk(ty::ReErased),
|
|
|
|
re_vars,
|
|
|
|
re_late_bounds,
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 23:09:53 +01:00
|
|
|
impl<'tcx> CommonConsts<'tcx> {
|
2023-11-22 23:28:28 +00:00
|
|
|
fn new(
|
|
|
|
interners: &CtxtInterners<'tcx>,
|
|
|
|
types: &CommonTypes<'tcx>,
|
|
|
|
sess: &Session,
|
|
|
|
untracked: &Untracked,
|
|
|
|
) -> CommonConsts<'tcx> {
|
2022-02-02 14:24:45 +11:00
|
|
|
let mk_const = |c| {
|
2023-11-22 23:28:28 +00:00
|
|
|
interners.intern_const(
|
|
|
|
c, sess, // This is only used to create a stable hashing context.
|
|
|
|
untracked,
|
|
|
|
)
|
2022-02-02 14:24:45 +11:00
|
|
|
};
|
2019-05-01 23:09:53 +01:00
|
|
|
|
|
|
|
CommonConsts {
|
2024-06-03 02:34:35 +01:00
|
|
|
unit: mk_const(ty::ConstKind::Value(types.unit, ty::ValTree::zst())),
|
|
|
|
true_: mk_const(ty::ConstKind::Value(
|
|
|
|
types.bool,
|
|
|
|
ty::ValTree::Leaf(ty::ScalarInt::TRUE),
|
|
|
|
)),
|
|
|
|
false_: mk_const(ty::ConstKind::Value(
|
|
|
|
types.bool,
|
|
|
|
ty::ValTree::Leaf(ty::ScalarInt::FALSE),
|
|
|
|
)),
|
2019-05-01 23:09:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-14 13:13:27 +00:00
|
|
|
/// This struct contains information regarding a free parameter region,
|
|
|
|
/// either a `ReEarlyParam` or `ReLateParam`.
|
2018-09-02 00:42:51 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct FreeRegionInfo {
|
2023-11-14 13:13:27 +00:00
|
|
|
/// `LocalDefId` of the free region.
|
2020-06-27 13:38:00 +02:00
|
|
|
pub def_id: LocalDefId,
|
2023-11-14 13:13:27 +00:00
|
|
|
/// the bound region corresponding to free region.
|
|
|
|
pub bound_region: ty::BoundRegionKind,
|
2022-11-27 11:15:06 +00:00
|
|
|
/// checks if bound region is in Impl Item
|
2018-09-02 00:42:51 +02:00
|
|
|
pub is_impl_item: bool,
|
|
|
|
}
|
|
|
|
|
2022-11-29 18:49:37 +00:00
|
|
|
/// This struct should only be created by `create_def`.
|
2022-11-26 13:54:45 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2022-12-03 13:36:39 +00:00
|
|
|
pub struct TyCtxtFeed<'tcx, KEY: Copy> {
|
2022-11-26 13:54:45 +00:00
|
|
|
pub tcx: TyCtxt<'tcx>,
|
2022-11-29 18:49:37 +00:00
|
|
|
// Do not allow direct access, as downstream code must not mutate this field.
|
2022-12-03 13:36:39 +00:00
|
|
|
key: KEY,
|
2022-11-29 18:49:37 +00:00
|
|
|
}
|
|
|
|
|
2024-02-19 12:02:10 +00:00
|
|
|
/// Never return a `Feed` from a query. Only queries that create a `DefId` are
|
|
|
|
/// allowed to feed queries for that `DefId`.
|
|
|
|
impl<KEY: Copy, CTX> !HashStable<CTX> for TyCtxtFeed<'_, KEY> {}
|
|
|
|
|
2024-02-14 15:13:09 +00:00
|
|
|
/// The same as `TyCtxtFeed`, but does not contain a `TyCtxt`.
|
|
|
|
/// Use this to pass around when you have a `TyCtxt` elsewhere.
|
|
|
|
/// Just an optimization to save space and not store hundreds of
|
|
|
|
/// `TyCtxtFeed` in the resolver.
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct Feed<'tcx, KEY: Copy> {
|
|
|
|
_tcx: PhantomData<TyCtxt<'tcx>>,
|
|
|
|
// Do not allow direct access, as downstream code must not mutate this field.
|
|
|
|
key: KEY,
|
|
|
|
}
|
|
|
|
|
2024-02-19 12:02:10 +00:00
|
|
|
/// Never return a `Feed` from a query. Only queries that create a `DefId` are
|
|
|
|
/// allowed to feed queries for that `DefId`.
|
|
|
|
impl<KEY: Copy, CTX> !HashStable<CTX> for Feed<'_, KEY> {}
|
|
|
|
|
2024-02-14 15:13:09 +00:00
|
|
|
impl<T: fmt::Debug + Copy> fmt::Debug for Feed<'_, T> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.key.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 16:07:48 +00:00
|
|
|
/// Some workarounds to use cases that cannot use `create_def`.
|
|
|
|
/// Do not add new ways to create `TyCtxtFeed` without consulting
|
|
|
|
/// with T-compiler and making an analysis about why your addition
|
|
|
|
/// does not cause incremental compilation issues.
|
2022-12-03 18:06:39 +00:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2024-02-19 16:58:18 +00:00
|
|
|
/// Can only be fed before queries are run, and is thus exempt from any
|
|
|
|
/// incremental issues. Do not use except for the initial query feeding.
|
2022-12-03 18:06:39 +00:00
|
|
|
pub fn feed_unit_query(self) -> TyCtxtFeed<'tcx, ()> {
|
2024-02-19 17:25:01 +00:00
|
|
|
self.dep_graph.assert_ignored();
|
2022-12-03 18:06:39 +00:00
|
|
|
TyCtxtFeed { tcx: self, key: () }
|
|
|
|
}
|
2024-02-19 16:58:18 +00:00
|
|
|
|
2024-02-19 16:07:48 +00:00
|
|
|
/// Only used in the resolver to register the `CRATE_DEF_ID` `DefId` and feed
|
|
|
|
/// some queries for it. It will panic if used twice.
|
|
|
|
pub fn create_local_crate_def_id(self, span: Span) -> TyCtxtFeed<'tcx, LocalDefId> {
|
|
|
|
let key = self.untracked().source_span.push(span);
|
|
|
|
assert_eq!(key, CRATE_DEF_ID);
|
2023-11-21 23:40:23 +03:00
|
|
|
TyCtxtFeed { tcx: self, key }
|
|
|
|
}
|
2023-02-19 10:02:00 +00:00
|
|
|
|
|
|
|
/// In order to break cycles involving `AnonConst`, we need to set the expected type by side
|
|
|
|
/// effect. However, we do not want this as a general capability, so this interface restricts
|
|
|
|
/// to the only allowed case.
|
2024-05-26 20:03:47 -04:00
|
|
|
pub fn feed_anon_const_type(self, key: LocalDefId, value: ty::EarlyBinder<'tcx, Ty<'tcx>>) {
|
2023-02-19 10:02:00 +00:00
|
|
|
debug_assert_eq!(self.def_kind(key), DefKind::AnonConst);
|
|
|
|
TyCtxtFeed { tcx: self, key }.type_of(value)
|
|
|
|
}
|
2022-12-03 18:06:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-03 13:36:39 +00:00
|
|
|
impl<'tcx, KEY: Copy> TyCtxtFeed<'tcx, KEY> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn key(&self) -> KEY {
|
|
|
|
self.key
|
|
|
|
}
|
2024-02-14 15:13:09 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn downgrade(self) -> Feed<'tcx, KEY> {
|
|
|
|
Feed { _tcx: PhantomData, key: self.key }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, KEY: Copy> Feed<'tcx, KEY> {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn key(&self) -> KEY {
|
|
|
|
self.key
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn upgrade(self, tcx: TyCtxt<'tcx>) -> TyCtxtFeed<'tcx, KEY> {
|
|
|
|
TyCtxtFeed { tcx, key: self.key }
|
|
|
|
}
|
2022-12-03 13:36:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TyCtxtFeed<'tcx, LocalDefId> {
|
2022-11-29 18:49:37 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn def_id(&self) -> LocalDefId {
|
2022-12-03 13:36:39 +00:00
|
|
|
self.key
|
2022-11-29 18:49:37 +00:00
|
|
|
}
|
2024-02-11 19:08:30 +03:00
|
|
|
|
|
|
|
// Caller must ensure that `self.key` ID is indeed an owner.
|
|
|
|
pub fn feed_owner_id(&self) -> TyCtxtFeed<'tcx, hir::OwnerId> {
|
|
|
|
TyCtxtFeed { tcx: self.tcx, key: hir::OwnerId { def_id: self.key } }
|
|
|
|
}
|
2024-03-19 09:00:57 +00:00
|
|
|
|
|
|
|
// Fills in all the important parts needed by HIR queries
|
|
|
|
pub fn feed_hir(&self) {
|
|
|
|
self.local_def_id_to_hir_id(HirId::make_owner(self.def_id()));
|
|
|
|
|
|
|
|
let node = hir::OwnerNode::Synthetic;
|
|
|
|
let bodies = Default::default();
|
|
|
|
let attrs = hir::AttributeMap::EMPTY;
|
|
|
|
|
|
|
|
let (opt_hash_including_bodies, _) = self.tcx.hash_owner_nodes(node, &bodies, &attrs.map);
|
|
|
|
let node = node.into();
|
|
|
|
self.opt_hir_owner_nodes(Some(self.tcx.arena.alloc(hir::OwnerNodes {
|
|
|
|
opt_hash_including_bodies,
|
|
|
|
nodes: IndexVec::from_elem_n(
|
|
|
|
hir::ParentedNode { parent: hir::ItemLocalId::INVALID, node },
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
bodies,
|
|
|
|
})));
|
|
|
|
self.feed_owner_id().hir_attrs(attrs);
|
|
|
|
}
|
2022-11-26 13:54:45 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 16:19:44 -04:00
|
|
|
/// The central data structure of the compiler. It stores references
|
|
|
|
/// to the various **arenas** and also houses the results of the
|
2017-12-31 17:08:04 +01:00
|
|
|
/// various **compiler queries** that have been performed. See the
|
2020-03-05 18:07:42 -03:00
|
|
|
/// [rustc dev guide] for more details.
|
2017-12-31 17:08:04 +01:00
|
|
|
///
|
2020-03-09 18:33:04 -03:00
|
|
|
/// [rustc dev guide]: https://rustc-dev-guide.rust-lang.org/ty.html
|
2023-02-10 10:26:24 +11:00
|
|
|
///
|
|
|
|
/// An implementation detail: `TyCtxt` is a wrapper type for [GlobalCtxt],
|
|
|
|
/// which is the struct that actually holds all the data. `TyCtxt` derefs to
|
|
|
|
/// `GlobalCtxt`, and in practice `TyCtxt` is passed around everywhere, and all
|
|
|
|
/// operations are done via `TyCtxt`. A `TyCtxt` is obtained for a `GlobalCtxt`
|
|
|
|
/// by calling `enter` with a closure `f`. That function creates both the
|
|
|
|
/// `TyCtxt`, and an `ImplicitCtxt` around it that is put into TLS. Within `f`:
|
|
|
|
/// - The `ImplicitCtxt` is available implicitly via TLS.
|
|
|
|
/// - The `TyCtxt` is available explicitly via the `tcx` parameter, and also
|
|
|
|
/// implicitly within the `ImplicitCtxt`. Explicit access is preferred when
|
|
|
|
/// possible.
|
2016-05-03 04:56:42 +03:00
|
|
|
#[derive(Copy, Clone)]
|
2019-09-25 08:42:46 -04:00
|
|
|
#[rustc_diagnostic_item = "TyCtxt"]
|
2022-02-23 08:06:22 -05:00
|
|
|
#[rustc_pass_by_value]
|
2019-06-14 00:48:52 +03:00
|
|
|
pub struct TyCtxt<'tcx> {
|
|
|
|
gcx: &'tcx GlobalCtxt<'tcx>,
|
2016-05-03 04:56:42 +03:00
|
|
|
}
|
|
|
|
|
2023-11-07 20:06:39 +01:00
|
|
|
// Explicitly implement `DynSync` and `DynSend` for `TyCtxt` to short circuit trait resolution.
|
|
|
|
unsafe impl DynSend for TyCtxt<'_> {}
|
|
|
|
unsafe impl DynSync for TyCtxt<'_> {}
|
|
|
|
fn _assert_tcx_fields() {
|
|
|
|
sync::assert_dyn_sync::<&'_ GlobalCtxt<'_>>();
|
|
|
|
sync::assert_dyn_send::<&'_ GlobalCtxt<'_>>();
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> Deref for TyCtxt<'tcx> {
|
|
|
|
type Target = &'tcx GlobalCtxt<'tcx>;
|
2018-11-29 21:13:04 +01:00
|
|
|
#[inline(always)]
|
2016-05-03 05:23:22 +03:00
|
|
|
fn deref(&self) -> &Self::Target {
|
2016-05-03 04:56:42 +03:00
|
|
|
&self.gcx
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-10 10:26:24 +11:00
|
|
|
/// See [TyCtxt] for details about this type.
|
2016-05-03 04:56:42 +03:00
|
|
|
pub struct GlobalCtxt<'tcx> {
|
2019-11-26 23:16:48 +01:00
|
|
|
pub arena: &'tcx WorkerLocal<Arena<'tcx>>,
|
2021-07-13 18:45:20 +02:00
|
|
|
pub hir_arena: &'tcx WorkerLocal<hir::Arena<'tcx>>,
|
2019-05-31 09:25:34 +02:00
|
|
|
|
2019-05-31 10:23:22 +02:00
|
|
|
interners: CtxtInterners<'tcx>,
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2016-09-29 02:30:53 +03:00
|
|
|
pub sess: &'tcx Session,
|
2023-08-08 18:28:20 +08:00
|
|
|
crate_types: Vec<CrateType>,
|
2023-08-08 20:08:24 +08:00
|
|
|
/// 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,
|
2016-09-29 02:30:53 +03:00
|
|
|
|
2016-01-05 13:07:45 -05:00
|
|
|
pub dep_graph: DepGraph,
|
|
|
|
|
2019-09-27 14:03:09 +02:00
|
|
|
pub prof: SelfProfilerRef,
|
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Common types, pre-interned for your convenience.
|
|
|
|
pub types: CommonTypes<'tcx>,
|
|
|
|
|
2019-04-25 22:04:51 +01:00
|
|
|
/// Common lifetimes, pre-interned for your convenience.
|
|
|
|
pub lifetimes: CommonLifetimes<'tcx>,
|
|
|
|
|
2019-05-01 23:09:53 +01:00
|
|
|
/// Common consts, pre-interned for your convenience.
|
|
|
|
pub consts: CommonConsts<'tcx>,
|
|
|
|
|
2023-09-22 09:14:39 +00:00
|
|
|
/// Hooks to be able to register functions in other crates that can then still
|
|
|
|
/// be called from rustc_middle.
|
|
|
|
pub(crate) hooks: crate::hooks::Providers,
|
|
|
|
|
2022-12-07 14:31:50 +00:00
|
|
|
untracked: Untracked,
|
2022-12-05 16:19:14 +00:00
|
|
|
|
2023-05-16 01:53:21 +02:00
|
|
|
pub query_system: QuerySystem<'tcx>,
|
2022-09-11 23:18:08 -05:00
|
|
|
pub(crate) query_kinds: &'tcx [DepKindStruct<'tcx>],
|
2016-10-28 13:55:49 +03:00
|
|
|
|
2020-06-10 09:26:54 +01:00
|
|
|
// Internal caches for metadata decoding. No need to track deps on this.
|
|
|
|
pub ty_rcache: Lock<FxHashMap<ty::CReaderCacheKey, Ty<'tcx>>>,
|
|
|
|
pub pred_rcache: Lock<FxHashMap<ty::CReaderCacheKey, Predicate<'tcx>>>,
|
2015-12-22 16:39:33 -05:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
/// Caches the results of trait selection. This cache is used
|
|
|
|
/// for things that do not have to do with the parameters in scope.
|
|
|
|
pub selection_cache: traits::SelectionCache<'tcx>,
|
|
|
|
|
2015-10-21 14:50:38 +03:00
|
|
|
/// Caches the results of trait evaluation. This cache is used
|
|
|
|
/// for things that do not have to do with the parameters in scope.
|
|
|
|
/// Merge this with `selection_cache`?
|
|
|
|
pub evaluation_cache: traits::EvaluationCache<'tcx>,
|
|
|
|
|
2023-02-24 02:35:15 +00:00
|
|
|
/// Caches the results of goal evaluation in the new solver.
|
2024-07-11 23:13:12 +02:00
|
|
|
pub new_solver_evaluation_cache: Lock<search_graph::GlobalCache<TyCtxt<'tcx>>>,
|
2023-02-24 02:35:15 +00:00
|
|
|
|
2023-12-04 20:42:41 +00:00
|
|
|
pub canonical_param_env_cache: CanonicalParamEnvCache<'tcx>,
|
|
|
|
|
2016-04-18 16:03:16 +03:00
|
|
|
/// Data layout specification for the current target.
|
|
|
|
pub data_layout: TargetDataLayout,
|
2016-04-19 09:11:46 +03:00
|
|
|
|
2020-03-21 19:19:10 +01:00
|
|
|
/// Stores memory for globals (statics/consts).
|
2020-04-24 12:53:18 +02:00
|
|
|
pub(crate) alloc_map: Lock<interpret::AllocMap<'tcx>>,
|
2023-08-25 17:26:24 +02:00
|
|
|
|
|
|
|
current_gcx: CurrentGcx,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2023-02-07 16:59:50 +11:00
|
|
|
impl<'tcx> GlobalCtxt<'tcx> {
|
|
|
|
/// Installs `self` in a `TyCtxt` and `ImplicitCtxt` for the duration of
|
|
|
|
/// `f`.
|
2023-11-27 22:40:54 +00:00
|
|
|
pub fn enter<F, R>(&'tcx self, f: F) -> R
|
2023-02-07 16:59:50 +11:00
|
|
|
where
|
|
|
|
F: FnOnce(TyCtxt<'tcx>) -> R,
|
|
|
|
{
|
|
|
|
let icx = tls::ImplicitCtxt::new(self);
|
2023-08-25 17:26:24 +02:00
|
|
|
|
|
|
|
// Reset `current_gcx` to `None` when we exit.
|
|
|
|
let _on_drop = defer(move || {
|
|
|
|
*self.current_gcx.value.write() = None;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set this `GlobalCtxt` as the current one.
|
|
|
|
{
|
|
|
|
let mut guard = self.current_gcx.value.write();
|
|
|
|
assert!(guard.is_none(), "no `GlobalCtxt` is currently set");
|
|
|
|
*guard = Some(self as *const _ as *const ());
|
|
|
|
}
|
|
|
|
|
2023-02-07 16:59:50 +11:00
|
|
|
tls::enter_context(&icx, || f(icx.tcx))
|
|
|
|
}
|
2023-10-27 21:26:43 -04:00
|
|
|
|
|
|
|
pub fn finish(&self) -> FileEncodeResult {
|
2024-03-06 04:17:17 +01:00
|
|
|
self.dep_graph.finish_encoding()
|
2023-10-27 21:26:43 -04:00
|
|
|
}
|
2023-02-07 16:59:50 +11:00
|
|
|
}
|
|
|
|
|
2023-08-25 17:26:24 +02:00
|
|
|
/// This is used to get a reference to a `GlobalCtxt` if one is available.
|
|
|
|
///
|
|
|
|
/// This is needed to allow the deadlock handler access to `GlobalCtxt` to look for query cycles.
|
|
|
|
/// It cannot use the `TLV` global because that's only guaranteed to be defined on the thread
|
|
|
|
/// creating the `GlobalCtxt`. Other threads have access to the `TLV` only inside Rayon jobs, but
|
|
|
|
/// the deadlock handler is not called inside such a job.
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct CurrentGcx {
|
|
|
|
/// This stores a pointer to a `GlobalCtxt`. This is set to `Some` inside `GlobalCtxt::enter`
|
|
|
|
/// and reset to `None` when that function returns or unwinds.
|
|
|
|
value: Lrc<RwLock<Option<*const ()>>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl DynSend for CurrentGcx {}
|
|
|
|
unsafe impl DynSync for CurrentGcx {}
|
|
|
|
|
|
|
|
impl CurrentGcx {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self { value: Lrc::new(RwLock::new(None)) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn access<R>(&self, f: impl for<'tcx> FnOnce(&'tcx GlobalCtxt<'tcx>) -> R) -> R {
|
|
|
|
let read_guard = self.value.read();
|
|
|
|
let gcx: *const GlobalCtxt<'_> = read_guard.unwrap() as *const _;
|
|
|
|
// SAFETY: We hold the read lock for the `GlobalCtxt` pointer. That prevents
|
|
|
|
// `GlobalCtxt::enter` from returning as it would first acquire the write lock.
|
|
|
|
// This ensures the `GlobalCtxt` is live during `f`.
|
|
|
|
f(unsafe { &*gcx })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2024-04-15 18:56:44 -04:00
|
|
|
pub fn has_typeck_results(self, def_id: LocalDefId) -> bool {
|
|
|
|
// Closures' typeck results come from their outermost function,
|
|
|
|
// as they are part of the same "inference environment".
|
|
|
|
let typeck_root_def_id = self.typeck_root_def_id(def_id.to_def_id());
|
|
|
|
if typeck_root_def_id != def_id.to_def_id() {
|
|
|
|
return self.has_typeck_results(typeck_root_def_id.expect_local());
|
|
|
|
}
|
|
|
|
|
|
|
|
self.hir_node_by_def_id(def_id).body_id().is_some()
|
|
|
|
}
|
|
|
|
|
2022-05-09 17:47:02 +02:00
|
|
|
/// Expects a body and returns its codegen attributes.
|
|
|
|
///
|
|
|
|
/// Unlike `codegen_fn_attrs`, this returns `CodegenFnAttrs::EMPTY` for
|
|
|
|
/// constants.
|
|
|
|
pub fn body_codegen_attrs(self, def_id: DefId) -> &'tcx CodegenFnAttrs {
|
|
|
|
let def_kind = self.def_kind(def_id);
|
|
|
|
if def_kind.has_codegen_attrs() {
|
|
|
|
self.codegen_fn_attrs(def_id)
|
|
|
|
} else if matches!(
|
|
|
|
def_kind,
|
|
|
|
DefKind::AnonConst | DefKind::AssocConst | DefKind::Const | DefKind::InlineConst
|
|
|
|
) {
|
|
|
|
CodegenFnAttrs::EMPTY
|
|
|
|
} else {
|
|
|
|
bug!(
|
|
|
|
"body_codegen_fn_attrs called on unexpected definition: {:?} {:?}",
|
|
|
|
def_id,
|
|
|
|
def_kind
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-04 18:42:17 +02:00
|
|
|
pub fn alloc_steal_thir(self, thir: Thir<'tcx>) -> &'tcx Steal<Thir<'tcx>> {
|
|
|
|
self.arena.alloc(Steal::new(thir))
|
|
|
|
}
|
|
|
|
|
2020-07-03 22:15:27 +02:00
|
|
|
pub fn alloc_steal_mir(self, mir: Body<'tcx>) -> &'tcx Steal<Body<'tcx>> {
|
|
|
|
self.arena.alloc(Steal::new(mir))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2019-12-24 17:38:22 -05:00
|
|
|
pub fn alloc_steal_promoted(
|
|
|
|
self,
|
2020-04-12 10:31:00 -07:00
|
|
|
promoted: IndexVec<Promoted, Body<'tcx>>,
|
2020-07-03 22:15:27 +02:00
|
|
|
) -> &'tcx Steal<IndexVec<Promoted, Body<'tcx>>> {
|
|
|
|
self.arena.alloc(Steal::new(promoted))
|
2019-08-04 16:20:21 -04:00
|
|
|
}
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
pub fn mk_adt_def(
|
2019-06-14 01:32:15 +03:00
|
|
|
self,
|
|
|
|
did: DefId,
|
|
|
|
kind: AdtKind,
|
|
|
|
variants: IndexVec<VariantIdx, ty::VariantDef>,
|
|
|
|
repr: ReprOptions,
|
2022-03-05 07:28:41 +11:00
|
|
|
) -> ty::AdtDef<'tcx> {
|
2024-09-29 22:10:16 -04:00
|
|
|
self.mk_adt_def_from_data(ty::AdtDefData::new(self, did, kind, variants, repr))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Allocates a read-only byte or string literal for `mir::interpret`.
|
2024-07-12 13:08:05 +08:00
|
|
|
/// Returns the same `AllocId` if called again with the same bytes.
|
2024-08-06 19:02:01 +02:00
|
|
|
pub fn allocate_bytes_dedup(self, bytes: &[u8], salt: usize) -> interpret::AllocId {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Create an allocation that just contains these bytes.
|
2021-05-23 12:03:39 +02:00
|
|
|
let alloc = interpret::Allocation::from_bytes_byte_aligned_immutable(bytes);
|
2023-02-17 14:33:08 +11:00
|
|
|
let alloc = self.mk_const_alloc(alloc);
|
2024-08-06 19:02:01 +02:00
|
|
|
self.reserve_and_set_memory_dedup(alloc, salt)
|
2017-12-06 09:25:29 +01:00
|
|
|
}
|
|
|
|
|
2018-09-10 13:40:34 +02:00
|
|
|
/// Returns a range of the start/end indices specified with the
|
|
|
|
/// `rustc_layout_scalar_valid_range` attribute.
|
2021-08-26 19:47:07 +03:00
|
|
|
// FIXME(eddyb) this is an awkward spot for this method, maybe move it?
|
2018-09-10 13:40:34 +02:00
|
|
|
pub fn layout_scalar_valid_range(self, def_id: DefId) -> (Bound<u128>, Bound<u128>) {
|
|
|
|
let get = |name| {
|
2022-05-02 09:31:56 +02:00
|
|
|
let Some(attr) = self.get_attr(def_id, name) else {
|
2022-02-19 00:48:49 +01:00
|
|
|
return Bound::Unbounded;
|
2018-09-10 13:40:34 +02:00
|
|
|
};
|
2020-06-20 20:59:04 -04:00
|
|
|
debug!("layout_scalar_valid_range: attr={:?}", attr);
|
2021-03-16 00:00:00 +00:00
|
|
|
if let Some(
|
2022-06-14 17:34:37 +02:00
|
|
|
&[
|
2024-10-04 21:59:04 +09:00
|
|
|
ast::MetaItemInner::Lit(ast::MetaItemLit {
|
|
|
|
kind: ast::LitKind::Int(a, _), ..
|
2022-06-14 17:34:37 +02:00
|
|
|
}),
|
|
|
|
],
|
2021-03-16 00:00:00 +00:00
|
|
|
) = attr.meta_item_list().as_deref()
|
|
|
|
{
|
2024-01-17 15:40:30 -08:00
|
|
|
Bound::Included(a.get())
|
2021-03-16 00:00:00 +00:00
|
|
|
} else {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.dcx().span_delayed_bug(
|
2023-11-30 15:01:11 +11:00
|
|
|
attr.span,
|
|
|
|
"invalid rustc_layout_scalar_valid_range attribute",
|
|
|
|
);
|
2021-03-16 00:00:00 +00:00
|
|
|
Bound::Unbounded
|
2018-09-07 13:41:59 +02:00
|
|
|
}
|
|
|
|
};
|
2019-12-24 17:38:22 -05:00
|
|
|
(
|
|
|
|
get(sym::rustc_layout_scalar_valid_range_start),
|
|
|
|
get(sym::rustc_layout_scalar_valid_range_end),
|
|
|
|
)
|
2018-09-07 13:41:59 +02:00
|
|
|
}
|
|
|
|
|
2024-05-10 14:27:48 -04:00
|
|
|
pub fn lift<T: Lift<TyCtxt<'tcx>>>(self, value: T) -> Option<T::Lifted> {
|
2024-07-16 00:03:37 -04:00
|
|
|
value.lift_to_interner(self)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2023-07-20 13:21:04 +02:00
|
|
|
/// 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
|
2024-09-02 07:50:22 +02:00
|
|
|
/// context and any interned value (types, args, etc.) can only be used while `ty::tls`
|
2023-07-20 13:21:04 +02:00
|
|
|
/// has a valid reference to the context, to allow formatting values that need it.
|
2018-12-08 20:30:23 +01:00
|
|
|
pub fn create_global_ctxt(
|
|
|
|
s: &'tcx Session,
|
2023-08-08 18:28:20 +08:00
|
|
|
crate_types: Vec<CrateType>,
|
2023-08-08 20:08:24 +08:00
|
|
|
stable_crate_id: StableCrateId,
|
2019-11-27 13:24:19 +01:00
|
|
|
arena: &'tcx WorkerLocal<Arena<'tcx>>,
|
2021-07-13 18:45:20 +02:00
|
|
|
hir_arena: &'tcx WorkerLocal<hir::Arena<'tcx>>,
|
2022-12-07 14:31:50 +00:00
|
|
|
untracked: Untracked,
|
2020-02-08 05:18:34 +01:00
|
|
|
dep_graph: DepGraph,
|
2022-08-29 10:20:14 -05:00
|
|
|
query_kinds: &'tcx [DepKindStruct<'tcx>],
|
2023-02-07 08:32:30 +01:00
|
|
|
query_system: QuerySystem<'tcx>,
|
2023-09-22 09:14:39 +00:00
|
|
|
hooks: crate::hooks::Providers,
|
2023-08-25 17:26:24 +02:00
|
|
|
current_gcx: CurrentGcx,
|
2018-12-08 20:30:23 +01:00
|
|
|
) -> GlobalCtxt<'tcx> {
|
2022-11-07 00:36:11 +03:30
|
|
|
let data_layout = s.target.parse_data_layout().unwrap_or_else(|err| {
|
2023-12-18 22:21:37 +11:00
|
|
|
s.dcx().emit_fatal(err);
|
2017-12-18 16:18:36 +02:00
|
|
|
});
|
2020-01-02 01:26:18 +01:00
|
|
|
let interners = CtxtInterners::new(arena);
|
2022-12-08 08:52:07 +00:00
|
|
|
let common_types = CommonTypes::new(&interners, s, &untracked);
|
2019-04-25 22:04:51 +01:00
|
|
|
let common_lifetimes = CommonLifetimes::new(&interners);
|
2023-11-22 23:28:28 +00:00
|
|
|
let common_consts = CommonConsts::new(&interners, &common_types, s, &untracked);
|
2021-06-01 09:05:22 +02:00
|
|
|
|
2018-12-08 20:30:23 +01:00
|
|
|
GlobalCtxt {
|
2016-09-29 02:30:53 +03:00
|
|
|
sess: s,
|
2023-08-08 18:28:20 +08:00
|
|
|
crate_types,
|
2023-08-08 20:08:24 +08:00
|
|
|
stable_crate_id,
|
2019-11-27 13:24:19 +01:00
|
|
|
arena,
|
2021-07-13 18:45:20 +02:00
|
|
|
hir_arena,
|
2019-05-31 10:23:22 +02:00
|
|
|
interners,
|
2018-10-26 03:11:11 +09:00
|
|
|
dep_graph,
|
2023-09-22 09:14:39 +00:00
|
|
|
hooks,
|
2019-09-27 14:03:09 +02:00
|
|
|
prof: s.prof.clone(),
|
2015-09-06 21:51:58 +03:00
|
|
|
types: common_types,
|
2019-04-25 22:04:51 +01:00
|
|
|
lifetimes: common_lifetimes,
|
2019-05-01 23:09:53 +01:00
|
|
|
consts: common_consts,
|
2022-12-07 14:31:50 +00:00
|
|
|
untracked,
|
2023-02-07 08:32:30 +01:00
|
|
|
query_system,
|
2021-10-16 20:10:23 +02:00
|
|
|
query_kinds,
|
2020-06-10 09:26:54 +01:00
|
|
|
ty_rcache: Default::default(),
|
|
|
|
pred_rcache: Default::default(),
|
2018-10-16 16:57:53 +02:00
|
|
|
selection_cache: Default::default(),
|
|
|
|
evaluation_cache: Default::default(),
|
2023-02-24 02:35:15 +00:00
|
|
|
new_solver_evaluation_cache: Default::default(),
|
2023-12-04 20:42:41 +00:00
|
|
|
canonical_param_env_cache: Default::default(),
|
2017-07-03 11:19:51 -07:00
|
|
|
data_layout,
|
2018-05-02 06:03:06 +02:00
|
|
|
alloc_map: Lock::new(interpret::AllocMap::new()),
|
2023-08-25 17:26:24 +02:00
|
|
|
current_gcx,
|
2018-12-08 20:30:23 +01:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2017-03-08 16:28:47 -05:00
|
|
|
|
2020-09-19 12:38:24 +02:00
|
|
|
pub fn consider_optimizing<T: Fn() -> String>(self, msg: T) -> bool {
|
2023-02-23 18:51:31 +00:00
|
|
|
self.sess.consider_optimizing(|| self.crate_name(LOCAL_CRATE), msg)
|
2017-03-08 16:28:47 -05:00
|
|
|
}
|
2017-08-31 08:57:41 -07:00
|
|
|
|
2019-05-19 20:16:04 +02:00
|
|
|
/// Obtain all lang items of this crate and all dependencies (recursively)
|
2020-03-31 21:38:14 +02:00
|
|
|
pub fn lang_items(self) -> &'tcx rustc_hir::lang_items::LanguageItems {
|
2021-05-11 13:50:41 +02:00
|
|
|
self.get_lang_items(())
|
2017-08-31 08:57:41 -07:00
|
|
|
}
|
2017-08-31 15:08:34 -07:00
|
|
|
|
2023-03-05 20:19:41 -08:00
|
|
|
/// Gets a `Ty` representing the [`LangItem::OrderingEnum`]
|
|
|
|
#[track_caller]
|
|
|
|
pub fn ty_ordering_enum(self, span: Option<Span>) -> Ty<'tcx> {
|
|
|
|
let ordering_enum = self.require_lang_item(hir::LangItem::OrderingEnum, span);
|
|
|
|
self.type_of(ordering_enum).no_bound_vars().unwrap()
|
|
|
|
}
|
|
|
|
|
2019-05-19 20:16:04 +02:00
|
|
|
/// Obtain the given diagnostic item's `DefId`. Use `is_diagnostic_item` if you just want to
|
|
|
|
/// compare against another `DefId`, since `is_diagnostic_item` is cheaper.
|
|
|
|
pub fn get_diagnostic_item(self, name: Symbol) -> Option<DefId> {
|
2021-10-04 15:57:39 -05:00
|
|
|
self.all_diagnostic_items(()).name_to_id.get(&name).copied()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Obtain the diagnostic item's name
|
|
|
|
pub fn get_diagnostic_name(self, id: DefId) -> Option<Symbol> {
|
|
|
|
self.diagnostic_items(id.krate).id_to_name.get(&id).copied()
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Check whether the diagnostic item with the given `name` has the given `DefId`.
|
|
|
|
pub fn is_diagnostic_item(self, name: Symbol, did: DefId) -> bool {
|
2021-10-04 15:57:39 -05:00
|
|
|
self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did)
|
2019-05-19 20:16:04 +02:00
|
|
|
}
|
|
|
|
|
2023-11-26 21:05:08 +08:00
|
|
|
pub fn is_coroutine(self, def_id: DefId) -> bool {
|
|
|
|
self.coroutine_kind(def_id).is_some()
|
|
|
|
}
|
|
|
|
|
2023-12-26 22:43:11 +00:00
|
|
|
/// Returns the movability of the coroutine of `def_id`, or panics
|
|
|
|
/// if given a `def_id` that is not a coroutine.
|
|
|
|
pub fn coroutine_movability(self, def_id: DefId) -> hir::Movability {
|
|
|
|
self.coroutine_kind(def_id).expect("expected a coroutine").movability()
|
|
|
|
}
|
|
|
|
|
2023-10-19 21:46:28 +00:00
|
|
|
/// Returns `true` if the node pointed to by `def_id` is a coroutine for an async construct.
|
|
|
|
pub fn coroutine_is_async(self, def_id: DefId) -> bool {
|
2023-12-21 18:49:20 +00:00
|
|
|
matches!(
|
|
|
|
self.coroutine_kind(def_id),
|
|
|
|
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _))
|
|
|
|
)
|
2022-11-26 21:33:12 +01:00
|
|
|
}
|
|
|
|
|
2024-08-01 13:05:17 -04:00
|
|
|
// Whether the body owner is synthetic, which in this case means it does not correspond to
|
|
|
|
// meaningful HIR. This is currently used to skip over MIR borrowck.
|
|
|
|
pub fn is_synthetic_mir(self, def_id: impl Into<DefId>) -> bool {
|
|
|
|
matches!(self.def_kind(def_id.into()), DefKind::SyntheticCoroutineBody)
|
|
|
|
}
|
|
|
|
|
2023-10-20 22:48:37 +00:00
|
|
|
/// Returns `true` if the node pointed to by `def_id` is a general coroutine that implements `Coroutine`.
|
|
|
|
/// This means it is neither an `async` or `gen` construct.
|
|
|
|
pub fn is_general_coroutine(self, def_id: DefId) -> bool {
|
2023-12-25 16:56:12 +00:00
|
|
|
matches!(self.coroutine_kind(def_id), Some(hir::CoroutineKind::Coroutine(_)))
|
2023-10-20 22:48:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 18:18:19 +00:00
|
|
|
/// Returns `true` if the node pointed to by `def_id` is a coroutine for a `gen` construct.
|
2023-10-20 22:48:37 +00:00
|
|
|
pub fn coroutine_is_gen(self, def_id: DefId) -> bool {
|
2023-12-21 18:49:20 +00:00
|
|
|
matches!(
|
|
|
|
self.coroutine_kind(def_id),
|
|
|
|
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _))
|
|
|
|
)
|
2023-10-20 22:48:37 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 18:18:19 +00:00
|
|
|
/// Returns `true` if the node pointed to by `def_id` is a coroutine for a `async gen` construct.
|
|
|
|
pub fn coroutine_is_async_gen(self, def_id: DefId) -> bool {
|
2023-12-21 18:49:20 +00:00
|
|
|
matches!(
|
|
|
|
self.coroutine_kind(def_id),
|
|
|
|
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _))
|
|
|
|
)
|
2023-11-28 18:18:19 +00:00
|
|
|
}
|
|
|
|
|
2022-02-19 15:36:11 +01:00
|
|
|
pub fn stability(self) -> &'tcx stability::Index {
|
2021-05-11 13:50:41 +02:00
|
|
|
self.stability_index(())
|
2017-08-31 15:08:34 -07:00
|
|
|
}
|
2017-09-07 08:13:41 -07:00
|
|
|
|
2019-11-30 00:23:38 +01:00
|
|
|
pub fn features(self) -> &'tcx rustc_feature::Features {
|
2021-05-11 13:50:41 +02:00
|
|
|
self.features_query(())
|
2018-02-14 16:11:02 +01:00
|
|
|
}
|
|
|
|
|
2023-02-16 09:25:11 +00:00
|
|
|
pub fn def_key(self, id: impl IntoQueryParam<DefId>) -> rustc_hir::definitions::DefKey {
|
|
|
|
let id = id.into_query_param();
|
2021-05-20 20:17:45 +02:00
|
|
|
// Accessing the DefKey is ok, since it is part of DefPathHash.
|
2021-04-04 14:40:35 +02:00
|
|
|
if let Some(id) = id.as_local() {
|
2021-07-12 22:19:25 +02:00
|
|
|
self.definitions_untracked().def_key(id)
|
2021-04-04 14:40:35 +02:00
|
|
|
} else {
|
2022-12-08 10:53:20 +00:00
|
|
|
self.cstore_untracked().def_key(id)
|
2021-04-04 14:40:35 +02:00
|
|
|
}
|
2017-09-07 13:21:46 -07:00
|
|
|
}
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Converts a `DefId` into its fully expanded `DefPath` (every
|
2019-09-06 03:57:44 +01:00
|
|
|
/// `DefId` is really just an interned `DefPath`).
|
2017-09-07 13:21:46 -07:00
|
|
|
///
|
|
|
|
/// Note that if `id` is not local to this crate, the result will
|
|
|
|
/// be a non-local `DefPath`.
|
2020-03-24 09:09:42 +01:00
|
|
|
pub fn def_path(self, id: DefId) -> rustc_hir::definitions::DefPath {
|
2021-05-20 20:17:45 +02:00
|
|
|
// Accessing the DefPath is ok, since it is part of DefPathHash.
|
2019-11-03 20:48:08 +02:00
|
|
|
if let Some(id) = id.as_local() {
|
2021-07-12 22:19:25 +02:00
|
|
|
self.definitions_untracked().def_path(id)
|
2019-11-03 20:48:08 +02:00
|
|
|
} else {
|
2022-12-08 10:53:20 +00:00
|
|
|
self.cstore_untracked().def_path(id)
|
2019-11-03 20:48:08 +02:00
|
|
|
}
|
2017-09-07 13:21:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-24 09:09:42 +01:00
|
|
|
pub fn def_path_hash(self, def_id: DefId) -> rustc_hir::definitions::DefPathHash {
|
2021-05-20 20:17:45 +02:00
|
|
|
// Accessing the DefPathHash is ok, it is incr. comp. stable.
|
2019-11-03 20:48:08 +02:00
|
|
|
if let Some(def_id) = def_id.as_local() {
|
2021-07-12 22:19:25 +02:00
|
|
|
self.definitions_untracked().def_path_hash(def_id)
|
2017-09-07 13:21:46 -07:00
|
|
|
} else {
|
2022-12-08 10:53:20 +00:00
|
|
|
self.cstore_untracked().def_path_hash(def_id)
|
2017-09-07 13:21:46 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-08 18:28:20 +08:00
|
|
|
#[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()
|
|
|
|
}
|
|
|
|
|
2021-05-30 22:32:45 +02:00
|
|
|
#[inline]
|
2021-06-30 18:39:28 +02:00
|
|
|
pub fn stable_crate_id(self, crate_num: CrateNum) -> StableCrateId {
|
|
|
|
if crate_num == LOCAL_CRATE {
|
2023-08-08 20:08:24 +08:00
|
|
|
self.stable_crate_id
|
2021-06-30 18:39:28 +02:00
|
|
|
} else {
|
2022-12-08 10:53:20 +00:00
|
|
|
self.cstore_untracked().stable_crate_id(crate_num)
|
2021-06-30 18:39:28 +02:00
|
|
|
}
|
2021-05-30 22:32:45 +02:00
|
|
|
}
|
|
|
|
|
2021-07-20 13:59:12 +02:00
|
|
|
/// Maps a StableCrateId to the corresponding CrateNum. This method assumes
|
|
|
|
/// 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 {
|
2023-08-08 20:08:24 +08:00
|
|
|
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
|
2021-07-20 13:59:12 +02:00
|
|
|
LOCAL_CRATE
|
|
|
|
} else {
|
2024-03-26 16:52:06 +00:00
|
|
|
*self
|
|
|
|
.untracked()
|
|
|
|
.stable_crate_ids
|
|
|
|
.read()
|
|
|
|
.get(&stable_crate_id)
|
|
|
|
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"))
|
2021-07-20 13:59:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 17:13:02 +02:00
|
|
|
/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
|
|
|
|
/// session, if it still exists. This is used during incremental compilation to
|
|
|
|
/// turn a deserialized `DefPathHash` into its current `DefId`.
|
2024-06-13 08:46:30 -04:00
|
|
|
pub fn def_path_hash_to_def_id(self, hash: DefPathHash) -> Option<DefId> {
|
2021-10-16 17:13:02 +02:00
|
|
|
debug!("def_path_hash_to_def_id({:?})", hash);
|
|
|
|
|
|
|
|
let stable_crate_id = hash.stable_crate_id();
|
|
|
|
|
|
|
|
// If this is a DefPathHash from the local crate, we can look up the
|
|
|
|
// DefId in the tcx's `Definitions`.
|
2023-08-08 20:08:24 +08:00
|
|
|
if stable_crate_id == self.stable_crate_id(LOCAL_CRATE) {
|
2024-06-13 08:46:30 -04:00
|
|
|
Some(self.untracked.definitions.read().local_def_path_hash_to_def_id(hash)?.to_def_id())
|
2021-10-16 17:13:02 +02:00
|
|
|
} else {
|
2024-06-13 08:46:30 -04:00
|
|
|
Some(self.def_path_hash_to_def_id_extern(hash, stable_crate_id))
|
2021-10-16 17:13:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 14:57:14 +02:00
|
|
|
pub fn def_path_debug_str(self, def_id: DefId) -> String {
|
|
|
|
// We are explicitly not going through queries here in order to get
|
2021-06-08 18:36:30 +02:00
|
|
|
// crate name and stable crate id since this code is called from debug!()
|
2017-10-04 14:57:14 +02:00
|
|
|
// statements within the query system and we'd run into endless
|
|
|
|
// recursion otherwise.
|
2021-06-08 18:36:30 +02:00
|
|
|
let (crate_name, stable_crate_id) = if def_id.is_local() {
|
2023-08-08 20:08:24 +08:00
|
|
|
(self.crate_name(LOCAL_CRATE), self.stable_crate_id(LOCAL_CRATE))
|
2017-10-04 14:57:14 +02:00
|
|
|
} else {
|
2022-12-08 10:53:20 +00:00
|
|
|
let cstore = &*self.cstore_untracked();
|
2021-07-12 21:20:16 +02:00
|
|
|
(cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))
|
2017-10-04 14:57:14 +02:00
|
|
|
};
|
|
|
|
|
2019-12-24 17:38:22 -05:00
|
|
|
format!(
|
2022-07-16 21:30:17 +03:00
|
|
|
"{}[{:04x}]{}",
|
2019-12-24 17:38:22 -05:00
|
|
|
crate_name,
|
2021-06-08 18:36:30 +02:00
|
|
|
// Don't print the whole stable crate id. That's just
|
2019-12-24 17:38:22 -05:00
|
|
|
// annoying in debug output.
|
2023-04-07 23:11:20 -04:00
|
|
|
stable_crate_id.as_u64() >> (8 * 6),
|
2020-09-23 23:38:38 +01:00
|
|
|
self.def_path(def_id).to_string_no_crate_verbose()
|
2019-12-24 17:38:22 -05:00
|
|
|
)
|
2017-10-04 14:57:14 +02:00
|
|
|
}
|
2023-12-18 22:21:37 +11:00
|
|
|
|
2024-06-18 10:35:56 +00:00
|
|
|
pub fn dcx(self) -> DiagCtxtHandle<'tcx> {
|
2023-12-18 22:21:37 +11:00
|
|
|
self.sess.dcx()
|
|
|
|
}
|
2022-12-01 10:33:28 +00:00
|
|
|
}
|
2019-09-06 03:57:44 +01:00
|
|
|
|
2022-12-01 10:33:28 +00:00
|
|
|
impl<'tcx> TyCtxtAt<'tcx> {
|
2021-07-12 22:19:25 +02:00
|
|
|
/// Create a new definition within the incr. comp. engine.
|
2022-11-26 13:54:45 +00:00
|
|
|
pub fn create_def(
|
|
|
|
self,
|
|
|
|
parent: LocalDefId,
|
2023-12-03 12:29:59 +03:00
|
|
|
name: Symbol,
|
2023-11-21 23:40:23 +03:00
|
|
|
def_kind: DefKind,
|
2022-12-03 13:36:39 +00:00
|
|
|
) -> TyCtxtFeed<'tcx, LocalDefId> {
|
2024-02-14 14:45:38 +00:00
|
|
|
let feed = self.tcx.create_def(parent, name, def_kind);
|
|
|
|
|
|
|
|
feed.def_span(self.span);
|
|
|
|
feed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
|
|
|
/// `tcx`-dependent operations performed for every created definition.
|
|
|
|
pub fn create_def(
|
|
|
|
self,
|
|
|
|
parent: LocalDefId,
|
|
|
|
name: Symbol,
|
|
|
|
def_kind: DefKind,
|
|
|
|
) -> TyCtxtFeed<'tcx, LocalDefId> {
|
|
|
|
let data = def_kind.def_path_data(name);
|
2022-05-03 22:14:07 +02:00
|
|
|
// The following call has the side effect of modifying the tables inside `definitions`.
|
|
|
|
// These very tables are relied on by the incr. comp. engine to decode DepNodes and to
|
|
|
|
// decode the on-disk cache.
|
|
|
|
//
|
2021-07-12 22:19:25 +02:00
|
|
|
// Any LocalDefId which is used within queries, either as key or result, either:
|
|
|
|
// - has been created before the construction of the TyCtxt;
|
2022-04-27 19:18:26 +02:00
|
|
|
// - has been created by this call to `create_def`.
|
2021-07-12 22:19:25 +02:00
|
|
|
// As a consequence, this LocalDefId is always re-created before it is needed by the incr.
|
|
|
|
// comp. engine itself.
|
2022-05-03 22:14:07 +02:00
|
|
|
//
|
|
|
|
// This call also writes to the value of `source_span` and `expn_that_defined` queries.
|
|
|
|
// This is fine because:
|
|
|
|
// - those queries are `eval_always` so we won't miss their result changing;
|
|
|
|
// - this write will have happened before these queries are called.
|
2023-12-19 23:16:49 +03:00
|
|
|
let def_id = self.untracked.definitions.write().create_def(parent, data);
|
|
|
|
|
2024-02-14 14:08:19 +00:00
|
|
|
// This function modifies `self.definitions` using a side-effect.
|
|
|
|
// We need to ensure that these side effects are re-run by the incr. comp. engine.
|
|
|
|
// Depending on the forever-red node will tell the graph that the calling query
|
|
|
|
// needs to be re-evaluated.
|
|
|
|
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
|
|
|
|
|
2024-02-14 15:44:01 +00:00
|
|
|
let feed = TyCtxtFeed { tcx: self, key: def_id };
|
2023-12-19 23:16:49 +03:00
|
|
|
feed.def_kind(def_kind);
|
|
|
|
// Unique types created for closures participate in type privacy checking.
|
|
|
|
// They have visibilities inherited from the module they are defined in.
|
|
|
|
// Visibilities for opaque types are meaningless, but still provided
|
|
|
|
// so that all items have visibilities.
|
|
|
|
if matches!(def_kind, DefKind::Closure | DefKind::OpaqueTy) {
|
|
|
|
let parent_mod = self.parent_module_from_def_id(def_id).to_def_id();
|
|
|
|
feed.visibility(ty::Visibility::Restricted(parent_mod));
|
|
|
|
}
|
|
|
|
|
2024-02-14 14:45:38 +00:00
|
|
|
feed
|
2023-12-19 23:16:49 +03:00
|
|
|
}
|
|
|
|
|
2024-03-26 17:01:47 +00:00
|
|
|
pub fn create_crate_num(
|
|
|
|
self,
|
|
|
|
stable_crate_id: StableCrateId,
|
|
|
|
) -> Result<TyCtxtFeed<'tcx, CrateNum>, CrateNum> {
|
2024-03-26 16:59:14 +00:00
|
|
|
if let Some(&existing) = self.untracked().stable_crate_ids.read().get(&stable_crate_id) {
|
|
|
|
return Err(existing);
|
|
|
|
}
|
|
|
|
|
|
|
|
let num = CrateNum::new(self.untracked().stable_crate_ids.read().len());
|
|
|
|
self.untracked().stable_crate_ids.write().insert(stable_crate_id, num);
|
2024-03-26 17:01:47 +00:00
|
|
|
Ok(TyCtxtFeed { key: num, tcx: self })
|
2024-03-26 16:59:14 +00:00
|
|
|
}
|
|
|
|
|
2021-07-12 22:19:25 +02:00
|
|
|
pub fn iter_local_def_id(self) -> impl Iterator<Item = LocalDefId> + 'tcx {
|
2022-11-26 13:54:45 +00:00
|
|
|
// Create a dependency to the red node to be sure we re-execute this when the amount of
|
2021-07-12 22:19:25 +02:00
|
|
|
// definitions change.
|
2022-11-26 13:54:45 +00:00
|
|
|
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
|
|
|
|
|
2022-12-08 08:52:07 +00:00
|
|
|
let definitions = &self.untracked.definitions;
|
2024-04-11 13:15:34 +00:00
|
|
|
std::iter::from_coroutine(
|
2024-04-29 09:27:34 -04:00
|
|
|
#[coroutine]
|
2024-04-11 13:15:34 +00:00
|
|
|
|| {
|
|
|
|
let mut i = 0;
|
|
|
|
|
|
|
|
// Recompute the number of definitions each time, because our caller may be creating
|
|
|
|
// new ones.
|
|
|
|
while i < { definitions.read().num_definitions() } {
|
|
|
|
let local_def_index = rustc_span::def_id::DefIndex::from_usize(i);
|
|
|
|
yield LocalDefId { local_def_index };
|
|
|
|
i += 1;
|
|
|
|
}
|
2022-11-26 13:54:45 +00:00
|
|
|
|
2024-04-11 13:15:34 +00:00
|
|
|
// Freeze definitions once we finish iterating on them, to prevent adding new ones.
|
|
|
|
definitions.freeze();
|
|
|
|
},
|
|
|
|
)
|
2021-07-12 22:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn def_path_table(self) -> &'tcx rustc_hir::definitions::DefPathTable {
|
2022-08-18 10:13:37 +08:00
|
|
|
// Create a dependency to the crate to be sure we re-execute this when the amount of
|
2021-07-12 22:19:25 +02:00
|
|
|
// definitions change.
|
2022-11-26 13:54:45 +00:00
|
|
|
self.dep_graph.read_index(DepNodeIndex::FOREVER_RED_NODE);
|
|
|
|
|
2023-08-31 09:16:33 +02:00
|
|
|
// Freeze definitions once we start iterating on them, to prevent adding new ones
|
2022-11-16 20:34:16 +00:00
|
|
|
// while iterating. If some query needs to add definitions, it should be `ensure`d above.
|
2023-08-31 09:16:33 +02:00
|
|
|
self.untracked.definitions.freeze().def_path_table()
|
2021-07-12 22:19:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn def_path_hash_to_def_index_map(
|
|
|
|
self,
|
|
|
|
) -> &'tcx rustc_hir::def_path_hash_map::DefPathHashMap {
|
2022-08-18 10:13:37 +08:00
|
|
|
// Create a dependency to the crate to be sure we re-execute this when the amount of
|
2021-07-12 22:19:25 +02:00
|
|
|
// definitions change.
|
|
|
|
self.ensure().hir_crate(());
|
2023-08-31 09:16:33 +02:00
|
|
|
// Freeze definitions once we start iterating on them, to prevent adding new ones
|
2022-11-16 20:34:16 +00:00
|
|
|
// while iterating. If some query needs to add definitions, it should be `ensure`d above.
|
2023-08-31 09:16:33 +02:00
|
|
|
self.untracked.definitions.freeze().def_path_hash_to_def_index_map()
|
2021-07-12 22:19:25 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:12:01 +02:00
|
|
|
/// Note that this is *untracked* and should only be used within the query
|
|
|
|
/// system if the result is otherwise tracked through queries
|
2023-02-23 01:14:48 +01:00
|
|
|
#[inline]
|
2023-09-09 16:02:11 +02:00
|
|
|
pub fn cstore_untracked(self) -> FreezeReadGuard<'tcx, CrateStoreDyn> {
|
|
|
|
FreezeReadGuard::map(self.untracked.cstore.read(), |c| &**c)
|
2021-06-28 21:12:01 +02:00
|
|
|
}
|
|
|
|
|
2023-02-15 17:19:38 +00:00
|
|
|
/// Give out access to the untracked data without any sanity checks.
|
|
|
|
pub fn untracked(self) -> &'tcx Untracked {
|
|
|
|
&self.untracked
|
|
|
|
}
|
2021-06-28 21:12:01 +02:00
|
|
|
/// Note that this is *untracked* and should only be used within the query
|
|
|
|
/// system if the result is otherwise tracked through queries
|
2021-07-12 22:19:25 +02:00
|
|
|
#[inline]
|
2023-08-31 09:16:33 +02:00
|
|
|
pub fn definitions_untracked(self) -> FreezeReadGuard<'tcx, Definitions> {
|
2022-12-08 08:52:07 +00:00
|
|
|
self.untracked.definitions.read()
|
2017-09-07 13:21:46 -07:00
|
|
|
}
|
2017-09-14 15:10:24 +02:00
|
|
|
|
2022-05-30 18:49:17 +02:00
|
|
|
/// Note that this is *untracked* and should only be used within the query
|
|
|
|
/// system if the result is otherwise tracked through queries
|
|
|
|
#[inline]
|
|
|
|
pub fn source_span_untracked(self, def_id: LocalDefId) -> Span {
|
2023-02-21 08:37:10 +00:00
|
|
|
self.untracked.source_span.get(def_id).unwrap_or(DUMMY_SP)
|
2017-09-07 13:21:46 -07:00
|
|
|
}
|
2017-09-14 15:10:24 +02:00
|
|
|
|
2018-12-04 16:26:34 +01:00
|
|
|
#[inline(always)]
|
2021-07-12 22:19:25 +02:00
|
|
|
pub fn with_stable_hashing_context<R>(
|
|
|
|
self,
|
|
|
|
f: impl FnOnce(StableHashingContext<'_>) -> R,
|
|
|
|
) -> R {
|
2022-12-08 08:52:07 +00:00
|
|
|
f(StableHashingContext::new(self.sess, &self.untracked))
|
2017-09-14 15:10:24 +02:00
|
|
|
}
|
2017-09-14 17:43:03 +02:00
|
|
|
|
Use delayed error handling for `Encodable` and `Encoder` infallible.
There are two impls of the `Encoder` trait: `opaque::Encoder` and
`opaque::FileEncoder`. The former encodes into memory and is infallible, the
latter writes to file and is fallible.
Currently, standard `Result`/`?`/`unwrap` error handling is used, but this is a
bit verbose and has non-trivial cost, which is annoying given how rare failures
are (especially in the infallible `opaque::Encoder` case).
This commit changes how `Encoder` fallibility is handled. All the `emit_*`
methods are now infallible. `opaque::Encoder` requires no great changes for
this. `opaque::FileEncoder` now implements a delayed error handling strategy.
If a failure occurs, it records this via the `res` field, and all subsequent
encoding operations are skipped if `res` indicates an error has occurred. Once
encoding is complete, the new `finish` method is called, which returns a
`Result`. In other words, there is now a single `Result`-producing method
instead of many of them.
This has very little effect on how any file errors are reported if
`opaque::FileEncoder` has any failures.
Much of this commit is boring mechanical changes, removing `Result` return
values and `?` or `unwrap` from expressions. The more interesting parts are as
follows.
- serialize.rs: The `Encoder` trait gains an `Ok` associated type. The
`into_inner` method is changed into `finish`, which returns
`Result<Vec<u8>, !>`.
- opaque.rs: The `FileEncoder` adopts the delayed error handling
strategy. Its `Ok` type is a `usize`, returning the number of bytes
written, replacing previous uses of `FileEncoder::position`.
- Various methods that take an encoder now consume it, rather than being
passed a mutable reference, e.g. `serialize_query_result_cache`.
2022-06-07 13:30:45 +10:00
|
|
|
pub fn serialize_query_result_cache(self, encoder: FileEncoder) -> FileEncodeResult {
|
2023-03-25 09:46:19 +01:00
|
|
|
self.query_system.on_disk_cache.as_ref().map_or(Ok(0), |c| c.serialize(self, encoder))
|
2017-10-19 14:32:39 +02:00
|
|
|
}
|
|
|
|
|
2018-03-06 14:44:14 +01:00
|
|
|
#[inline]
|
|
|
|
pub fn local_crate_exports_generics(self) -> bool {
|
2018-07-26 13:20:47 -06:00
|
|
|
debug_assert!(self.sess.opts.share_generics());
|
2018-03-21 12:23:57 +01:00
|
|
|
|
2023-08-08 18:28:20 +08:00
|
|
|
self.crate_types().iter().any(|crate_type| {
|
2018-03-06 14:44:14 +01:00
|
|
|
match crate_type {
|
2019-12-24 17:38:22 -05:00
|
|
|
CrateType::Executable
|
|
|
|
| CrateType::Staticlib
|
|
|
|
| CrateType::ProcMacro
|
|
|
|
| CrateType::Cdylib => false,
|
2019-11-01 13:46:05 +01:00
|
|
|
|
|
|
|
// FIXME rust-lang/rust#64319, rust-lang/rust#64872:
|
|
|
|
// We want to block export of generics from dylibs,
|
|
|
|
// but we must fix rust-lang/rust#65890 before we can
|
|
|
|
// do that robustly.
|
2019-12-24 17:38:22 -05:00
|
|
|
CrateType::Dylib => true,
|
2019-11-01 13:46:05 +01:00
|
|
|
|
2019-12-24 17:38:22 -05:00
|
|
|
CrateType::Rlib => true,
|
2018-03-06 14:44:14 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-09-02 00:42:51 +02:00
|
|
|
|
2022-07-21 11:51:06 +01:00
|
|
|
/// Returns the `DefId` and the `BoundRegionKind` corresponding to the given region.
|
2024-05-24 01:57:06 +01:00
|
|
|
pub fn is_suitable_region(
|
|
|
|
self,
|
|
|
|
generic_param_scope: LocalDefId,
|
|
|
|
mut region: Region<'tcx>,
|
|
|
|
) -> Option<FreeRegionInfo> {
|
2023-09-08 01:35:51 +00:00
|
|
|
let (suitable_region_binding_scope, bound_region) = loop {
|
2024-05-24 01:57:06 +01:00
|
|
|
let def_id =
|
|
|
|
region.opt_param_def_id(self, generic_param_scope.to_def_id())?.as_local()?;
|
2023-09-08 01:35:51 +00:00
|
|
|
let scope = self.local_parent(def_id);
|
|
|
|
if self.def_kind(scope) == DefKind::OpaqueTy {
|
|
|
|
// Lifetime params of opaque types are synthetic and thus irrelevant to
|
|
|
|
// diagnostics. Map them back to their origin!
|
2024-03-06 18:48:13 +00:00
|
|
|
region = self.map_opaque_lifetime_to_parent_lifetime(def_id);
|
2023-09-08 01:35:51 +00:00
|
|
|
continue;
|
2019-12-24 17:38:22 -05:00
|
|
|
}
|
2024-11-03 22:06:03 +00:00
|
|
|
break (
|
|
|
|
scope,
|
|
|
|
ty::BoundRegionKind::Named(def_id.into(), self.item_name(def_id.into())),
|
|
|
|
);
|
2018-09-02 00:42:51 +02:00
|
|
|
};
|
|
|
|
|
2024-03-14 21:05:06 +03:00
|
|
|
let is_impl_item = match self.hir_node_by_def_id(suitable_region_binding_scope) {
|
|
|
|
Node::Item(..) | Node::TraitItem(..) => false,
|
|
|
|
Node::ImplItem(..) => self.is_bound_region_in_impl_item(suitable_region_binding_scope),
|
2023-09-08 01:35:51 +00:00
|
|
|
_ => false,
|
2018-09-02 00:42:51 +02:00
|
|
|
};
|
|
|
|
|
2023-11-14 13:13:27 +00:00
|
|
|
Some(FreeRegionInfo { def_id: suitable_region_binding_scope, bound_region, is_impl_item })
|
2018-09-02 00:42:51 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 16:05:31 -07:00
|
|
|
/// Given a `DefId` for an `fn`, return all the `dyn` and `impl` traits in its return type.
|
2020-06-27 13:15:12 +02:00
|
|
|
pub fn return_type_impl_or_dyn_traits(
|
2020-09-18 20:49:25 +02:00
|
|
|
self,
|
2020-06-27 13:15:12 +02:00
|
|
|
scope_def_id: LocalDefId,
|
|
|
|
) -> Vec<&'tcx hir::Ty<'tcx>> {
|
2023-11-24 19:28:19 +03:00
|
|
|
let hir_id = self.local_def_id_to_hir_id(scope_def_id);
|
2023-06-24 00:00:08 -03:00
|
|
|
let Some(hir::FnDecl { output: hir::FnRetTy::Return(hir_output), .. }) =
|
|
|
|
self.hir().fn_decl_by_hir_id(hir_id)
|
|
|
|
else {
|
2022-02-19 00:48:49 +01:00
|
|
|
return vec![];
|
2020-05-26 13:13:19 -07:00
|
|
|
};
|
|
|
|
|
2020-06-09 13:57:33 -07:00
|
|
|
let mut v = TraitObjectVisitor(vec![], self.hir());
|
|
|
|
v.visit_ty(hir_output);
|
|
|
|
v.0
|
2020-05-26 13:13:19 -07:00
|
|
|
}
|
|
|
|
|
2023-04-25 20:41:59 +00:00
|
|
|
/// Given a `DefId` for an `fn`, return all the `dyn` and `impl` traits in
|
|
|
|
/// its return type, and the associated alias span when type alias is used,
|
|
|
|
/// along with a span for lifetime suggestion (if there are existing generics).
|
2022-12-05 16:51:49 -08:00
|
|
|
pub fn return_type_impl_or_dyn_traits_with_type_alias(
|
|
|
|
self,
|
|
|
|
scope_def_id: LocalDefId,
|
2023-04-25 20:41:59 +00:00
|
|
|
) -> Option<(Vec<&'tcx hir::Ty<'tcx>>, Span, Option<Span>)> {
|
2023-11-24 19:28:19 +03:00
|
|
|
let hir_id = self.local_def_id_to_hir_id(scope_def_id);
|
2022-12-05 16:51:49 -08:00
|
|
|
let mut v = TraitObjectVisitor(vec![], self.hir());
|
|
|
|
// when the return type is a type alias
|
|
|
|
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,
|
2023-09-26 02:15:32 +00:00
|
|
|
hir::Path { res: hir::def::Res::Def(DefKind::TyAlias, def_id), .. }, )) = hir_output.kind
|
2022-12-05 16:51:49 -08:00
|
|
|
&& let Some(local_id) = def_id.as_local()
|
2023-12-01 05:28:34 -08:00
|
|
|
&& let Some(alias_ty) = self.hir_node_by_def_id(local_id).alias_ty() // it is type alias
|
|
|
|
&& let Some(alias_generics) = self.hir_node_by_def_id(local_id).generics()
|
2022-12-05 16:51:49 -08:00
|
|
|
{
|
|
|
|
v.visit_ty(alias_ty);
|
|
|
|
if !v.0.is_empty() {
|
2023-04-25 20:41:59 +00:00
|
|
|
return Some((
|
|
|
|
v.0,
|
|
|
|
alias_generics.span,
|
|
|
|
alias_generics.span_for_lifetime_suggestion(),
|
|
|
|
));
|
2022-12-05 16:51:49 -08:00
|
|
|
}
|
|
|
|
}
|
2024-09-09 12:22:00 +02:00
|
|
|
None
|
2022-12-05 16:51:49 -08:00
|
|
|
}
|
2018-09-02 00:42:51 +02:00
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
/// Checks if the bound region is in Impl Item.
|
2020-09-18 20:49:25 +02:00
|
|
|
pub fn is_bound_region_in_impl_item(self, suitable_region_binding_scope: LocalDefId) -> bool {
|
2022-03-13 00:52:25 +01:00
|
|
|
let container_id = self.parent(suitable_region_binding_scope.to_def_id());
|
2018-09-02 00:42:51 +02:00
|
|
|
if self.impl_trait_ref(container_id).is_some() {
|
|
|
|
// For now, we do not try to target impls of traits. This is
|
|
|
|
// because this message is going to suggest that the user
|
|
|
|
// change the fn signature, but they may not be free to do so,
|
|
|
|
// since the signature must match the trait.
|
|
|
|
//
|
|
|
|
// FIXME(#42706) -- in some cases, we could do better here.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
2019-01-19 21:59:34 +01:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// Determines whether identifiers in the assembly have strict naming rules.
|
2019-01-19 21:59:34 +01:00
|
|
|
/// Currently, only NVPTX* targets need it.
|
2020-09-18 20:49:25 +02:00
|
|
|
pub fn has_strict_asm_symbol_naming(self) -> bool {
|
2020-10-15 11:44:00 +02:00
|
|
|
self.sess.target.arch.contains("nvptx")
|
2017-09-07 13:21:46 -07:00
|
|
|
}
|
2019-10-28 20:02:41 -07:00
|
|
|
|
|
|
|
/// Returns `&'static core::panic::Location<'static>`.
|
2020-09-18 20:49:25 +02:00
|
|
|
pub fn caller_location_ty(self) -> Ty<'tcx> {
|
2023-07-05 20:13:26 +01:00
|
|
|
Ty::new_imm_ref(
|
|
|
|
self,
|
2019-10-28 20:02:41 -07:00
|
|
|
self.lifetimes.re_static,
|
2023-02-07 01:29:48 -07:00
|
|
|
self.type_of(self.require_lang_item(LangItem::PanicLocation, None))
|
2023-07-11 22:35:29 +01:00
|
|
|
.instantiate(self, self.mk_args(&[self.lifetimes.re_static.into()])),
|
2019-10-28 20:02:41 -07:00
|
|
|
)
|
|
|
|
}
|
2019-12-30 19:46:30 -06:00
|
|
|
|
2020-01-25 19:09:23 -06:00
|
|
|
/// Returns a displayable description and article for the given `def_id` (e.g. `("a", "struct")`).
|
2020-09-18 20:49:25 +02:00
|
|
|
pub fn article_and_description(self, def_id: DefId) -> (&'static str, &'static str) {
|
2023-02-21 14:05:32 -07:00
|
|
|
let kind = self.def_kind(def_id);
|
|
|
|
(self.def_kind_descr_article(kind, def_id), self.def_kind_descr(kind, def_id))
|
2019-12-30 19:46:30 -06:00
|
|
|
}
|
2021-07-04 13:02:51 -05:00
|
|
|
|
|
|
|
pub fn type_length_limit(self) -> Limit {
|
|
|
|
self.limits(()).type_length_limit
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn recursion_limit(self) -> Limit {
|
|
|
|
self.limits(()).recursion_limit
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_size_limit(self) -> Limit {
|
|
|
|
self.limits(()).move_size_limit
|
|
|
|
}
|
|
|
|
|
2021-12-24 11:09:32 +08:00
|
|
|
pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx {
|
|
|
|
iter::once(LOCAL_CRATE)
|
2024-06-06 09:45:50 +00:00
|
|
|
.chain(self.crates(()).iter().copied())
|
2023-05-23 13:21:22 +00:00
|
|
|
.flat_map(move |cnum| self.traits(cnum).iter().copied())
|
2021-12-24 11:09:32 +08:00
|
|
|
}
|
2022-08-28 00:10:06 +03:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn local_visibility(self, def_id: LocalDefId) -> Visibility {
|
2022-09-20 14:11:23 +09:00
|
|
|
self.visibility(def_id).expect_local()
|
2022-08-28 00:10:06 +03:00
|
|
|
}
|
2023-06-15 07:40:17 +00:00
|
|
|
|
|
|
|
/// Returns the origin of the opaque type `def_id`.
|
2024-10-02 23:16:31 -04:00
|
|
|
#[instrument(skip(self), level = "trace", ret)]
|
|
|
|
pub fn local_opaque_ty_origin(self, def_id: LocalDefId) -> hir::OpaqueTyOrigin<LocalDefId> {
|
|
|
|
self.hir().expect_opaque_ty(def_id).origin
|
2023-06-15 07:40:17 +00:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
macro_rules! nop_lift {
|
|
|
|
($set:ident; $ty:ty => $lifted:ty) => {
|
2024-05-10 14:27:48 -04:00
|
|
|
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for $ty {
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
type Lifted = $lifted;
|
2024-07-16 00:03:37 -04:00
|
|
|
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
|
2023-09-17 09:35:18 +02:00
|
|
|
// Assert that the set has the right type.
|
|
|
|
// Given an argument that has an interned type, the return type has the type of
|
|
|
|
// the corresponding interner set. This won't actually return anything, we're
|
|
|
|
// just doing this to compute said type!
|
|
|
|
fn _intern_set_ty_from_interned_ty<'tcx, Inner>(
|
|
|
|
_x: Interned<'tcx, Inner>,
|
|
|
|
) -> InternedSet<'tcx, Inner> {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
fn _type_eq<T>(_x: &T, _y: &T) {}
|
|
|
|
fn _test<'tcx>(x: $lifted, tcx: TyCtxt<'tcx>) {
|
|
|
|
// If `x` is a newtype around an `Interned<T>`, then `interner` is an
|
|
|
|
// interner of appropriate type. (Ideally we'd also check that `x` is a
|
|
|
|
// newtype with just that one field. Not sure how to do that.)
|
|
|
|
let interner = _intern_set_ty_from_interned_ty(x.0);
|
|
|
|
// Now check that this is the same type as `interners.$set`.
|
|
|
|
_type_eq(&interner, &tcx.interners.$set);
|
|
|
|
}
|
|
|
|
|
2023-02-15 11:43:41 +00:00
|
|
|
tcx.interners
|
|
|
|
.$set
|
|
|
|
.contains_pointer_to(&InternedInSet(&*self.0.0))
|
2022-02-11 13:20:38 +01:00
|
|
|
// SAFETY: `self` is interned and therefore valid
|
|
|
|
// for the entire lifetime of the `TyCtxt`.
|
2023-02-15 11:43:41 +00:00
|
|
|
.then(|| unsafe { mem::transmute(self) })
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 14:13:38 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-12-28 20:30:06 +01:00
|
|
|
macro_rules! nop_list_lift {
|
2020-01-02 00:44:34 +01:00
|
|
|
($set:ident; $ty:ty => $lifted:ty) => {
|
2024-05-10 14:27:48 -04:00
|
|
|
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for &'a List<$ty> {
|
2019-12-24 17:38:22 -05:00
|
|
|
type Lifted = &'tcx List<$lifted>;
|
2024-07-16 00:03:37 -04:00
|
|
|
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
|
2023-09-17 09:35:18 +02:00
|
|
|
// Assert that the set has the right type.
|
|
|
|
if false {
|
|
|
|
let _x: &InternedSet<'tcx, List<$lifted>> = &tcx.interners.$set;
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:38:22 -05:00
|
|
|
if self.is_empty() {
|
|
|
|
return Some(List::empty());
|
2018-12-28 20:30:06 +01:00
|
|
|
}
|
2023-02-15 11:43:41 +00:00
|
|
|
tcx.interners
|
|
|
|
.$set
|
|
|
|
.contains_pointer_to(&InternedInSet(self))
|
|
|
|
.then(|| unsafe { mem::transmute(self) })
|
2019-12-24 17:38:22 -05:00
|
|
|
}
|
|
|
|
}
|
2018-12-28 20:30:06 +01:00
|
|
|
};
|
2018-05-08 18:15:48 -03:00
|
|
|
}
|
|
|
|
|
2020-01-02 00:44:34 +01:00
|
|
|
nop_lift! {type_; Ty<'a> => Ty<'tcx>}
|
2022-01-28 11:25:15 +11:00
|
|
|
nop_lift! {region; Region<'a> => Region<'tcx>}
|
2022-02-02 14:24:45 +11:00
|
|
|
nop_lift! {const_; Const<'a> => Const<'tcx>}
|
2023-02-02 13:57:36 +00:00
|
|
|
nop_lift! {pat; Pattern<'a> => Pattern<'tcx>}
|
Introduce `ConstAllocation`.
Currently some `Allocation`s are interned, some are not, and it's very
hard to tell at a use point which is which.
This commit introduces `ConstAllocation` for the known-interned ones,
which makes the division much clearer. `ConstAllocation::inner()` is
used to get the underlying `Allocation`.
In some places it's natural to use an `Allocation`, in some it's natural
to use a `ConstAllocation`, and in some places there's no clear choice.
I've tried to make things look as nice as possible, while generally
favouring `ConstAllocation`, which is the type that embodies more
information. This does require quite a few calls to `inner()`.
The commit also tweaks how `PartialOrd` works for `Interned`. The
previous code was too clever by half, building on `T: Ord` to make the
code shorter. That caused problems with deriving `PartialOrd` and `Ord`
for `ConstAllocation`, so I changed it to build on `T: PartialOrd`,
which is slightly more verbose but much more standard and avoided the
problems.
2022-03-02 07:15:04 +11:00
|
|
|
nop_lift! {const_allocation; ConstAllocation<'a> => ConstAllocation<'tcx>}
|
2022-01-27 17:00:16 +11:00
|
|
|
nop_lift! {predicate; Predicate<'a> => Predicate<'tcx>}
|
2023-06-22 18:17:13 +00:00
|
|
|
nop_lift! {predicate; Clause<'a> => Clause<'tcx>}
|
2024-01-19 09:42:51 +00:00
|
|
|
nop_lift! {layout; Layout<'a> => Layout<'tcx>}
|
2018-11-23 19:47:22 +01:00
|
|
|
|
2023-04-18 17:42:30 +00:00
|
|
|
nop_list_lift! {type_lists; Ty<'a> => Ty<'tcx>}
|
2022-11-19 03:28:56 +00:00
|
|
|
nop_list_lift! {poly_existential_predicates; PolyExistentialPredicate<'a> => PolyExistentialPredicate<'tcx>}
|
2020-10-05 20:41:46 -04:00
|
|
|
nop_list_lift! {bound_variable_kinds; ty::BoundVariableKind => ty::BoundVariableKind}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
// This is the impl for `&'a GenericArgs<'a>`.
|
|
|
|
nop_list_lift! {args; GenericArg<'a> => GenericArg<'tcx>}
|
2018-12-11 19:56:59 +01:00
|
|
|
|
2024-01-19 09:42:51 +00:00
|
|
|
macro_rules! nop_slice_lift {
|
|
|
|
($ty:ty => $lifted:ty) => {
|
2024-05-10 14:27:48 -04:00
|
|
|
impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for &'a [$ty] {
|
2024-01-19 09:42:51 +00:00
|
|
|
type Lifted = &'tcx [$lifted];
|
2024-07-16 00:03:37 -04:00
|
|
|
fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
|
2024-01-19 09:42:51 +00:00
|
|
|
if self.is_empty() {
|
|
|
|
return Some(&[]);
|
|
|
|
}
|
|
|
|
tcx.interners
|
|
|
|
.arena
|
|
|
|
.dropless
|
|
|
|
.contains_slice(self)
|
|
|
|
.then(|| unsafe { mem::transmute(self) })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
nop_slice_lift! {ty::ValTree<'a> => ty::ValTree<'tcx>}
|
|
|
|
|
2023-09-14 12:03:56 +10:00
|
|
|
TrivialLiftImpls! {
|
2024-10-29 23:42:59 +00:00
|
|
|
ImplPolarity, PredicatePolarity, Promoted, BoundConstness,
|
2023-04-16 14:01:43 +10:00
|
|
|
}
|
2020-11-22 02:13:53 +01:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
macro_rules! sty_debug_print {
|
2020-07-28 16:15:40 +02:00
|
|
|
($fmt: expr, $ctxt: expr, $($variant: ident),*) => {{
|
2019-09-06 03:57:44 +01:00
|
|
|
// Curious inner module to allow variant names to be used as
|
2015-09-06 21:51:58 +03:00
|
|
|
// variable names.
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
mod inner {
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::ty::{self, TyCtxt};
|
2022-02-04 14:27:17 +11:00
|
|
|
use crate::ty::context::InternedInSet;
|
2016-03-23 04:56:49 +02:00
|
|
|
|
2015-09-06 21:51:58 +03:00
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
struct DebugStat {
|
|
|
|
total: usize,
|
2019-02-20 01:14:56 +00:00
|
|
|
lt_infer: usize,
|
2015-09-06 21:51:58 +03:00
|
|
|
ty_infer: usize,
|
2019-02-20 01:14:56 +00:00
|
|
|
ct_infer: usize,
|
|
|
|
all_infer: usize,
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2024-08-29 09:59:05 +10:00
|
|
|
pub(crate) fn go(fmt: &mut std::fmt::Formatter<'_>, tcx: TyCtxt<'_>) -> std::fmt::Result {
|
2015-09-06 21:51:58 +03:00
|
|
|
let mut total = DebugStat {
|
|
|
|
total: 0,
|
2019-02-20 01:14:56 +00:00
|
|
|
lt_infer: 0,
|
|
|
|
ty_infer: 0,
|
|
|
|
ct_infer: 0,
|
|
|
|
all_infer: 0,
|
2015-09-06 21:51:58 +03:00
|
|
|
};
|
|
|
|
$(let mut $variant = total;)*
|
|
|
|
|
2023-08-17 11:07:50 +02:00
|
|
|
for shard in tcx.interners.type_.lock_shards() {
|
|
|
|
let types = shard.keys();
|
|
|
|
for &InternedInSet(t) in types {
|
|
|
|
let variant = match t.internee {
|
|
|
|
ty::Bool | ty::Char | ty::Int(..) | ty::Uint(..) |
|
|
|
|
ty::Float(..) | ty::Str | ty::Never => continue,
|
|
|
|
ty::Error(_) => /* unimportant */ continue,
|
|
|
|
$(ty::$variant(..) => &mut $variant,)*
|
|
|
|
};
|
|
|
|
let lt = t.flags.intersects(ty::TypeFlags::HAS_RE_INFER);
|
|
|
|
let ty = t.flags.intersects(ty::TypeFlags::HAS_TY_INFER);
|
|
|
|
let ct = t.flags.intersects(ty::TypeFlags::HAS_CT_INFER);
|
|
|
|
|
|
|
|
variant.total += 1;
|
|
|
|
total.total += 1;
|
|
|
|
if lt { total.lt_infer += 1; variant.lt_infer += 1 }
|
|
|
|
if ty { total.ty_infer += 1; variant.ty_infer += 1 }
|
|
|
|
if ct { total.ct_infer += 1; variant.ct_infer += 1 }
|
|
|
|
if lt && ty && ct { total.all_infer += 1; variant.all_infer += 1 }
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2020-07-28 16:15:40 +02:00
|
|
|
writeln!(fmt, "Ty interner total ty lt ct all")?;
|
|
|
|
$(writeln!(fmt, " {:18}: {uses:6} {usespc:4.1}%, \
|
2019-02-20 01:14:56 +00:00
|
|
|
{ty:4.1}% {lt:5.1}% {ct:4.1}% {all:4.1}%",
|
|
|
|
stringify!($variant),
|
|
|
|
uses = $variant.total,
|
|
|
|
usespc = $variant.total as f64 * 100.0 / total.total as f64,
|
|
|
|
ty = $variant.ty_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
lt = $variant.lt_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
ct = $variant.ct_infer as f64 * 100.0 / total.total as f64,
|
2020-07-28 16:15:40 +02:00
|
|
|
all = $variant.all_infer as f64 * 100.0 / total.total as f64)?;
|
2019-02-20 01:14:56 +00:00
|
|
|
)*
|
2020-07-28 16:15:40 +02:00
|
|
|
writeln!(fmt, " total {uses:6} \
|
2019-02-20 01:14:56 +00:00
|
|
|
{ty:4.1}% {lt:5.1}% {ct:4.1}% {all:4.1}%",
|
|
|
|
uses = total.total,
|
|
|
|
ty = total.ty_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
lt = total.lt_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
ct = total.ct_infer as f64 * 100.0 / total.total as f64,
|
|
|
|
all = total.all_infer as f64 * 100.0 / total.total as f64)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-28 16:15:40 +02:00
|
|
|
inner::go($fmt, $ctxt)
|
2015-09-06 21:51:58 +03:00
|
|
|
}}
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2020-07-28 16:15:40 +02:00
|
|
|
pub fn debug_stats(self) -> impl std::fmt::Debug + 'tcx {
|
2020-07-29 11:37:33 +02:00
|
|
|
struct DebugStats<'tcx>(TyCtxt<'tcx>);
|
|
|
|
|
2021-12-15 19:32:30 -05:00
|
|
|
impl<'tcx> std::fmt::Debug for DebugStats<'tcx> {
|
2020-07-29 11:37:33 +02:00
|
|
|
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
sty_debug_print!(
|
|
|
|
fmt,
|
|
|
|
self.0,
|
|
|
|
Adt,
|
|
|
|
Array,
|
|
|
|
Slice,
|
|
|
|
RawPtr,
|
|
|
|
Ref,
|
|
|
|
FnDef,
|
|
|
|
FnPtr,
|
|
|
|
Placeholder,
|
2023-10-19 16:06:43 +00:00
|
|
|
Coroutine,
|
|
|
|
CoroutineWitness,
|
2020-07-29 11:37:33 +02:00
|
|
|
Dynamic,
|
|
|
|
Closure,
|
2024-01-24 18:01:56 +00:00
|
|
|
CoroutineClosure,
|
2020-07-29 11:37:33 +02:00
|
|
|
Tuple,
|
|
|
|
Bound,
|
|
|
|
Param,
|
|
|
|
Infer,
|
2022-11-26 21:51:55 +00:00
|
|
|
Alias,
|
2023-02-02 13:57:36 +00:00
|
|
|
Pat,
|
2020-07-29 11:37:33 +02:00
|
|
|
Foreign
|
|
|
|
)?;
|
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
writeln!(fmt, "GenericArgs interner: #{}", self.0.interners.args.len())?;
|
2020-07-29 11:37:33 +02:00
|
|
|
writeln!(fmt, "Region interner: #{}", self.0.interners.region.len())?;
|
2021-08-26 19:47:07 +03:00
|
|
|
writeln!(
|
|
|
|
fmt,
|
|
|
|
"Const Allocation interner: #{}",
|
|
|
|
self.0.interners.const_allocation.len()
|
|
|
|
)?;
|
|
|
|
writeln!(fmt, "Layout interner: #{}", self.0.interners.layout.len())?;
|
2020-07-29 11:37:33 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 16:15:40 +02:00
|
|
|
|
2020-07-29 11:37:33 +02:00
|
|
|
DebugStats(self)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 13:25:26 +11:00
|
|
|
// This type holds a `T` in the interner. The `T` is stored in the arena and
|
|
|
|
// this type just holds a pointer to it, but it still effectively owns it. It
|
|
|
|
// impls `Borrow` so that it can be looked up using the original
|
|
|
|
// (non-arena-memory-owning) types.
|
2022-02-04 14:27:17 +11:00
|
|
|
struct InternedInSet<'tcx, T: ?Sized>(&'tcx T);
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2022-02-04 14:27:17 +11:00
|
|
|
impl<'tcx, T: 'tcx + ?Sized> Clone for InternedInSet<'tcx, T> {
|
2018-05-17 05:19:08 +02:00
|
|
|
fn clone(&self) -> Self {
|
2022-02-04 14:27:17 +11:00
|
|
|
InternedInSet(self.0)
|
2018-05-17 05:19:08 +02:00
|
|
|
}
|
|
|
|
}
|
2022-01-21 13:25:26 +11:00
|
|
|
|
2022-02-04 14:27:17 +11:00
|
|
|
impl<'tcx, T: 'tcx + ?Sized> Copy for InternedInSet<'tcx, T> {}
|
2018-05-17 05:19:08 +02:00
|
|
|
|
2022-02-04 14:27:17 +11:00
|
|
|
impl<'tcx, T: 'tcx + ?Sized> IntoPointer for InternedInSet<'tcx, T> {
|
2020-01-02 00:44:34 +01:00
|
|
|
fn into_pointer(&self) -> *const () {
|
|
|
|
self.0 as *const _ as *const ()
|
|
|
|
}
|
|
|
|
}
|
2022-01-21 13:25:26 +11:00
|
|
|
|
|
|
|
#[allow(rustc::usage_of_ty_tykind)]
|
2022-11-30 14:52:05 +00:00
|
|
|
impl<'tcx, T> Borrow<T> for InternedInSet<'tcx, WithCachedTypeInfo<T>> {
|
2022-12-20 22:10:40 +01:00
|
|
|
fn borrow(&self) -> &T {
|
2022-11-25 16:55:25 +00:00
|
|
|
&self.0.internee
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
}
|
2022-01-21 13:25:26 +11:00
|
|
|
|
2022-11-30 14:52:05 +00:00
|
|
|
impl<'tcx, T: PartialEq> PartialEq for InternedInSet<'tcx, WithCachedTypeInfo<T>> {
|
|
|
|
fn eq(&self, other: &InternedInSet<'tcx, WithCachedTypeInfo<T>>) -> bool {
|
2022-01-21 13:25:26 +11:00
|
|
|
// The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
|
|
|
|
// `x == y`.
|
2022-11-25 16:55:25 +00:00
|
|
|
self.0.internee == other.0.internee
|
2020-06-10 09:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 14:52:05 +00:00
|
|
|
impl<'tcx, T: Eq> Eq for InternedInSet<'tcx, WithCachedTypeInfo<T>> {}
|
2020-06-10 09:30:39 +01:00
|
|
|
|
2022-11-30 14:52:05 +00:00
|
|
|
impl<'tcx, T: Hash> Hash for InternedInSet<'tcx, WithCachedTypeInfo<T>> {
|
2020-06-10 09:30:39 +01:00
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
2022-01-21 13:25:26 +11:00
|
|
|
// The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
|
2022-11-30 14:47:40 +00:00
|
|
|
self.0.internee.hash(s)
|
2020-06-10 09:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 14:27:17 +11:00
|
|
|
impl<'tcx, T> Borrow<[T]> for InternedInSet<'tcx, List<T>> {
|
2022-12-20 22:10:40 +01:00
|
|
|
fn borrow(&self) -> &[T] {
|
2022-01-21 13:25:26 +11:00
|
|
|
&self.0[..]
|
2020-06-10 09:30:39 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
|
2022-02-04 14:27:17 +11:00
|
|
|
impl<'tcx, T: PartialEq> PartialEq for InternedInSet<'tcx, List<T>> {
|
|
|
|
fn eq(&self, other: &InternedInSet<'tcx, List<T>>) -> bool {
|
2022-01-21 13:25:26 +11:00
|
|
|
// The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
|
|
|
|
// `x == y`.
|
2016-09-02 11:08:16 +03:00
|
|
|
self.0[..] == other.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-04 14:27:17 +11:00
|
|
|
impl<'tcx, T: Eq> Eq for InternedInSet<'tcx, List<T>> {}
|
2016-09-02 11:08:16 +03:00
|
|
|
|
2022-02-04 14:27:17 +11:00
|
|
|
impl<'tcx, T: Hash> Hash for InternedInSet<'tcx, List<T>> {
|
2016-09-02 11:08:16 +03:00
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
2022-01-21 13:25:26 +11:00
|
|
|
// The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
|
2016-09-02 11:08:16 +03:00
|
|
|
self.0[..].hash(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-24 22:49:31 +01:00
|
|
|
impl<'tcx, T> Borrow<[T]> for InternedInSet<'tcx, ListWithCachedTypeInfo<T>> {
|
|
|
|
fn borrow(&self) -> &[T] {
|
|
|
|
&self.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, T: PartialEq> PartialEq for InternedInSet<'tcx, ListWithCachedTypeInfo<T>> {
|
|
|
|
fn eq(&self, other: &InternedInSet<'tcx, ListWithCachedTypeInfo<T>>) -> bool {
|
|
|
|
// The `Borrow` trait requires that `x.borrow() == y.borrow()` equals
|
|
|
|
// `x == y`.
|
|
|
|
self.0[..] == other.0[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx, T: Eq> Eq for InternedInSet<'tcx, ListWithCachedTypeInfo<T>> {}
|
|
|
|
|
|
|
|
impl<'tcx, T: Hash> Hash for InternedInSet<'tcx, ListWithCachedTypeInfo<T>> {
|
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
|
|
|
// The `Borrow` trait requires that `x.borrow().hash(s) == x.hash(s)`.
|
|
|
|
self.0[..].hash(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-02 11:08:16 +03:00
|
|
|
macro_rules! direct_interners {
|
2023-02-17 14:33:08 +11:00
|
|
|
($($name:ident: $vis:vis $method:ident($ty:ty): $ret_ctor:ident -> $ret_ty:ty,)+) => {
|
2022-01-28 11:25:15 +11:00
|
|
|
$(impl<'tcx> Borrow<$ty> for InternedInSet<'tcx, $ty> {
|
|
|
|
fn borrow<'a>(&'a self) -> &'a $ty {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> PartialEq for InternedInSet<'tcx, $ty> {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
// The `Borrow` trait requires that `x.borrow() == y.borrow()`
|
|
|
|
// equals `x == y`.
|
|
|
|
self.0 == other.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Eq for InternedInSet<'tcx, $ty> {}
|
|
|
|
|
|
|
|
impl<'tcx> Hash for InternedInSet<'tcx, $ty> {
|
|
|
|
fn hash<H: Hasher>(&self, s: &mut H) {
|
|
|
|
// The `Borrow` trait requires that `x.borrow().hash(s) ==
|
|
|
|
// x.hash(s)`.
|
|
|
|
self.0.hash(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2023-02-17 14:33:08 +11:00
|
|
|
$vis fn $method(self, v: $ty) -> $ret_ty {
|
2022-01-28 11:25:15 +11:00
|
|
|
$ret_ctor(Interned::new_unchecked(self.interners.$name.intern(v, |v| {
|
|
|
|
InternedInSet(self.interners.arena.alloc(v))
|
|
|
|
}).0))
|
|
|
|
}
|
|
|
|
})+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
// Functions with a `mk_` prefix are intended for use outside this file and
|
|
|
|
// crate. Functions with an `intern_` prefix are intended for use within this
|
2023-05-29 17:54:53 +00:00
|
|
|
// crate only, and have a corresponding `mk_` function.
|
2022-01-28 11:25:15 +11:00
|
|
|
direct_interners! {
|
2023-05-29 17:54:53 +00:00
|
|
|
region: pub(crate) intern_region(RegionKind<'tcx>): Region -> Region<'tcx>,
|
2023-02-02 13:57:36 +00:00
|
|
|
pat: pub mk_pat(PatternKind<'tcx>): Pattern -> Pattern<'tcx>,
|
2023-02-17 14:33:08 +11:00
|
|
|
const_allocation: pub mk_const_alloc(Allocation): ConstAllocation -> ConstAllocation<'tcx>,
|
2024-10-27 22:31:09 -07:00
|
|
|
layout: pub mk_layout(LayoutData<FieldIdx, VariantIdx>): Layout -> Layout<'tcx>,
|
2023-02-17 14:33:08 +11:00
|
|
|
adt_def: pub mk_adt_def_from_data(AdtDefData): AdtDef -> AdtDef<'tcx>,
|
2024-06-14 15:16:47 -04:00
|
|
|
external_constraints: pub mk_external_constraints(ExternalConstraintsData<TyCtxt<'tcx>>):
|
2023-02-17 14:33:08 +11:00
|
|
|
ExternalConstraints -> ExternalConstraints<'tcx>,
|
2024-06-17 11:03:06 -04:00
|
|
|
predefined_opaques_in_body: pub mk_predefined_opaques_in_body(PredefinedOpaquesData<TyCtxt<'tcx>>):
|
2023-05-16 23:51:23 +00:00
|
|
|
PredefinedOpaques -> PredefinedOpaques<'tcx>,
|
2020-06-10 09:30:39 +01:00
|
|
|
}
|
2016-05-11 04:14:41 +03:00
|
|
|
|
2016-10-16 21:21:25 -06:00
|
|
|
macro_rules! slice_interners {
|
2023-02-17 14:33:08 +11:00
|
|
|
($($field:ident: $vis:vis $method:ident($ty:ty)),+ $(,)?) => (
|
2020-10-24 11:55:00 +02:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2023-02-17 14:33:08 +11:00
|
|
|
$($vis fn $method(self, v: &[$ty]) -> &'tcx List<$ty> {
|
2023-02-17 14:24:13 +11:00
|
|
|
if v.is_empty() {
|
|
|
|
List::empty()
|
|
|
|
} else {
|
|
|
|
self.interners.$field.intern_ref(v, || {
|
2024-04-06 17:20:30 +02:00
|
|
|
InternedInSet(List::from_arena(&*self.arena, (), v))
|
2023-02-17 14:24:13 +11:00
|
|
|
}).0
|
|
|
|
}
|
2020-10-24 11:55:00 +02:00
|
|
|
})+
|
|
|
|
}
|
2019-03-28 18:00:17 -07:00
|
|
|
);
|
2016-10-16 21:21:25 -06:00
|
|
|
}
|
2016-09-02 11:08:16 +03:00
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
// These functions intern slices. They all have a corresponding
|
|
|
|
// `mk_foo_from_iter` function that interns an iterator. The slice version
|
|
|
|
// should be used when possible, because it's faster.
|
2016-10-16 21:21:25 -06:00
|
|
|
slice_interners!(
|
2023-02-17 14:33:08 +11:00
|
|
|
const_lists: pub mk_const_list(Const<'tcx>),
|
2023-07-11 22:35:29 +01:00
|
|
|
args: pub mk_args(GenericArg<'tcx>),
|
2023-04-18 17:42:30 +00:00
|
|
|
type_lists: pub mk_type_list(Ty<'tcx>),
|
2023-02-17 14:33:08 +11:00
|
|
|
canonical_var_infos: pub mk_canonical_var_infos(CanonicalVarInfo<'tcx>),
|
|
|
|
poly_existential_predicates: intern_poly_existential_predicates(PolyExistentialPredicate<'tcx>),
|
|
|
|
projs: pub mk_projs(ProjectionKind),
|
|
|
|
place_elems: pub mk_place_elems(PlaceElem<'tcx>),
|
|
|
|
bound_variable_kinds: pub mk_bound_variable_kinds(ty::BoundVariableKind),
|
2023-03-11 16:01:25 -08:00
|
|
|
fields: pub mk_fields(FieldIdx),
|
2023-10-18 19:28:46 +00:00
|
|
|
local_def_ids: intern_local_def_ids(LocalDefId),
|
2024-03-07 12:27:58 +00:00
|
|
|
captures: intern_captures(&'tcx ty::CapturedPlace<'tcx>),
|
2023-08-15 22:32:55 +01:00
|
|
|
offset_of: pub mk_offset_of((VariantIdx, FieldIdx)),
|
2016-10-09 11:36:12 -07:00
|
|
|
);
|
|
|
|
|
2019-06-14 00:48:52 +03:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2017-12-12 14:21:10 -05:00
|
|
|
/// Given a `fn` type, returns an equivalent `unsafe fn` type;
|
|
|
|
/// that is, a `fn` type that is equivalent in every way for being
|
|
|
|
/// unsafe.
|
2017-02-13 10:51:06 +02:00
|
|
|
pub fn safe_to_unsafe_fn_ty(self, sig: PolyFnSig<'tcx>) -> Ty<'tcx> {
|
2024-05-17 14:17:48 -03:00
|
|
|
assert_eq!(sig.safety(), hir::Safety::Safe);
|
|
|
|
Ty::new_fn_ptr(self, sig.map_bound(|sig| ty::FnSig { safety: hir::Safety::Unsafe, ..sig }))
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2020-12-03 20:10:55 -03:00
|
|
|
/// Given the def_id of a Trait `trait_def_id` and the name of an associated item `assoc_name`
|
|
|
|
/// returns true if the `trait_def_id` defines an associated item of name `assoc_name`.
|
2023-05-03 20:13:32 +00:00
|
|
|
pub fn trait_may_define_assoc_item(self, trait_def_id: DefId, assoc_name: Ident) -> bool {
|
2024-06-27 12:06:47 -04:00
|
|
|
self.supertrait_def_ids(trait_def_id).any(|trait_did| {
|
2020-12-03 20:10:55 -03:00
|
|
|
self.associated_items(trait_did)
|
2023-05-03 20:13:32 +00:00
|
|
|
.filter_by_name_unhygienic(assoc_name.name)
|
|
|
|
.any(|item| self.hygienic_eq(assoc_name, item.ident(self), trait_did))
|
2020-12-03 20:10:55 -03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-21 19:43:46 -05:00
|
|
|
/// Given a `ty`, return whether it's an `impl Future<...>`.
|
|
|
|
pub fn ty_is_opaque_future(self, ty: Ty<'_>) -> bool {
|
2022-12-13 11:07:42 +00:00
|
|
|
let ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) = ty.kind() else { return false };
|
2022-11-22 17:19:19 +00:00
|
|
|
let future_trait = self.require_lang_item(LangItem::Future, None);
|
2022-06-21 19:43:46 -05:00
|
|
|
|
2024-03-20 12:52:48 -04:00
|
|
|
self.explicit_item_super_predicates(def_id).skip_binder().iter().any(|&(predicate, _)| {
|
2023-06-19 20:46:46 +00:00
|
|
|
let ty::ClauseKind::Trait(trait_predicate) = predicate.kind().skip_binder() else {
|
2022-06-21 19:43:46 -05:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
trait_predicate.trait_ref.def_id == future_trait
|
2024-03-21 15:45:28 -04:00
|
|
|
&& trait_predicate.polarity == PredicatePolarity::Positive
|
2022-06-21 19:43:46 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-02 07:21:01 +07:00
|
|
|
/// Computes the def-ids of the transitive supertraits of `trait_def_id`. This (intentionally)
|
2020-12-03 20:10:55 -03:00
|
|
|
/// does not compute the full elaborated super-predicates but just the set of def-ids. It is used
|
2024-06-27 12:29:34 -04:00
|
|
|
/// to identify which traits may define a given associated type to help avoid cycle errors,
|
|
|
|
/// and to make size estimates for vtable layout computation.
|
|
|
|
pub fn supertrait_def_ids(self, trait_def_id: DefId) -> impl Iterator<Item = DefId> + 'tcx {
|
2024-07-06 12:33:03 -04:00
|
|
|
rustc_type_ir::elaborate::supertrait_def_ids(self, trait_def_id)
|
2020-12-03 20:10:55 -03:00
|
|
|
}
|
|
|
|
|
2020-05-07 13:11:24 +08:00
|
|
|
/// Given a closure signature, returns an equivalent fn signature. Detuples
|
|
|
|
/// and so forth -- so e.g., if we have a sig with `Fn<(u32, i32)>` then
|
|
|
|
/// you would get a `fn(u32, i32)`.
|
|
|
|
/// `unsafety` determines the unsafety of the fn signature. If you pass
|
2024-05-17 14:17:48 -03:00
|
|
|
/// `hir::Safety::Unsafe` in the previous example, then you would get
|
2019-04-01 00:00:43 +09:00
|
|
|
/// an `unsafe fn (u32, i32)`.
|
|
|
|
/// It cannot convert a closure that requires unsafe.
|
2024-05-17 14:17:48 -03:00
|
|
|
pub fn signature_unclosure(self, sig: PolyFnSig<'tcx>, safety: hir::Safety) -> PolyFnSig<'tcx> {
|
2020-05-07 13:11:24 +08:00
|
|
|
sig.map_bound(|s| {
|
2023-02-16 16:05:08 +11:00
|
|
|
let params = match s.inputs()[0].kind() {
|
|
|
|
ty::Tuple(params) => *params,
|
2017-11-30 12:22:11 -03:00
|
|
|
_ => bug!(),
|
|
|
|
};
|
2024-11-02 19:33:00 -07:00
|
|
|
self.mk_fn_sig(params, s.output(), s.c_variadic, safety, ExternAbi::Rust)
|
2020-05-07 13:11:24 +08:00
|
|
|
})
|
2017-11-30 12:22:11 -03:00
|
|
|
}
|
|
|
|
|
2020-05-11 22:06:41 +02:00
|
|
|
#[inline]
|
2020-10-05 16:51:33 -04:00
|
|
|
pub fn mk_predicate(self, binder: Binder<'tcx, PredicateKind<'tcx>>) -> Predicate<'tcx> {
|
2022-03-14 16:54:31 +00:00
|
|
|
self.interners.intern_predicate(
|
|
|
|
binder,
|
|
|
|
self.sess,
|
|
|
|
// This is only used to create a stable hashing context.
|
2022-12-07 14:31:50 +00:00
|
|
|
&self.untracked,
|
2022-03-14 16:54:31 +00:00
|
|
|
)
|
2020-05-11 22:06:41 +02:00
|
|
|
}
|
|
|
|
|
2020-06-24 18:06:04 +02:00
|
|
|
#[inline]
|
|
|
|
pub fn reuse_or_mk_predicate(
|
|
|
|
self,
|
|
|
|
pred: Predicate<'tcx>,
|
2020-10-05 16:51:33 -04:00
|
|
|
binder: Binder<'tcx, PredicateKind<'tcx>>,
|
2020-06-24 18:06:04 +02:00
|
|
|
) -> Predicate<'tcx> {
|
2022-06-14 17:34:37 +02:00
|
|
|
if pred.kind() != binder { self.mk_predicate(binder) } else { pred }
|
2020-06-24 18:06:04 +02:00
|
|
|
}
|
|
|
|
|
2024-03-30 12:05:14 -04:00
|
|
|
pub fn check_args_compatible(self, def_id: DefId, args: &'tcx [ty::GenericArg<'tcx>]) -> bool {
|
|
|
|
self.check_args_compatible_inner(def_id, args, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_args_compatible_inner(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
args: &'tcx [ty::GenericArg<'tcx>],
|
|
|
|
nested: bool,
|
|
|
|
) -> bool {
|
|
|
|
let generics = self.generics_of(def_id);
|
|
|
|
|
|
|
|
// IATs themselves have a weird arg setup (self + own args), but nested items *in* IATs
|
|
|
|
// (namely: opaques, i.e. ATPITs) do not.
|
|
|
|
let own_args = if !nested
|
|
|
|
&& let DefKind::AssocTy = self.def_kind(def_id)
|
|
|
|
&& let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id))
|
|
|
|
{
|
2024-05-09 20:56:44 -04:00
|
|
|
if generics.own_params.len() + 1 != args.len() {
|
2024-03-30 12:05:14 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !matches!(args[0].unpack(), ty::GenericArgKind::Type(_)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
&args[1..]
|
|
|
|
} else {
|
|
|
|
if generics.count() != args.len() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let (parent_args, own_args) = args.split_at(generics.parent_count);
|
|
|
|
|
|
|
|
if let Some(parent) = generics.parent
|
|
|
|
&& !self.check_args_compatible_inner(parent, parent_args, true)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
own_args
|
|
|
|
};
|
|
|
|
|
2024-05-09 20:56:44 -04:00
|
|
|
for (param, arg) in std::iter::zip(&generics.own_params, own_args) {
|
2024-03-30 12:05:14 -04:00
|
|
|
match (¶m.kind, arg.unpack()) {
|
|
|
|
(ty::GenericParamDefKind::Type { .. }, ty::GenericArgKind::Type(_))
|
|
|
|
| (ty::GenericParamDefKind::Lifetime, ty::GenericArgKind::Lifetime(_))
|
|
|
|
| (ty::GenericParamDefKind::Const { .. }, ty::GenericArgKind::Const(_)) => {}
|
|
|
|
_ => return false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2024-03-30 12:34:15 -04:00
|
|
|
/// With `cfg(debug_assertions)`, assert that args are compatible with their generics,
|
|
|
|
/// and print out the args if not.
|
2024-03-30 12:10:47 -04:00
|
|
|
pub fn debug_assert_args_compatible(self, def_id: DefId, args: &'tcx [ty::GenericArg<'tcx>]) {
|
2024-09-11 13:32:53 -04:00
|
|
|
if cfg!(debug_assertions) && !self.check_args_compatible(def_id, args) {
|
|
|
|
if let DefKind::AssocTy = self.def_kind(def_id)
|
|
|
|
&& let DefKind::Impl { of_trait: false } = self.def_kind(self.parent(def_id))
|
|
|
|
{
|
|
|
|
bug!(
|
|
|
|
"args not compatible with generics for {}: args={:#?}, generics={:#?}",
|
|
|
|
self.def_path_str(def_id),
|
|
|
|
args,
|
|
|
|
// Make `[Self, GAT_ARGS...]` (this could be simplified)
|
|
|
|
self.mk_args_from_iter(
|
|
|
|
[self.types.self_param.into()].into_iter().chain(
|
|
|
|
self.generics_of(def_id)
|
|
|
|
.own_args(ty::GenericArgs::identity_for_item(self, def_id))
|
|
|
|
.iter()
|
|
|
|
.copied()
|
2024-03-30 12:34:15 -04:00
|
|
|
)
|
2024-09-11 13:32:53 -04:00
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
bug!(
|
|
|
|
"args not compatible with generics for {}: args={:#?}, generics={:#?}",
|
|
|
|
self.def_path_str(def_id),
|
|
|
|
args,
|
|
|
|
ty::GenericArgs::identity_for_item(self, def_id)
|
|
|
|
);
|
2024-03-30 12:05:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-14 14:37:16 +00:00
|
|
|
#[inline(always)]
|
2023-07-11 22:35:29 +01:00
|
|
|
pub(crate) fn check_and_mk_args(
|
2022-12-14 14:37:16 +00:00
|
|
|
self,
|
2024-03-30 12:34:15 -04:00
|
|
|
def_id: DefId,
|
2023-07-11 22:35:29 +01:00
|
|
|
args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
|
|
|
|
) -> GenericArgsRef<'tcx> {
|
2024-03-30 12:05:14 -04:00
|
|
|
let args = self.mk_args_from_iter(args.into_iter().map(Into::into));
|
2024-03-30 12:34:15 -04:00
|
|
|
self.debug_assert_args_compatible(def_id, args);
|
2024-03-30 12:05:14 -04:00
|
|
|
args
|
2022-12-14 14:37:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-29 21:13:04 +01:00
|
|
|
#[inline]
|
2024-06-03 01:10:24 +01:00
|
|
|
pub fn mk_ct_from_kind(self, kind: ty::ConstKind<'tcx>) -> Const<'tcx> {
|
2023-11-22 23:28:28 +00:00
|
|
|
self.interners.intern_const(
|
2024-06-03 01:10:24 +01:00
|
|
|
kind,
|
2023-11-22 23:28:28 +00:00
|
|
|
self.sess,
|
|
|
|
// This is only used to create a stable hashing context.
|
|
|
|
&self.untracked,
|
|
|
|
)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2023-07-05 13:58:04 -07:00
|
|
|
// Avoid this in favour of more specific `Ty::new_*` methods, where possible.
|
2023-07-05 20:13:26 +01:00
|
|
|
#[allow(rustc::usage_of_ty_tykind)]
|
2022-11-04 20:33:32 +00:00
|
|
|
#[inline]
|
2023-07-05 20:13:26 +01:00
|
|
|
pub fn mk_ty_from_kind(self, st: TyKind<'tcx>) -> Ty<'tcx> {
|
|
|
|
self.interners.intern_ty(
|
|
|
|
st,
|
|
|
|
self.sess,
|
|
|
|
// This is only used to create a stable hashing context.
|
|
|
|
&self.untracked,
|
|
|
|
)
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
|
|
|
|
2019-09-25 16:39:44 +01:00
|
|
|
pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> GenericArg<'tcx> {
|
2018-05-15 13:35:53 +01:00
|
|
|
match param.kind {
|
|
|
|
GenericParamDefKind::Lifetime => {
|
2023-11-14 13:13:27 +00:00
|
|
|
ty::Region::new_early_param(self, param.to_early_bound_region_data()).into()
|
2018-05-15 13:35:53 +01:00
|
|
|
}
|
2023-07-05 20:13:26 +01:00
|
|
|
GenericParamDefKind::Type { .. } => Ty::new_param(self, param.index, param.name).into(),
|
2024-06-03 01:10:24 +01:00
|
|
|
GenericParamDefKind::Const { .. } => {
|
|
|
|
ty::Const::new_param(self, ParamConst { index: param.index, name: param.name })
|
|
|
|
.into()
|
|
|
|
}
|
2018-05-15 13:35:53 +01:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2015-12-22 16:39:33 -05:00
|
|
|
|
2023-03-28 12:32:57 -07:00
|
|
|
pub fn mk_place_field(self, place: Place<'tcx>, f: FieldIdx, ty: Ty<'tcx>) -> Place<'tcx> {
|
2019-10-20 21:04:59 -04:00
|
|
|
self.mk_place_elem(place, PlaceElem::Field(f, ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_place_deref(self, place: Place<'tcx>) -> Place<'tcx> {
|
|
|
|
self.mk_place_elem(place, PlaceElem::Deref)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_place_downcast(
|
|
|
|
self,
|
|
|
|
place: Place<'tcx>,
|
2022-03-05 07:28:41 +11:00
|
|
|
adt_def: AdtDef<'tcx>,
|
2019-10-20 21:04:59 -04:00
|
|
|
variant_index: VariantIdx,
|
|
|
|
) -> Place<'tcx> {
|
|
|
|
self.mk_place_elem(
|
|
|
|
place,
|
2022-03-05 07:28:41 +11:00
|
|
|
PlaceElem::Downcast(Some(adt_def.variant(variant_index).name), variant_index),
|
2019-10-20 21:04:59 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_place_downcast_unnamed(
|
|
|
|
self,
|
|
|
|
place: Place<'tcx>,
|
|
|
|
variant_index: VariantIdx,
|
|
|
|
) -> Place<'tcx> {
|
|
|
|
self.mk_place_elem(place, PlaceElem::Downcast(None, variant_index))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn mk_place_index(self, place: Place<'tcx>, index: Local) -> Place<'tcx> {
|
|
|
|
self.mk_place_elem(place, PlaceElem::Index(index))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This method copies `Place`'s projection, add an element and reintern it. Should not be used
|
|
|
|
/// to build a full `Place` it's just a convenient way to grab a projection and modify it in
|
|
|
|
/// flight.
|
|
|
|
pub fn mk_place_elem(self, place: Place<'tcx>, elem: PlaceElem<'tcx>) -> Place<'tcx> {
|
|
|
|
let mut projection = place.projection.to_vec();
|
|
|
|
projection.push(elem);
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
Place { local: place.local, projection: self.mk_place_elems(&projection) }
|
2019-10-20 21:04:59 -04:00
|
|
|
}
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
pub fn mk_poly_existential_predicates(
|
2019-12-24 17:38:22 -05:00
|
|
|
self,
|
2022-11-19 03:28:56 +00:00
|
|
|
eps: &[PolyExistentialPredicate<'tcx>],
|
|
|
|
) -> &'tcx List<PolyExistentialPredicate<'tcx>> {
|
2016-11-16 09:21:49 -07:00
|
|
|
assert!(!eps.is_empty());
|
2022-06-14 17:34:37 +02:00
|
|
|
assert!(
|
|
|
|
eps.array_windows()
|
|
|
|
.all(|[a, b]| a.skip_binder().stable_cmp(self, &b.skip_binder())
|
|
|
|
!= Ordering::Greater)
|
|
|
|
);
|
2023-02-17 14:33:08 +11:00
|
|
|
self.intern_poly_existential_predicates(eps)
|
2016-11-16 09:21:49 -07:00
|
|
|
}
|
|
|
|
|
2024-03-24 22:49:31 +01:00
|
|
|
pub fn mk_clauses(self, clauses: &[Clause<'tcx>]) -> Clauses<'tcx> {
|
2017-05-02 14:37:00 -04:00
|
|
|
// FIXME consider asking the input slice to be sorted to avoid
|
|
|
|
// re-interning permutations, in which case that would be asserted
|
|
|
|
// here.
|
2024-03-24 22:49:31 +01:00
|
|
|
self.interners.intern_clauses(clauses)
|
2023-06-19 20:46:46 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 19:28:46 +00:00
|
|
|
pub fn mk_local_def_ids(self, clauses: &[LocalDefId]) -> &'tcx List<LocalDefId> {
|
|
|
|
// FIXME consider asking the input slice to be sorted to avoid
|
|
|
|
// re-interning permutations, in which case that would be asserted
|
|
|
|
// here.
|
|
|
|
self.intern_local_def_ids(clauses)
|
|
|
|
}
|
|
|
|
|
2024-02-29 12:21:20 +00:00
|
|
|
pub fn mk_local_def_ids_from_iter<I, T>(self, iter: I) -> T::Output
|
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
T: CollectAndApply<LocalDefId, &'tcx List<LocalDefId>>,
|
|
|
|
{
|
|
|
|
T::collect_and_apply(iter, |xs| self.mk_local_def_ids(xs))
|
|
|
|
}
|
|
|
|
|
2024-03-07 12:27:58 +00:00
|
|
|
pub fn mk_captures_from_iter<I, T>(self, iter: I) -> T::Output
|
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
T: CollectAndApply<
|
|
|
|
&'tcx ty::CapturedPlace<'tcx>,
|
|
|
|
&'tcx List<&'tcx ty::CapturedPlace<'tcx>>,
|
|
|
|
>,
|
|
|
|
{
|
|
|
|
T::collect_and_apply(iter, |xs| self.intern_captures(xs))
|
|
|
|
}
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
pub fn mk_const_list_from_iter<I, T>(self, iter: I) -> T::Output
|
Clarify iterator interners.
There are two traits, `InternAs` and `InternIteratorElement`. I found
them confusing to use, particularly this:
```
pub fn mk_tup<I: InternAs<Ty<'tcx>, Ty<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|ts| self.intern_tup(ts))
}
```
where I thought there might have been two levels of interning going on
(there isn't) due to the `intern_with`/`InternAs` + `intern_tup` naming.
And then I found the actual traits and impls themselves *very*
confusing.
- `InternAs` has a single impl, for iterators, with four type variables.
- `InternAs` is only implemented for iterators because it wouldn't
really make sense to implement for any other type. And you can't
really understand the trait without seeing that single impl, which is
suspicious.
- `InternAs` is basically just a wrapper for `InternIteratorElement`
which does all the actual work.
- Neither trait actually does any interning. They just have `Intern` in
their name because they are used *by* interning code.
- There are no comments.
So this commit improves things.
- It removes `InternAs` completely. This makes the `mk_*` function
signatures slightly more verbose -- two trait bounds instead of one --
but much easier to read, because you only need to understand one trait
instead of two.
- It renames `InternIteratorElement` as `CollectAndApply`. Likewise, it
renames its method `intern_with` as `collect_and_apply`. These names
describe better what's going on: we collect the iterator elements into
a slice and then apply a function to the slice.
- It adds comments, making clear that all this is all there just to
provide an optimized version of `f(&iter.collect::<Vec<_>>())`.
It took me a couple of attempts to come up with this commit. My initial
attempt kept `InternAs` around, but renamed things and added comments,
and I wasn't happy with it. I think this version is much better. The
resulting code is shorter, despite the addition of the comments.
2023-02-16 12:06:36 +11:00
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
T: CollectAndApply<ty::Const<'tcx>, &'tcx List<ty::Const<'tcx>>>,
|
|
|
|
{
|
2023-02-17 14:33:08 +11:00
|
|
|
T::collect_and_apply(iter, |xs| self.mk_const_list(xs))
|
2022-07-27 07:27:52 +00:00
|
|
|
}
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
// Unlike various other `mk_*_from_iter` functions, this one uses `I:
|
|
|
|
// IntoIterator` instead of `I: Iterator`, and it doesn't have a slice
|
|
|
|
// variant, because of the need to combine `inputs` and `output`. This
|
|
|
|
// explains the lack of `_from_iter` suffix.
|
2023-02-16 11:55:23 +11:00
|
|
|
pub fn mk_fn_sig<I, T>(
|
2019-12-24 17:38:22 -05:00
|
|
|
self,
|
|
|
|
inputs: I,
|
|
|
|
output: I::Item,
|
|
|
|
c_variadic: bool,
|
2024-05-17 14:17:48 -03:00
|
|
|
safety: hir::Safety,
|
2024-11-02 19:33:00 -07:00
|
|
|
abi: ExternAbi,
|
2023-02-16 11:55:23 +11:00
|
|
|
) -> T::Output
|
2019-07-31 21:00:35 +02:00
|
|
|
where
|
2023-02-16 16:05:08 +11:00
|
|
|
I: IntoIterator<Item = T>,
|
Clarify iterator interners.
There are two traits, `InternAs` and `InternIteratorElement`. I found
them confusing to use, particularly this:
```
pub fn mk_tup<I: InternAs<Ty<'tcx>, Ty<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|ts| self.intern_tup(ts))
}
```
where I thought there might have been two levels of interning going on
(there isn't) due to the `intern_with`/`InternAs` + `intern_tup` naming.
And then I found the actual traits and impls themselves *very*
confusing.
- `InternAs` has a single impl, for iterators, with four type variables.
- `InternAs` is only implemented for iterators because it wouldn't
really make sense to implement for any other type. And you can't
really understand the trait without seeing that single impl, which is
suspicious.
- `InternAs` is basically just a wrapper for `InternIteratorElement`
which does all the actual work.
- Neither trait actually does any interning. They just have `Intern` in
their name because they are used *by* interning code.
- There are no comments.
So this commit improves things.
- It removes `InternAs` completely. This makes the `mk_*` function
signatures slightly more verbose -- two trait bounds instead of one --
but much easier to read, because you only need to understand one trait
instead of two.
- It renames `InternIteratorElement` as `CollectAndApply`. Likewise, it
renames its method `intern_with` as `collect_and_apply`. These names
describe better what's going on: we collect the iterator elements into
a slice and then apply a function to the slice.
- It adds comments, making clear that all this is all there just to
provide an optimized version of `f(&iter.collect::<Vec<_>>())`.
It took me a couple of attempts to come up with this commit. My initial
attempt kept `InternAs` around, but renamed things and added comments,
and I wasn't happy with it. I think this version is much better. The
resulting code is shorter, despite the addition of the comments.
2023-02-16 12:06:36 +11:00
|
|
|
T: CollectAndApply<Ty<'tcx>, ty::FnSig<'tcx>>,
|
2016-11-28 20:25:33 -07:00
|
|
|
{
|
2023-02-16 16:05:08 +11:00
|
|
|
T::collect_and_apply(inputs.into_iter().chain(iter::once(output)), |xs| ty::FnSig {
|
2023-02-17 14:33:08 +11:00
|
|
|
inputs_and_output: self.mk_type_list(xs),
|
2019-12-24 17:38:22 -05:00
|
|
|
c_variadic,
|
2024-05-17 14:17:48 -03:00
|
|
|
safety,
|
2019-12-24 17:38:22 -05:00
|
|
|
abi,
|
2016-11-28 20:25:33 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
pub fn mk_poly_existential_predicates_from_iter<I, T>(self, iter: I) -> T::Output
|
Clarify iterator interners.
There are two traits, `InternAs` and `InternIteratorElement`. I found
them confusing to use, particularly this:
```
pub fn mk_tup<I: InternAs<Ty<'tcx>, Ty<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|ts| self.intern_tup(ts))
}
```
where I thought there might have been two levels of interning going on
(there isn't) due to the `intern_with`/`InternAs` + `intern_tup` naming.
And then I found the actual traits and impls themselves *very*
confusing.
- `InternAs` has a single impl, for iterators, with four type variables.
- `InternAs` is only implemented for iterators because it wouldn't
really make sense to implement for any other type. And you can't
really understand the trait without seeing that single impl, which is
suspicious.
- `InternAs` is basically just a wrapper for `InternIteratorElement`
which does all the actual work.
- Neither trait actually does any interning. They just have `Intern` in
their name because they are used *by* interning code.
- There are no comments.
So this commit improves things.
- It removes `InternAs` completely. This makes the `mk_*` function
signatures slightly more verbose -- two trait bounds instead of one --
but much easier to read, because you only need to understand one trait
instead of two.
- It renames `InternIteratorElement` as `CollectAndApply`. Likewise, it
renames its method `intern_with` as `collect_and_apply`. These names
describe better what's going on: we collect the iterator elements into
a slice and then apply a function to the slice.
- It adds comments, making clear that all this is all there just to
provide an optimized version of `f(&iter.collect::<Vec<_>>())`.
It took me a couple of attempts to come up with this commit. My initial
attempt kept `InternAs` around, but renamed things and added comments,
and I wasn't happy with it. I think this version is much better. The
resulting code is shorter, despite the addition of the comments.
2023-02-16 12:06:36 +11:00
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
T: CollectAndApply<
|
|
|
|
PolyExistentialPredicate<'tcx>,
|
|
|
|
&'tcx List<PolyExistentialPredicate<'tcx>>,
|
|
|
|
>,
|
|
|
|
{
|
2023-02-17 14:33:08 +11:00
|
|
|
T::collect_and_apply(iter, |xs| self.mk_poly_existential_predicates(xs))
|
2016-11-16 09:21:49 -07:00
|
|
|
}
|
|
|
|
|
2023-06-19 20:46:46 +00:00
|
|
|
pub fn mk_clauses_from_iter<I, T>(self, iter: I) -> T::Output
|
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
2024-03-24 22:49:31 +01:00
|
|
|
T: CollectAndApply<Clause<'tcx>, Clauses<'tcx>>,
|
2023-06-19 20:46:46 +00:00
|
|
|
{
|
|
|
|
T::collect_and_apply(iter, |xs| self.mk_clauses(xs))
|
|
|
|
}
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
pub fn mk_type_list_from_iter<I, T>(self, iter: I) -> T::Output
|
Clarify iterator interners.
There are two traits, `InternAs` and `InternIteratorElement`. I found
them confusing to use, particularly this:
```
pub fn mk_tup<I: InternAs<Ty<'tcx>, Ty<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|ts| self.intern_tup(ts))
}
```
where I thought there might have been two levels of interning going on
(there isn't) due to the `intern_with`/`InternAs` + `intern_tup` naming.
And then I found the actual traits and impls themselves *very*
confusing.
- `InternAs` has a single impl, for iterators, with four type variables.
- `InternAs` is only implemented for iterators because it wouldn't
really make sense to implement for any other type. And you can't
really understand the trait without seeing that single impl, which is
suspicious.
- `InternAs` is basically just a wrapper for `InternIteratorElement`
which does all the actual work.
- Neither trait actually does any interning. They just have `Intern` in
their name because they are used *by* interning code.
- There are no comments.
So this commit improves things.
- It removes `InternAs` completely. This makes the `mk_*` function
signatures slightly more verbose -- two trait bounds instead of one --
but much easier to read, because you only need to understand one trait
instead of two.
- It renames `InternIteratorElement` as `CollectAndApply`. Likewise, it
renames its method `intern_with` as `collect_and_apply`. These names
describe better what's going on: we collect the iterator elements into
a slice and then apply a function to the slice.
- It adds comments, making clear that all this is all there just to
provide an optimized version of `f(&iter.collect::<Vec<_>>())`.
It took me a couple of attempts to come up with this commit. My initial
attempt kept `InternAs` around, but renamed things and added comments,
and I wasn't happy with it. I think this version is much better. The
resulting code is shorter, despite the addition of the comments.
2023-02-16 12:06:36 +11:00
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
T: CollectAndApply<Ty<'tcx>, &'tcx List<Ty<'tcx>>>,
|
|
|
|
{
|
2023-02-17 14:33:08 +11:00
|
|
|
T::collect_and_apply(iter, |xs| self.mk_type_list(xs))
|
2016-10-24 18:23:29 -06:00
|
|
|
}
|
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
pub fn mk_args_from_iter<I, T>(self, iter: I) -> T::Output
|
Clarify iterator interners.
There are two traits, `InternAs` and `InternIteratorElement`. I found
them confusing to use, particularly this:
```
pub fn mk_tup<I: InternAs<Ty<'tcx>, Ty<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|ts| self.intern_tup(ts))
}
```
where I thought there might have been two levels of interning going on
(there isn't) due to the `intern_with`/`InternAs` + `intern_tup` naming.
And then I found the actual traits and impls themselves *very*
confusing.
- `InternAs` has a single impl, for iterators, with four type variables.
- `InternAs` is only implemented for iterators because it wouldn't
really make sense to implement for any other type. And you can't
really understand the trait without seeing that single impl, which is
suspicious.
- `InternAs` is basically just a wrapper for `InternIteratorElement`
which does all the actual work.
- Neither trait actually does any interning. They just have `Intern` in
their name because they are used *by* interning code.
- There are no comments.
So this commit improves things.
- It removes `InternAs` completely. This makes the `mk_*` function
signatures slightly more verbose -- two trait bounds instead of one --
but much easier to read, because you only need to understand one trait
instead of two.
- It renames `InternIteratorElement` as `CollectAndApply`. Likewise, it
renames its method `intern_with` as `collect_and_apply`. These names
describe better what's going on: we collect the iterator elements into
a slice and then apply a function to the slice.
- It adds comments, making clear that all this is all there just to
provide an optimized version of `f(&iter.collect::<Vec<_>>())`.
It took me a couple of attempts to come up with this commit. My initial
attempt kept `InternAs` around, but renamed things and added comments,
and I wasn't happy with it. I think this version is much better. The
resulting code is shorter, despite the addition of the comments.
2023-02-16 12:06:36 +11:00
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
2024-05-09 19:47:08 +00:00
|
|
|
T: CollectAndApply<GenericArg<'tcx>, ty::GenericArgsRef<'tcx>>,
|
Clarify iterator interners.
There are two traits, `InternAs` and `InternIteratorElement`. I found
them confusing to use, particularly this:
```
pub fn mk_tup<I: InternAs<Ty<'tcx>, Ty<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|ts| self.intern_tup(ts))
}
```
where I thought there might have been two levels of interning going on
(there isn't) due to the `intern_with`/`InternAs` + `intern_tup` naming.
And then I found the actual traits and impls themselves *very*
confusing.
- `InternAs` has a single impl, for iterators, with four type variables.
- `InternAs` is only implemented for iterators because it wouldn't
really make sense to implement for any other type. And you can't
really understand the trait without seeing that single impl, which is
suspicious.
- `InternAs` is basically just a wrapper for `InternIteratorElement`
which does all the actual work.
- Neither trait actually does any interning. They just have `Intern` in
their name because they are used *by* interning code.
- There are no comments.
So this commit improves things.
- It removes `InternAs` completely. This makes the `mk_*` function
signatures slightly more verbose -- two trait bounds instead of one --
but much easier to read, because you only need to understand one trait
instead of two.
- It renames `InternIteratorElement` as `CollectAndApply`. Likewise, it
renames its method `intern_with` as `collect_and_apply`. These names
describe better what's going on: we collect the iterator elements into
a slice and then apply a function to the slice.
- It adds comments, making clear that all this is all there just to
provide an optimized version of `f(&iter.collect::<Vec<_>>())`.
It took me a couple of attempts to come up with this commit. My initial
attempt kept `InternAs` around, but renamed things and added comments,
and I wasn't happy with it. I think this version is much better. The
resulting code is shorter, despite the addition of the comments.
2023-02-16 12:06:36 +11:00
|
|
|
{
|
2023-07-11 22:35:29 +01:00
|
|
|
T::collect_and_apply(iter, |xs| self.mk_args(xs))
|
2016-10-24 18:23:29 -06:00
|
|
|
}
|
|
|
|
|
2023-02-20 10:46:37 +11:00
|
|
|
pub fn mk_canonical_var_infos_from_iter<I, T>(self, iter: I) -> T::Output
|
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
T: CollectAndApply<CanonicalVarInfo<'tcx>, &'tcx List<CanonicalVarInfo<'tcx>>>,
|
|
|
|
{
|
|
|
|
T::collect_and_apply(iter, |xs| self.mk_canonical_var_infos(xs))
|
|
|
|
}
|
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
pub fn mk_place_elems_from_iter<I, T>(self, iter: I) -> T::Output
|
Clarify iterator interners.
There are two traits, `InternAs` and `InternIteratorElement`. I found
them confusing to use, particularly this:
```
pub fn mk_tup<I: InternAs<Ty<'tcx>, Ty<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|ts| self.intern_tup(ts))
}
```
where I thought there might have been two levels of interning going on
(there isn't) due to the `intern_with`/`InternAs` + `intern_tup` naming.
And then I found the actual traits and impls themselves *very*
confusing.
- `InternAs` has a single impl, for iterators, with four type variables.
- `InternAs` is only implemented for iterators because it wouldn't
really make sense to implement for any other type. And you can't
really understand the trait without seeing that single impl, which is
suspicious.
- `InternAs` is basically just a wrapper for `InternIteratorElement`
which does all the actual work.
- Neither trait actually does any interning. They just have `Intern` in
their name because they are used *by* interning code.
- There are no comments.
So this commit improves things.
- It removes `InternAs` completely. This makes the `mk_*` function
signatures slightly more verbose -- two trait bounds instead of one --
but much easier to read, because you only need to understand one trait
instead of two.
- It renames `InternIteratorElement` as `CollectAndApply`. Likewise, it
renames its method `intern_with` as `collect_and_apply`. These names
describe better what's going on: we collect the iterator elements into
a slice and then apply a function to the slice.
- It adds comments, making clear that all this is all there just to
provide an optimized version of `f(&iter.collect::<Vec<_>>())`.
It took me a couple of attempts to come up with this commit. My initial
attempt kept `InternAs` around, but renamed things and added comments,
and I wasn't happy with it. I think this version is much better. The
resulting code is shorter, despite the addition of the comments.
2023-02-16 12:06:36 +11:00
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
T: CollectAndApply<PlaceElem<'tcx>, &'tcx List<PlaceElem<'tcx>>>,
|
|
|
|
{
|
2023-02-17 14:33:08 +11:00
|
|
|
T::collect_and_apply(iter, |xs| self.mk_place_elems(xs))
|
2019-10-20 16:11:04 -04:00
|
|
|
}
|
|
|
|
|
2023-03-11 16:01:25 -08:00
|
|
|
pub fn mk_fields_from_iter<I, T>(self, iter: I) -> T::Output
|
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
T: CollectAndApply<FieldIdx, &'tcx List<FieldIdx>>,
|
|
|
|
{
|
|
|
|
T::collect_and_apply(iter, |xs| self.mk_fields(xs))
|
|
|
|
}
|
|
|
|
|
2023-08-15 20:10:45 +01:00
|
|
|
pub fn mk_offset_of_from_iter<I, T>(self, iter: I) -> T::Output
|
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
2023-08-15 22:32:55 +01:00
|
|
|
T: CollectAndApply<(VariantIdx, FieldIdx), &'tcx List<(VariantIdx, FieldIdx)>>,
|
2023-08-15 20:10:45 +01:00
|
|
|
{
|
|
|
|
T::collect_and_apply(iter, |xs| self.mk_offset_of(xs))
|
|
|
|
}
|
|
|
|
|
2023-07-11 22:35:29 +01:00
|
|
|
pub fn mk_args_trait(
|
2022-11-17 13:00:35 +00:00
|
|
|
self,
|
|
|
|
self_ty: Ty<'tcx>,
|
|
|
|
rest: impl IntoIterator<Item = GenericArg<'tcx>>,
|
2023-07-11 22:35:29 +01:00
|
|
|
) -> GenericArgsRef<'tcx> {
|
|
|
|
self.mk_args_from_iter(iter::once(self_ty.into()).chain(rest))
|
2016-10-24 18:23:29 -06:00
|
|
|
}
|
2017-07-26 21:51:09 -07:00
|
|
|
|
2023-02-17 14:33:08 +11:00
|
|
|
pub fn mk_bound_variable_kinds_from_iter<I, T>(self, iter: I) -> T::Output
|
Clarify iterator interners.
There are two traits, `InternAs` and `InternIteratorElement`. I found
them confusing to use, particularly this:
```
pub fn mk_tup<I: InternAs<Ty<'tcx>, Ty<'tcx>>>(self, iter: I) -> I::Output {
iter.intern_with(|ts| self.intern_tup(ts))
}
```
where I thought there might have been two levels of interning going on
(there isn't) due to the `intern_with`/`InternAs` + `intern_tup` naming.
And then I found the actual traits and impls themselves *very*
confusing.
- `InternAs` has a single impl, for iterators, with four type variables.
- `InternAs` is only implemented for iterators because it wouldn't
really make sense to implement for any other type. And you can't
really understand the trait without seeing that single impl, which is
suspicious.
- `InternAs` is basically just a wrapper for `InternIteratorElement`
which does all the actual work.
- Neither trait actually does any interning. They just have `Intern` in
their name because they are used *by* interning code.
- There are no comments.
So this commit improves things.
- It removes `InternAs` completely. This makes the `mk_*` function
signatures slightly more verbose -- two trait bounds instead of one --
but much easier to read, because you only need to understand one trait
instead of two.
- It renames `InternIteratorElement` as `CollectAndApply`. Likewise, it
renames its method `intern_with` as `collect_and_apply`. These names
describe better what's going on: we collect the iterator elements into
a slice and then apply a function to the slice.
- It adds comments, making clear that all this is all there just to
provide an optimized version of `f(&iter.collect::<Vec<_>>())`.
It took me a couple of attempts to come up with this commit. My initial
attempt kept `InternAs` around, but renamed things and added comments,
and I wasn't happy with it. I think this version is much better. The
resulting code is shorter, despite the addition of the comments.
2023-02-16 12:06:36 +11:00
|
|
|
where
|
|
|
|
I: Iterator<Item = T>,
|
|
|
|
T: CollectAndApply<ty::BoundVariableKind, &'tcx List<ty::BoundVariableKind>>,
|
|
|
|
{
|
2023-02-17 14:33:08 +11:00
|
|
|
T::collect_and_apply(iter, |xs| self.mk_bound_variable_kinds(xs))
|
2020-10-05 20:41:46 -04:00
|
|
|
}
|
|
|
|
|
2024-03-08 12:03:51 +11:00
|
|
|
/// Emit a lint at `span` from a lint struct (some type that implements `LintDiagnostic`,
|
2022-06-29 16:07:46 +01:00
|
|
|
/// typically generated by `#[derive(LintDiagnostic)]`).
|
2023-07-18 15:48:07 +00:00
|
|
|
#[track_caller]
|
2024-01-16 16:27:02 +11:00
|
|
|
pub fn emit_node_span_lint(
|
2022-06-29 16:07:46 +01:00
|
|
|
self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
hir_id: HirId,
|
|
|
|
span: impl Into<MultiSpan>,
|
2024-03-08 12:03:51 +11:00
|
|
|
decorator: impl for<'a> LintDiagnostic<'a, ()>,
|
2022-06-29 16:07:46 +01:00
|
|
|
) {
|
2022-10-29 18:03:47 +08:00
|
|
|
let (level, src) = self.lint_level_at_node(lint, hir_id);
|
2024-05-22 16:46:05 +02:00
|
|
|
lint_level(self.sess, lint, level, src, Some(span.into()), |lint| {
|
|
|
|
decorator.decorate_lint(lint);
|
2022-09-16 11:01:02 +04:00
|
|
|
})
|
2022-06-29 16:07:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-12 14:16:24 +00:00
|
|
|
/// Emit a lint at the appropriate level for a hir node, with an associated span.
|
|
|
|
///
|
2024-01-16 16:42:00 +11:00
|
|
|
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
|
2022-10-29 18:03:47 +08:00
|
|
|
#[rustc_lint_diagnostics]
|
2023-07-18 15:48:07 +00:00
|
|
|
#[track_caller]
|
2024-01-16 16:14:33 +11:00
|
|
|
pub fn node_span_lint(
|
2019-12-24 17:38:22 -05:00
|
|
|
self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
hir_id: HirId,
|
2020-01-05 10:58:44 +01:00
|
|
|
span: impl Into<MultiSpan>,
|
2024-02-23 10:20:45 +11:00
|
|
|
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
|
2020-01-31 22:24:57 +10:00
|
|
|
) {
|
2019-02-26 11:48:34 +01:00
|
|
|
let (level, src) = self.lint_level_at_node(lint, hir_id);
|
2024-05-22 16:46:05 +02:00
|
|
|
lint_level(self.sess, lint, level, src, Some(span.into()), decorate);
|
2017-07-26 21:51:09 -07:00
|
|
|
}
|
|
|
|
|
2024-03-07 22:09:00 +00:00
|
|
|
/// Find the crate root and the appropriate span where `use` and outer attributes can be
|
|
|
|
/// inserted at.
|
|
|
|
pub fn crate_level_attribute_injection_span(self, hir_id: HirId) -> Option<Span> {
|
|
|
|
for (_hir_id, node) in self.hir().parent_iter(hir_id) {
|
|
|
|
if let hir::Node::Crate(m) = node {
|
|
|
|
return Some(m.spans.inject_use_span.shrink_to_lo());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn disabled_nightly_features<E: rustc_errors::EmissionGuarantee>(
|
|
|
|
self,
|
|
|
|
diag: &mut Diag<'_, E>,
|
|
|
|
hir_id: Option<HirId>,
|
|
|
|
features: impl IntoIterator<Item = (String, Symbol)>,
|
|
|
|
) {
|
|
|
|
if !self.sess.is_nightly_build() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let span = hir_id.and_then(|id| self.crate_level_attribute_injection_span(id));
|
|
|
|
for (desc, feature) in features {
|
|
|
|
// FIXME: make this string translatable
|
|
|
|
let msg =
|
|
|
|
format!("add `#![feature({feature})]` to the crate attributes to enable{desc}");
|
|
|
|
if let Some(span) = span {
|
|
|
|
diag.span_suggestion_verbose(
|
|
|
|
span,
|
|
|
|
msg,
|
|
|
|
format!("#![feature({feature})]\n"),
|
2024-05-09 13:18:09 +01:00
|
|
|
Applicability::MaybeIncorrect,
|
2024-03-07 22:09:00 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
diag.help(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-08 12:03:51 +11:00
|
|
|
/// Emit a lint from a lint struct (some type that implements `LintDiagnostic`, typically
|
2022-06-29 16:07:46 +01:00
|
|
|
/// generated by `#[derive(LintDiagnostic)]`).
|
2023-07-18 15:48:07 +00:00
|
|
|
#[track_caller]
|
2024-01-16 16:17:08 +11:00
|
|
|
pub fn emit_node_lint(
|
2022-06-29 16:07:46 +01:00
|
|
|
self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: HirId,
|
2024-03-08 12:03:51 +11:00
|
|
|
decorator: impl for<'a> LintDiagnostic<'a, ()>,
|
2022-06-29 16:07:46 +01:00
|
|
|
) {
|
2024-05-22 16:46:05 +02:00
|
|
|
self.node_lint(lint, id, |lint| {
|
|
|
|
decorator.decorate_lint(lint);
|
2023-12-08 01:52:56 +00:00
|
|
|
})
|
2022-06-29 16:07:46 +01:00
|
|
|
}
|
|
|
|
|
2022-10-12 14:16:24 +00:00
|
|
|
/// Emit a lint at the appropriate level for a hir node.
|
|
|
|
///
|
2024-01-16 16:42:00 +11:00
|
|
|
/// [`lint_level`]: rustc_middle::lint::lint_level#decorate-signature
|
2022-11-07 19:23:29 -05:00
|
|
|
#[rustc_lint_diagnostics]
|
2023-07-18 15:48:07 +00:00
|
|
|
#[track_caller]
|
2024-01-16 15:49:28 +11:00
|
|
|
pub fn node_lint(
|
2019-12-24 17:38:22 -05:00
|
|
|
self,
|
|
|
|
lint: &'static Lint,
|
|
|
|
id: HirId,
|
2024-02-23 10:20:45 +11:00
|
|
|
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
|
2020-01-31 22:24:57 +10:00
|
|
|
) {
|
2017-07-26 21:51:09 -07:00
|
|
|
let (level, src) = self.lint_level_at_node(lint, id);
|
2024-05-22 16:46:05 +02:00
|
|
|
lint_level(self.sess, lint, level, src, None, decorate);
|
2017-07-26 21:51:09 -07:00
|
|
|
}
|
2017-09-08 13:51:57 -07:00
|
|
|
|
2021-05-11 10:38:54 +02:00
|
|
|
pub fn in_scope_traits(self, id: HirId) -> Option<&'tcx [TraitCandidate]> {
|
|
|
|
let map = self.in_scope_traits_map(id.owner)?;
|
|
|
|
let candidates = map.get(&id.local_id)?;
|
2023-04-09 23:07:18 +02:00
|
|
|
Some(candidates)
|
2017-09-08 13:51:57 -07:00
|
|
|
}
|
|
|
|
|
2023-02-06 18:38:52 +00:00
|
|
|
pub fn named_bound_var(self, id: HirId) -> Option<resolve_bound_vars::ResolvedArg> {
|
2021-02-27 21:31:56 -05:00
|
|
|
debug!(?id, "named_region");
|
2024-08-18 16:10:54 +00:00
|
|
|
self.named_variable_map(id.owner).get(&id.local_id).cloned()
|
2017-09-08 13:51:57 -07:00
|
|
|
}
|
|
|
|
|
2022-05-26 08:59:15 +02:00
|
|
|
pub fn is_late_bound(self, id: HirId) -> bool {
|
2023-05-24 14:19:22 +00:00
|
|
|
self.is_late_bound_map(id.owner).is_some_and(|set| set.contains(&id.local_id))
|
2022-05-26 08:59:15 +02:00
|
|
|
}
|
|
|
|
|
2020-10-26 14:18:31 -04:00
|
|
|
pub fn late_bound_vars(self, id: HirId) -> &'tcx List<ty::BoundVariableKind> {
|
2023-02-17 14:33:08 +11:00
|
|
|
self.mk_bound_variable_kinds(
|
2024-08-18 16:10:54 +00:00
|
|
|
&self.late_bound_vars_map(id.owner).get(&id.local_id).cloned().unwrap_or_else(|| {
|
|
|
|
bug!("No bound vars found for {}", self.hir().node_to_string(id))
|
|
|
|
}),
|
2020-10-26 14:18:31 -04:00
|
|
|
)
|
|
|
|
}
|
2021-02-18 21:01:44 +01:00
|
|
|
|
2024-03-06 18:48:13 +00:00
|
|
|
/// Given the def-id of an early-bound lifetime on an opaque corresponding to
|
2023-08-07 23:37:57 +00:00
|
|
|
/// 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
|
2023-11-14 13:13:27 +00:00
|
|
|
/// a late-bound lifetime, this will represent the liberated (`ReLateParam`) lifetime
|
2023-08-07 23:37:57 +00:00
|
|
|
/// 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.
|
2024-03-06 18:48:13 +00:00
|
|
|
pub fn map_opaque_lifetime_to_parent_lifetime(
|
2023-08-07 23:37:57 +00:00
|
|
|
self,
|
2024-03-06 18:48:13 +00:00
|
|
|
mut opaque_lifetime_param_def_id: LocalDefId,
|
2023-08-07 23:37:57 +00:00
|
|
|
) -> ty::Region<'tcx> {
|
|
|
|
debug_assert!(
|
2024-03-06 18:48:13 +00:00
|
|
|
matches!(self.def_kind(opaque_lifetime_param_def_id), DefKind::LifetimeParam),
|
|
|
|
"{opaque_lifetime_param_def_id:?} is a {}",
|
|
|
|
self.def_descr(opaque_lifetime_param_def_id.to_def_id())
|
2023-08-07 23:37:57 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
loop {
|
2024-03-06 18:48:13 +00:00
|
|
|
let parent = self.local_parent(opaque_lifetime_param_def_id);
|
2024-08-20 00:48:43 +00:00
|
|
|
let lifetime_mapping = self.opaque_captured_lifetimes(parent);
|
2023-08-07 23:37:57 +00:00
|
|
|
|
|
|
|
let Some((lifetime, _)) = lifetime_mapping
|
|
|
|
.iter()
|
2024-03-06 18:48:13 +00:00
|
|
|
.find(|(_, duplicated_param)| *duplicated_param == opaque_lifetime_param_def_id)
|
2023-08-07 23:37:57 +00:00
|
|
|
else {
|
|
|
|
bug!("duplicated lifetime param should be present");
|
|
|
|
};
|
|
|
|
|
2024-08-20 00:48:43 +00:00
|
|
|
match *lifetime {
|
|
|
|
resolve_bound_vars::ResolvedArg::EarlyBound(ebv) => {
|
2024-08-22 01:17:01 +00:00
|
|
|
let new_parent = self.local_parent(ebv);
|
2023-08-07 23:37:57 +00:00
|
|
|
|
|
|
|
// 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) {
|
2024-08-22 01:17:01 +00:00
|
|
|
debug_assert_eq!(self.local_parent(parent), new_parent);
|
|
|
|
opaque_lifetime_param_def_id = ebv;
|
2023-08-07 23:37:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let generics = self.generics_of(new_parent);
|
2023-11-14 13:13:27 +00:00
|
|
|
return ty::Region::new_early_param(self, ty::EarlyParamRegion {
|
2023-08-07 23:37:57 +00:00
|
|
|
index: generics
|
2024-08-22 01:17:01 +00:00
|
|
|
.param_def_id_to_index(self, ebv.to_def_id())
|
2023-08-07 23:37:57 +00:00
|
|
|
.expect("early-bound var should be present in fn generics"),
|
2024-08-22 01:17:01 +00:00
|
|
|
name: self.item_name(ebv.to_def_id()),
|
2023-08-07 23:37:57 +00:00
|
|
|
});
|
|
|
|
}
|
2024-08-20 00:48:43 +00:00
|
|
|
resolve_bound_vars::ResolvedArg::LateBound(_, _, lbv) => {
|
2024-08-22 01:17:01 +00:00
|
|
|
let new_parent = self.local_parent(lbv);
|
2023-11-14 13:13:27 +00:00
|
|
|
return ty::Region::new_late_param(
|
2023-08-07 23:37:57 +00:00
|
|
|
self,
|
2024-08-22 01:17:01 +00:00
|
|
|
new_parent.to_def_id(),
|
2024-11-03 22:06:03 +00:00
|
|
|
ty::BoundRegionKind::Named(
|
2024-08-22 01:17:01 +00:00
|
|
|
lbv.to_def_id(),
|
|
|
|
self.item_name(lbv.to_def_id()),
|
2023-08-07 23:37:57 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-08-20 00:48:43 +00:00
|
|
|
resolve_bound_vars::ResolvedArg::Error(guar) => {
|
2023-08-07 23:54:04 +00:00
|
|
|
return ty::Region::new_error(self, guar);
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
return ty::Region::new_error_with_message(
|
|
|
|
self,
|
2024-08-20 00:48:43 +00:00
|
|
|
self.def_span(opaque_lifetime_param_def_id),
|
2023-08-07 23:54:04 +00:00
|
|
|
"cannot resolve lifetime",
|
|
|
|
);
|
|
|
|
}
|
2023-08-07 23:37:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 20:37:35 +02:00
|
|
|
/// Whether `def_id` is a stable const fn (i.e., doesn't need any feature gates to be called).
|
|
|
|
///
|
|
|
|
/// When this is `false`, the function may still be callable as a `const fn` due to features
|
|
|
|
/// being enabled!
|
|
|
|
pub fn is_stable_const_fn(self, def_id: DefId) -> bool {
|
|
|
|
self.is_const_fn(def_id)
|
|
|
|
&& match self.lookup_const_stability(def_id) {
|
|
|
|
None => true, // a fn in a non-staged_api crate
|
|
|
|
Some(stability) if stability.is_const_stable() => true,
|
|
|
|
_ => false,
|
2021-09-15 10:03:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-02-13 05:54:00 -05:00
|
|
|
|
|
|
|
/// Whether the trait impl is marked const. This does not consider stability or feature gates.
|
2024-10-12 20:37:35 +02:00
|
|
|
pub fn is_const_trait_impl(self, def_id: DefId) -> bool {
|
|
|
|
self.def_kind(def_id) == DefKind::Impl { of_trait: true }
|
|
|
|
&& self.constness(def_id) == hir::Constness::Const
|
2022-02-13 05:54:00 -05:00
|
|
|
}
|
2023-02-10 14:29:28 +00:00
|
|
|
|
2024-03-05 04:29:12 +00:00
|
|
|
pub fn intrinsic(self, def_id: impl IntoQueryParam<DefId> + Copy) -> Option<ty::IntrinsicDef> {
|
|
|
|
match self.def_kind(def_id) {
|
|
|
|
DefKind::Fn | DefKind::AssocFn => {}
|
|
|
|
_ => return None,
|
|
|
|
}
|
|
|
|
self.intrinsic_raw(def_id)
|
|
|
|
}
|
|
|
|
|
2023-05-31 01:02:32 +00:00
|
|
|
pub fn next_trait_solver_globally(self) -> bool {
|
2024-09-21 07:02:51 +00:00
|
|
|
self.sess.opts.unstable_opts.next_solver.globally
|
2023-02-10 14:29:28 +00:00
|
|
|
}
|
2023-03-03 18:27:25 -03:00
|
|
|
|
2023-05-31 01:02:40 +00:00
|
|
|
pub fn next_trait_solver_in_coherence(self) -> bool {
|
2024-09-21 07:02:51 +00:00
|
|
|
self.sess.opts.unstable_opts.next_solver.coherence
|
2023-05-31 01:02:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 11:33:51 -03:00
|
|
|
pub fn is_impl_trait_in_trait(self, def_id: DefId) -> bool {
|
2023-06-24 00:00:08 -03:00
|
|
|
self.opt_rpitit_info(def_id).is_some()
|
2023-03-06 11:33:51 -03:00
|
|
|
}
|
2023-04-10 19:07:57 +03:00
|
|
|
|
2023-04-27 18:34:43 +03:00
|
|
|
/// Named module children from all kinds of items, including imports.
|
|
|
|
/// In addition to regular items this list also includes struct and variant constructors, and
|
2023-04-10 19:07:57 +03:00
|
|
|
/// items inside `extern {}` blocks because all of them introduce names into parent module.
|
|
|
|
///
|
|
|
|
/// Module here is understood in name resolution sense - it can be a `mod` item,
|
|
|
|
/// or a crate root, or an enum, or a trait.
|
|
|
|
///
|
2023-04-27 18:34:43 +03:00
|
|
|
/// This is not a query, making it a query causes perf regressions
|
|
|
|
/// (probably due to hashing spans in `ModChild`ren).
|
|
|
|
pub fn module_children_local(self, def_id: LocalDefId) -> &'tcx [ModChild] {
|
|
|
|
self.resolutions(()).module_children.get(&def_id).map_or(&[], |v| &v[..])
|
2023-04-10 19:07:57 +03:00
|
|
|
}
|
2024-02-10 21:26:48 +00:00
|
|
|
|
2024-02-19 17:25:01 +00:00
|
|
|
pub fn resolver_for_lowering(self) -> &'tcx Steal<(ty::ResolverAstLowering, Lrc<ast::Crate>)> {
|
|
|
|
self.resolver_for_lowering_raw(()).0
|
|
|
|
}
|
|
|
|
|
2024-02-10 21:26:48 +00:00
|
|
|
/// Given an `impl_id`, return the trait it implements.
|
|
|
|
/// Return `None` if this is an inherent impl.
|
|
|
|
pub fn impl_trait_ref(
|
|
|
|
self,
|
|
|
|
def_id: impl IntoQueryParam<DefId>,
|
2024-05-26 20:03:47 -04:00
|
|
|
) -> Option<ty::EarlyBinder<'tcx, ty::TraitRef<'tcx>>> {
|
2024-03-05 20:19:05 +01:00
|
|
|
Some(self.impl_trait_header(def_id)?.trait_ref)
|
2024-02-10 21:26:48 +00:00
|
|
|
}
|
2024-02-11 07:11:48 +00:00
|
|
|
|
|
|
|
pub fn impl_polarity(self, def_id: impl IntoQueryParam<DefId>) -> ty::ImplPolarity {
|
2024-03-05 20:19:05 +01:00
|
|
|
self.impl_trait_header(def_id).map_or(ty::ImplPolarity::Positive, |h| h.polarity)
|
2024-02-11 07:11:48 +00:00
|
|
|
}
|
2024-08-09 22:02:20 -04:00
|
|
|
|
2024-09-10 10:54:59 -04:00
|
|
|
pub fn needs_coroutine_by_move_body_def_id(self, def_id: DefId) -> bool {
|
2024-08-01 13:05:17 -04:00
|
|
|
if let Some(hir::CoroutineKind::Desugared(_, hir::CoroutineSource::Closure)) =
|
|
|
|
self.coroutine_kind(def_id)
|
|
|
|
&& let ty::Coroutine(_, args) = self.type_of(def_id).instantiate_identity().kind()
|
|
|
|
&& args.as_coroutine().kind_ty().to_opt_closure_kind() != Some(ty::ClosureKind::FnOnce)
|
|
|
|
{
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-09 22:02:20 -04:00
|
|
|
/// Whether this is a trait implementation that has `#[diagnostic::do_not_recommend]`
|
|
|
|
pub fn do_not_recommend_impl(self, def_id: DefId) -> bool {
|
2024-09-06 18:47:22 +02:00
|
|
|
self.get_diagnostic_attr(def_id, sym::do_not_recommend).is_some()
|
2024-08-09 22:02:20 -04:00
|
|
|
}
|
2015-09-06 21:51:58 +03:00
|
|
|
}
|
2016-10-24 18:23:29 -06:00
|
|
|
|
Introduce deduced parameter attributes, and use them for deducing `readonly` on
indirect immutable freeze by-value function parameters.
Right now, `rustc` only examines function signatures and the platform ABI when
determining the LLVM attributes to apply to parameters. This results in missed
optimizations, because there are some attributes that can be determined via
analysis of the MIR making up the function body. In particular, `readonly`
could be applied to most indirectly-passed by-value function arguments
(specifically, those that are freeze and are observed not to be mutated), but
it currently is not.
This patch introduces the machinery that allows `rustc` to determine those
attributes. It consists of a query, `deduced_param_attrs`, that, when
evaluated, analyzes the MIR of the function to determine supplementary
attributes. The results of this query for each function are written into the
crate metadata so that the deduced parameter attributes can be applied to
cross-crate functions. In this patch, we simply check the parameter for
mutations to determine whether the `readonly` attribute should be applied to
parameters that are indirect immutable freeze by-value. More attributes could
conceivably be deduced in the future: `nocapture` and `noalias` come to mind.
Adding `readonly` to indirect function parameters where applicable enables some
potential optimizations in LLVM that are discussed in [issue 103103] and [PR
103070] around avoiding stack-to-stack memory copies that appear in functions
like `core::fmt::Write::write_fmt` and `core::panicking::assert_failed`. These
functions pass a large structure unchanged by value to a subfunction that also
doesn't mutate it. Since the structure in this case is passed as an indirect
parameter, it's a pointer from LLVM's perspective. As a result, the
intermediate copy of the structure that our codegen emits could be optimized
away by LLVM's MemCpyOptimizer if it knew that the pointer is `readonly
nocapture noalias` in both the caller and callee. We already pass `nocapture
noalias`, but we're missing `readonly`, as we can't determine whether a
by-value parameter is mutated by examining the signature in Rust. I didn't have
much success with having LLVM infer the `readonly` attribute, even with fat
LTO; it seems that deducing it at the MIR level is necessary.
No large benefits should be expected from this optimization *now*; LLVM needs
some changes (discussed in [PR 103070]) to more aggressively use the `noalias
nocapture readonly` combination in its alias analysis. I have some LLVM patches
for these optimizations and have had them looked over. With all the patches
applied locally, I enabled LLVM to remove all the `memcpy`s from the following
code:
```rust
fn main() {
println!("Hello {}", 3);
}
```
which is a significant codegen improvement over the status quo. I expect that
if this optimization kicks in in multiple places even for such a simple
program, then it will apply to Rust code all over the place.
[issue 103103]: https://github.com/rust-lang/rust/issues/103103
[PR 103070]: https://github.com/rust-lang/rust/pull/103070
2022-10-17 19:42:15 -07:00
|
|
|
/// Parameter attributes that can only be determined by examining the body of a function instead
|
|
|
|
/// of just its signature.
|
|
|
|
///
|
|
|
|
/// These can be useful for optimization purposes when a function is directly called. We compute
|
|
|
|
/// them and store them into the crate metadata so that downstream crates can make use of them.
|
|
|
|
///
|
|
|
|
/// Right now, we only have `read_only`, but `no_capture` and `no_alias` might be useful in the
|
|
|
|
/// future.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Debug, Default, TyDecodable, TyEncodable, HashStable)]
|
|
|
|
pub struct DeducedParamAttrs {
|
|
|
|
/// The parameter is marked immutable in the function and contains no `UnsafeCell` (i.e. its
|
|
|
|
/// type is freeze).
|
|
|
|
pub read_only: bool,
|
|
|
|
}
|
|
|
|
|
2023-05-15 06:24:45 +02:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2022-05-31 16:59:28 -07:00
|
|
|
providers.maybe_unused_trait_imports =
|
|
|
|
|tcx, ()| &tcx.resolutions(()).maybe_unused_trait_imports;
|
2021-04-04 14:40:35 +02:00
|
|
|
providers.names_imported_by_glob_use = |tcx, id| {
|
2023-02-21 15:18:10 +01:00
|
|
|
tcx.arena.alloc(UnordSet::from(
|
|
|
|
tcx.resolutions(()).glob_map.get(&id).cloned().unwrap_or_default(),
|
|
|
|
))
|
2021-04-04 14:40:35 +02:00
|
|
|
};
|
2017-08-31 15:08:34 -07:00
|
|
|
|
2021-04-04 14:40:35 +02:00
|
|
|
providers.extern_mod_stmt_cnum =
|
|
|
|
|tcx, id| tcx.resolutions(()).extern_crate_map.get(&id).cloned();
|
2023-03-13 18:54:05 +00:00
|
|
|
providers.is_panic_runtime =
|
2023-03-19 21:32:34 +04:00
|
|
|
|tcx, LocalCrate| attr::contains_name(tcx.hir().krate_attrs(), sym::panic_runtime);
|
2023-03-13 18:54:05 +00:00
|
|
|
providers.is_compiler_builtins =
|
2023-03-19 21:32:34 +04:00
|
|
|
|tcx, LocalCrate| attr::contains_name(tcx.hir().krate_attrs(), sym::compiler_builtins);
|
2023-03-13 22:22:59 +00:00
|
|
|
providers.has_panic_handler = |tcx, LocalCrate| {
|
2019-10-28 17:07:15 -04:00
|
|
|
// We want to check if the panic handler was defined in this crate
|
2023-05-24 14:19:22 +00:00
|
|
|
tcx.lang_items().panic_impl().is_some_and(|did| did.is_local())
|
2019-10-28 17:07:15 -04:00
|
|
|
};
|
2023-02-21 08:37:10 +00:00
|
|
|
providers.source_span = |tcx, def_id| tcx.untracked.source_span.get(def_id).unwrap_or(DUMMY_SP);
|
2017-08-29 11:10:22 -07:00
|
|
|
}
|