@@ -217,10 +217,10 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
217
217
check_item_fn ( tcx, def_id, item. ident , item. span , sig. decl ) ;
218
218
}
219
219
hir:: ItemKind :: Static ( ty, ..) => {
220
- check_item_type ( tcx, def_id, ty. span , false ) ;
220
+ check_item_type ( tcx, def_id, ty. span , UnsizedHandling :: Forbid ) ;
221
221
}
222
222
hir:: ItemKind :: Const ( ty, ..) => {
223
- check_item_type ( tcx, def_id, ty. span , false ) ;
223
+ check_item_type ( tcx, def_id, ty. span , UnsizedHandling :: Forbid ) ;
224
224
}
225
225
hir:: ItemKind :: Struct ( _, ast_generics) => {
226
226
check_type_defn ( tcx, item, false ) ;
@@ -242,6 +242,12 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
242
242
}
243
243
// `ForeignItem`s are handled separately.
244
244
hir:: ItemKind :: ForeignMod { .. } => { }
245
+ hir:: ItemKind :: TyAlias ( hir_ty, ..) => {
246
+ if tcx. type_of ( item. owner_id . def_id ) . skip_binder ( ) . has_opaque_types ( ) {
247
+ // Bounds are respected for `type X = impl Trait` and `type X = (impl Trait, Y);`
248
+ check_item_type ( tcx, def_id, hir_ty. span , UnsizedHandling :: Allow ) ;
249
+ }
250
+ }
245
251
_ => { }
246
252
}
247
253
}
@@ -258,7 +264,9 @@ fn check_foreign_item(tcx: TyCtxt<'_>, item: &hir::ForeignItem<'_>) {
258
264
hir:: ForeignItemKind :: Fn ( decl, ..) => {
259
265
check_item_fn ( tcx, def_id, item. ident , item. span , decl)
260
266
}
261
- hir:: ForeignItemKind :: Static ( ty, ..) => check_item_type ( tcx, def_id, ty. span , true ) ,
267
+ hir:: ForeignItemKind :: Static ( ty, ..) => {
268
+ check_item_type ( tcx, def_id, ty. span , UnsizedHandling :: AllowIfForeignTail )
269
+ }
262
270
hir:: ForeignItemKind :: Type => ( ) ,
263
271
}
264
272
}
@@ -1100,20 +1108,32 @@ fn check_item_fn(
1100
1108
} )
1101
1109
}
1102
1110
1103
- fn check_item_type ( tcx : TyCtxt < ' _ > , item_id : LocalDefId , ty_span : Span , allow_foreign_ty : bool ) {
1111
+ enum UnsizedHandling {
1112
+ Forbid ,
1113
+ Allow ,
1114
+ AllowIfForeignTail ,
1115
+ }
1116
+
1117
+ fn check_item_type (
1118
+ tcx : TyCtxt < ' _ > ,
1119
+ item_id : LocalDefId ,
1120
+ ty_span : Span ,
1121
+ unsized_handling : UnsizedHandling ,
1122
+ ) {
1104
1123
debug ! ( "check_item_type: {:?}" , item_id) ;
1105
1124
1106
1125
enter_wf_checking_ctxt ( tcx, ty_span, item_id, |wfcx| {
1107
1126
let ty = tcx. type_of ( item_id) . subst_identity ( ) ;
1108
1127
let item_ty = wfcx. normalize ( ty_span, Some ( WellFormedLoc :: Ty ( item_id) ) , ty) ;
1109
1128
1110
- let mut forbid_unsized = true ;
1111
- if allow_foreign_ty {
1112
- let tail = tcx. struct_tail_erasing_lifetimes ( item_ty, wfcx. param_env ) ;
1113
- if let ty:: Foreign ( _) = tail. kind ( ) {
1114
- forbid_unsized = false ;
1129
+ let forbid_unsized = match unsized_handling {
1130
+ UnsizedHandling :: Forbid => true ,
1131
+ UnsizedHandling :: Allow => false ,
1132
+ UnsizedHandling :: AllowIfForeignTail => {
1133
+ let tail = tcx. struct_tail_erasing_lifetimes ( item_ty, wfcx. param_env ) ;
1134
+ !matches ! ( tail. kind( ) , ty:: Foreign ( _) )
1115
1135
}
1116
- }
1136
+ } ;
1117
1137
1118
1138
wfcx. register_wf_obligation ( ty_span, Some ( WellFormedLoc :: Ty ( item_id) ) , item_ty. into ( ) ) ;
1119
1139
if forbid_unsized {
0 commit comments