@@ -102,12 +102,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
102
102
// N.B., this code relies on `self.diverges` to be accurate. In
103
103
// particular, assignments to `!` will be permitted if the
104
104
// 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 > > ) {
111
112
let expected = self . resolve_vars_with_obligations ( expected) ;
112
113
113
114
let e = match self . try_coerce ( expr, checked_ty, expected, allow_two_phase) {
@@ -126,6 +127,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
126
127
return ( expected, None )
127
128
}
128
129
130
+ self . annotate_expected_due_to_let_ty ( & mut err, expr) ;
129
131
self . suggest_compatible_variants ( & mut err, expr, expected, expr_ty) ;
130
132
self . suggest_ref_or_into ( & mut err, expr, expected, expr_ty) ;
131
133
self . suggest_boxing_when_appropriate ( & mut err, expr, expected, expr_ty) ;
@@ -134,6 +136,20 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
134
136
( expected, Some ( err) )
135
137
}
136
138
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
+
137
153
/// Returns whether the expected type is `bool` and the expression is `x = y`.
138
154
pub fn is_assign_to_bool ( & self , expr : & hir:: Expr , expected : Ty < ' tcx > ) -> bool {
139
155
if let hir:: ExprKind :: Assign ( ..) = expr. kind {
0 commit comments