@@ -104,14 +104,12 @@ fn map_error<'tcx>(
104104 // This is sometimes not a compile error if there are trivially false where clauses.
105105 // See `tests/ui/layout/trivial-bounds-sized.rs` for an example.
106106 assert ! ( field. layout. is_unsized( ) , "invalid layout error {err:#?}" ) ;
107- if !field . ty . is_sized ( cx . tcx ( ) , cx . typing_env ) {
108- let guar = cx. tcx ( ) . dcx ( ) . delayed_bug ( format ! (
107+ if cx . typing_env . param_env . caller_bounds ( ) . is_empty ( ) {
108+ cx. tcx ( ) . dcx ( ) . delayed_bug ( format ! (
109109 "encountered unexpected unsized field in layout of {ty:?}: {field:#?}"
110110 ) ) ;
111- LayoutError :: ReferencesError ( guar)
112- } else {
113- LayoutError :: Unknown ( ty)
114111 }
112+ LayoutError :: Unknown ( ty)
115113 }
116114 LayoutCalculatorError :: EmptyUnion => {
117115 // This is always a compile error.
@@ -146,6 +144,35 @@ fn univariant_uninterned<'tcx>(
146144 cx. calc . univariant ( fields, repr, kind) . map_err ( |err| map_error ( cx, ty, err) )
147145}
148146
147+ fn validate_const_with_value < ' tcx > (
148+ const_ : ty:: Const < ' tcx > ,
149+ ty : Ty < ' tcx > ,
150+ cx : & LayoutCx < ' tcx > ,
151+ ) -> Result < ty:: Const < ' tcx > , & ' tcx LayoutError < ' tcx > > {
152+ match const_. kind ( ) {
153+ ty:: ConstKind :: Value ( ..) => Ok ( const_) ,
154+ ty:: ConstKind :: Error ( guar) => {
155+ return Err ( error ( cx, LayoutError :: ReferencesError ( guar) ) ) ;
156+ }
157+ ty:: ConstKind :: Param ( _) | ty:: ConstKind :: Expr ( _) => {
158+ if !const_. has_param ( ) {
159+ bug ! ( "no generic type found in the type: {ty:?}" ) ;
160+ }
161+ return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
162+ }
163+ ty:: ConstKind :: Unevaluated ( _) => {
164+ if !const_. has_param ( ) {
165+ return Err ( error ( cx, LayoutError :: Unknown ( ty) ) ) ;
166+ } else {
167+ return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
168+ }
169+ }
170+ ty:: ConstKind :: Infer ( _) | ty:: ConstKind :: Bound ( ..) | ty:: ConstKind :: Placeholder ( _) => {
171+ bug ! ( "unexpected type: {ty:?}" ) ;
172+ }
173+ }
174+ }
175+
149176fn layout_of_uncached < ' tcx > (
150177 cx : & LayoutCx < ' tcx > ,
151178 ty : Ty < ' tcx > ,
@@ -182,12 +209,13 @@ fn layout_of_uncached<'tcx>(
182209 & mut layout. backend_repr
183210 {
184211 if let Some ( start) = start {
185- scalar. valid_range_mut ( ) . start = start
186- . try_to_bits ( tcx, cx. typing_env )
187- . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
212+ scalar. valid_range_mut ( ) . start =
213+ validate_const_with_value ( start, ty, cx) ?
214+ . try_to_bits ( tcx, cx. typing_env )
215+ . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
188216 }
189217 if let Some ( end) = end {
190- let mut end = end
218+ let mut end = validate_const_with_value ( end, ty , cx ) ?
191219 . try_to_bits ( tcx, cx. typing_env )
192220 . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
193221 if !include_end {
@@ -319,17 +347,13 @@ fn layout_of_uncached<'tcx>(
319347 }
320348
321349 // Arrays and slices.
322- ty:: Array ( element, mut count) => {
323- if count. has_aliases ( ) {
324- count = tcx. normalize_erasing_regions ( cx. typing_env , count) ;
325- if count. has_aliases ( ) {
326- return Err ( error ( cx, LayoutError :: Unknown ( ty) ) ) ;
327- }
328- }
329-
330- let count = count
350+ ty:: Array ( element, count) => {
351+ let count = validate_const_with_value ( count, ty, cx) ?
352+ . to_valtree ( )
353+ . 0
331354 . try_to_target_usize ( tcx)
332355 . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
356+
333357 let element = cx. layout_of ( element) ?;
334358 let size = element
335359 . size
@@ -687,6 +711,9 @@ fn layout_of_uncached<'tcx>(
687711
688712 // Types with no meaningful known layout.
689713 ty:: Alias ( ..) => {
714+ if ty. has_param ( ) {
715+ return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
716+ }
690717 // NOTE(eddyb) `layout_of` query should've normalized these away,
691718 // if that was possible, so there's no reason to try again here.
692719 return Err ( error ( cx, LayoutError :: Unknown ( ty) ) ) ;
@@ -696,7 +723,11 @@ fn layout_of_uncached<'tcx>(
696723 bug ! ( "Layout::compute: unexpected type `{}`" , ty)
697724 }
698725
699- ty:: Placeholder ( ..) | ty:: Param ( _) => {
726+ ty:: Param ( _) => {
727+ return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
728+ }
729+
730+ ty:: Placeholder ( ..) => {
700731 return Err ( error ( cx, LayoutError :: Unknown ( ty) ) ) ;
701732 }
702733 } )
0 commit comments