@@ -102,12 +102,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
102102 // N.B., this code relies on `self.diverges` to be accurate. In
103103 // particular, assignments to `!` will be permitted if the
104104 // diverges flag is currently "always".
105- pub fn demand_coerce_diag ( & self ,
106- expr : & hir:: Expr ,
107- checked_ty : Ty < ' tcx > ,
108- expected : Ty < ' tcx > ,
109- allow_two_phase : AllowTwoPhase )
110- -> ( Ty < ' tcx > , Option < DiagnosticBuilder < ' tcx > > ) {
105+ pub fn demand_coerce_diag (
106+ & self ,
107+ expr : & hir:: Expr ,
108+ checked_ty : Ty < ' tcx > ,
109+ expected : Ty < ' tcx > ,
110+ allow_two_phase : AllowTwoPhase ,
111+ ) -> ( Ty < ' tcx > , Option < DiagnosticBuilder < ' tcx > > ) {
111112 let expected = self . resolve_vars_with_obligations ( expected) ;
112113
113114 let e = match self . try_coerce ( expr, checked_ty, expected, allow_two_phase) {
@@ -126,6 +127,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
126127 return ( expected, None )
127128 }
128129
130+ self . annotate_expected_due_to_let_ty ( & mut err, expr) ;
129131 self . suggest_compatible_variants ( & mut err, expr, expected, expr_ty) ;
130132 self . suggest_ref_or_into ( & mut err, expr, expected, expr_ty) ;
131133 self . suggest_boxing_when_appropriate ( & mut err, expr, expected, expr_ty) ;
@@ -134,6 +136,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
134136 ( expected, Some ( err) )
135137 }
136138
139+ fn annotate_expected_due_to_let_ty ( & self , err : & mut DiagnosticBuilder < ' _ > , expr : & hir:: Expr ) {
140+ let parent = self . tcx . hir ( ) . get_parent_node ( expr. hir_id ) ;
141+ if let Some ( hir:: Node :: Local ( hir:: Local {
142+ ty : Some ( ty) ,
143+ init : Some ( init) ,
144+ ..
145+ } ) ) = self . tcx . hir ( ) . find ( parent) {
146+ if init. hir_id == expr. hir_id {
147+ // Point at `let` assignment type.
148+ err. span_label ( ty. span , "expected due to this" ) ;
149+ }
150+ }
151+ }
152+
137153 /// Returns whether the expected type is `bool` and the expression is `x = y`.
138154 pub fn is_assign_to_bool ( & self , expr : & hir:: Expr , expected : Ty < ' tcx > ) -> bool {
139155 if let hir:: ExprKind :: Assign ( ..) = expr. kind {
0 commit comments