@@ -1595,7 +1595,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1595
1595
Some ( blk_id) ,
1596
1596
) ;
1597
1597
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) ;
1599
1599
}
1600
1600
if let Some ( expression) = expression
1601
1601
&& let hir:: ExprKind :: Loop ( loop_blk, ..) = expression. kind {
@@ -1614,8 +1614,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1614
1614
None ,
1615
1615
) ;
1616
1616
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) ;
1619
1618
}
1620
1619
}
1621
1620
_ => {
@@ -1896,15 +1895,24 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
1896
1895
err. help ( "you could instead create a new `enum` with a variant for each returned type" ) ;
1897
1896
}
1898
1897
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
1906
1915
}
1907
- false
1908
1916
}
1909
1917
1910
1918
pub fn complete < ' a > ( self , fcx : & FnCtxt < ' a , ' tcx > ) -> Ty < ' tcx > {
0 commit comments