@@ -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;
@@ -1677,7 +1678,7 @@ impl<'tcx> Ty<'tcx> {
16771678 let Some ( pointee_ty) = self . builtin_deref ( true ) else {
16781679 bug ! ( "Type {self:?} is not a pointer or reference type" )
16791680 } ;
1680- if pointee_ty. is_trivially_sized ( tcx) {
1681+ if pointee_ty. has_trivial_sizedness ( tcx, SizedTraitKind :: Sized ) {
16811682 tcx. types . unit
16821683 } else {
16831684 match pointee_ty. ptr_metadata_ty_or_tail ( tcx, |x| x) {
@@ -1778,17 +1779,17 @@ impl<'tcx> Ty<'tcx> {
17781779 }
17791780 }
17801781
1781- /// Fast path helper for testing if a type is `Sized`.
1782+ /// Fast path helper for testing if a type is `Sized` or `MetaSized` .
17821783 ///
1783- /// Returning true means the type is known to implement `Sized` . Returning `false` means
1784- /// nothing -- could be sized, might not be.
1784+ /// Returning true means the type is known to implement the sizedness trait . Returning `false`
1785+ /// means nothing -- could be sized, might not be.
17851786 ///
17861787 /// Note that we could never rely on the fact that a type such as `[_]` is trivially `!Sized`
17871788 /// because we could be in a type environment with a bound such as `[_]: Copy`. A function with
17881789 /// such a bound obviously never can be called, but that doesn't mean it shouldn't typecheck.
17891790 /// This is why this method doesn't return `Option<bool>`.
17901791 #[ instrument( skip( tcx) , level = "debug" ) ]
1791- pub fn is_trivially_sized ( self , tcx : TyCtxt < ' tcx > ) -> bool {
1792+ pub fn has_trivial_sizedness ( self , tcx : TyCtxt < ' tcx > , sizedness : SizedTraitKind ) -> bool {
17921793 match self . kind ( ) {
17931794 ty:: Infer ( ty:: IntVar ( _) | ty:: FloatVar ( _) )
17941795 | ty:: Uint ( _)
@@ -1811,13 +1812,20 @@ impl<'tcx> Ty<'tcx> {
18111812 | ty:: Error ( _)
18121813 | ty:: Dynamic ( _, _, ty:: DynStar ) => true ,
18131814
1814- ty:: Str | ty:: Slice ( _) | ty:: Dynamic ( _, _, ty:: Dyn ) | ty:: Foreign ( ..) => false ,
1815+ ty:: Str | ty:: Slice ( _) | ty:: Dynamic ( _, _, ty:: Dyn ) => match sizedness {
1816+ SizedTraitKind :: Sized => false ,
1817+ SizedTraitKind :: MetaSized => true ,
1818+ } ,
1819+
1820+ ty:: Foreign ( ..) => match sizedness {
1821+ SizedTraitKind :: Sized | SizedTraitKind :: MetaSized => false ,
1822+ } ,
18151823
1816- ty:: Tuple ( tys) => tys. last ( ) . is_none_or ( |ty| ty. is_trivially_sized ( tcx) ) ,
1824+ ty:: Tuple ( tys) => tys. last ( ) . is_none_or ( |ty| ty. has_trivial_sizedness ( tcx, sizedness ) ) ,
18171825
18181826 ty:: Adt ( def, args) => def
1819- . sizedness_constraint ( tcx, ty :: SizedTraitKind :: Sized )
1820- . is_none_or ( |ty| ty. instantiate ( tcx, args) . is_trivially_sized ( tcx) ) ,
1827+ . sizedness_constraint ( tcx, sizedness )
1828+ . is_none_or ( |ty| ty. instantiate ( tcx, args) . has_trivial_sizedness ( tcx, sizedness ) ) ,
18211829
18221830 ty:: Alias ( ..) | ty:: Param ( _) | ty:: Placeholder ( ..) | ty:: Bound ( ..) => false ,
18231831
0 commit comments