@@ -15,6 +15,7 @@ use rustc_hir::def_id::DefId;
1515use rustc_macros:: { HashStable , TyDecodable , TyEncodable , TypeFoldable , extension} ;
1616use rustc_span:: { DUMMY_SP , Span , Symbol , sym} ;
1717use rustc_type_ir:: TyKind :: * ;
18+ use rustc_type_ir:: solve:: SizedTraitKind ;
1819use rustc_type_ir:: walk:: TypeWalker ;
1920use rustc_type_ir:: { self as ir, BoundVar , CollectAndApply , DynKind , TypeVisitableExt , elaborate} ;
2021use 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
0 commit comments