Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit deab445

Browse files
committed
trait_sel: extend fast path with sized hierarchy
Extend the fast path for `Sized` traits to include constness and `MetaSized`.
1 parent 74ccb5f commit deab445

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ use rustc_infer::infer::{DefineOpaqueTypes, InferResult};
2222
use rustc_lint::builtin::SELF_CONSTRUCTOR_FROM_OUTER_ITEM;
2323
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
2424
use rustc_middle::ty::{
25-
self, AdtKind, CanonicalUserType, GenericArgsRef, GenericParamDefKind, IsIdentity, Ty, TyCtxt,
26-
TypeFoldable, TypeVisitable, TypeVisitableExt, UserArgs, UserSelfTy,
25+
self, AdtKind, CanonicalUserType, GenericArgsRef, GenericParamDefKind, IsIdentity,
26+
SizedTraitKind, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitableExt, UserArgs,
27+
UserSelfTy,
2728
};
2829
use rustc_middle::{bug, span_bug};
2930
use rustc_session::lint;
@@ -439,7 +440,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
439440
|| {},
440441
);
441442
// Sized types have static alignment, and so do slices.
442-
if tail.is_trivially_sized(self.tcx) || matches!(tail.kind(), ty::Slice(..)) {
443+
if tail.has_trivial_sizedness(self.tcx, SizedTraitKind::Sized)
444+
|| matches!(tail.kind(), ty::Slice(..))
445+
{
443446
// Nothing else is required here.
444447
} else {
445448
// We can't be sure, let's required full `Sized`.

compiler/rustc_middle/src/ty/sty.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_hir::def_id::DefId;
1515
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, extension};
1616
use rustc_span::{DUMMY_SP, Span, Symbol, sym};
1717
use rustc_type_ir::TyKind::*;
18+
use rustc_type_ir::solve::SizedTraitKind;
1819
use rustc_type_ir::walk::TypeWalker;
1920
use rustc_type_ir::{self as ir, BoundVar, CollectAndApply, DynKind, TypeVisitableExt, elaborate};
2021
use tracing::instrument;
@@ -1675,7 +1676,7 @@ impl<'tcx> Ty<'tcx> {
16751676
let Some(pointee_ty) = self.builtin_deref(true) else {
16761677
bug!("Type {self:?} is not a pointer or reference type")
16771678
};
1678-
if pointee_ty.is_trivially_sized(tcx) {
1679+
if pointee_ty.has_trivial_sizedness(tcx, SizedTraitKind::Sized) {
16791680
tcx.types.unit
16801681
} else {
16811682
match pointee_ty.ptr_metadata_ty_or_tail(tcx, |x| x) {
@@ -1776,17 +1777,17 @@ impl<'tcx> Ty<'tcx> {
17761777
}
17771778
}
17781779

1779-
/// Fast path helper for testing if a type is `Sized`.
1780+
/// Fast path helper for testing if a type is `Sized`, `MetaSized` or `PointeeSized`.
17801781
///
1781-
/// Returning true means the type is known to implement `Sized`. Returning `false` means
1782-
/// nothing -- could be sized, might not be.
1782+
/// Returning true means the type is known to implement the sizedness trait. Returning `false`
1783+
/// means nothing -- could be sized, might not be.
17831784
///
17841785
/// Note that we could never rely on the fact that a type such as `[_]` is trivially `!Sized`
17851786
/// because we could be in a type environment with a bound such as `[_]: Copy`. A function with
17861787
/// such a bound obviously never can be called, but that doesn't mean it shouldn't typecheck.
17871788
/// This is why this method doesn't return `Option<bool>`.
17881789
#[instrument(skip(tcx), level = "debug")]
1789-
pub fn is_trivially_sized(self, tcx: TyCtxt<'tcx>) -> bool {
1790+
pub fn has_trivial_sizedness(self, tcx: TyCtxt<'tcx>, sizedness: SizedTraitKind) -> bool {
17901791
match self.kind() {
17911792
ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
17921793
| ty::Uint(_)
@@ -1809,13 +1810,21 @@ impl<'tcx> Ty<'tcx> {
18091810
| ty::Error(_)
18101811
| ty::Dynamic(_, _, ty::DynStar) => true,
18111812

1812-
ty::Str | ty::Slice(_) | ty::Dynamic(_, _, ty::Dyn) | ty::Foreign(..) => false,
1813+
ty::Str | ty::Slice(_) | ty::Dynamic(_, _, ty::Dyn) => match sizedness {
1814+
SizedTraitKind::Sized => false,
1815+
SizedTraitKind::MetaSized | SizedTraitKind::PointeeSized => true,
1816+
},
1817+
1818+
ty::Foreign(..) => match sizedness {
1819+
SizedTraitKind::Sized | SizedTraitKind::MetaSized => false,
1820+
SizedTraitKind::PointeeSized => true,
1821+
},
18131822

1814-
ty::Tuple(tys) => tys.last().is_none_or(|ty| ty.is_trivially_sized(tcx)),
1823+
ty::Tuple(tys) => tys.last().is_none_or(|ty| ty.has_trivial_sizedness(tcx, sizedness)),
18151824

18161825
ty::Adt(def, args) => def
1817-
.sizedness_constraint(tcx, ty::SizedTraitKind::Sized)
1818-
.is_none_or(|ty| ty.instantiate(tcx, args).is_trivially_sized(tcx)),
1826+
.sizedness_constraint(tcx, sizedness)
1827+
.is_none_or(|ty| ty.instantiate(tcx, args).has_trivial_sizedness(tcx, sizedness)),
18191828

18201829
ty::Alias(..) | ty::Param(_) | ty::Placeholder(..) | ty::Bound(..) => false,
18211830

compiler/rustc_middle/src/ty/util.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_index::bit_set::GrowableBitSet;
1616
use rustc_macros::{HashStable, TyDecodable, TyEncodable, extension};
1717
use rustc_session::Limit;
1818
use rustc_span::sym;
19+
use rustc_type_ir::solve::SizedTraitKind;
1920
use smallvec::{SmallVec, smallvec};
2021
use tracing::{debug, instrument};
2122

@@ -1166,7 +1167,8 @@ impl<'tcx> Ty<'tcx> {
11661167
/// strange rules like `<T as Foo<'static>>::Bar: Sized` that
11671168
/// actually carry lifetime requirements.
11681169
pub fn is_sized(self, tcx: TyCtxt<'tcx>, typing_env: ty::TypingEnv<'tcx>) -> bool {
1169-
self.is_trivially_sized(tcx) || tcx.is_sized_raw(typing_env.as_query_input(self))
1170+
self.has_trivial_sizedness(tcx, SizedTraitKind::Sized)
1171+
|| tcx.is_sized_raw(typing_env.as_query_input(self))
11701172
}
11711173

11721174
/// Checks whether values of this type `T` implement the `Freeze`

compiler/rustc_trait_selection/src/error_reporting/traits/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ fn attempt_dyn_to_enum_suggestion(
556556
let Some(impl_type) = tcx.type_of(*impl_id).no_bound_vars() else { return None };
557557

558558
// Obviously unsized impl types won't be usable in an enum.
559-
// Note: this doesn't use `Ty::is_trivially_sized` because that function
559+
// Note: this doesn't use `Ty::has_trivial_sizedness` because that function
560560
// defaults to assuming that things are *not* sized, whereas we want to
561561
// fall back to assuming that things may be sized.
562562
match impl_type.kind() {

compiler/rustc_trait_selection/src/traits/util.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_infer::infer::InferCtxt;
77
pub use rustc_infer::traits::util::*;
88
use rustc_middle::bug;
99
use rustc_middle::ty::{
10-
self, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
10+
self, SizedTraitKind, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
1111
};
1212
use rustc_span::Span;
1313
use smallvec::{SmallVec, smallvec};
@@ -507,15 +507,23 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for PlaceholderReplacer<'_, 'tcx> {
507507
}
508508

509509
pub fn sizedness_fast_path<'tcx>(tcx: TyCtxt<'tcx>, predicate: ty::Predicate<'tcx>) -> bool {
510-
// Proving `Sized` very often on "obviously sized" types like `&T`, accounts for about 60%
511-
// percentage of the predicates we have to prove. No need to canonicalize and all that for
512-
// such cases.
510+
// Proving `Sized`/`MetaSized`/`PointeeSized`, very often on "obviously sized" types like
511+
// `&T`, accounts for about 60% percentage of the predicates we have to prove. No need to
512+
// canonicalize and all that for such cases.
513513
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_ref)) =
514514
predicate.kind().skip_binder()
515515
{
516-
if tcx.is_lang_item(trait_ref.def_id(), LangItem::Sized)
517-
&& trait_ref.self_ty().is_trivially_sized(tcx)
518-
{
516+
let sizedness = if tcx.is_lang_item(trait_ref.def_id(), LangItem::Sized) {
517+
SizedTraitKind::Sized
518+
} else if tcx.is_lang_item(trait_ref.def_id(), LangItem::MetaSized) {
519+
SizedTraitKind::MetaSized
520+
} else if tcx.is_lang_item(trait_ref.def_id(), LangItem::PointeeSized) {
521+
SizedTraitKind::PointeeSized
522+
} else {
523+
return false;
524+
};
525+
526+
if trait_ref.self_ty().has_trivial_sizedness(tcx, sizedness) {
519527
debug!("fast path -- trivial sizedness");
520528
return true;
521529
}

0 commit comments

Comments
 (0)