Auto merge of #122493 - lukas-code:sized-constraint, r=lcnr
clean up `Sized` checking This PR cleans up `sized_constraint` and related functions to make them simpler and faster. This should not make more or less code compile, but it can change error output in some rare cases. ## enums and unions are `Sized`, even if they are not WF The previous code has some special handling for enums, which made them sized if and only if the last field of each variant is sized. For example given this definition (which is not WF) ```rust enum E<T1: ?Sized, T2: ?Sized, U1: ?Sized, U2: ?Sized> { A(T1, T2), B(U1, U2), } ``` the enum was sized if and only if `T2` and `U2` are sized, while `T1` and `T2` were ignored for `Sized` checking. After this PR this enum will always be sized. Unsized enums are not a thing in Rust and removing this special case allows us to return an `Option<Ty>` from `sized_constraint`, rather than a `List<Ty>`. Similarly, the old code made an union defined like this ```rust union Union<T: ?Sized, U: ?Sized> { head: T, tail: U, } ``` sized if and only if `U` is sized, completely ignoring `T`. This just makes no sense at all and now this union is always sized. ## apply the "perf hack" to all (non-error) types, instead of just type parameters This "perf hack" skips evaluating `sized_constraint(adt): Sized` if `sized_constraint(adt): Sized` exactly matches a predicate defined on `adt`, for example: ```rust // `Foo<T>: Sized` iff `T: Sized`, but we know `T: Sized` from a predicate of `Foo` struct Foo<T /*: Sized */>(T); ``` Previously this was only applied to type parameters and now it is applied to every type. This means that for example this type is now always sized: ```rust // Note that this definition is WF, but the type `S<T>` not WF in the global/empty ParamEnv struct S<T>([T]) where [T]: Sized; ``` I don't anticipate this to affect compile time of any real-world program, but it makes the code a bit nicer and it also makes error messages a bit more consistent if someone does write such a cursed type. ## tuples are sized if the last type is sized The old solver already has this behavior and this PR also implements it for the new solver and `is_trivially_sized`. This makes it so that tuples work more like a struct defined like this: ```rust struct TupleN<T1, T2, /* ... */ Tn: ?Sized>(T1, T2, /* ... */ Tn); ``` This might improve the compile time of programs with large tuples a little, but is mostly also a consistency fix. ## `is_trivially_sized` for more types This function is used post-typeck code (borrowck, const eval, codegen) to skip evaluating `T: Sized` in some cases. It will now return `true` in more cases, most notably `UnsafeCell<T>` and `ManuallyDrop<T>` where `T.is_trivially_sized`. I'm anticipating that this change will improve compile time for some real world programs.
This commit is contained in:
commit
196ff446d2
14 changed files with 121 additions and 121 deletions
|
@ -12,7 +12,7 @@ pub(crate) fn provide(providers: &mut Providers) {
|
|||
macro_rules! rtry {
|
||||
($e:expr) => {
|
||||
match $e {
|
||||
e @ Representability::Infinite => return e,
|
||||
e @ Representability::Infinite(_) => return e,
|
||||
Representability::Representable => {}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -1,78 +1,56 @@
|
|||
use rustc_data_structures::fx::FxHashSet;
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def::DefKind;
|
||||
use rustc_hir::LangItem;
|
||||
use rustc_index::bit_set::BitSet;
|
||||
use rustc_middle::query::Providers;
|
||||
use rustc_middle::ty::{self, EarlyBinder, Ty, TyCtxt, TypeVisitor};
|
||||
use rustc_middle::ty::{self, EarlyBinder, Ty, TyCtxt, TypeVisitableExt, TypeVisitor};
|
||||
use rustc_middle::ty::{ToPredicate, TypeSuperVisitable, TypeVisitable};
|
||||
use rustc_span::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
|
||||
use rustc_span::DUMMY_SP;
|
||||
use rustc_trait_selection::traits;
|
||||
|
||||
fn sized_constraint_for_ty<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
adtdef: ty::AdtDef<'tcx>,
|
||||
ty: Ty<'tcx>,
|
||||
) -> Vec<Ty<'tcx>> {
|
||||
#[instrument(level = "debug", skip(tcx), ret)]
|
||||
fn sized_constraint_for_ty<'tcx>(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
|
||||
use rustc_type_ir::TyKind::*;
|
||||
|
||||
let result = match ty.kind() {
|
||||
Bool | Char | Int(..) | Uint(..) | Float(..) | RawPtr(..) | Ref(..) | FnDef(..)
|
||||
| FnPtr(_) | Array(..) | Closure(..) | CoroutineClosure(..) | Coroutine(..) | Never => {
|
||||
vec![]
|
||||
}
|
||||
match ty.kind() {
|
||||
// these are always sized
|
||||
Bool
|
||||
| Char
|
||||
| Int(..)
|
||||
| Uint(..)
|
||||
| Float(..)
|
||||
| RawPtr(..)
|
||||
| Ref(..)
|
||||
| FnDef(..)
|
||||
| FnPtr(..)
|
||||
| Array(..)
|
||||
| Closure(..)
|
||||
| CoroutineClosure(..)
|
||||
| Coroutine(..)
|
||||
| CoroutineWitness(..)
|
||||
| Never
|
||||
| Dynamic(_, _, ty::DynStar) => None,
|
||||
|
||||
Str | Dynamic(..) | Slice(_) | Foreign(..) | Error(_) | CoroutineWitness(..) => {
|
||||
// these are never sized - return the target type
|
||||
vec![ty]
|
||||
}
|
||||
// these are never sized
|
||||
Str | Slice(..) | Dynamic(_, _, ty::Dyn) | Foreign(..) => Some(ty),
|
||||
|
||||
Tuple(tys) => match tys.last() {
|
||||
None => vec![],
|
||||
Some(&ty) => sized_constraint_for_ty(tcx, adtdef, ty),
|
||||
},
|
||||
Tuple(tys) => tys.last().and_then(|&ty| sized_constraint_for_ty(tcx, ty)),
|
||||
|
||||
Adt(adt, args) => {
|
||||
// recursive case
|
||||
let adt_tys = adt.sized_constraint(tcx);
|
||||
debug!("sized_constraint_for_ty({:?}) intermediate = {:?}", ty, adt_tys);
|
||||
adt_tys
|
||||
.iter_instantiated(tcx, args)
|
||||
.flat_map(|ty| sized_constraint_for_ty(tcx, adtdef, ty))
|
||||
.collect()
|
||||
}
|
||||
// recursive case
|
||||
Adt(adt, args) => adt.sized_constraint(tcx).and_then(|intermediate| {
|
||||
let ty = intermediate.instantiate(tcx, args);
|
||||
sized_constraint_for_ty(tcx, ty)
|
||||
}),
|
||||
|
||||
Alias(..) => {
|
||||
// must calculate explicitly.
|
||||
// FIXME: consider special-casing always-Sized projections
|
||||
vec![ty]
|
||||
}
|
||||
|
||||
Param(..) => {
|
||||
// perf hack: if there is a `T: Sized` bound, then
|
||||
// we know that `T` is Sized and do not need to check
|
||||
// it on the impl.
|
||||
|
||||
let Some(sized_trait_def_id) = tcx.lang_items().sized_trait() else { return vec![ty] };
|
||||
let predicates = tcx.predicates_of(adtdef.did()).predicates;
|
||||
if predicates.iter().any(|(p, _)| {
|
||||
p.as_trait_clause().is_some_and(|trait_pred| {
|
||||
trait_pred.def_id() == sized_trait_def_id
|
||||
&& trait_pred.self_ty().skip_binder() == ty
|
||||
})
|
||||
}) {
|
||||
vec![]
|
||||
} else {
|
||||
vec![ty]
|
||||
}
|
||||
}
|
||||
// these can be sized or unsized
|
||||
Param(..) | Alias(..) | Error(_) => Some(ty),
|
||||
|
||||
Placeholder(..) | Bound(..) | Infer(..) => {
|
||||
bug!("unexpected type `{:?}` in sized_constraint_for_ty", ty)
|
||||
bug!("unexpected type `{ty:?}` in sized_constraint_for_ty")
|
||||
}
|
||||
};
|
||||
debug!("sized_constraint_for_ty({:?}) = {:?}", ty, result);
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
fn defaultness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Defaultness {
|
||||
|
@ -90,29 +68,45 @@ fn defaultness(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::Defaultness {
|
|||
///
|
||||
/// In fact, there are only a few options for the types in the constraint:
|
||||
/// - an obviously-unsized type
|
||||
/// - a type parameter or projection whose Sizedness can't be known
|
||||
/// - a tuple of type parameters or projections, if there are multiple
|
||||
/// such.
|
||||
/// - an Error, if a type is infinitely sized
|
||||
/// - a type parameter or projection whose sizedness can't be known
|
||||
#[instrument(level = "debug", skip(tcx), ret)]
|
||||
fn adt_sized_constraint<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
def_id: DefId,
|
||||
) -> ty::EarlyBinder<&'tcx ty::List<Ty<'tcx>>> {
|
||||
) -> Option<ty::EarlyBinder<Ty<'tcx>>> {
|
||||
if let Some(def_id) = def_id.as_local() {
|
||||
if matches!(tcx.representability(def_id), ty::Representability::Infinite) {
|
||||
return ty::EarlyBinder::bind(tcx.mk_type_list(&[Ty::new_misc_error(tcx)]));
|
||||
if let ty::Representability::Infinite(_) = tcx.representability(def_id) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
let def = tcx.adt_def(def_id);
|
||||
|
||||
let result =
|
||||
tcx.mk_type_list_from_iter(def.variants().iter().filter_map(|v| v.tail_opt()).flat_map(
|
||||
|f| sized_constraint_for_ty(tcx, def, tcx.type_of(f.did).instantiate_identity()),
|
||||
));
|
||||
if !def.is_struct() {
|
||||
bug!("`adt_sized_constraint` called on non-struct type: {def:?}");
|
||||
}
|
||||
|
||||
debug!("adt_sized_constraint: {:?} => {:?}", def, result);
|
||||
let tail_def = def.non_enum_variant().tail_opt()?;
|
||||
let tail_ty = tcx.type_of(tail_def.did).instantiate_identity();
|
||||
|
||||
ty::EarlyBinder::bind(result)
|
||||
let constraint_ty = sized_constraint_for_ty(tcx, tail_ty)?;
|
||||
if constraint_ty.references_error() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// perf hack: if there is a `constraint_ty: Sized` bound, then we know
|
||||
// that the type is sized and do not need to check it on the impl.
|
||||
let sized_trait_def_id = tcx.require_lang_item(LangItem::Sized, None);
|
||||
let predicates = tcx.predicates_of(def.did()).predicates;
|
||||
if predicates.iter().any(|(p, _)| {
|
||||
p.as_trait_clause().is_some_and(|trait_pred| {
|
||||
trait_pred.def_id() == sized_trait_def_id
|
||||
&& trait_pred.self_ty().skip_binder() == constraint_ty
|
||||
})
|
||||
}) {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(ty::EarlyBinder::bind(constraint_ty))
|
||||
}
|
||||
|
||||
/// See `ParamEnv` struct definition for details.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue