@@ -217,10 +217,10 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
217217 check_item_fn ( tcx, def_id, item. ident , item. span , sig. decl ) ;
218218 }
219219 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 ) ;
221221 }
222222 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 ) ;
224224 }
225225 hir:: ItemKind :: Struct ( _, ast_generics) => {
226226 check_type_defn ( tcx, item, false ) ;
@@ -242,6 +242,12 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) {
242242 }
243243 // `ForeignItem`s are handled separately.
244244 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+ }
245251 _ => { }
246252 }
247253}
@@ -258,7 +264,9 @@ fn check_foreign_item(tcx: TyCtxt<'_>, item: &hir::ForeignItem<'_>) {
258264 hir:: ForeignItemKind :: Fn ( decl, ..) => {
259265 check_item_fn ( tcx, def_id, item. ident , item. span , decl)
260266 }
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+ }
262270 hir:: ForeignItemKind :: Type => ( ) ,
263271 }
264272}
@@ -1100,20 +1108,32 @@ fn check_item_fn(
11001108 } )
11011109}
11021110
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+ ) {
11041123 debug ! ( "check_item_type: {:?}" , item_id) ;
11051124
11061125 enter_wf_checking_ctxt ( tcx, ty_span, item_id, |wfcx| {
11071126 let ty = tcx. type_of ( item_id) . subst_identity ( ) ;
11081127 let item_ty = wfcx. normalize ( ty_span, Some ( WellFormedLoc :: Ty ( item_id) ) , ty) ;
11091128
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 ( _) )
11151135 }
1116- }
1136+ } ;
11171137
11181138 wfcx. register_wf_obligation ( ty_span, Some ( WellFormedLoc :: Ty ( item_id) ) , item_ty. into ( ) ) ;
11191139 if forbid_unsized {
0 commit comments