2019-02-08 13:53:55 +00:00
|
|
|
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
|
2015-09-14 11:55:56 +00:00
|
|
|
|
2020-04-17 16:55:23 +00:00
|
|
|
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
|
2024-01-08 01:00:31 +00:00
|
|
|
use crate::query::{IntoQueryParam, Providers};
|
2020-03-31 16:16:47 +00:00
|
|
|
use crate::ty::layout::IntegerExt;
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 01:38:15 +00:00
|
|
|
use crate::ty::{
|
2023-02-22 15:51:17 +00:00
|
|
|
self, FallibleTypeFolder, ToPredicate, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
|
|
|
|
TypeVisitableExt,
|
Folding revamp.
This commit makes type folding more like the way chalk does it.
Currently, `TypeFoldable` has `fold_with` and `super_fold_with` methods.
- `fold_with` is the standard entry point, and defaults to calling
`super_fold_with`.
- `super_fold_with` does the actual work of traversing a type.
- For a few types of interest (`Ty`, `Region`, etc.) `fold_with` instead
calls into a `TypeFolder`, which can then call back into
`super_fold_with`.
With the new approach, `TypeFoldable` has `fold_with` and
`TypeSuperFoldable` has `super_fold_with`.
- `fold_with` is still the standard entry point, *and* it does the
actual work of traversing a type, for all types except types of
interest.
- `super_fold_with` is only implemented for the types of interest.
Benefits of the new model.
- I find it easier to understand. The distinction between types of
interest and other types is clearer, and `super_fold_with` doesn't
exist for most types.
- With the current model is easy to get confused and implement a
`super_fold_with` method that should be left defaulted. (Some of the
precursor commits fixed such cases.)
- With the current model it's easy to call `super_fold_with` within
`TypeFolder` impls where `fold_with` should be called. The new
approach makes this mistake impossible, and this commit fixes a number
of such cases.
- It's potentially faster, because it avoids the `fold_with` ->
`super_fold_with` call in all cases except types of interest. A lot of
the time the compile would inline those away, but not necessarily
always.
2022-06-02 01:38:15 +00:00
|
|
|
};
|
2023-07-11 21:35:29 +00:00
|
|
|
use crate::ty::{GenericArgKind, GenericArgsRef};
|
2019-12-11 09:04:34 +00:00
|
|
|
use rustc_apfloat::Float as _;
|
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2023-04-05 00:55:20 +00:00
|
|
|
use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher};
|
2022-01-23 18:34:26 +00:00
|
|
|
use rustc_errors::ErrorGuaranteed;
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir as hir;
|
2021-10-13 19:31:18 +00:00
|
|
|
use rustc_hir::def::{CtorOf, DefKind, Res};
|
2023-05-01 19:05:20 +00:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
|
2022-03-23 08:41:31 +00:00
|
|
|
use rustc_index::bit_set::GrowableBitSet;
|
2018-12-03 00:14:35 +00:00
|
|
|
use rustc_macros::HashStable;
|
2023-04-02 10:50:01 +00:00
|
|
|
use rustc_session::Limit;
|
|
|
|
use rustc_span::sym;
|
2023-10-12 21:29:16 +00:00
|
|
|
use rustc_target::abi::{Integer, IntegerType, Primitive, Size};
|
2022-05-13 13:50:21 +00:00
|
|
|
use rustc_target::spec::abi::Abi;
|
2020-01-30 20:28:16 +00:00
|
|
|
use smallvec::SmallVec;
|
2021-04-27 13:01:37 +00:00
|
|
|
use std::{fmt, iter};
|
2015-09-14 11:55:56 +00:00
|
|
|
|
2018-01-25 15:44:45 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct Discr<'tcx> {
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Bit representation of the discriminant (e.g., `-128i8` is `0xFF_u128`).
|
2018-01-25 15:44:45 +00:00
|
|
|
pub val: u128,
|
|
|
|
pub ty: Ty<'tcx>,
|
2017-03-13 00:12:13 +00:00
|
|
|
}
|
2017-02-05 05:01:48 +00:00
|
|
|
|
2022-04-04 08:56:59 +00:00
|
|
|
/// Used as an input to [`TyCtxt::uses_unique_generic_params`].
|
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2023-04-25 07:36:58 +00:00
|
|
|
pub enum CheckRegions {
|
2022-04-04 08:56:59 +00:00
|
|
|
No,
|
2023-11-14 13:13:27 +00:00
|
|
|
/// Only permit parameter regions. This should be used
|
|
|
|
/// for everything apart from functions, which may use
|
|
|
|
/// `ReBound` to represent late-bound regions.
|
|
|
|
OnlyParam,
|
|
|
|
/// Check region parameters from a function definition.
|
|
|
|
/// Allows `ReEarlyParam` and `ReBound` to handle early
|
|
|
|
/// and late-bound region parameters.
|
|
|
|
FromFunction,
|
2022-04-04 08:56:59 +00:00
|
|
|
}
|
|
|
|
|
2022-03-23 08:41:31 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum NotUniqueParam<'tcx> {
|
|
|
|
DuplicateParam(ty::GenericArg<'tcx>),
|
|
|
|
NotParam(ty::GenericArg<'tcx>),
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:44:45 +00:00
|
|
|
impl<'tcx> fmt::Display for Discr<'tcx> {
|
2018-08-30 05:02:42 +00:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2020-08-02 22:49:11 +00:00
|
|
|
match *self.ty.kind() {
|
2018-08-22 00:35:55 +00:00
|
|
|
ty::Int(ity) => {
|
2020-12-12 14:28:49 +00:00
|
|
|
let size = ty::tls::with(|tcx| Integer::from_int_ty(&tcx, ity).size());
|
2019-02-26 08:54:57 +00:00
|
|
|
let x = self.val;
|
2018-03-22 11:38:40 +00:00
|
|
|
// sign extend the raw representation to be an i128
|
2020-11-04 13:41:58 +00:00
|
|
|
let x = size.sign_extend(x) as i128;
|
2023-07-25 20:00:13 +00:00
|
|
|
write!(fmt, "{x}")
|
2018-03-22 11:38:40 +00:00
|
|
|
}
|
|
|
|
_ => write!(fmt, "{}", self.val),
|
2018-01-25 15:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
|
2018-01-25 15:44:45 +00:00
|
|
|
impl<'tcx> Discr<'tcx> {
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Adds `1` to the value and wraps around if the maximum for the type is reached.
|
2019-06-13 21:48:52 +00:00
|
|
|
pub fn wrap_incr(self, tcx: TyCtxt<'tcx>) -> Self {
|
2018-01-25 15:44:45 +00:00
|
|
|
self.checked_add(tcx, 1).0
|
|
|
|
}
|
2019-06-13 21:48:52 +00:00
|
|
|
pub fn checked_add(self, tcx: TyCtxt<'tcx>, n: u128) -> (Self, bool) {
|
2023-02-13 21:08:15 +00:00
|
|
|
let (size, signed) = self.ty.int_size_and_signed(tcx);
|
2019-12-11 09:04:34 +00:00
|
|
|
let (val, oflo) = if signed {
|
2021-09-07 18:44:33 +00:00
|
|
|
let min = size.signed_int_min();
|
|
|
|
let max = size.signed_int_max();
|
2020-11-04 13:41:58 +00:00
|
|
|
let val = size.sign_extend(self.val) as i128;
|
2020-03-04 12:18:08 +00:00
|
|
|
assert!(n < (i128::MAX as u128));
|
2018-01-25 15:44:45 +00:00
|
|
|
let n = n as i128;
|
|
|
|
let oflo = val > max - n;
|
2018-01-26 14:19:01 +00:00
|
|
|
let val = if oflo { min + (n - (max - val) - 1) } else { val + n };
|
2018-03-22 11:38:40 +00:00
|
|
|
// zero the upper bits
|
|
|
|
let val = val as u128;
|
2020-11-04 13:41:58 +00:00
|
|
|
let val = size.truncate(val);
|
2019-12-11 09:04:34 +00:00
|
|
|
(val, oflo)
|
2018-01-25 15:44:45 +00:00
|
|
|
} else {
|
2021-09-07 18:44:33 +00:00
|
|
|
let max = size.unsigned_int_max();
|
2018-01-26 12:37:46 +00:00
|
|
|
let val = self.val;
|
|
|
|
let oflo = val > max - n;
|
2018-03-22 11:38:40 +00:00
|
|
|
let val = if oflo { n - (max - val) - 1 } else { val + n };
|
2019-12-11 09:04:34 +00:00
|
|
|
(val, oflo)
|
|
|
|
};
|
|
|
|
(Self { val, ty: self.ty }, oflo)
|
2017-02-15 13:00:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 15:44:45 +00:00
|
|
|
pub trait IntTypeExt {
|
2019-06-13 21:48:52 +00:00
|
|
|
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx>;
|
2019-06-13 22:32:15 +00:00
|
|
|
fn disr_incr<'tcx>(&self, tcx: TyCtxt<'tcx>, val: Option<Discr<'tcx>>) -> Option<Discr<'tcx>>;
|
2019-06-13 21:48:52 +00:00
|
|
|
fn initial_discriminant<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Discr<'tcx>;
|
2018-01-25 15:44:45 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 16:20:30 +00:00
|
|
|
impl IntTypeExt for IntegerType {
|
2019-06-13 21:48:52 +00:00
|
|
|
fn to_ty<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
|
2022-11-01 16:20:30 +00:00
|
|
|
match self {
|
|
|
|
IntegerType::Pointer(true) => tcx.types.isize,
|
|
|
|
IntegerType::Pointer(false) => tcx.types.usize,
|
|
|
|
IntegerType::Fixed(i, s) => i.to_ty(tcx, *s),
|
2017-02-05 05:01:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
fn initial_discriminant<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Discr<'tcx> {
|
2018-01-25 15:44:45 +00:00
|
|
|
Discr { val: 0, ty: self.to_ty(tcx) }
|
2017-02-05 05:01:48 +00:00
|
|
|
}
|
|
|
|
|
2019-06-13 22:32:15 +00:00
|
|
|
fn disr_incr<'tcx>(&self, tcx: TyCtxt<'tcx>, val: Option<Discr<'tcx>>) -> Option<Discr<'tcx>> {
|
2017-02-05 05:01:48 +00:00
|
|
|
if let Some(val) = val {
|
2018-01-25 15:44:45 +00:00
|
|
|
assert_eq!(self.to_ty(tcx), val.ty);
|
|
|
|
let (new, oflo) = val.checked_add(tcx, 1);
|
|
|
|
if oflo { None } else { Some(new) }
|
2017-02-05 05:01:48 +00:00
|
|
|
} else {
|
|
|
|
Some(self.initial_discriminant(tcx))
|
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:48:52 +00:00
|
|
|
impl<'tcx> TyCtxt<'tcx> {
|
2017-04-05 21:39:02 +00:00
|
|
|
/// Creates a hash of the type `Ty` which will be the same no matter what crate
|
|
|
|
/// context it's calculated within. This is used by the `type_id` intrinsic.
|
2023-04-05 00:55:20 +00:00
|
|
|
pub fn type_id_hash(self, ty: Ty<'tcx>) -> Hash128 {
|
2017-08-08 16:11:39 +00:00
|
|
|
// We want the type_id be independent of the types free regions, so we
|
|
|
|
// erase them. The erase_regions() call will also anonymize bound
|
|
|
|
// regions, which is desirable too.
|
2020-10-24 00:21:18 +00:00
|
|
|
let ty = self.erase_regions(ty);
|
2017-08-08 16:11:39 +00:00
|
|
|
|
2021-07-12 20:19:25 +00:00
|
|
|
self.with_stable_hashing_context(|mut hcx| {
|
|
|
|
let mut hasher = StableHasher::new();
|
|
|
|
hcx.while_hashing_spans(false, |hcx| ty.hash_stable(hcx, &mut hasher));
|
|
|
|
hasher.finish()
|
|
|
|
})
|
2017-04-05 21:39:02 +00:00
|
|
|
}
|
|
|
|
|
2021-10-13 19:31:18 +00:00
|
|
|
pub fn res_generics_def_id(self, res: Res) -> Option<DefId> {
|
|
|
|
match res {
|
|
|
|
Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id) => {
|
2022-04-25 19:08:45 +00:00
|
|
|
Some(self.parent(self.parent(def_id)))
|
2021-10-13 19:31:18 +00:00
|
|
|
}
|
|
|
|
Res::Def(DefKind::Variant | DefKind::Ctor(CtorOf::Struct, _), def_id) => {
|
2022-04-25 19:08:45 +00:00
|
|
|
Some(self.parent(def_id))
|
2021-10-13 19:31:18 +00:00
|
|
|
}
|
|
|
|
// Other `DefKind`s don't have generics and would ICE when calling
|
|
|
|
// `generics_of`.
|
|
|
|
Res::Def(
|
|
|
|
DefKind::Struct
|
|
|
|
| DefKind::Union
|
|
|
|
| DefKind::Enum
|
|
|
|
| DefKind::Trait
|
|
|
|
| DefKind::OpaqueTy
|
2023-09-26 02:15:32 +00:00
|
|
|
| DefKind::TyAlias
|
2021-10-13 19:31:18 +00:00
|
|
|
| DefKind::ForeignTy
|
|
|
|
| DefKind::TraitAlias
|
|
|
|
| DefKind::AssocTy
|
|
|
|
| DefKind::Fn
|
|
|
|
| DefKind::AssocFn
|
|
|
|
| DefKind::AssocConst
|
2023-02-12 18:26:47 +00:00
|
|
|
| DefKind::Impl { .. },
|
2021-10-13 19:31:18 +00:00
|
|
|
def_id,
|
|
|
|
) => Some(def_id),
|
|
|
|
Res::Err => None,
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-11 11:27:41 +00:00
|
|
|
/// Attempts to returns the deeply last field of nested structures, but
|
|
|
|
/// does not apply any normalization in its search. Returns the same type
|
|
|
|
/// if input `ty` is not a structure at all.
|
|
|
|
pub fn struct_tail_without_normalization(self, ty: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
let tcx = self;
|
2022-04-26 08:56:04 +00:00
|
|
|
tcx.struct_tail_with_normalize(ty, |ty| ty, || {})
|
2019-07-11 11:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the deeply last field of nested structures, or the same type if
|
|
|
|
/// not a structure at all. Corresponds to the only possible unsized field,
|
|
|
|
/// and its type can be used to determine unsizing strategy.
|
2019-07-12 09:34:23 +00:00
|
|
|
///
|
|
|
|
/// Should only be called if `ty` has no inference variables and does not
|
|
|
|
/// need its lifetimes preserved (e.g. as part of codegen); otherwise
|
|
|
|
/// normalization attempt may cause compiler bugs.
|
2019-07-11 11:27:41 +00:00
|
|
|
pub fn struct_tail_erasing_lifetimes(
|
|
|
|
self,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
) -> Ty<'tcx> {
|
|
|
|
let tcx = self;
|
2022-04-26 08:56:04 +00:00
|
|
|
tcx.struct_tail_with_normalize(ty, |ty| tcx.normalize_erasing_regions(param_env, ty), || {})
|
2019-07-11 11:27:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the deeply last field of nested structures, or the same type if
|
|
|
|
/// not a structure at all. Corresponds to the only possible unsized field,
|
|
|
|
/// and its type can be used to determine unsizing strategy.
|
|
|
|
///
|
|
|
|
/// This is parameterized over the normalization strategy (i.e. how to
|
|
|
|
/// handle `<T as Trait>::Assoc` and `impl Trait`); pass the identity
|
|
|
|
/// function to indicate no normalization should take place.
|
|
|
|
///
|
2019-07-12 09:34:23 +00:00
|
|
|
/// See also `struct_tail_erasing_lifetimes`, which is suitable for use
|
|
|
|
/// during codegen.
|
2019-07-11 11:27:41 +00:00
|
|
|
pub fn struct_tail_with_normalize(
|
|
|
|
self,
|
|
|
|
mut ty: Ty<'tcx>,
|
2021-12-24 09:41:18 +00:00
|
|
|
mut normalize: impl FnMut(Ty<'tcx>) -> Ty<'tcx>,
|
2022-04-27 09:11:54 +00:00
|
|
|
// This is currently used to allow us to walk a ValTree
|
2022-04-26 08:56:04 +00:00
|
|
|
// in lockstep with the type in order to get the ValTree branch that
|
|
|
|
// corresponds to an unsized field.
|
|
|
|
mut f: impl FnMut() -> (),
|
2019-07-11 11:27:41 +00:00
|
|
|
) -> Ty<'tcx> {
|
2021-07-04 18:02:51 +00:00
|
|
|
let recursion_limit = self.recursion_limit();
|
2020-11-26 15:32:41 +00:00
|
|
|
for iteration in 0.. {
|
2021-06-25 23:48:26 +00:00
|
|
|
if !recursion_limit.value_within_limit(iteration) {
|
2023-04-02 10:50:01 +00:00
|
|
|
let suggested_limit = match recursion_limit {
|
|
|
|
Limit(0) => Limit(2),
|
|
|
|
limit => limit * 2,
|
|
|
|
};
|
2023-12-18 11:21:37 +00:00
|
|
|
let reported = self
|
|
|
|
.dcx()
|
|
|
|
.emit_err(crate::error::RecursionLimitReached { ty, suggested_limit });
|
2023-07-05 19:13:26 +00:00
|
|
|
return Ty::new_error(self, reported);
|
2020-11-26 15:32:41 +00:00
|
|
|
}
|
2020-08-02 22:49:11 +00:00
|
|
|
match *ty.kind() {
|
2023-07-11 21:35:29 +00:00
|
|
|
ty::Adt(def, args) => {
|
2017-05-26 20:36:40 +00:00
|
|
|
if !def.is_struct() {
|
|
|
|
break;
|
|
|
|
}
|
2023-07-05 18:44:24 +00:00
|
|
|
match def.non_enum_variant().tail_opt() {
|
2022-04-26 08:56:04 +00:00
|
|
|
Some(field) => {
|
|
|
|
f();
|
2023-07-11 21:35:29 +00:00
|
|
|
ty = field.ty(self, args);
|
2022-04-26 08:56:04 +00:00
|
|
|
}
|
2017-05-26 20:36:40 +00:00
|
|
|
None => break,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 15:29:49 +00:00
|
|
|
ty::Tuple(tys) if let Some((&last_ty, _)) = tys.split_last() => {
|
2022-04-26 08:56:04 +00:00
|
|
|
f();
|
2022-02-07 15:06:31 +00:00
|
|
|
ty = last_ty;
|
2017-05-26 20:36:40 +00:00
|
|
|
}
|
|
|
|
|
2021-08-16 15:29:49 +00:00
|
|
|
ty::Tuple(_) => break,
|
|
|
|
|
2022-11-27 17:52:17 +00:00
|
|
|
ty::Alias(..) => {
|
2019-07-11 11:27:41 +00:00
|
|
|
let normalized = normalize(ty);
|
|
|
|
if ty == normalized {
|
|
|
|
return ty;
|
|
|
|
} else {
|
|
|
|
ty = normalized;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 20:36:40 +00:00
|
|
|
_ => {
|
|
|
|
break;
|
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
|
2019-05-17 01:20:14 +00:00
|
|
|
/// Same as applying `struct_tail` on `source` and `target`, but only
|
2015-09-14 11:55:56 +00:00
|
|
|
/// keeps going as long as the two types are instances of the same
|
|
|
|
/// structure definitions.
|
2018-11-20 14:34:15 +00:00
|
|
|
/// For `(Foo<Foo<T>>, Foo<dyn Trait>)`, the result will be `(Foo<T>, Trait)`,
|
2015-09-14 11:55:56 +00:00
|
|
|
/// whereas struct_tail produces `T`, and `Trait`, respectively.
|
2019-07-11 11:27:41 +00:00
|
|
|
///
|
2019-07-12 09:34:23 +00:00
|
|
|
/// Should only be called if the types have no inference variables and do
|
2019-05-17 01:20:14 +00:00
|
|
|
/// not need their lifetimes preserved (e.g., as part of codegen); otherwise,
|
2019-07-11 11:27:41 +00:00
|
|
|
/// normalization attempt may cause compiler bugs.
|
|
|
|
pub fn struct_lockstep_tails_erasing_lifetimes(
|
|
|
|
self,
|
|
|
|
source: Ty<'tcx>,
|
|
|
|
target: Ty<'tcx>,
|
|
|
|
param_env: ty::ParamEnv<'tcx>,
|
|
|
|
) -> (Ty<'tcx>, Ty<'tcx>) {
|
|
|
|
let tcx = self;
|
|
|
|
tcx.struct_lockstep_tails_with_normalize(source, target, |ty| {
|
|
|
|
tcx.normalize_erasing_regions(param_env, ty)
|
2019-12-22 22:42:04 +00:00
|
|
|
})
|
2019-07-11 11:27:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 01:20:14 +00:00
|
|
|
/// Same as applying `struct_tail` on `source` and `target`, but only
|
2019-07-11 11:27:41 +00:00
|
|
|
/// keeps going as long as the two types are instances of the same
|
|
|
|
/// structure definitions.
|
|
|
|
/// For `(Foo<Foo<T>>, Foo<dyn Trait>)`, the result will be `(Foo<T>, Trait)`,
|
|
|
|
/// whereas struct_tail produces `T`, and `Trait`, respectively.
|
|
|
|
///
|
2019-07-12 09:34:23 +00:00
|
|
|
/// See also `struct_lockstep_tails_erasing_lifetimes`, which is suitable for use
|
|
|
|
/// during codegen.
|
2019-07-11 11:27:41 +00:00
|
|
|
pub fn struct_lockstep_tails_with_normalize(
|
|
|
|
self,
|
|
|
|
source: Ty<'tcx>,
|
|
|
|
target: Ty<'tcx>,
|
|
|
|
normalize: impl Fn(Ty<'tcx>) -> Ty<'tcx>,
|
|
|
|
) -> (Ty<'tcx>, Ty<'tcx>) {
|
2015-09-14 11:55:56 +00:00
|
|
|
let (mut a, mut b) = (source, target);
|
2017-06-08 05:49:54 +00:00
|
|
|
loop {
|
2020-08-02 22:49:11 +00:00
|
|
|
match (&a.kind(), &b.kind()) {
|
2023-07-11 21:35:29 +00:00
|
|
|
(&ty::Adt(a_def, a_args), &ty::Adt(b_def, b_args))
|
2017-06-08 05:49:54 +00:00
|
|
|
if a_def == b_def && a_def.is_struct() =>
|
|
|
|
{
|
2023-07-05 18:44:24 +00:00
|
|
|
if let Some(f) = a_def.non_enum_variant().tail_opt() {
|
2023-07-11 21:35:29 +00:00
|
|
|
a = f.ty(self, a_args);
|
|
|
|
b = f.ty(self, b_args);
|
2017-06-08 05:49:54 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-03-23 08:41:31 +00:00
|
|
|
(&ty::Tuple(a_tys), &ty::Tuple(b_tys)) if a_tys.len() == b_tys.len() => {
|
2022-02-07 15:06:31 +00:00
|
|
|
if let Some(&a_last) = a_tys.last() {
|
|
|
|
a = a_last;
|
|
|
|
b = *b_tys.last().unwrap();
|
2017-06-08 05:49:54 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2022-11-27 17:52:17 +00:00
|
|
|
(ty::Alias(..), _) | (_, ty::Alias(..)) => {
|
2019-07-11 11:27:41 +00:00
|
|
|
// If either side is a projection, attempt to
|
|
|
|
// progress via normalization. (Should be safe to
|
|
|
|
// apply to both sides as normalization is
|
|
|
|
// idempotent.)
|
|
|
|
let a_norm = normalize(a);
|
|
|
|
let b_norm = normalize(b);
|
|
|
|
if a == a_norm && b == b_norm {
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
a = a_norm;
|
|
|
|
b = b_norm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-13 00:12:13 +00:00
|
|
|
_ => break,
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
(a, b)
|
|
|
|
}
|
|
|
|
|
2017-03-01 16:42:26 +00:00
|
|
|
/// Calculate the destructor of a given type.
|
|
|
|
pub fn calculate_dtor(
|
|
|
|
self,
|
|
|
|
adt_did: DefId,
|
2022-01-23 18:34:26 +00:00
|
|
|
validate: impl Fn(Self, DefId) -> Result<(), ErrorGuaranteed>,
|
2017-03-01 16:42:26 +00:00
|
|
|
) -> Option<ty::Destructor> {
|
2020-03-05 20:50:44 +00:00
|
|
|
let drop_trait = self.lang_items().drop_trait()?;
|
2024-01-23 15:23:22 +00:00
|
|
|
self.ensure().coherent_trait(drop_trait).ok()?;
|
2017-03-01 16:42:26 +00:00
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
let ty = self.type_of(adt_did).instantiate_identity();
|
2023-04-19 01:41:01 +00:00
|
|
|
let mut dtor_candidate = None;
|
|
|
|
self.for_each_relevant_impl(drop_trait, ty, |impl_did| {
|
|
|
|
if validate(self, impl_did).is_err() {
|
|
|
|
// Already `ErrorGuaranteed`, no need to delay a span bug here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-26 18:22:32 +00:00
|
|
|
let Some(item_id) = self.associated_item_def_ids(impl_did).first() else {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.dcx()
|
2023-11-30 04:01:11 +00:00
|
|
|
.span_delayed_bug(self.def_span(impl_did), "Drop impl without drop function");
|
2023-04-26 18:22:32 +00:00
|
|
|
return;
|
|
|
|
};
|
|
|
|
|
2023-04-19 01:41:01 +00:00
|
|
|
if let Some((old_item_id, _)) = dtor_candidate {
|
2023-12-18 11:21:37 +00:00
|
|
|
self.dcx()
|
2023-04-19 01:41:01 +00:00
|
|
|
.struct_span_err(self.def_span(item_id), "multiple drop impls found")
|
2024-01-08 22:08:49 +00:00
|
|
|
.with_span_note(self.def_span(old_item_id), "other impl here")
|
2023-04-19 01:41:01 +00:00
|
|
|
.delay_as_bug();
|
|
|
|
}
|
|
|
|
|
|
|
|
dtor_candidate = Some((*item_id, self.constness(impl_did)));
|
|
|
|
});
|
2017-03-01 16:42:26 +00:00
|
|
|
|
2023-04-19 01:41:01 +00:00
|
|
|
let (did, constness) = dtor_candidate?;
|
2021-09-01 11:06:15 +00:00
|
|
|
Some(ty::Destructor { did, constness })
|
2017-04-23 14:43:23 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns the set of types that are required to be alive in
|
2017-04-23 14:43:23 +00:00
|
|
|
/// order to run the destructor of `def` (see RFCs 769 and
|
|
|
|
/// 1238).
|
|
|
|
///
|
|
|
|
/// Note that this returns only the constraints for the
|
|
|
|
/// destructor of `def` itself. For the destructors of the
|
|
|
|
/// contents, you need `adt_dtorck_constraint`.
|
2023-07-11 21:35:29 +00:00
|
|
|
pub fn destructor_constraints(self, def: ty::AdtDef<'tcx>) -> Vec<ty::GenericArg<'tcx>> {
|
2017-04-23 14:43:23 +00:00
|
|
|
let dtor = match def.destructor(self) {
|
|
|
|
None => {
|
2022-03-04 20:28:41 +00:00
|
|
|
debug!("destructor_constraints({:?}) - no dtor", def.did());
|
2017-04-23 14:43:23 +00:00
|
|
|
return vec![];
|
|
|
|
}
|
|
|
|
Some(dtor) => dtor.did,
|
|
|
|
};
|
|
|
|
|
2022-03-12 23:52:25 +00:00
|
|
|
let impl_def_id = self.parent(dtor);
|
2017-04-24 12:20:46 +00:00
|
|
|
let impl_generics = self.generics_of(impl_def_id);
|
2017-04-23 14:43:23 +00:00
|
|
|
|
|
|
|
// We have a destructor - all the parameters that are not
|
|
|
|
// pure_wrt_drop (i.e, don't have a #[may_dangle] attribute)
|
|
|
|
// must be live.
|
|
|
|
|
|
|
|
// We need to return the list of parameters from the ADTs
|
2023-07-11 21:35:29 +00:00
|
|
|
// generics/args that correspond to impure parameters on the
|
2017-04-23 14:43:23 +00:00
|
|
|
// impl's generics. This is a bit ugly, but conceptually simple:
|
|
|
|
//
|
|
|
|
// Suppose our ADT looks like the following
|
|
|
|
//
|
|
|
|
// struct S<X, Y, Z>(X, Y, Z);
|
|
|
|
//
|
|
|
|
// and the impl is
|
|
|
|
//
|
|
|
|
// impl<#[may_dangle] P0, P1, P2> Drop for S<P1, P2, P0>
|
|
|
|
//
|
|
|
|
// We want to return the parameters (X, Y). For that, we match
|
2023-07-11 21:35:29 +00:00
|
|
|
// up the item-args <X, Y, Z> with the args on the impl ADT,
|
|
|
|
// <P1, P2, P0>, and then look up which of the impl args refer to
|
2017-04-23 14:43:23 +00:00
|
|
|
// parameters marked as pure.
|
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
let impl_args = match *self.type_of(impl_def_id).instantiate_identity().kind() {
|
|
|
|
ty::Adt(def_, args) if def_ == def => args,
|
2023-12-15 03:34:37 +00:00
|
|
|
_ => span_bug!(self.def_span(impl_def_id), "expected ADT for self type of `Drop` impl"),
|
2017-04-23 14:43:23 +00:00
|
|
|
};
|
|
|
|
|
2023-12-15 03:34:37 +00:00
|
|
|
let item_args = ty::GenericArgs::identity_for_item(self, def.did());
|
2017-04-23 14:43:23 +00:00
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
let result = iter::zip(item_args, impl_args)
|
2020-05-23 09:49:24 +00:00
|
|
|
.filter(|&(_, k)| {
|
2018-02-23 01:13:54 +00:00
|
|
|
match k.unpack() {
|
2022-04-08 15:57:44 +00:00
|
|
|
GenericArgKind::Lifetime(region) => match region.kind() {
|
2023-11-14 13:13:27 +00:00
|
|
|
ty::ReEarlyParam(ref ebr) => {
|
2022-04-08 15:57:44 +00:00
|
|
|
!impl_generics.region_param(ebr, self).pure_wrt_drop
|
|
|
|
}
|
|
|
|
// Error: not a region param
|
|
|
|
_ => false,
|
|
|
|
},
|
|
|
|
GenericArgKind::Type(ty) => match ty.kind() {
|
|
|
|
ty::Param(ref pt) => !impl_generics.type_param(pt, self).pure_wrt_drop,
|
|
|
|
// Error: not a type param
|
|
|
|
_ => false,
|
|
|
|
},
|
2022-06-10 01:18:06 +00:00
|
|
|
GenericArgKind::Const(ct) => match ct.kind() {
|
2022-04-08 15:57:44 +00:00
|
|
|
ty::ConstKind::Param(ref pc) => {
|
|
|
|
!impl_generics.const_param(pc, self).pure_wrt_drop
|
|
|
|
}
|
|
|
|
// Error: not a const param
|
|
|
|
_ => false,
|
|
|
|
},
|
2017-04-23 14:43:23 +00:00
|
|
|
}
|
2018-10-02 08:52:43 +00:00
|
|
|
})
|
2020-05-23 09:49:24 +00:00
|
|
|
.map(|(item_param, _)| item_param)
|
2018-10-02 08:52:43 +00:00
|
|
|
.collect();
|
2022-03-04 20:28:41 +00:00
|
|
|
debug!("destructor_constraint({:?}) = {:?}", def.did(), result);
|
2017-04-23 14:43:23 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2022-03-23 08:41:31 +00:00
|
|
|
/// Checks whether each generic argument is simply a unique generic parameter.
|
|
|
|
pub fn uses_unique_generic_params(
|
|
|
|
self,
|
2023-10-29 21:45:01 +00:00
|
|
|
args: &[ty::GenericArg<'tcx>],
|
2023-04-25 07:36:58 +00:00
|
|
|
ignore_regions: CheckRegions,
|
2022-03-23 08:41:31 +00:00
|
|
|
) -> Result<(), NotUniqueParam<'tcx>> {
|
|
|
|
let mut seen = GrowableBitSet::default();
|
2023-04-25 08:07:44 +00:00
|
|
|
let mut seen_late = FxHashSet::default();
|
2023-07-11 21:35:29 +00:00
|
|
|
for arg in args {
|
2022-03-23 08:41:31 +00:00
|
|
|
match arg.unpack() {
|
2023-04-25 07:42:09 +00:00
|
|
|
GenericArgKind::Lifetime(lt) => match (ignore_regions, lt.kind()) {
|
2023-11-14 13:13:27 +00:00
|
|
|
(CheckRegions::FromFunction, ty::ReBound(di, reg)) => {
|
2023-04-25 08:07:44 +00:00
|
|
|
if !seen_late.insert((di, reg)) {
|
|
|
|
return Err(NotUniqueParam::DuplicateParam(lt.into()));
|
|
|
|
}
|
|
|
|
}
|
2023-11-14 13:13:27 +00:00
|
|
|
(CheckRegions::OnlyParam | CheckRegions::FromFunction, ty::ReEarlyParam(p)) => {
|
2022-04-04 08:56:59 +00:00
|
|
|
if !seen.insert(p.index) {
|
|
|
|
return Err(NotUniqueParam::DuplicateParam(lt.into()));
|
2022-03-23 08:41:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-14 13:13:27 +00:00
|
|
|
(CheckRegions::OnlyParam | CheckRegions::FromFunction, _) => {
|
2023-04-25 07:42:09 +00:00
|
|
|
return Err(NotUniqueParam::NotParam(lt.into()));
|
|
|
|
}
|
|
|
|
(CheckRegions::No, _) => {}
|
|
|
|
},
|
2022-03-23 08:41:31 +00:00
|
|
|
GenericArgKind::Type(t) => match t.kind() {
|
|
|
|
ty::Param(p) => {
|
|
|
|
if !seen.insert(p.index) {
|
|
|
|
return Err(NotUniqueParam::DuplicateParam(t.into()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(NotUniqueParam::NotParam(t.into())),
|
|
|
|
},
|
2022-06-10 01:18:06 +00:00
|
|
|
GenericArgKind::Const(c) => match c.kind() {
|
2022-03-23 08:41:31 +00:00
|
|
|
ty::ConstKind::Param(p) => {
|
|
|
|
if !seen.insert(p.index) {
|
|
|
|
return Err(NotUniqueParam::DuplicateParam(c.into()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(NotUniqueParam::NotParam(c.into())),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-05-17 20:45:14 +00:00
|
|
|
/// Checks whether each generic argument is simply a unique generic placeholder.
|
|
|
|
///
|
|
|
|
/// This is used in the new solver, which canonicalizes params to placeholders
|
|
|
|
/// for better caching.
|
|
|
|
pub fn uses_unique_placeholders_ignoring_regions(
|
|
|
|
self,
|
2023-07-11 21:35:29 +00:00
|
|
|
args: GenericArgsRef<'tcx>,
|
2023-05-17 20:45:14 +00:00
|
|
|
) -> Result<(), NotUniqueParam<'tcx>> {
|
|
|
|
let mut seen = GrowableBitSet::default();
|
2023-07-11 21:35:29 +00:00
|
|
|
for arg in args {
|
2023-05-17 20:45:14 +00:00
|
|
|
match arg.unpack() {
|
|
|
|
// Ignore regions, since we can't resolve those in a canonicalized
|
|
|
|
// query in the trait solver.
|
|
|
|
GenericArgKind::Lifetime(_) => {}
|
|
|
|
GenericArgKind::Type(t) => match t.kind() {
|
|
|
|
ty::Placeholder(p) => {
|
|
|
|
if !seen.insert(p.bound.var) {
|
|
|
|
return Err(NotUniqueParam::DuplicateParam(t.into()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(NotUniqueParam::NotParam(t.into())),
|
|
|
|
},
|
|
|
|
GenericArgKind::Const(c) => match c.kind() {
|
|
|
|
ty::ConstKind::Placeholder(p) => {
|
|
|
|
if !seen.insert(p.bound) {
|
|
|
|
return Err(NotUniqueParam::DuplicateParam(c.into()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return Err(NotUniqueParam::NotParam(c.into())),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns `true` if `def_id` refers to a closure (e.g., `|x| x * 2`). Note
|
|
|
|
/// that closures have a `DefId`, but the closure *expression* also
|
2018-07-02 14:34:19 +00:00
|
|
|
/// has a `HirId` that is located within the context where the
|
|
|
|
/// closure appears (and, sadly, a corresponding `NodeId`, since
|
|
|
|
/// those are not yet phased out). The parent of the closure's
|
2019-02-08 13:53:55 +00:00
|
|
|
/// `DefId` will also be the context where it appears.
|
2023-12-30 15:19:54 +00:00
|
|
|
pub fn is_closure_or_coroutine(self, def_id: DefId) -> bool {
|
2023-11-26 13:05:08 +00:00
|
|
|
matches!(self.def_kind(def_id), DefKind::Closure)
|
2017-11-10 17:20:53 +00:00
|
|
|
}
|
|
|
|
|
2021-11-06 20:01:35 +00:00
|
|
|
/// Returns `true` if `def_id` refers to a definition that does not have its own
|
2023-10-19 21:46:28 +00:00
|
|
|
/// type-checking context, i.e. closure, coroutine or inline const.
|
2021-11-06 20:01:35 +00:00
|
|
|
pub fn is_typeck_child(self, def_id: DefId) -> bool {
|
2023-11-26 13:05:08 +00:00
|
|
|
matches!(self.def_kind(def_id), DefKind::Closure | DefKind::InlineConst)
|
2021-10-02 12:12:33 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns `true` if `def_id` refers to a trait (i.e., `trait Foo { ... }`).
|
2018-07-02 14:34:19 +00:00
|
|
|
pub fn is_trait(self, def_id: DefId) -> bool {
|
2020-04-17 18:55:17 +00:00
|
|
|
self.def_kind(def_id) == DefKind::Trait
|
2018-07-02 14:34:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns `true` if `def_id` refers to a trait alias (i.e., `trait Foo = ...;`),
|
|
|
|
/// and `false` otherwise.
|
2019-01-08 15:55:18 +00:00
|
|
|
pub fn is_trait_alias(self, def_id: DefId) -> bool {
|
2020-04-17 18:55:17 +00:00
|
|
|
self.def_kind(def_id) == DefKind::TraitAlias
|
2019-01-08 15:55:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Returns `true` if this `DefId` refers to the implicit constructor for
|
|
|
|
/// a tuple struct like `struct Foo(u32)`, and `false` otherwise.
|
2019-03-24 14:49:58 +00:00
|
|
|
pub fn is_constructor(self, def_id: DefId) -> bool {
|
2020-04-17 18:55:17 +00:00
|
|
|
matches!(self.def_kind(def_id), DefKind::Ctor(..))
|
2018-06-26 15:00:39 +00:00
|
|
|
}
|
|
|
|
|
2021-10-02 12:12:33 +00:00
|
|
|
/// Given the `DefId`, returns the `DefId` of the innermost item that
|
2022-03-16 12:12:30 +00:00
|
|
|
/// has its own type-checking context or "inference environment".
|
2021-10-02 12:12:33 +00:00
|
|
|
///
|
|
|
|
/// For example, a closure has its own `DefId`, but it is type-checked
|
|
|
|
/// with the containing item. Similarly, an inline const block has its
|
|
|
|
/// own `DefId` but it is type-checked together with the containing item.
|
|
|
|
///
|
|
|
|
/// Therefore, when we fetch the
|
2020-07-17 08:47:04 +00:00
|
|
|
/// `typeck` the closure, for example, we really wind up
|
|
|
|
/// fetching the `typeck` the enclosing fn item.
|
2021-11-06 20:01:35 +00:00
|
|
|
pub fn typeck_root_def_id(self, def_id: DefId) -> DefId {
|
2016-11-03 20:19:33 +00:00
|
|
|
let mut def_id = def_id;
|
2021-11-06 20:01:35 +00:00
|
|
|
while self.is_typeck_child(def_id) {
|
2022-04-25 19:08:45 +00:00
|
|
|
def_id = self.parent(def_id);
|
2016-11-03 20:19:33 +00:00
|
|
|
}
|
|
|
|
def_id
|
|
|
|
}
|
2017-02-08 17:31:03 +00:00
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
/// Given the `DefId` and args a closure, creates the type of
|
2017-11-21 16:18:40 +00:00
|
|
|
/// `self` argument that the closure expects. For example, for a
|
|
|
|
/// `Fn` closure, this would return a reference type `&T` where
|
2019-02-08 13:53:55 +00:00
|
|
|
/// `T = closure_ty`.
|
2017-11-21 16:18:40 +00:00
|
|
|
///
|
|
|
|
/// Returns `None` if this closure's kind has not yet been inferred.
|
|
|
|
/// This should only be possible during type checking.
|
|
|
|
///
|
|
|
|
/// Note that the return value is a late-bound region and hence
|
|
|
|
/// wrapped in a binder.
|
|
|
|
pub fn closure_env_ty(
|
|
|
|
self,
|
2024-01-14 18:29:01 +00:00
|
|
|
closure_ty: Ty<'tcx>,
|
|
|
|
closure_kind: ty::ClosureKind,
|
2023-02-13 02:03:45 +00:00
|
|
|
env_region: ty::Region<'tcx>,
|
2024-01-14 18:29:01 +00:00
|
|
|
) -> Ty<'tcx> {
|
|
|
|
match closure_kind {
|
2023-07-05 19:13:26 +00:00
|
|
|
ty::ClosureKind::Fn => Ty::new_imm_ref(self, env_region, closure_ty),
|
|
|
|
ty::ClosureKind::FnMut => Ty::new_mut_ref(self, env_region, closure_ty),
|
2017-11-21 16:18:40 +00:00
|
|
|
ty::ClosureKind::FnOnce => closure_ty,
|
2024-01-14 18:29:01 +00:00
|
|
|
}
|
2017-11-21 16:18:40 +00:00
|
|
|
}
|
|
|
|
|
2019-04-21 11:41:51 +00:00
|
|
|
/// Returns `true` if the node pointed to by `def_id` is a `static` item.
|
2022-03-29 15:11:12 +00:00
|
|
|
#[inline]
|
2020-09-18 18:49:25 +00:00
|
|
|
pub fn is_static(self, def_id: DefId) -> bool {
|
2022-03-29 15:11:12 +00:00
|
|
|
matches!(self.def_kind(def_id), DefKind::Static(_))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn static_mutability(self, def_id: DefId) -> Option<hir::Mutability> {
|
|
|
|
if let DefKind::Static(mt) = self.def_kind(def_id) { Some(mt) } else { None }
|
2019-04-21 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 16:55:23 +00:00
|
|
|
/// Returns `true` if this is a `static` item with the `#[thread_local]` attribute.
|
2020-09-18 18:49:25 +00:00
|
|
|
pub fn is_thread_local_static(self, def_id: DefId) -> bool {
|
2020-04-17 16:55:23 +00:00
|
|
|
self.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
|
|
|
|
}
|
|
|
|
|
2019-04-21 11:41:51 +00:00
|
|
|
/// Returns `true` if the node pointed to by `def_id` is a mutable `static` item.
|
2022-03-29 15:11:12 +00:00
|
|
|
#[inline]
|
2020-09-18 18:49:25 +00:00
|
|
|
pub fn is_mutable_static(self, def_id: DefId) -> bool {
|
2019-12-16 16:28:40 +00:00
|
|
|
self.static_mutability(def_id) == Some(hir::Mutability::Mut)
|
2017-11-12 11:40:56 +00:00
|
|
|
}
|
2018-11-18 18:33:44 +00:00
|
|
|
|
2023-02-03 08:04:12 +00:00
|
|
|
/// Returns `true` if the item pointed to by `def_id` is a thread local which needs a
|
|
|
|
/// thread local shim generated.
|
|
|
|
#[inline]
|
|
|
|
pub fn needs_thread_local_shim(self, def_id: DefId) -> bool {
|
|
|
|
!self.sess.target.dll_tls_export
|
|
|
|
&& self.is_thread_local_static(def_id)
|
|
|
|
&& !self.is_foreign_item(def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the type a reference to the thread local takes in MIR.
|
|
|
|
pub fn thread_local_ptr_ty(self, def_id: DefId) -> Ty<'tcx> {
|
2023-07-11 21:35:29 +00:00
|
|
|
let static_ty = self.type_of(def_id).instantiate_identity();
|
2023-02-03 08:04:12 +00:00
|
|
|
if self.is_mutable_static(def_id) {
|
2023-07-05 19:13:26 +00:00
|
|
|
Ty::new_mut_ptr(self, static_ty)
|
2023-02-03 08:04:12 +00:00
|
|
|
} else if self.is_foreign_item(def_id) {
|
2023-07-05 19:13:26 +00:00
|
|
|
Ty::new_imm_ptr(self, static_ty)
|
2023-02-03 08:04:12 +00:00
|
|
|
} else {
|
|
|
|
// FIXME: These things don't *really* have 'static lifetime.
|
2023-07-05 19:13:26 +00:00
|
|
|
Ty::new_imm_ref(self, self.lifetimes.re_static, static_ty)
|
2023-02-03 08:04:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-26 22:30:54 +00:00
|
|
|
/// Get the type of the pointer to the static that we use in MIR.
|
2020-09-18 18:49:25 +00:00
|
|
|
pub fn static_ptr_ty(self, def_id: DefId) -> Ty<'tcx> {
|
2019-11-26 22:30:54 +00:00
|
|
|
// Make sure that any constants in the static's type are evaluated.
|
2023-02-07 00:48:12 +00:00
|
|
|
let static_ty = self.normalize_erasing_regions(
|
|
|
|
ty::ParamEnv::empty(),
|
2023-07-11 21:35:29 +00:00
|
|
|
self.type_of(def_id).instantiate_identity(),
|
2023-02-07 00:48:12 +00:00
|
|
|
);
|
2019-11-26 22:30:54 +00:00
|
|
|
|
2020-10-19 07:47:18 +00:00
|
|
|
// Make sure that accesses to unsafe statics end up using raw pointers.
|
2020-10-19 08:53:20 +00:00
|
|
|
// For thread-locals, this needs to be kept in sync with `Rvalue::ty`.
|
2019-11-26 22:30:54 +00:00
|
|
|
if self.is_mutable_static(def_id) {
|
2023-07-05 19:13:26 +00:00
|
|
|
Ty::new_mut_ptr(self, static_ty)
|
2020-10-19 07:47:18 +00:00
|
|
|
} else if self.is_foreign_item(def_id) {
|
2023-07-05 19:13:26 +00:00
|
|
|
Ty::new_imm_ptr(self, static_ty)
|
2019-11-26 22:30:54 +00:00
|
|
|
} else {
|
2023-07-05 19:13:26 +00:00
|
|
|
Ty::new_imm_ref(self, self.lifetimes.re_erased, static_ty)
|
2019-11-26 22:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-09 21:35:02 +00:00
|
|
|
/// Return the set of types that should be taken into account when checking
|
2023-10-19 21:46:28 +00:00
|
|
|
/// trait bounds on a coroutine's internal state.
|
|
|
|
pub fn coroutine_hidden_types(
|
2022-10-01 13:57:22 +00:00
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> impl Iterator<Item = ty::EarlyBinder<Ty<'tcx>>> {
|
2023-10-19 21:46:28 +00:00
|
|
|
let coroutine_layout = self.mir_coroutine_witnesses(def_id);
|
|
|
|
coroutine_layout
|
2023-05-13 12:19:01 +00:00
|
|
|
.as_ref()
|
|
|
|
.map_or_else(|| [].iter(), |l| l.field_tys.iter())
|
2023-01-21 10:03:12 +00:00
|
|
|
.filter(|decl| !decl.ignore_for_traits)
|
2023-05-29 11:46:10 +00:00
|
|
|
.map(|decl| ty::EarlyBinder::bind(decl.ty))
|
2022-10-01 13:57:22 +00:00
|
|
|
}
|
|
|
|
|
2018-11-18 18:33:44 +00:00
|
|
|
/// Expands the given impl trait type, stopping if the type is recursive.
|
2022-06-28 15:18:07 +00:00
|
|
|
#[instrument(skip(self), level = "debug", ret)]
|
2018-11-18 18:33:44 +00:00
|
|
|
pub fn try_expand_impl_trait_type(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
2023-07-11 21:35:29 +00:00
|
|
|
args: GenericArgsRef<'tcx>,
|
2023-11-13 02:08:14 +00:00
|
|
|
inspect_coroutine_fields: InspectCoroutineFields,
|
2018-11-18 18:33:44 +00:00
|
|
|
) -> Result<Ty<'tcx>, Ty<'tcx>> {
|
|
|
|
let mut visitor = OpaqueTypeExpander {
|
|
|
|
seen_opaque_tys: FxHashSet::default(),
|
2019-10-10 23:38:05 +00:00
|
|
|
expanded_cache: FxHashMap::default(),
|
2020-04-11 04:50:02 +00:00
|
|
|
primary_def_id: Some(def_id),
|
2018-11-18 18:33:44 +00:00
|
|
|
found_recursion: false,
|
2021-07-28 12:21:59 +00:00
|
|
|
found_any_recursion: false,
|
2020-04-11 04:50:02 +00:00
|
|
|
check_recursion: true,
|
2023-10-19 21:46:28 +00:00
|
|
|
expand_coroutines: true,
|
2018-11-18 18:33:44 +00:00
|
|
|
tcx: self,
|
2023-11-13 02:08:14 +00:00
|
|
|
inspect_coroutine_fields,
|
2018-11-18 18:33:44 +00:00
|
|
|
};
|
2020-04-11 04:50:02 +00:00
|
|
|
|
2023-07-11 21:35:29 +00:00
|
|
|
let expanded_type = visitor.expand_opaque_ty(def_id, args).unwrap();
|
2018-11-18 18:33:44 +00:00
|
|
|
if visitor.found_recursion { Err(expanded_type) } else { Ok(expanded_type) }
|
|
|
|
}
|
2022-05-08 19:12:56 +00:00
|
|
|
|
2023-02-21 21:05:32 +00:00
|
|
|
/// Query and get an English description for the item's kind.
|
|
|
|
pub fn def_descr(self, def_id: DefId) -> &'static str {
|
|
|
|
self.def_kind_descr(self.def_kind(def_id), def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get an English description for the item's kind.
|
|
|
|
pub fn def_kind_descr(self, def_kind: DefKind, def_id: DefId) -> &'static str {
|
|
|
|
match def_kind {
|
|
|
|
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "method",
|
2023-11-26 13:05:08 +00:00
|
|
|
DefKind::Closure if let Some(coroutine_kind) = self.coroutine_kind(def_id) => {
|
|
|
|
match coroutine_kind {
|
2023-11-08 07:19:01 +00:00
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::Async,
|
|
|
|
hir::CoroutineSource::Fn,
|
|
|
|
) => "async fn",
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::Async,
|
|
|
|
hir::CoroutineSource::Block,
|
|
|
|
) => "async block",
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::Async,
|
|
|
|
hir::CoroutineSource::Closure,
|
|
|
|
) => "async closure",
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::AsyncGen,
|
|
|
|
hir::CoroutineSource::Fn,
|
|
|
|
) => "async gen fn",
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::AsyncGen,
|
|
|
|
hir::CoroutineSource::Block,
|
|
|
|
) => "async gen block",
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::AsyncGen,
|
|
|
|
hir::CoroutineSource::Closure,
|
|
|
|
) => "async gen closure",
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::Gen,
|
|
|
|
hir::CoroutineSource::Fn,
|
|
|
|
) => "gen fn",
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::Gen,
|
|
|
|
hir::CoroutineSource::Block,
|
|
|
|
) => "gen block",
|
|
|
|
hir::CoroutineKind::Desugared(
|
|
|
|
hir::CoroutineDesugaring::Gen,
|
|
|
|
hir::CoroutineSource::Closure,
|
|
|
|
) => "gen closure",
|
2023-12-25 16:56:12 +00:00
|
|
|
hir::CoroutineKind::Coroutine(_) => "coroutine",
|
2023-11-26 13:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 21:05:32 +00:00
|
|
|
_ => def_kind.descr(def_id),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets an English article for the [`TyCtxt::def_descr`].
|
|
|
|
pub fn def_descr_article(self, def_id: DefId) -> &'static str {
|
|
|
|
self.def_kind_descr_article(self.def_kind(def_id), def_id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets an English article for the [`TyCtxt::def_kind_descr`].
|
|
|
|
pub fn def_kind_descr_article(self, def_kind: DefKind, def_id: DefId) -> &'static str {
|
|
|
|
match def_kind {
|
|
|
|
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "a",
|
2023-11-26 13:05:08 +00:00
|
|
|
DefKind::Closure if let Some(coroutine_kind) = self.coroutine_kind(def_id) => {
|
|
|
|
match coroutine_kind {
|
2023-12-21 18:49:20 +00:00
|
|
|
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, ..) => "an",
|
|
|
|
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, ..) => "an",
|
|
|
|
hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, ..) => "a",
|
2023-12-25 16:56:12 +00:00
|
|
|
hir::CoroutineKind::Coroutine(_) => "a",
|
2023-11-26 13:05:08 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-21 21:05:32 +00:00
|
|
|
_ => def_kind.article(),
|
|
|
|
}
|
|
|
|
}
|
2023-05-01 19:05:20 +00:00
|
|
|
|
|
|
|
/// Return `true` if the supplied `CrateNum` is "user-visible," meaning either a [public]
|
|
|
|
/// dependency, or a [direct] private dependency. This is used to decide whether the crate can
|
|
|
|
/// be shown in `impl` suggestions.
|
|
|
|
///
|
|
|
|
/// [public]: TyCtxt::is_private_dep
|
|
|
|
/// [direct]: rustc_session::cstore::ExternCrate::is_direct
|
|
|
|
pub fn is_user_visible_dep(self, key: CrateNum) -> bool {
|
|
|
|
// | Private | Direct | Visible | |
|
|
|
|
// |---------|--------|---------|--------------------|
|
2023-05-09 20:27:51 +00:00
|
|
|
// | Yes | Yes | Yes | !true || true |
|
|
|
|
// | No | Yes | Yes | !false || true |
|
|
|
|
// | Yes | No | No | !true || false |
|
|
|
|
// | No | No | Yes | !false || false |
|
|
|
|
!self.is_private_dep(key)
|
2023-05-08 20:41:39 +00:00
|
|
|
// If `extern_crate` is `None`, then the crate was injected (e.g., by the allocator).
|
|
|
|
// Treat that kind of crate as "indirect", since it's an implementation detail of
|
|
|
|
// the language.
|
2023-11-25 17:12:15 +00:00
|
|
|
|| self.extern_crate(key.as_def_id()).is_some_and(|e| e.is_direct())
|
2023-05-01 19:05:20 +00:00
|
|
|
}
|
2023-11-27 16:05:48 +00:00
|
|
|
|
2024-01-08 01:00:31 +00:00
|
|
|
/// Whether the item has a host effect param. This is different from `TyCtxt::is_const`,
|
|
|
|
/// because the item must also be "maybe const", and the crate where the item is
|
|
|
|
/// defined must also have the effects feature enabled.
|
|
|
|
pub fn has_host_param(self, def_id: impl IntoQueryParam<DefId>) -> bool {
|
|
|
|
self.generics_of(def_id).host_effect_index.is_some()
|
|
|
|
}
|
|
|
|
|
2023-12-10 13:06:18 +00:00
|
|
|
pub fn expected_host_effect_param_for_body(self, def_id: impl Into<DefId>) -> ty::Const<'tcx> {
|
|
|
|
let def_id = def_id.into();
|
2023-12-06 22:01:21 +00:00
|
|
|
// FIXME(effects): This is suspicious and should probably not be done,
|
|
|
|
// especially now that we enforce host effects and then properly handle
|
|
|
|
// effect vars during fallback.
|
2023-11-27 16:05:48 +00:00
|
|
|
let mut host_always_on =
|
|
|
|
!self.features().effects || self.sess.opts.unstable_opts.unleash_the_miri_inside_of_you;
|
|
|
|
|
|
|
|
// Compute the constness required by the context.
|
|
|
|
let const_context = self.hir().body_const_context(def_id);
|
|
|
|
|
|
|
|
let kind = self.def_kind(def_id);
|
|
|
|
debug_assert_ne!(kind, DefKind::ConstParam);
|
|
|
|
|
|
|
|
if self.has_attr(def_id, sym::rustc_do_not_const_check) {
|
|
|
|
trace!("do not const check this context");
|
|
|
|
host_always_on = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
match const_context {
|
|
|
|
_ if host_always_on => self.consts.true_,
|
|
|
|
Some(hir::ConstContext::Static(_) | hir::ConstContext::Const { .. }) => {
|
|
|
|
self.consts.false_
|
|
|
|
}
|
|
|
|
Some(hir::ConstContext::ConstFn) => {
|
|
|
|
let host_idx = self
|
|
|
|
.generics_of(def_id)
|
|
|
|
.host_effect_index
|
|
|
|
.expect("ConstContext::Maybe must have host effect param");
|
|
|
|
ty::GenericArgs::identity_for_item(self, def_id).const_at(host_idx)
|
|
|
|
}
|
|
|
|
None => self.consts.true_,
|
|
|
|
}
|
|
|
|
}
|
2023-11-28 21:17:55 +00:00
|
|
|
|
|
|
|
/// Constructs generic args for an item, optionally appending a const effect param type
|
2023-12-09 17:43:01 +00:00
|
|
|
pub fn with_opt_host_effect_param(
|
2023-11-28 21:17:55 +00:00
|
|
|
self,
|
|
|
|
caller_def_id: LocalDefId,
|
|
|
|
callee_def_id: DefId,
|
|
|
|
args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
|
|
|
|
) -> ty::GenericArgsRef<'tcx> {
|
|
|
|
let generics = self.generics_of(callee_def_id);
|
|
|
|
assert_eq!(generics.parent, None);
|
|
|
|
|
2023-12-09 17:43:01 +00:00
|
|
|
let opt_const_param = generics
|
|
|
|
.host_effect_index
|
|
|
|
.is_some()
|
|
|
|
.then(|| ty::GenericArg::from(self.expected_host_effect_param_for_body(caller_def_id)));
|
2023-11-28 21:17:55 +00:00
|
|
|
|
|
|
|
self.mk_args_from_iter(args.into_iter().map(|arg| arg.into()).chain(opt_const_param))
|
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 04:50:02 +00:00
|
|
|
struct OpaqueTypeExpander<'tcx> {
|
|
|
|
// Contains the DefIds of the opaque types that are currently being
|
|
|
|
// expanded. When we expand an opaque type we insert the DefId of
|
|
|
|
// that type, and when we finish expanding that type we remove the
|
|
|
|
// its DefId.
|
|
|
|
seen_opaque_tys: FxHashSet<DefId>,
|
|
|
|
// Cache of all expansions we've seen so far. This is a critical
|
|
|
|
// optimization for some large types produced by async fn trees.
|
2023-07-11 21:35:29 +00:00
|
|
|
expanded_cache: FxHashMap<(DefId, GenericArgsRef<'tcx>), Ty<'tcx>>,
|
2020-04-11 04:50:02 +00:00
|
|
|
primary_def_id: Option<DefId>,
|
|
|
|
found_recursion: bool,
|
2021-07-28 12:21:59 +00:00
|
|
|
found_any_recursion: bool,
|
2023-10-19 21:46:28 +00:00
|
|
|
expand_coroutines: bool,
|
2020-04-11 04:50:02 +00:00
|
|
|
/// Whether or not to check for recursive opaque types.
|
|
|
|
/// This is `true` when we're explicitly checking for opaque type
|
2020-08-02 15:20:00 +00:00
|
|
|
/// recursion, and 'false' otherwise to avoid unnecessary work.
|
2020-04-11 04:50:02 +00:00
|
|
|
check_recursion: bool,
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-11-13 02:08:14 +00:00
|
|
|
inspect_coroutine_fields: InspectCoroutineFields,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum InspectCoroutineFields {
|
|
|
|
No,
|
|
|
|
Yes,
|
2020-04-11 04:50:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> OpaqueTypeExpander<'tcx> {
|
2023-07-11 21:35:29 +00:00
|
|
|
fn expand_opaque_ty(&mut self, def_id: DefId, args: GenericArgsRef<'tcx>) -> Option<Ty<'tcx>> {
|
2021-07-28 12:21:59 +00:00
|
|
|
if self.found_any_recursion {
|
2020-04-11 04:50:02 +00:00
|
|
|
return None;
|
|
|
|
}
|
2023-07-11 21:35:29 +00:00
|
|
|
let args = args.fold_with(self);
|
2020-04-11 04:50:02 +00:00
|
|
|
if !self.check_recursion || self.seen_opaque_tys.insert(def_id) {
|
2023-07-11 21:35:29 +00:00
|
|
|
let expanded_ty = match self.expanded_cache.get(&(def_id, args)) {
|
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 03:13:38 +00:00
|
|
|
Some(expanded_ty) => *expanded_ty,
|
2020-04-11 04:50:02 +00:00
|
|
|
None => {
|
2023-02-07 08:29:48 +00:00
|
|
|
let generic_ty = self.tcx.type_of(def_id);
|
2023-07-11 21:35:29 +00:00
|
|
|
let concrete_ty = generic_ty.instantiate(self.tcx, args);
|
2021-12-01 00:55:57 +00:00
|
|
|
let expanded_ty = self.fold_ty(concrete_ty);
|
2023-07-11 21:35:29 +00:00
|
|
|
self.expanded_cache.insert((def_id, args), expanded_ty);
|
2020-04-11 04:50:02 +00:00
|
|
|
expanded_ty
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if self.check_recursion {
|
|
|
|
self.seen_opaque_tys.remove(&def_id);
|
|
|
|
}
|
|
|
|
Some(expanded_ty)
|
|
|
|
} else {
|
|
|
|
// If another opaque type that we contain is recursive, then it
|
|
|
|
// will report the error, so we don't have to.
|
2021-07-28 12:21:59 +00:00
|
|
|
self.found_any_recursion = true;
|
2020-04-11 04:50:02 +00:00
|
|
|
self.found_recursion = def_id == *self.primary_def_id.as_ref().unwrap();
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2022-10-01 13:57:22 +00:00
|
|
|
|
2023-10-19 21:46:28 +00:00
|
|
|
fn expand_coroutine(&mut self, def_id: DefId, args: GenericArgsRef<'tcx>) -> Option<Ty<'tcx>> {
|
2022-10-01 13:57:22 +00:00
|
|
|
if self.found_any_recursion {
|
|
|
|
return None;
|
|
|
|
}
|
2023-07-11 21:35:29 +00:00
|
|
|
let args = args.fold_with(self);
|
2022-10-01 13:57:22 +00:00
|
|
|
if !self.check_recursion || self.seen_opaque_tys.insert(def_id) {
|
2023-07-11 21:35:29 +00:00
|
|
|
let expanded_ty = match self.expanded_cache.get(&(def_id, args)) {
|
2022-10-01 13:57:22 +00:00
|
|
|
Some(expanded_ty) => *expanded_ty,
|
|
|
|
None => {
|
2023-11-13 02:08:14 +00:00
|
|
|
if matches!(self.inspect_coroutine_fields, InspectCoroutineFields::Yes) {
|
|
|
|
for bty in self.tcx.coroutine_hidden_types(def_id) {
|
|
|
|
let hidden_ty = bty.instantiate(self.tcx, args);
|
|
|
|
self.fold_ty(hidden_ty);
|
|
|
|
}
|
2022-10-01 13:57:22 +00:00
|
|
|
}
|
2023-10-19 21:46:28 +00:00
|
|
|
let expanded_ty = Ty::new_coroutine_witness(self.tcx, def_id, args);
|
2023-07-11 21:35:29 +00:00
|
|
|
self.expanded_cache.insert((def_id, args), expanded_ty);
|
2022-10-01 13:57:22 +00:00
|
|
|
expanded_ty
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if self.check_recursion {
|
|
|
|
self.seen_opaque_tys.remove(&def_id);
|
|
|
|
}
|
|
|
|
Some(expanded_ty)
|
|
|
|
} else {
|
|
|
|
// If another opaque type that we contain is recursive, then it
|
|
|
|
// will report the error, so we don't have to.
|
|
|
|
self.found_any_recursion = true;
|
|
|
|
self.found_recursion = def_id == *self.primary_def_id.as_ref().unwrap();
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2020-04-11 04:50:02 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 09:13:27 +00:00
|
|
|
impl<'tcx> TypeFolder<TyCtxt<'tcx>> for OpaqueTypeExpander<'tcx> {
|
2023-02-11 09:18:12 +00:00
|
|
|
fn interner(&self) -> TyCtxt<'tcx> {
|
2020-04-11 04:50:02 +00:00
|
|
|
self.tcx
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:55:57 +00:00
|
|
|
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
|
2023-07-11 21:35:29 +00:00
|
|
|
let mut t = if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, args, .. }) = *t.kind() {
|
|
|
|
self.expand_opaque_ty(def_id, args).unwrap_or(t)
|
2023-10-19 21:46:28 +00:00
|
|
|
} else if t.has_opaque_types() || t.has_coroutines() {
|
2020-04-11 04:50:02 +00:00
|
|
|
t.super_fold_with(self)
|
|
|
|
} else {
|
2021-12-01 00:55:57 +00:00
|
|
|
t
|
2022-10-01 13:57:22 +00:00
|
|
|
};
|
2023-10-19 21:46:28 +00:00
|
|
|
if self.expand_coroutines {
|
2023-10-19 16:06:43 +00:00
|
|
|
if let ty::CoroutineWitness(def_id, args) = *t.kind() {
|
2023-10-19 21:46:28 +00:00
|
|
|
t = self.expand_coroutine(def_id, args).unwrap_or(t);
|
2022-10-01 13:57:22 +00:00
|
|
|
}
|
2020-04-11 04:50:02 +00:00
|
|
|
}
|
2022-10-01 13:57:22 +00:00
|
|
|
t
|
2020-04-11 04:50:02 +00:00
|
|
|
}
|
2023-02-17 17:16:43 +00:00
|
|
|
|
|
|
|
fn fold_predicate(&mut self, p: ty::Predicate<'tcx>) -> ty::Predicate<'tcx> {
|
|
|
|
if let ty::PredicateKind::Clause(clause) = p.kind().skip_binder()
|
2023-06-16 05:59:42 +00:00
|
|
|
&& let ty::ClauseKind::Projection(projection_pred) = clause
|
2023-02-17 17:16:43 +00:00
|
|
|
{
|
|
|
|
p.kind()
|
|
|
|
.rebind(ty::ProjectionPredicate {
|
|
|
|
projection_ty: projection_pred.projection_ty.fold_with(self),
|
|
|
|
// Don't fold the term on the RHS of the projection predicate.
|
|
|
|
// This is because for default trait methods with RPITITs, we
|
|
|
|
// install a `NormalizesTo(Projection(RPITIT) -> Opaque(RPITIT))`
|
|
|
|
// predicate, which would trivially cause a cycle when we do
|
|
|
|
// anything that requires `ParamEnv::with_reveal_all_normalized`.
|
|
|
|
term: projection_pred.term,
|
|
|
|
})
|
|
|
|
.to_predicate(self.tcx)
|
|
|
|
} else {
|
|
|
|
p.super_fold_with(self)
|
|
|
|
}
|
|
|
|
}
|
2020-04-11 04:50:02 +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 03:13:38 +00:00
|
|
|
impl<'tcx> Ty<'tcx> {
|
2023-10-12 21:29:16 +00:00
|
|
|
/// Returns the `Size` for primitive types (bool, uint, int, char, float).
|
|
|
|
pub fn primitive_size(self, tcx: TyCtxt<'tcx>) -> Size {
|
|
|
|
match *self.kind() {
|
|
|
|
ty::Bool => Size::from_bytes(1),
|
|
|
|
ty::Char => Size::from_bytes(4),
|
|
|
|
ty::Int(ity) => Integer::from_int_ty(&tcx, ity).size(),
|
|
|
|
ty::Uint(uty) => Integer::from_uint_ty(&tcx, uty).size(),
|
|
|
|
ty::Float(ty::FloatTy::F32) => Primitive::F32.size(&tcx),
|
|
|
|
ty::Float(ty::FloatTy::F64) => Primitive::F64.size(&tcx),
|
|
|
|
_ => bug!("non primitive type"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 21:08:15 +00:00
|
|
|
pub fn int_size_and_signed(self, tcx: TyCtxt<'tcx>) -> (Size, bool) {
|
2023-10-12 21:29:16 +00:00
|
|
|
match *self.kind() {
|
|
|
|
ty::Int(ity) => (Integer::from_int_ty(&tcx, ity).size(), true),
|
|
|
|
ty::Uint(uty) => (Integer::from_uint_ty(&tcx, uty).size(), false),
|
2023-02-13 21:08:15 +00:00
|
|
|
_ => bug!("non integer discriminant"),
|
2023-10-12 21:29:16 +00:00
|
|
|
}
|
2023-02-13 21:08:15 +00:00
|
|
|
}
|
|
|
|
|
2023-10-12 21:29:16 +00:00
|
|
|
/// Returns the minimum and maximum values for the given numeric type (including `char`s) or
|
|
|
|
/// returns `None` if the type is not numeric.
|
|
|
|
pub fn numeric_min_and_max_as_bits(self, tcx: TyCtxt<'tcx>) -> Option<(u128, u128)> {
|
|
|
|
use rustc_apfloat::ieee::{Double, Single};
|
|
|
|
Some(match self.kind() {
|
2019-12-11 09:04:34 +00:00
|
|
|
ty::Int(_) | ty::Uint(_) => {
|
2023-02-13 21:08:15 +00:00
|
|
|
let (size, signed) = self.int_size_and_signed(tcx);
|
2023-10-12 21:29:16 +00:00
|
|
|
let min = if signed { size.truncate(size.signed_int_min() as u128) } else { 0 };
|
|
|
|
let max =
|
2021-09-07 18:44:33 +00:00
|
|
|
if signed { size.signed_int_max() as u128 } else { size.unsigned_int_max() };
|
2023-10-12 21:29:16 +00:00
|
|
|
(min, max)
|
2019-12-11 09:04:34 +00:00
|
|
|
}
|
2023-10-12 21:29:16 +00:00
|
|
|
ty::Char => (0, std::char::MAX as u128),
|
|
|
|
ty::Float(ty::FloatTy::F32) => {
|
|
|
|
((-Single::INFINITY).to_bits(), Single::INFINITY.to_bits())
|
|
|
|
}
|
|
|
|
ty::Float(ty::FloatTy::F64) => {
|
|
|
|
((-Double::INFINITY).to_bits(), Double::INFINITY.to_bits())
|
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
})
|
|
|
|
}
|
2022-04-12 16:14:28 +00:00
|
|
|
|
2023-10-12 21:29:16 +00:00
|
|
|
/// Returns the maximum value for the given numeric type (including `char`s)
|
|
|
|
/// or returns `None` if the type is not numeric.
|
|
|
|
pub fn numeric_max_val(self, tcx: TyCtxt<'tcx>) -> Option<ty::Const<'tcx>> {
|
|
|
|
self.numeric_min_and_max_as_bits(tcx)
|
|
|
|
.map(|(_, max)| ty::Const::from_bits(tcx, max, ty::ParamEnv::empty().and(self)))
|
2019-12-11 09:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the minimum value for the given numeric type (including `char`s)
|
|
|
|
/// or returns `None` if the type is not numeric.
|
2022-04-12 16:14:28 +00:00
|
|
|
pub fn numeric_min_val(self, tcx: TyCtxt<'tcx>) -> Option<ty::Const<'tcx>> {
|
2023-10-12 21:29:16 +00:00
|
|
|
self.numeric_min_and_max_as_bits(tcx)
|
|
|
|
.map(|(min, _)| ty::Const::from_bits(tcx, min, ty::ParamEnv::empty().and(self)))
|
2019-12-11 09:04:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 15:56:24 +00:00
|
|
|
/// Checks whether values of this type `T` are *moved* or *copied*
|
|
|
|
/// when referenced -- this amounts to a check for whether `T:
|
|
|
|
/// Copy`, but note that we **don't** consider lifetimes when
|
|
|
|
/// doing this check. This means that we may generate MIR which
|
|
|
|
/// does copies even when the type actually doesn't satisfy the
|
|
|
|
/// full requirements for the `Copy` trait (cc #29149) -- this
|
|
|
|
/// winds up being reported as an error during NLL borrow check.
|
2022-10-27 10:45:02 +00:00
|
|
|
pub fn is_copy_modulo_regions(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
|
|
|
|
self.is_trivially_pure_clone_copy() || tcx.is_copy_raw(param_env.and(self))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 15:56:24 +00:00
|
|
|
/// Checks whether values of this type `T` have a size known at
|
|
|
|
/// compile time (i.e., whether `T: Sized`). Lifetimes are ignored
|
|
|
|
/// for the purposes of this check, so it can be an
|
|
|
|
/// over-approximation in generic contexts, where one can have
|
|
|
|
/// strange rules like `<T as Foo<'static>>::Bar: Sized` that
|
|
|
|
/// actually carry lifetime requirements.
|
2022-10-27 10:45:02 +00:00
|
|
|
pub fn is_sized(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
|
|
|
|
self.is_trivially_sized(tcx) || tcx.is_sized_raw(param_env.and(self))
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 15:56:24 +00:00
|
|
|
/// Checks whether values of this type `T` implement the `Freeze`
|
2021-08-22 15:27:18 +00:00
|
|
|
/// trait -- frozen types are those that do not contain an
|
2019-02-08 13:53:55 +00:00
|
|
|
/// `UnsafeCell` anywhere. This is a language concept used to
|
2018-11-19 16:57:22 +00:00
|
|
|
/// distinguish "true immutability", which is relevant to
|
|
|
|
/// optimization as well as the rules around static values. Note
|
|
|
|
/// that the `Freeze` trait is not exposed to end users and is
|
|
|
|
/// effectively an implementation detail.
|
2022-10-27 10:45:02 +00:00
|
|
|
pub fn is_freeze(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
|
|
|
|
self.is_trivially_freeze() || tcx.is_freeze_raw(param_env.and(self))
|
2020-02-15 14:21:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fast path helper for testing if a type is `Freeze`.
|
|
|
|
///
|
|
|
|
/// Returning true means the type is known to be `Freeze`. Returning
|
|
|
|
/// `false` means nothing -- could be `Freeze`, might not be.
|
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 03:13:38 +00:00
|
|
|
fn is_trivially_freeze(self) -> bool {
|
2020-08-02 22:49:11 +00:00
|
|
|
match self.kind() {
|
2020-02-15 14:21:50 +00:00
|
|
|
ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Str
|
|
|
|
| ty::Never
|
|
|
|
| ty::Ref(..)
|
|
|
|
| ty::RawPtr(_)
|
|
|
|
| ty::FnDef(..)
|
2020-05-06 04:02:09 +00:00
|
|
|
| ty::Error(_)
|
2020-02-15 14:21:50 +00:00
|
|
|
| ty::FnPtr(_) => true,
|
2022-02-07 15:06:31 +00:00
|
|
|
ty::Tuple(fields) => fields.iter().all(Self::is_trivially_freeze),
|
2020-02-15 14:21:50 +00:00
|
|
|
ty::Slice(elem_ty) | ty::Array(elem_ty, _) => elem_ty.is_trivially_freeze(),
|
|
|
|
ty::Adt(..)
|
|
|
|
| ty::Bound(..)
|
|
|
|
| ty::Closure(..)
|
|
|
|
| ty::Dynamic(..)
|
|
|
|
| ty::Foreign(_)
|
2023-10-19 16:06:43 +00:00
|
|
|
| ty::Coroutine(..)
|
|
|
|
| ty::CoroutineWitness(..)
|
2020-02-15 14:21:50 +00:00
|
|
|
| ty::Infer(_)
|
2022-11-27 17:52:17 +00:00
|
|
|
| ty::Alias(..)
|
2020-02-15 14:21:50 +00:00
|
|
|
| ty::Param(_)
|
2022-11-27 17:52:17 +00:00
|
|
|
| ty::Placeholder(_) => false,
|
2020-02-15 14:21:50 +00:00
|
|
|
}
|
2017-04-17 18:18:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 21:44:36 +00:00
|
|
|
/// Checks whether values of this type `T` implement the `Unpin` trait.
|
2022-10-27 10:45:02 +00:00
|
|
|
pub fn is_unpin(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
|
|
|
|
self.is_trivially_unpin() || tcx.is_unpin_raw(param_env.and(self))
|
2021-03-18 21:44:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Fast path helper for testing if a type is `Unpin`.
|
|
|
|
///
|
|
|
|
/// Returning true means the type is known to be `Unpin`. Returning
|
|
|
|
/// `false` means nothing -- could be `Unpin`, might not be.
|
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 03:13:38 +00:00
|
|
|
fn is_trivially_unpin(self) -> bool {
|
2021-03-18 21:44:36 +00:00
|
|
|
match self.kind() {
|
|
|
|
ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Str
|
|
|
|
| ty::Never
|
|
|
|
| ty::Ref(..)
|
|
|
|
| ty::RawPtr(_)
|
|
|
|
| ty::FnDef(..)
|
|
|
|
| ty::Error(_)
|
|
|
|
| ty::FnPtr(_) => true,
|
2022-02-07 15:06:31 +00:00
|
|
|
ty::Tuple(fields) => fields.iter().all(Self::is_trivially_unpin),
|
2021-03-18 21:44:36 +00:00
|
|
|
ty::Slice(elem_ty) | ty::Array(elem_ty, _) => elem_ty.is_trivially_unpin(),
|
|
|
|
ty::Adt(..)
|
|
|
|
| ty::Bound(..)
|
|
|
|
| ty::Closure(..)
|
|
|
|
| ty::Dynamic(..)
|
|
|
|
| ty::Foreign(_)
|
2023-10-19 16:06:43 +00:00
|
|
|
| ty::Coroutine(..)
|
|
|
|
| ty::CoroutineWitness(..)
|
2021-03-18 21:44:36 +00:00
|
|
|
| ty::Infer(_)
|
2022-11-27 17:52:17 +00:00
|
|
|
| ty::Alias(..)
|
2021-03-18 21:44:36 +00:00
|
|
|
| ty::Param(_)
|
2022-11-27 17:52:17 +00:00
|
|
|
| ty::Placeholder(_) => false,
|
2021-03-18 21:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 21:22:55 +00:00
|
|
|
/// If `ty.needs_drop(...)` returns `true`, then `ty` is definitely
|
|
|
|
/// non-copy and *might* have a destructor attached; if it returns
|
2018-11-27 02:59:49 +00:00
|
|
|
/// `false`, then `ty` definitely has no destructor (i.e., no drop glue).
|
2017-04-17 21:22:55 +00:00
|
|
|
///
|
|
|
|
/// (Note that this implies that if `ty` has a destructor attached,
|
|
|
|
/// then `needs_drop` will definitely return `true` for `ty`.)
|
2018-12-19 19:58:20 +00:00
|
|
|
///
|
|
|
|
/// Note that this method is used to check eligible types in unions.
|
2017-04-17 20:26:48 +00:00
|
|
|
#[inline]
|
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 03:13:38 +00:00
|
|
|
pub fn needs_drop(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
|
2020-01-30 20:28:16 +00:00
|
|
|
// Avoid querying in simple cases.
|
2023-07-18 13:50:34 +00:00
|
|
|
match needs_drop_components(tcx, self) {
|
2020-01-30 20:28:16 +00:00
|
|
|
Err(AlwaysRequiresDrop) => true,
|
|
|
|
Ok(components) => {
|
|
|
|
let query_ty = match *components {
|
|
|
|
[] => return false,
|
|
|
|
// If we've got a single component, call the query with that
|
|
|
|
// to increase the chance that we hit the query cache.
|
|
|
|
[component_ty] => component_ty,
|
|
|
|
_ => self,
|
|
|
|
};
|
2021-12-02 19:30:35 +00:00
|
|
|
|
2020-01-31 22:22:30 +00:00
|
|
|
// This doesn't depend on regions, so try to minimize distinct
|
2020-01-30 20:28:16 +00:00
|
|
|
// query keys used.
|
2021-12-02 19:30:35 +00:00
|
|
|
// If normalization fails, we just use `query_ty`.
|
2023-10-26 10:19:23 +00:00
|
|
|
debug_assert!(!param_env.has_infer());
|
2023-10-24 17:16:15 +00:00
|
|
|
let query_ty = tcx
|
|
|
|
.try_normalize_erasing_regions(param_env, query_ty)
|
|
|
|
.unwrap_or_else(|_| tcx.erase_regions(query_ty));
|
2021-12-02 19:30:35 +00:00
|
|
|
|
|
|
|
tcx.needs_drop_raw(param_env.and(query_ty))
|
2020-01-30 20:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-17 20:26:48 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 16:25:34 +00:00
|
|
|
/// Checks if `ty` has a significant drop.
|
2021-04-13 07:43:11 +00:00
|
|
|
///
|
|
|
|
/// Note that this method can return false even if `ty` has a destructor
|
|
|
|
/// attached; even if that is the case then the adt has been marked with
|
|
|
|
/// the attribute `rustc_insignificant_dtor`.
|
|
|
|
///
|
|
|
|
/// Note that this method is used to check for change in drop order for
|
|
|
|
/// 2229 drop reorder migration analysis.
|
|
|
|
#[inline]
|
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 03:13:38 +00:00
|
|
|
pub fn has_significant_drop(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> bool {
|
2021-04-13 07:43:11 +00:00
|
|
|
// Avoid querying in simple cases.
|
2023-07-18 13:50:34 +00:00
|
|
|
match needs_drop_components(tcx, self) {
|
2021-04-13 07:43:11 +00:00
|
|
|
Err(AlwaysRequiresDrop) => true,
|
|
|
|
Ok(components) => {
|
|
|
|
let query_ty = match *components {
|
|
|
|
[] => return false,
|
|
|
|
// If we've got a single component, call the query with that
|
|
|
|
// to increase the chance that we hit the query cache.
|
|
|
|
[component_ty] => component_ty,
|
|
|
|
_ => self,
|
|
|
|
};
|
2021-07-04 15:41:40 +00:00
|
|
|
|
2021-07-04 16:50:28 +00:00
|
|
|
// FIXME(#86868): We should be canonicalizing, or else moving this to a method of inference
|
2021-07-04 15:41:40 +00:00
|
|
|
// context, or *something* like that, but for now just avoid passing inference
|
|
|
|
// variables to queries that can't cope with them. Instead, conservatively
|
|
|
|
// return "true" (may change drop order).
|
2023-04-27 07:34:11 +00:00
|
|
|
if query_ty.has_infer() {
|
2021-07-04 15:41:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-13 07:43:11 +00:00
|
|
|
// This doesn't depend on regions, so try to minimize distinct
|
|
|
|
// query keys used.
|
|
|
|
let erased = tcx.normalize_erasing_regions(param_env, query_ty);
|
|
|
|
tcx.has_significant_drop_raw(param_env.and(erased))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 15:47:13 +00:00
|
|
|
/// Returns `true` if equality for this type is both reflexive and structural.
|
2020-05-13 20:40:22 +00:00
|
|
|
///
|
2020-06-12 15:47:13 +00:00
|
|
|
/// Reflexive equality for a type is indicated by an `Eq` impl for that type.
|
2020-05-13 20:40:22 +00:00
|
|
|
///
|
|
|
|
/// Primitive types (`u32`, `str`) have structural equality by definition. For composite data
|
|
|
|
/// types, equality for the type as a whole is structural when it is the same as equality
|
|
|
|
/// between all components (fields, array elements, etc.) of that type. For ADTs, structural
|
|
|
|
/// equality is indicated by an implementation of `PartialStructuralEq` and `StructuralEq` for
|
|
|
|
/// that type.
|
|
|
|
///
|
|
|
|
/// This function is "shallow" because it may return `true` for a composite type whose fields
|
|
|
|
/// are not `StructuralEq`. For example, `[T; 4]` has structural equality regardless of `T`
|
|
|
|
/// because equality for arrays is determined by the equality of each array element. If you
|
|
|
|
/// want to know whether a given call to `PartialEq::eq` will proceed structurally all the way
|
|
|
|
/// down, you will need to use a type visitor.
|
|
|
|
#[inline]
|
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 03:13:38 +00:00
|
|
|
pub fn is_structural_eq_shallow(self, tcx: TyCtxt<'tcx>) -> bool {
|
2020-08-02 22:49:11 +00:00
|
|
|
match self.kind() {
|
2020-05-13 20:40:22 +00:00
|
|
|
// Look for an impl of both `PartialStructuralEq` and `StructuralEq`.
|
2022-03-23 08:41:31 +00:00
|
|
|
ty::Adt(..) => tcx.has_structural_eq_impls(self),
|
2020-05-13 20:40:22 +00:00
|
|
|
|
|
|
|
// Primitive types that satisfy `Eq`.
|
2022-03-23 08:41:31 +00:00
|
|
|
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Str | ty::Never => true,
|
2020-05-13 20:40:22 +00:00
|
|
|
|
|
|
|
// Composite types that satisfy `Eq` when all of their fields do.
|
|
|
|
//
|
|
|
|
// Because this function is "shallow", we return `true` for these composites regardless
|
|
|
|
// of the type(s) contained within.
|
2022-03-23 08:41:31 +00:00
|
|
|
ty::Ref(..) | ty::Array(..) | ty::Slice(_) | ty::Tuple(..) => true,
|
2020-05-13 20:40:22 +00:00
|
|
|
|
|
|
|
// Raw pointers use bitwise comparison.
|
2022-03-23 08:41:31 +00:00
|
|
|
ty::RawPtr(_) | ty::FnPtr(_) => true,
|
2020-05-13 20:40:22 +00:00
|
|
|
|
|
|
|
// Floating point numbers are not `Eq`.
|
2022-03-23 08:41:31 +00:00
|
|
|
ty::Float(_) => false,
|
2020-05-13 20:40:22 +00:00
|
|
|
|
|
|
|
// Conservatively return `false` for all others...
|
|
|
|
|
|
|
|
// Anonymous function types
|
2023-10-19 16:06:43 +00:00
|
|
|
ty::FnDef(..) | ty::Closure(..) | ty::Dynamic(..) | ty::Coroutine(..) => false,
|
2020-05-13 20:40:22 +00:00
|
|
|
|
|
|
|
// Generic or inferred types
|
|
|
|
//
|
|
|
|
// FIXME(ecstaticmorse): Maybe we should `bug` here? This should probably only be
|
|
|
|
// called for known, fully-monomorphized types.
|
2022-11-27 17:52:17 +00:00
|
|
|
ty::Alias(..) | ty::Param(_) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) => {
|
|
|
|
false
|
|
|
|
}
|
2020-05-13 20:40:22 +00:00
|
|
|
|
2023-10-19 16:06:43 +00:00
|
|
|
ty::Foreign(_) | ty::CoroutineWitness(..) | ty::Error(_) => false,
|
2020-05-13 20:40:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-07 22:55:38 +00:00
|
|
|
/// Peel off all reference types in this type until there are none left.
|
|
|
|
///
|
|
|
|
/// This method is idempotent, i.e. `ty.peel_refs().peel_refs() == ty.peel_refs()`.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// - `u8` -> `u8`
|
|
|
|
/// - `&'a mut u8` -> `u8`
|
|
|
|
/// - `&'a &'b u8` -> `u8`
|
|
|
|
/// - `&'a *const &'b u8 -> *const &'b u8`
|
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 03:13:38 +00:00
|
|
|
pub fn peel_refs(self) -> Ty<'tcx> {
|
2019-09-07 22:55:38 +00:00
|
|
|
let mut ty = self;
|
2022-03-23 08:41:31 +00:00
|
|
|
while let ty::Ref(_, inner_ty, _) = ty.kind() {
|
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 03:13:38 +00:00
|
|
|
ty = *inner_ty;
|
2019-09-07 22:55:38 +00:00
|
|
|
}
|
|
|
|
ty
|
|
|
|
}
|
2021-07-02 01:14:13 +00:00
|
|
|
|
2022-07-07 00:00:00 +00:00
|
|
|
#[inline]
|
2022-03-23 08:41:31 +00:00
|
|
|
pub fn outer_exclusive_binder(self) -> ty::DebruijnIndex {
|
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 03:13:38 +00:00
|
|
|
self.0.outer_exclusive_binder
|
2021-07-02 01:14:13 +00:00
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
}
|
2017-05-10 14:28:06 +00:00
|
|
|
|
2017-11-08 10:27:39 +00:00
|
|
|
pub enum ExplicitSelf<'tcx> {
|
|
|
|
ByValue,
|
|
|
|
ByReference(ty::Region<'tcx>, hir::Mutability),
|
2017-11-09 21:15:35 +00:00
|
|
|
ByRawPointer(hir::Mutability),
|
2017-11-08 10:27:39 +00:00
|
|
|
ByBox,
|
|
|
|
Other,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> ExplicitSelf<'tcx> {
|
|
|
|
/// Categorizes an explicit self declaration like `self: SomeType`
|
2023-05-11 11:25:01 +00:00
|
|
|
/// into either `self`, `&self`, `&mut self`, `Box<Self>`, or
|
2017-11-08 10:27:39 +00:00
|
|
|
/// `Other`.
|
|
|
|
/// This is mainly used to require the arbitrary_self_types feature
|
|
|
|
/// in the case of `Other`, to improve error messages in the common cases,
|
|
|
|
/// and to make `Other` non-object-safe.
|
|
|
|
///
|
|
|
|
/// Examples:
|
|
|
|
///
|
2022-04-15 22:04:34 +00:00
|
|
|
/// ```ignore (illustrative)
|
2017-11-08 10:27:39 +00:00
|
|
|
/// impl<'a> Foo for &'a T {
|
|
|
|
/// // Legal declarations:
|
|
|
|
/// fn method1(self: &&'a T); // ExplicitSelf::ByReference
|
|
|
|
/// fn method2(self: &'a T); // ExplicitSelf::ByValue
|
|
|
|
/// fn method3(self: Box<&'a T>); // ExplicitSelf::ByBox
|
|
|
|
/// fn method4(self: Rc<&'a T>); // ExplicitSelf::Other
|
|
|
|
///
|
|
|
|
/// // Invalid cases will be caught by `check_method_receiver`:
|
|
|
|
/// fn method_err1(self: &'a mut T); // ExplicitSelf::Other
|
|
|
|
/// fn method_err2(self: &'static T) // ExplicitSelf::ByValue
|
|
|
|
/// fn method_err3(self: &&T) // ExplicitSelf::ByReference
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
pub fn determine<P>(self_arg_ty: Ty<'tcx>, is_self_ty: P) -> ExplicitSelf<'tcx>
|
|
|
|
where
|
|
|
|
P: Fn(Ty<'tcx>) -> bool,
|
|
|
|
{
|
|
|
|
use self::ExplicitSelf::*;
|
|
|
|
|
2020-08-02 22:49:11 +00:00
|
|
|
match *self_arg_ty.kind() {
|
2017-11-08 10:27:39 +00:00
|
|
|
_ if is_self_ty(self_arg_ty) => ByValue,
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Ref(region, ty, mutbl) if is_self_ty(ty) => ByReference(region, mutbl),
|
|
|
|
ty::RawPtr(ty::TypeAndMut { ty, mutbl }) if is_self_ty(ty) => ByRawPointer(mutbl),
|
|
|
|
ty::Adt(def, _) if def.is_box() && is_self_ty(self_arg_ty.boxed_ty()) => ByBox,
|
2017-11-08 10:27:39 +00:00
|
|
|
_ => Other,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-30 20:28:16 +00:00
|
|
|
|
|
|
|
/// Returns a list of types such that the given type needs drop if and only if
|
|
|
|
/// *any* of the returned types need drop. Returns `Err(AlwaysRequiresDrop)` if
|
|
|
|
/// this type always needs drop.
|
2021-12-16 00:32:30 +00:00
|
|
|
pub fn needs_drop_components<'tcx>(
|
2023-07-18 13:50:34 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-01-31 22:22:30 +00:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
) -> Result<SmallVec<[Ty<'tcx>; 2]>, AlwaysRequiresDrop> {
|
2023-07-18 13:50:34 +00:00
|
|
|
match *ty.kind() {
|
2020-01-30 20:28:16 +00:00
|
|
|
ty::Infer(ty::FreshIntTy(_))
|
|
|
|
| ty::Infer(ty::FreshFloatTy(_))
|
|
|
|
| ty::Bool
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Never
|
|
|
|
| ty::FnDef(..)
|
|
|
|
| ty::FnPtr(_)
|
|
|
|
| ty::Char
|
|
|
|
| ty::RawPtr(_)
|
|
|
|
| ty::Ref(..)
|
|
|
|
| ty::Str => Ok(SmallVec::new()),
|
|
|
|
|
2020-02-09 10:16:57 +00:00
|
|
|
// Foreign types can never have destructors.
|
2020-01-30 20:28:16 +00:00
|
|
|
ty::Foreign(..) => Ok(SmallVec::new()),
|
|
|
|
|
2020-05-06 04:02:09 +00:00
|
|
|
ty::Dynamic(..) | ty::Error(_) => Err(AlwaysRequiresDrop),
|
2020-01-30 20:28:16 +00:00
|
|
|
|
2023-07-18 13:50:34 +00:00
|
|
|
ty::Slice(ty) => needs_drop_components(tcx, ty),
|
2020-01-31 22:22:30 +00:00
|
|
|
ty::Array(elem_ty, size) => {
|
2023-07-18 13:50:34 +00:00
|
|
|
match needs_drop_components(tcx, elem_ty) {
|
2020-01-30 20:28:16 +00:00
|
|
|
Ok(v) if v.is_empty() => Ok(v),
|
2023-07-18 13:50:34 +00:00
|
|
|
res => match size.try_to_target_usize(tcx) {
|
2020-01-31 22:22:30 +00:00
|
|
|
// Arrays of size zero don't need drop, even if their element
|
|
|
|
// type does.
|
|
|
|
Some(0) => Ok(SmallVec::new()),
|
|
|
|
Some(_) => res,
|
|
|
|
// We don't know which of the cases above we are in, so
|
|
|
|
// return the whole type and let the caller decide what to
|
|
|
|
// do.
|
|
|
|
None => Ok(smallvec![ty]),
|
|
|
|
},
|
2020-01-30 20:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// If any field needs drop, then the whole tuple does.
|
2022-02-07 15:06:31 +00:00
|
|
|
ty::Tuple(fields) => fields.iter().try_fold(SmallVec::new(), move |mut acc, elem| {
|
2023-07-18 13:50:34 +00:00
|
|
|
acc.extend(needs_drop_components(tcx, elem)?);
|
2020-01-30 20:28:16 +00:00
|
|
|
Ok(acc)
|
|
|
|
}),
|
|
|
|
|
|
|
|
// These require checking for `Copy` bounds or `Adt` destructors.
|
|
|
|
ty::Adt(..)
|
2022-11-27 17:52:17 +00:00
|
|
|
| ty::Alias(..)
|
2020-01-30 20:28:16 +00:00
|
|
|
| ty::Param(_)
|
|
|
|
| ty::Bound(..)
|
|
|
|
| ty::Placeholder(..)
|
|
|
|
| ty::Infer(_)
|
2020-03-14 23:24:47 +00:00
|
|
|
| ty::Closure(..)
|
2023-10-24 17:16:15 +00:00
|
|
|
| ty::Coroutine(..)
|
|
|
|
| ty::CoroutineWitness(..) => Ok(smallvec![ty]),
|
2020-01-30 20:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-20 21:10:40 +00:00
|
|
|
pub fn is_trivially_const_drop(ty: Ty<'_>) -> bool {
|
2022-01-19 01:01:52 +00:00
|
|
|
match *ty.kind() {
|
|
|
|
ty::Bool
|
|
|
|
| ty::Char
|
|
|
|
| ty::Int(_)
|
|
|
|
| ty::Uint(_)
|
|
|
|
| ty::Float(_)
|
|
|
|
| ty::Infer(ty::IntVar(_))
|
|
|
|
| ty::Infer(ty::FloatVar(_))
|
|
|
|
| ty::Str
|
|
|
|
| ty::RawPtr(_)
|
|
|
|
| ty::Ref(..)
|
|
|
|
| ty::FnDef(..)
|
2022-01-19 20:59:28 +00:00
|
|
|
| ty::FnPtr(_)
|
2022-01-20 04:07:04 +00:00
|
|
|
| ty::Never
|
|
|
|
| ty::Foreign(_) => true,
|
2022-01-19 01:01:52 +00:00
|
|
|
|
2022-11-27 17:52:17 +00:00
|
|
|
ty::Alias(..)
|
2022-01-19 01:01:52 +00:00
|
|
|
| ty::Dynamic(..)
|
|
|
|
| ty::Error(_)
|
|
|
|
| ty::Bound(..)
|
|
|
|
| ty::Param(_)
|
|
|
|
| ty::Placeholder(_)
|
|
|
|
| ty::Infer(_) => false,
|
|
|
|
|
|
|
|
// Not trivial because they have components, and instead of looking inside,
|
|
|
|
// we'll just perform trait selection.
|
2023-10-19 16:06:43 +00:00
|
|
|
ty::Closure(..) | ty::Coroutine(..) | ty::CoroutineWitness(..) | ty::Adt(..) => false,
|
2022-01-19 01:01:52 +00:00
|
|
|
|
2022-01-20 04:07:04 +00:00
|
|
|
ty::Array(ty, _) | ty::Slice(ty) => is_trivially_const_drop(ty),
|
2022-01-19 01:01:52 +00:00
|
|
|
|
2022-02-07 15:06:31 +00:00
|
|
|
ty::Tuple(tys) => tys.iter().all(|ty| is_trivially_const_drop(ty)),
|
2022-01-19 01:01:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 11:15:06 +00:00
|
|
|
/// Does the equivalent of
|
2023-04-09 21:35:02 +00:00
|
|
|
/// ```ignore (illustrative)
|
2022-11-27 11:15:06 +00:00
|
|
|
/// let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
|
|
|
|
/// folder.tcx().intern_*(&v)
|
|
|
|
/// ```
|
2020-10-24 07:27:15 +00:00
|
|
|
pub fn fold_list<'tcx, F, T>(
|
|
|
|
list: &'tcx ty::List<T>,
|
|
|
|
folder: &mut F,
|
|
|
|
intern: impl FnOnce(TyCtxt<'tcx>, &[T]) -> &'tcx ty::List<T>,
|
2021-05-19 13:01:30 +00:00
|
|
|
) -> Result<&'tcx ty::List<T>, F::Error>
|
2020-10-24 07:27:15 +00:00
|
|
|
where
|
2023-02-22 02:18:40 +00:00
|
|
|
F: FallibleTypeFolder<TyCtxt<'tcx>>,
|
|
|
|
T: TypeFoldable<TyCtxt<'tcx>> + PartialEq + Copy,
|
2020-10-24 07:27:15 +00:00
|
|
|
{
|
|
|
|
let mut iter = list.iter();
|
|
|
|
// Look for the first element that changed
|
2021-12-01 00:55:57 +00:00
|
|
|
match iter.by_ref().enumerate().find_map(|(i, t)| match t.try_fold_with(folder) {
|
2021-05-19 13:01:30 +00:00
|
|
|
Ok(new_t) if new_t == t => None,
|
|
|
|
new_t => Some((i, new_t)),
|
2020-10-24 07:27:15 +00:00
|
|
|
}) {
|
2021-05-19 13:01:30 +00:00
|
|
|
Some((i, Ok(new_t))) => {
|
|
|
|
// An element changed, prepare to intern the resulting list
|
|
|
|
let mut new_list = SmallVec::<[_; 8]>::with_capacity(list.len());
|
|
|
|
new_list.extend_from_slice(&list[..i]);
|
|
|
|
new_list.push(new_t);
|
|
|
|
for t in iter {
|
2021-12-01 00:55:57 +00:00
|
|
|
new_list.push(t.try_fold_with(folder)?)
|
2021-05-19 13:01:30 +00:00
|
|
|
}
|
2023-02-11 09:18:12 +00:00
|
|
|
Ok(intern(folder.interner(), &new_list))
|
2021-05-19 13:01:30 +00:00
|
|
|
}
|
|
|
|
Some((_, Err(err))) => {
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
None => Ok(list),
|
2020-10-24 07:27:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-11 14:49:57 +00:00
|
|
|
#[derive(Copy, Clone, Debug, HashStable, TyEncodable, TyDecodable)]
|
2020-01-30 20:28:16 +00:00
|
|
|
pub struct AlwaysRequiresDrop;
|
2020-04-11 04:50:02 +00:00
|
|
|
|
2022-11-25 19:30:37 +00:00
|
|
|
/// Reveals all opaque types in the given value, replacing them
|
2020-04-11 04:50:02 +00:00
|
|
|
/// with their underlying types.
|
2022-11-25 19:30:37 +00:00
|
|
|
pub fn reveal_opaque_types_in_bounds<'tcx>(
|
2020-04-11 04:50:02 +00:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2023-06-22 18:17:13 +00:00
|
|
|
val: &'tcx ty::List<ty::Clause<'tcx>>,
|
|
|
|
) -> &'tcx ty::List<ty::Clause<'tcx>> {
|
2020-04-11 04:50:02 +00:00
|
|
|
let mut visitor = OpaqueTypeExpander {
|
|
|
|
seen_opaque_tys: FxHashSet::default(),
|
|
|
|
expanded_cache: FxHashMap::default(),
|
|
|
|
primary_def_id: None,
|
|
|
|
found_recursion: false,
|
2021-07-28 12:21:59 +00:00
|
|
|
found_any_recursion: false,
|
2020-04-11 04:50:02 +00:00
|
|
|
check_recursion: false,
|
2023-10-19 21:46:28 +00:00
|
|
|
expand_coroutines: false,
|
2020-04-11 04:50:02 +00:00
|
|
|
tcx,
|
2023-11-13 02:08:14 +00:00
|
|
|
inspect_coroutine_fields: InspectCoroutineFields::No,
|
2020-04-11 04:50:02 +00:00
|
|
|
};
|
2021-12-01 00:55:57 +00:00
|
|
|
val.fold_with(&mut visitor)
|
2020-04-11 04:50:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-20 03:52:04 +00:00
|
|
|
/// Determines whether an item is directly annotated with `doc(hidden)`.
|
2023-03-13 18:54:05 +00:00
|
|
|
fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
|
2022-05-02 07:31:56 +00:00
|
|
|
tcx.get_attrs(def_id, sym::doc)
|
|
|
|
.filter_map(|attr| attr.meta_item_list())
|
2022-03-12 22:27:51 +00:00
|
|
|
.any(|items| items.iter().any(|item| item.has_name(sym::hidden)))
|
|
|
|
}
|
|
|
|
|
2022-09-28 00:06:38 +00:00
|
|
|
/// Determines whether an item is annotated with `doc(notable_trait)`.
|
|
|
|
pub fn is_doc_notable_trait(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
|
|
|
tcx.get_attrs(def_id, sym::doc)
|
|
|
|
.filter_map(|attr| attr.meta_item_list())
|
|
|
|
.any(|items| items.iter().any(|item| item.has_name(sym::notable_trait)))
|
|
|
|
}
|
|
|
|
|
2022-05-13 13:50:21 +00:00
|
|
|
/// Determines whether an item is an intrinsic by Abi.
|
2023-03-13 18:54:05 +00:00
|
|
|
pub fn is_intrinsic(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
|
2023-01-18 23:52:47 +00:00
|
|
|
matches!(tcx.fn_sig(def_id).skip_binder().abi(), Abi::RustIntrinsic | Abi::PlatformIntrinsic)
|
2022-05-13 13:50:21 +00:00
|
|
|
}
|
|
|
|
|
2023-05-15 04:24:45 +00:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
|
|
|
*providers = Providers {
|
2022-11-25 19:30:37 +00:00
|
|
|
reveal_opaque_types_in_bounds,
|
2022-09-28 00:06:38 +00:00
|
|
|
is_doc_hidden,
|
|
|
|
is_doc_notable_trait,
|
|
|
|
is_intrinsic,
|
|
|
|
..*providers
|
|
|
|
}
|
2020-04-11 04:50:02 +00:00
|
|
|
}
|