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;
|
2022-12-07 17:00:33 +00:00
|
|
|
use crate::mir;
|
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-09 14:02:47 +00:00
|
|
|
self, ir::TypeFolder, DefIdTree, FallibleTypeFolder, Ty, TyCtxt, TypeFoldable,
|
|
|
|
TypeSuperFoldable,
|
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
|
|
|
};
|
2022-09-16 13:31:10 +00:00
|
|
|
use crate::ty::{GenericArgKind, SubstsRef};
|
2019-12-11 09:04:34 +00:00
|
|
|
use rustc_apfloat::Float as _;
|
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
|
|
|
use rustc_data_structures::stable_hasher::{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};
|
2020-01-05 01:37:57 +00:00
|
|
|
use rustc_hir::def_id::DefId;
|
2022-03-23 08:41:31 +00:00
|
|
|
use rustc_index::bit_set::GrowableBitSet;
|
2022-12-07 17:00:33 +00:00
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2018-12-03 00:14:35 +00:00
|
|
|
use rustc_macros::HashStable;
|
2022-03-12 22:27:51 +00:00
|
|
|
use rustc_span::{sym, DUMMY_SP};
|
2022-11-01 16:20:30 +00:00
|
|
|
use rustc_target::abi::{Integer, IntegerType, Size, TargetDataLayout};
|
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)]
|
|
|
|
pub enum IgnoreRegions {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
}
|
|
|
|
|
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;
|
2018-03-22 11:38:40 +00:00
|
|
|
write!(fmt, "{}", x)
|
|
|
|
}
|
|
|
|
_ => write!(fmt, "{}", self.val),
|
2018-01-25 15:44:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-09-14 11:55:56 +00:00
|
|
|
|
2019-12-11 09:04:34 +00:00
|
|
|
fn int_size_and_signed<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> (Size, bool) {
|
2020-08-02 22:49:11 +00:00
|
|
|
let (int, signed) = match *ty.kind() {
|
2022-03-23 08:41:31 +00:00
|
|
|
ty::Int(ity) => (Integer::from_int_ty(&tcx, ity), true),
|
|
|
|
ty::Uint(uty) => (Integer::from_uint_ty(&tcx, uty), false),
|
2019-12-11 09:04:34 +00:00
|
|
|
_ => bug!("non integer discriminant"),
|
|
|
|
};
|
|
|
|
(int.size(), signed)
|
|
|
|
}
|
|
|
|
|
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) {
|
2019-12-11 09:04:34 +00:00
|
|
|
let (size, signed) = int_size_and_signed(tcx, self.ty);
|
|
|
|
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.
|
|
|
|
pub fn type_id_hash(self, ty: Ty<'tcx>) -> u64 {
|
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
|
|
|
|
| DefKind::TyAlias
|
|
|
|
| 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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-30 15:58:30 +00:00
|
|
|
pub fn has_error_field(self, ty: Ty<'tcx>) -> bool {
|
2020-08-02 22:49:11 +00:00
|
|
|
if let ty::Adt(def, substs) = *ty.kind() {
|
2018-10-02 08:56:36 +00:00
|
|
|
for field in def.all_fields() {
|
|
|
|
let field_ty = field.ty(self, substs);
|
2022-03-23 08:41:31 +00:00
|
|
|
if let ty::Error(_) = field_ty.kind() {
|
2018-10-02 08:56:36 +00:00
|
|
|
return true;
|
2016-07-30 15:58:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
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) {
|
2020-11-26 15:32:41 +00:00
|
|
|
return self.ty_error_with_message(
|
|
|
|
DUMMY_SP,
|
|
|
|
&format!("reached the recursion limit finding the struct tail for {}", ty),
|
|
|
|
);
|
|
|
|
}
|
2020-08-02 22:49:11 +00:00
|
|
|
match *ty.kind() {
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Adt(def, substs) => {
|
2017-05-26 20:36:40 +00:00
|
|
|
if !def.is_struct() {
|
|
|
|
break;
|
|
|
|
}
|
2018-01-07 21:41:41 +00:00
|
|
|
match def.non_enum_variant().fields.last() {
|
2022-04-26 08:56:04 +00:00
|
|
|
Some(field) => {
|
|
|
|
f();
|
|
|
|
ty = field.ty(self, substs);
|
|
|
|
}
|
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()) {
|
2022-03-23 08:41:31 +00:00
|
|
|
(&ty::Adt(a_def, a_substs), &ty::Adt(b_def, b_substs))
|
2017-06-08 05:49:54 +00:00
|
|
|
if a_def == b_def && a_def.is_struct() =>
|
|
|
|
{
|
2018-01-07 21:41:41 +00:00
|
|
|
if let Some(f) = a_def.non_enum_variant().fields.last() {
|
2017-06-08 05:49:54 +00:00
|
|
|
a = f.ty(self, a_substs);
|
|
|
|
b = f.ty(self, b_substs);
|
|
|
|
} 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()?;
|
2019-01-26 10:47:56 +00:00
|
|
|
self.ensure().coherent_trait(drop_trait);
|
2017-03-01 16:42:26 +00:00
|
|
|
|
2023-02-07 00:48:12 +00:00
|
|
|
let ty = self.bound_type_of(adt_did).subst_identity();
|
2021-09-01 11:06:15 +00:00
|
|
|
let (did, constness) = self.find_map_relevant_impl(drop_trait, ty, |impl_did| {
|
2021-11-05 18:47:52 +00:00
|
|
|
if let Some(item_id) = self.associated_item_def_ids(impl_did).first() {
|
2018-10-02 08:56:36 +00:00
|
|
|
if validate(self, impl_did).is_ok() {
|
2022-06-15 10:54:43 +00:00
|
|
|
return Some((*item_id, self.constness(impl_did)));
|
2017-03-01 16:42:26 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-09 13:28:32 +00:00
|
|
|
None
|
2021-09-01 11:06:15 +00:00
|
|
|
})?;
|
2017-03-01 16:42:26 +00:00
|
|
|
|
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`.
|
2022-03-04 20:28:41 +00:00
|
|
|
pub fn destructor_constraints(self, def: ty::AdtDef<'tcx>) -> Vec<ty::subst::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
|
|
|
|
// generics/substs that correspond to impure parameters on the
|
|
|
|
// impl's generics. This is a bit ugly, but conceptually simple:
|
|
|
|
//
|
|
|
|
// Suppose our ADT looks like the following
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// up the item-substs <X, Y, Z> with the substs on the impl ADT,
|
|
|
|
// <P1, P2, P0>, and then look up which of the impl substs refer to
|
|
|
|
// parameters marked as pure.
|
|
|
|
|
2023-02-07 00:48:12 +00:00
|
|
|
let impl_substs = match *self.bound_type_of(impl_def_id).subst_identity().kind() {
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Adt(def_, substs) if def_ == def => substs,
|
2017-04-23 14:43:23 +00:00
|
|
|
_ => bug!(),
|
|
|
|
};
|
|
|
|
|
2023-02-07 00:48:12 +00:00
|
|
|
let item_substs = match *self.bound_type_of(def.did()).subst_identity().kind() {
|
2018-08-22 00:35:02 +00:00
|
|
|
ty::Adt(def_, substs) if def_ == def => substs,
|
2017-04-23 14:43:23 +00:00
|
|
|
_ => bug!(),
|
|
|
|
};
|
|
|
|
|
2021-03-08 23:32:41 +00:00
|
|
|
let result = iter::zip(item_substs, impl_substs)
|
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() {
|
2022-03-23 08:41:31 +00:00
|
|
|
ty::ReEarlyBound(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,
|
|
|
|
substs: SubstsRef<'tcx>,
|
2022-04-04 08:56:59 +00:00
|
|
|
ignore_regions: IgnoreRegions,
|
2022-03-23 08:41:31 +00:00
|
|
|
) -> Result<(), NotUniqueParam<'tcx>> {
|
|
|
|
let mut seen = GrowableBitSet::default();
|
|
|
|
for arg in substs {
|
|
|
|
match arg.unpack() {
|
|
|
|
GenericArgKind::Lifetime(lt) => {
|
2022-04-04 08:56:59 +00:00
|
|
|
if ignore_regions == IgnoreRegions::No {
|
|
|
|
let ty::ReEarlyBound(p) = lt.kind() else {
|
|
|
|
return Err(NotUniqueParam::NotParam(lt.into()))
|
|
|
|
};
|
|
|
|
if !seen.insert(p.index) {
|
|
|
|
return Err(NotUniqueParam::DuplicateParam(lt.into()));
|
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(())
|
|
|
|
}
|
|
|
|
|
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.
|
2017-11-10 17:20:53 +00:00
|
|
|
pub fn is_closure(self, def_id: DefId) -> bool {
|
2020-04-17 18:55:17 +00:00
|
|
|
matches!(self.def_kind(def_id), DefKind::Closure | DefKind::Generator)
|
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
|
|
|
|
/// type-checking context, i.e. closure, generator or inline const.
|
|
|
|
pub fn is_typeck_child(self, def_id: DefId) -> bool {
|
2021-10-02 12:12:33 +00:00
|
|
|
matches!(
|
|
|
|
self.def_kind(def_id),
|
|
|
|
DefKind::Closure | DefKind::Generator | DefKind::InlineConst
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-02-08 13:53:55 +00:00
|
|
|
/// Given the `DefId` and substs 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,
|
|
|
|
closure_def_id: DefId,
|
2019-09-26 16:09:51 +00:00
|
|
|
closure_substs: SubstsRef<'tcx>,
|
2023-02-13 02:03:45 +00:00
|
|
|
env_region: ty::Region<'tcx>,
|
2020-10-26 18:18:31 +00:00
|
|
|
) -> Option<Ty<'tcx>> {
|
2017-11-21 16:18:40 +00:00
|
|
|
let closure_ty = self.mk_closure(closure_def_id, closure_substs);
|
2020-03-13 01:23:38 +00:00
|
|
|
let closure_kind_ty = closure_substs.as_closure().kind_ty();
|
2017-11-21 16:18:40 +00:00
|
|
|
let closure_kind = closure_kind_ty.to_opt_closure_kind()?;
|
|
|
|
let env_ty = match closure_kind {
|
2023-02-13 02:03:45 +00:00
|
|
|
ty::ClosureKind::Fn => self.mk_imm_ref(env_region, closure_ty),
|
|
|
|
ty::ClosureKind::FnMut => self.mk_mut_ref(env_region, closure_ty),
|
2017-11-21 16:18:40 +00:00
|
|
|
ty::ClosureKind::FnOnce => closure_ty,
|
|
|
|
};
|
2020-10-26 18:18:31 +00:00
|
|
|
Some(env_ty)
|
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
|
|
|
|
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(),
|
|
|
|
self.bound_type_of(def_id).subst_identity(),
|
|
|
|
);
|
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) {
|
|
|
|
self.mk_mut_ptr(static_ty)
|
2020-10-19 07:47:18 +00:00
|
|
|
} else if self.is_foreign_item(def_id) {
|
|
|
|
self.mk_imm_ptr(static_ty)
|
2019-11-26 22:30:54 +00:00
|
|
|
} else {
|
|
|
|
self.mk_imm_ref(self.lifetimes.re_erased, static_ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-01 13:57:22 +00:00
|
|
|
/// Return the set of types that should be taken into accound when checking
|
|
|
|
/// trait bounds on a generator's internal state.
|
|
|
|
pub fn generator_hidden_types(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> impl Iterator<Item = ty::EarlyBinder<Ty<'tcx>>> {
|
|
|
|
let generator_layout = &self.mir_generator_witnesses(def_id);
|
|
|
|
generator_layout
|
|
|
|
.field_tys
|
|
|
|
.iter()
|
2023-01-21 10:03:12 +00:00
|
|
|
.filter(|decl| !decl.ignore_for_traits)
|
2022-10-01 13:57:22 +00:00
|
|
|
.map(|decl| ty::EarlyBinder(decl.ty))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Normalizes all opaque types in the given value, replacing them
|
|
|
|
/// with their underlying types.
|
|
|
|
pub fn expand_opaque_types(self, val: Ty<'tcx>) -> Ty<'tcx> {
|
|
|
|
let mut visitor = OpaqueTypeExpander {
|
|
|
|
seen_opaque_tys: FxHashSet::default(),
|
|
|
|
expanded_cache: FxHashMap::default(),
|
|
|
|
primary_def_id: None,
|
|
|
|
found_recursion: false,
|
|
|
|
found_any_recursion: false,
|
|
|
|
check_recursion: false,
|
|
|
|
expand_generators: false,
|
|
|
|
tcx: self,
|
|
|
|
};
|
|
|
|
val.fold_with(&mut visitor)
|
|
|
|
}
|
|
|
|
|
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,
|
2019-02-09 14:11:53 +00:00
|
|
|
substs: SubstsRef<'tcx>,
|
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,
|
2022-10-01 13:57:22 +00:00
|
|
|
expand_generators: true,
|
2018-11-18 18:33:44 +00:00
|
|
|
tcx: self,
|
|
|
|
};
|
2020-04-11 04:50:02 +00:00
|
|
|
|
2018-11-18 18:33:44 +00:00
|
|
|
let expanded_type = visitor.expand_opaque_ty(def_id, substs).unwrap();
|
|
|
|
if visitor.found_recursion { Err(expanded_type) } else { Ok(expanded_type) }
|
|
|
|
}
|
2022-05-08 19:12:56 +00:00
|
|
|
|
2022-12-28 04:16:36 +00:00
|
|
|
pub fn bound_return_position_impl_trait_in_trait_tys(
|
2022-09-02 21:02:59 +00:00
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> ty::EarlyBinder<Result<&'tcx FxHashMap<DefId, Ty<'tcx>>, ErrorGuaranteed>> {
|
2022-12-28 04:16:36 +00:00
|
|
|
ty::EarlyBinder(self.collect_return_position_impl_trait_in_trait_tys(def_id))
|
2022-09-02 21:02:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 02:28:50 +00:00
|
|
|
pub fn bound_explicit_item_bounds(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
2022-03-23 08:41:31 +00:00
|
|
|
) -> ty::EarlyBinder<&'tcx [(ty::Predicate<'tcx>, rustc_span::Span)]> {
|
|
|
|
ty::EarlyBinder(self.explicit_item_bounds(def_id))
|
2022-05-11 02:28:50 +00:00
|
|
|
}
|
|
|
|
|
2022-08-03 05:02:46 +00:00
|
|
|
pub fn bound_impl_subject(self, def_id: DefId) -> ty::EarlyBinder<ty::ImplSubject<'tcx>> {
|
|
|
|
ty::EarlyBinder(self.impl_subject(def_id))
|
|
|
|
}
|
2022-12-07 17:00:33 +00:00
|
|
|
|
|
|
|
/// Returns names of captured upvars for closures and generators.
|
|
|
|
///
|
|
|
|
/// Here are some examples:
|
|
|
|
/// - `name__field1__field2` when the upvar is captured by value.
|
|
|
|
/// - `_ref__name__field` when the upvar is captured by reference.
|
|
|
|
///
|
|
|
|
/// For generators this only contains upvars that are shared by all states.
|
|
|
|
pub fn closure_saved_names_of_captured_variables(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> SmallVec<[String; 16]> {
|
|
|
|
let body = self.optimized_mir(def_id);
|
|
|
|
|
|
|
|
body.var_debug_info
|
|
|
|
.iter()
|
|
|
|
.filter_map(|var| {
|
|
|
|
let is_ref = match var.value {
|
|
|
|
mir::VarDebugInfoContents::Place(place)
|
|
|
|
if place.local == mir::Local::new(1) =>
|
|
|
|
{
|
|
|
|
// The projection is either `[.., Field, Deref]` or `[.., Field]`. It
|
|
|
|
// implies whether the variable is captured by value or by reference.
|
|
|
|
matches!(place.projection.last().unwrap(), mir::ProjectionElem::Deref)
|
|
|
|
}
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
let prefix = if is_ref { "_ref__" } else { "" };
|
|
|
|
Some(prefix.to_owned() + var.name.as_str())
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(eddyb) maybe precompute this? Right now it's computed once
|
|
|
|
// per generator monomorphization, but it doesn't depend on substs.
|
|
|
|
pub fn generator_layout_and_saved_local_names(
|
|
|
|
self,
|
|
|
|
def_id: DefId,
|
|
|
|
) -> (
|
|
|
|
&'tcx ty::GeneratorLayout<'tcx>,
|
|
|
|
IndexVec<mir::GeneratorSavedLocal, Option<rustc_span::Symbol>>,
|
|
|
|
) {
|
|
|
|
let tcx = self;
|
|
|
|
let body = tcx.optimized_mir(def_id);
|
|
|
|
let generator_layout = body.generator_layout().unwrap();
|
|
|
|
let mut generator_saved_local_names =
|
|
|
|
IndexVec::from_elem(None, &generator_layout.field_tys);
|
|
|
|
|
|
|
|
let state_arg = mir::Local::new(1);
|
|
|
|
for var in &body.var_debug_info {
|
|
|
|
let mir::VarDebugInfoContents::Place(place) = &var.value else { continue };
|
|
|
|
if place.local != state_arg {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
match place.projection[..] {
|
|
|
|
[
|
|
|
|
// Deref of the `Pin<&mut Self>` state argument.
|
|
|
|
mir::ProjectionElem::Field(..),
|
|
|
|
mir::ProjectionElem::Deref,
|
|
|
|
// Field of a variant of the state.
|
|
|
|
mir::ProjectionElem::Downcast(_, variant),
|
|
|
|
mir::ProjectionElem::Field(field, _),
|
|
|
|
] => {
|
|
|
|
let name = &mut generator_saved_local_names
|
|
|
|
[generator_layout.variant_fields[variant][field]];
|
|
|
|
if name.is_none() {
|
|
|
|
name.replace(var.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(generator_layout, generator_saved_local_names)
|
|
|
|
}
|
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.
|
|
|
|
expanded_cache: FxHashMap<(DefId, SubstsRef<'tcx>), Ty<'tcx>>,
|
|
|
|
primary_def_id: Option<DefId>,
|
|
|
|
found_recursion: bool,
|
2021-07-28 12:21:59 +00:00
|
|
|
found_any_recursion: bool,
|
2022-10-01 13:57:22 +00:00
|
|
|
expand_generators: 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>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> OpaqueTypeExpander<'tcx> {
|
|
|
|
fn expand_opaque_ty(&mut self, def_id: DefId, substs: SubstsRef<'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;
|
|
|
|
}
|
2021-12-01 00:55:57 +00:00
|
|
|
let substs = substs.fold_with(self);
|
2020-04-11 04:50:02 +00:00
|
|
|
if !self.check_recursion || self.seen_opaque_tys.insert(def_id) {
|
|
|
|
let expanded_ty = match self.expanded_cache.get(&(def_id, substs)) {
|
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 => {
|
2022-05-08 19:12:56 +00:00
|
|
|
let generic_ty = self.tcx.bound_type_of(def_id);
|
2020-04-11 04:50:02 +00:00
|
|
|
let concrete_ty = generic_ty.subst(self.tcx, substs);
|
2021-12-01 00:55:57 +00:00
|
|
|
let expanded_ty = self.fold_ty(concrete_ty);
|
2020-04-11 04:50:02 +00:00
|
|
|
self.expanded_cache.insert((def_id, substs), expanded_ty);
|
|
|
|
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
|
|
|
|
|
|
|
fn expand_generator(&mut self, def_id: DefId, substs: SubstsRef<'tcx>) -> Option<Ty<'tcx>> {
|
|
|
|
if self.found_any_recursion {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let substs = substs.fold_with(self);
|
|
|
|
if !self.check_recursion || self.seen_opaque_tys.insert(def_id) {
|
|
|
|
let expanded_ty = match self.expanded_cache.get(&(def_id, substs)) {
|
|
|
|
Some(expanded_ty) => *expanded_ty,
|
|
|
|
None => {
|
|
|
|
for bty in self.tcx.generator_hidden_types(def_id) {
|
|
|
|
let hidden_ty = bty.subst(self.tcx, substs);
|
|
|
|
self.fold_ty(hidden_ty);
|
|
|
|
}
|
|
|
|
let expanded_ty = self.tcx.mk_generator_witness_mir(def_id, substs);
|
|
|
|
self.expanded_cache.insert((def_id, substs), expanded_ty);
|
|
|
|
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> {
|
2022-10-01 13:57:22 +00:00
|
|
|
let mut t = if let ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) = *t.kind() {
|
2021-12-01 00:55:57 +00:00
|
|
|
self.expand_opaque_ty(def_id, substs).unwrap_or(t)
|
2022-10-01 13:57:22 +00:00
|
|
|
} else if t.has_opaque_types() || t.has_generators() {
|
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
|
|
|
};
|
|
|
|
if self.expand_generators {
|
|
|
|
if let ty::GeneratorWitnessMIR(def_id, substs) = *t.kind() {
|
|
|
|
t = self.expand_generator(def_id, substs).unwrap_or(t);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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> {
|
2019-12-11 09:04:34 +00:00
|
|
|
/// Returns the maximum 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_max_val(self, tcx: TyCtxt<'tcx>) -> Option<ty::Const<'tcx>> {
|
2020-08-02 22:49:11 +00:00
|
|
|
let val = match self.kind() {
|
2019-12-11 09:04:34 +00:00
|
|
|
ty::Int(_) | ty::Uint(_) => {
|
|
|
|
let (size, signed) = int_size_and_signed(tcx, self);
|
2021-09-07 18:44:33 +00:00
|
|
|
let val =
|
|
|
|
if signed { size.signed_int_max() as u128 } else { size.unsigned_int_max() };
|
2019-12-11 09:04:34 +00:00
|
|
|
Some(val)
|
|
|
|
}
|
|
|
|
ty::Char => Some(std::char::MAX as u128),
|
|
|
|
ty::Float(fty) => Some(match fty {
|
2020-12-12 14:28:49 +00:00
|
|
|
ty::FloatTy::F32 => rustc_apfloat::ieee::Single::INFINITY.to_bits(),
|
|
|
|
ty::FloatTy::F64 => rustc_apfloat::ieee::Double::INFINITY.to_bits(),
|
2019-12-11 09:04:34 +00:00
|
|
|
}),
|
|
|
|
_ => None,
|
|
|
|
};
|
2022-04-12 16:14:28 +00:00
|
|
|
|
|
|
|
val.map(|v| ty::Const::from_bits(tcx, v, 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>> {
|
2020-08-02 22:49:11 +00:00
|
|
|
let val = match self.kind() {
|
2019-12-11 09:04:34 +00:00
|
|
|
ty::Int(_) | ty::Uint(_) => {
|
|
|
|
let (size, signed) = int_size_and_signed(tcx, self);
|
2021-09-07 18:44:33 +00:00
|
|
|
let val = if signed { size.truncate(size.signed_int_min() as u128) } else { 0 };
|
2019-12-11 09:04:34 +00:00
|
|
|
Some(val)
|
|
|
|
}
|
|
|
|
ty::Char => Some(0),
|
|
|
|
ty::Float(fty) => Some(match fty {
|
2020-12-12 14:28:49 +00:00
|
|
|
ty::FloatTy::F32 => (-::rustc_apfloat::ieee::Single::INFINITY).to_bits(),
|
|
|
|
ty::FloatTy::F64 => (-::rustc_apfloat::ieee::Double::INFINITY).to_bits(),
|
2019-12-11 09:04:34 +00:00
|
|
|
}),
|
|
|
|
_ => None,
|
|
|
|
};
|
2022-04-12 16:14:28 +00:00
|
|
|
|
|
|
|
val.map(|v| ty::Const::from_bits(tcx, v, 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(_)
|
|
|
|
| ty::Generator(..)
|
|
|
|
| ty::GeneratorWitness(_)
|
2022-10-01 12:56:24 +00:00
|
|
|
| ty::GeneratorWitnessMIR(..)
|
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(_)
|
|
|
|
| ty::Generator(..)
|
|
|
|
| ty::GeneratorWitness(_)
|
2022-10-01 12:56:24 +00:00
|
|
|
| ty::GeneratorWitnessMIR(..)
|
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.
|
2020-01-31 22:22:30 +00:00
|
|
|
match needs_drop_components(self, &tcx.data_layout) {
|
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`.
|
|
|
|
let query_ty =
|
|
|
|
tcx.try_normalize_erasing_regions(param_env, query_ty).unwrap_or(query_ty);
|
|
|
|
|
|
|
|
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.
|
|
|
|
match needs_drop_components(self, &tcx.data_layout) {
|
|
|
|
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).
|
|
|
|
if query_ty.needs_infer() {
|
|
|
|
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
|
2022-03-23 08:41:31 +00:00
|
|
|
ty::FnDef(..) | ty::Closure(..) | ty::Dynamic(..) | ty::Generator(..) => 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
|
|
|
|
2022-10-01 12:56:24 +00:00
|
|
|
ty::Foreign(_)
|
|
|
|
| ty::GeneratorWitness(..)
|
|
|
|
| ty::GeneratorWitnessMIR(..)
|
|
|
|
| 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`
|
|
|
|
/// into either `self`, `&self`, `&mut self`, `Box<self>`, or
|
|
|
|
/// `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>(
|
2020-01-31 22:22:30 +00:00
|
|
|
ty: Ty<'tcx>,
|
|
|
|
target_layout: &TargetDataLayout,
|
|
|
|
) -> Result<SmallVec<[Ty<'tcx>; 2]>, AlwaysRequiresDrop> {
|
2020-08-02 22:49:11 +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::GeneratorWitness(..)
|
2022-10-01 12:56:24 +00:00
|
|
|
| ty::GeneratorWitnessMIR(..)
|
2020-01-30 20:28:16 +00:00
|
|
|
| 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
|
|
|
|
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::Slice(ty) => needs_drop_components(*ty, target_layout),
|
2020-01-31 22:22:30 +00:00
|
|
|
ty::Array(elem_ty, size) => {
|
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
|
|
|
match needs_drop_components(*elem_ty, target_layout) {
|
2020-01-30 20:28:16 +00:00
|
|
|
Ok(v) if v.is_empty() => Ok(v),
|
2022-06-10 01:18:06 +00:00
|
|
|
res => match size.kind().try_to_bits(target_layout.pointer_size) {
|
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| {
|
2020-01-31 22:22:30 +00:00
|
|
|
acc.extend(needs_drop_components(elem, target_layout)?);
|
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(..)
|
|
|
|
| ty::Generator(..) => 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.
|
2022-10-01 12:56:24 +00:00
|
|
|
ty::Closure(..)
|
|
|
|
| ty::Generator(..)
|
|
|
|
| ty::GeneratorWitness(_)
|
|
|
|
| ty::GeneratorWitnessMIR(..)
|
|
|
|
| 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
|
|
|
|
/// ```ignore (ilustrative)
|
|
|
|
/// 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
|
2021-12-01 15:11:24 +00:00
|
|
|
F: FallibleTypeFolder<'tcx>,
|
2020-10-24 07:27:15 +00:00
|
|
|
T: TypeFoldable<'tcx> + PartialEq + Copy,
|
|
|
|
{
|
|
|
|
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>,
|
2022-03-23 08:41:31 +00:00
|
|
|
val: &'tcx ty::List<ty::Predicate<'tcx>>,
|
|
|
|
) -> &'tcx ty::List<ty::Predicate<'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,
|
2022-10-01 13:57:22 +00:00
|
|
|
expand_generators: false,
|
2020-04-11 04:50:02 +00:00
|
|
|
tcx,
|
|
|
|
};
|
2021-12-01 00:55:57 +00:00
|
|
|
val.fold_with(&mut visitor)
|
2020-04-11 04:50:02 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 22:27:51 +00:00
|
|
|
/// Determines whether an item is annotated with `doc(hidden)`.
|
2023-01-20 20:59:19 +00:00
|
|
|
fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
|
|
|
|
assert!(def_id.is_local());
|
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.
|
|
|
|
pub fn is_intrinsic(tcx: TyCtxt<'_>, def_id: DefId) -> 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
|
|
|
}
|
|
|
|
|
2020-07-22 05:13:42 +00:00
|
|
|
pub fn provide(providers: &mut ty::query::Providers) {
|
2022-09-28 00:06:38 +00:00
|
|
|
*providers = ty::query::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
|
|
|
}
|