@@ -104,14 +104,12 @@ fn map_error<'tcx>(
104
104
// This is sometimes not a compile error if there are trivially false where clauses.
105
105
// See `tests/ui/layout/trivial-bounds-sized.rs` for an example.
106
106
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 ! (
109
109
"encountered unexpected unsized field in layout of {ty:?}: {field:#?}"
110
110
) ) ;
111
- LayoutError :: ReferencesError ( guar)
112
- } else {
113
- LayoutError :: Unknown ( ty)
114
111
}
112
+ LayoutError :: Unknown ( ty)
115
113
}
116
114
LayoutCalculatorError :: EmptyUnion => {
117
115
// This is always a compile error.
@@ -146,6 +144,35 @@ fn univariant_uninterned<'tcx>(
146
144
cx. calc . univariant ( fields, repr, kind) . map_err ( |err| map_error ( cx, ty, err) )
147
145
}
148
146
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
+
149
176
fn layout_of_uncached < ' tcx > (
150
177
cx : & LayoutCx < ' tcx > ,
151
178
ty : Ty < ' tcx > ,
@@ -182,12 +209,13 @@ fn layout_of_uncached<'tcx>(
182
209
& mut layout. backend_repr
183
210
{
184
211
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) ) ) ?;
188
216
}
189
217
if let Some ( end) = end {
190
- let mut end = end
218
+ let mut end = validate_const_with_value ( end, ty , cx ) ?
191
219
. try_to_bits ( tcx, cx. typing_env )
192
220
. ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
193
221
if !include_end {
@@ -319,17 +347,13 @@ fn layout_of_uncached<'tcx>(
319
347
}
320
348
321
349
// 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
331
354
. try_to_target_usize ( tcx)
332
355
. ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
356
+
333
357
let element = cx. layout_of ( element) ?;
334
358
let size = element
335
359
. size
@@ -687,6 +711,9 @@ fn layout_of_uncached<'tcx>(
687
711
688
712
// Types with no meaningful known layout.
689
713
ty:: Alias ( ..) => {
714
+ if ty. has_param ( ) {
715
+ return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
716
+ }
690
717
// NOTE(eddyb) `layout_of` query should've normalized these away,
691
718
// if that was possible, so there's no reason to try again here.
692
719
return Err ( error ( cx, LayoutError :: Unknown ( ty) ) ) ;
@@ -696,7 +723,11 @@ fn layout_of_uncached<'tcx>(
696
723
bug ! ( "Layout::compute: unexpected type `{}`" , ty)
697
724
}
698
725
699
- ty:: Placeholder ( ..) | ty:: Param ( _) => {
726
+ ty:: Param ( _) => {
727
+ return Err ( error ( cx, LayoutError :: TooGeneric ( ty) ) ) ;
728
+ }
729
+
730
+ ty:: Placeholder ( ..) => {
700
731
return Err ( error ( cx, LayoutError :: Unknown ( ty) ) ) ;
701
732
}
702
733
} )
0 commit comments