@@ -1738,6 +1738,7 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
17381738 // label pointing out the cause for the type coercion will be wrong
17391739 // as prior return coercions would not be relevant (#57664).
17401740 let fn_decl = if let ( Some ( expr) , Some ( blk_id) ) = ( expression, blk_id) {
1741+ self . explain_self_literal ( fcx, & mut err, expr) ;
17411742 let pointing_at_return_type =
17421743 fcx. suggest_mismatched_types_on_tail ( & mut err, expr, expected, found, blk_id) ;
17431744 if let ( Some ( cond_expr) , true , false ) = (
@@ -1810,6 +1811,43 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
18101811 err
18111812 }
18121813
1814+ fn explain_self_literal (
1815+ & self ,
1816+ fcx : & FnCtxt < ' _ , ' tcx > ,
1817+ err : & mut Diagnostic ,
1818+ expr : & ' tcx hir:: Expr < ' tcx > ,
1819+ ) {
1820+ match expr. peel_drop_temps ( ) . kind {
1821+ hir:: ExprKind :: Struct (
1822+ hir:: QPath :: Resolved (
1823+ None ,
1824+ hir:: Path { res : hir:: def:: Res :: SelfTyAlias { alias_to, .. } , .. } ,
1825+ ) ,
1826+ ..,
1827+ )
1828+ | hir:: ExprKind :: Call (
1829+ hir:: Expr {
1830+ kind :
1831+ hir:: ExprKind :: Path ( hir:: QPath :: Resolved (
1832+ None ,
1833+ hir:: Path { res : hir:: def:: Res :: SelfTyAlias { alias_to, .. } , .. } ,
1834+ ) ) ,
1835+ ..
1836+ } ,
1837+ ..,
1838+ ) => {
1839+ if let Some ( hir:: Node :: Item ( hir:: Item {
1840+ kind : hir:: ItemKind :: Impl ( hir:: Impl { self_ty, .. } ) ,
1841+ ..
1842+ } ) ) = fcx. tcx . hir ( ) . get_if_local ( * alias_to)
1843+ {
1844+ err. span_label ( self_ty. span , "this is the type of the `Self` literal" ) ;
1845+ }
1846+ }
1847+ _ => { }
1848+ }
1849+ }
1850+
18131851 /// Checks whether the return type is unsized via an obligation, which makes
18141852 /// sure we consider `dyn Trait: Sized` where clauses, which are trivially
18151853 /// false but technically valid for typeck.
0 commit comments