@@ -1595,7 +1595,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
15951595 Some ( blk_id) ,
15961596 ) ;
15971597 if !fcx. tcx . features ( ) . unsized_locals {
1598- unsized_return = self . is_return_ty_unsized ( fcx, blk_id ) ;
1598+ unsized_return = self . is_return_ty_definitely_unsized ( fcx) ;
15991599 }
16001600 if let Some ( expression) = expression
16011601 && let hir:: ExprKind :: Loop ( loop_blk, ..) = expression. kind {
@@ -1614,8 +1614,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
16141614 None ,
16151615 ) ;
16161616 if !fcx. tcx . features ( ) . unsized_locals {
1617- let id = fcx. tcx . hir ( ) . parent_id ( id) ;
1618- unsized_return = self . is_return_ty_unsized ( fcx, id) ;
1617+ unsized_return = self . is_return_ty_definitely_unsized ( fcx) ;
16191618 }
16201619 }
16211620 _ => {
@@ -1896,15 +1895,24 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18961895 err. help ( "you could instead create a new `enum` with a variant for each returned type" ) ;
18971896 }
18981897
1899- fn is_return_ty_unsized < ' a > ( & self , fcx : & FnCtxt < ' a , ' tcx > , blk_id : hir:: HirId ) -> bool {
1900- if let Some ( ( _, fn_decl, _) ) = fcx. get_fn_decl ( blk_id)
1901- && let hir:: FnRetTy :: Return ( ty) = fn_decl. output
1902- && let ty = fcx. astconv ( ) . ast_ty_to_ty ( ty)
1903- && let ty:: Dynamic ( ..) = ty. kind ( )
1904- {
1905- return true ;
1898+ /// Checks whether the return type is unsized via an obligation, which makes
1899+ /// sure we consider `dyn Trait: Sized` where clauses, which are trivially
1900+ /// false but technically valid for typeck.
1901+ fn is_return_ty_definitely_unsized ( & self , fcx : & FnCtxt < ' _ , ' tcx > ) -> bool {
1902+ if let Some ( sig) = fcx. body_fn_sig ( ) {
1903+ !fcx. predicate_may_hold ( & Obligation :: new (
1904+ fcx. tcx ,
1905+ ObligationCause :: dummy ( ) ,
1906+ fcx. param_env ,
1907+ ty:: TraitRef :: new (
1908+ fcx. tcx ,
1909+ fcx. tcx . require_lang_item ( hir:: LangItem :: Sized , None ) ,
1910+ [ sig. output ( ) ] ,
1911+ ) ,
1912+ ) )
1913+ } else {
1914+ false
19061915 }
1907- false
19081916 }
19091917
19101918 pub fn complete < ' a > ( self , fcx : & FnCtxt < ' a , ' tcx > ) -> Ty < ' tcx > {
0 commit comments