@@ -15,6 +15,7 @@ use rustc_hir::def_id::DefId;
15
15
use rustc_macros:: { HashStable , TyDecodable , TyEncodable , TypeFoldable , extension} ;
16
16
use rustc_span:: { DUMMY_SP , Span , Symbol , sym} ;
17
17
use rustc_type_ir:: TyKind :: * ;
18
+ use rustc_type_ir:: solve:: SizedTraitKind ;
18
19
use rustc_type_ir:: walk:: TypeWalker ;
19
20
use rustc_type_ir:: { self as ir, BoundVar , CollectAndApply , DynKind , TypeVisitableExt , elaborate} ;
20
21
use tracing:: instrument;
@@ -1677,7 +1678,7 @@ impl<'tcx> Ty<'tcx> {
1677
1678
let Some ( pointee_ty) = self . builtin_deref ( true ) else {
1678
1679
bug ! ( "Type {self:?} is not a pointer or reference type" )
1679
1680
} ;
1680
- if pointee_ty. is_trivially_sized ( tcx) {
1681
+ if pointee_ty. has_trivial_sizedness ( tcx, SizedTraitKind :: Sized ) {
1681
1682
tcx. types . unit
1682
1683
} else {
1683
1684
match pointee_ty. ptr_metadata_ty_or_tail ( tcx, |x| x) {
@@ -1778,17 +1779,17 @@ impl<'tcx> Ty<'tcx> {
1778
1779
}
1779
1780
}
1780
1781
1781
- /// Fast path helper for testing if a type is `Sized`.
1782
+ /// Fast path helper for testing if a type is `Sized` or `MetaSized` .
1782
1783
///
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.
1785
1786
///
1786
1787
/// Note that we could never rely on the fact that a type such as `[_]` is trivially `!Sized`
1787
1788
/// because we could be in a type environment with a bound such as `[_]: Copy`. A function with
1788
1789
/// such a bound obviously never can be called, but that doesn't mean it shouldn't typecheck.
1789
1790
/// This is why this method doesn't return `Option<bool>`.
1790
1791
#[ 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 {
1792
1793
match self . kind ( ) {
1793
1794
ty:: Infer ( ty:: IntVar ( _) | ty:: FloatVar ( _) )
1794
1795
| ty:: Uint ( _)
@@ -1811,13 +1812,20 @@ impl<'tcx> Ty<'tcx> {
1811
1812
| ty:: Error ( _)
1812
1813
| ty:: Dynamic ( _, _, ty:: DynStar ) => true ,
1813
1814
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
+ } ,
1815
1823
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 ) ) ,
1817
1825
1818
1826
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 ) ) ,
1821
1829
1822
1830
ty:: Alias ( ..) | ty:: Param ( _) | ty:: Placeholder ( ..) | ty:: Bound ( ..) => false ,
1823
1831
0 commit comments