Skip to content

Commit 672d370

Browse files
committed
Remove Session.if_let_suggestions
We can instead if either the LHS or RHS types contain `TyKind::Error`. In addition to covering the case where we would have previously updated `if_let_suggestions`, this might also prevent redundant errors in other cases as well.
1 parent dfd8472 commit 672d370

File tree

5 files changed

+17
-9
lines changed

5 files changed

+17
-9
lines changed

compiler/rustc_resolve/src/late/diagnostics.rs

-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
215215
"let ".to_string(),
216216
Applicability::MaybeIncorrect,
217217
);
218-
self.r.session.if_let_suggestions.borrow_mut().insert(*span);
219218
}
220219
_ => {}
221220
}

compiler/rustc_session/src/session.rs

-4
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,6 @@ pub struct Session {
209209

210210
/// Set of enabled features for the current target.
211211
pub target_features: FxHashSet<Symbol>,
212-
213-
/// `Span`s for `if` conditions that we have suggested turning into `if let`.
214-
pub if_let_suggestions: Lock<FxHashSet<Span>>,
215212
}
216213

217214
pub struct PerfStats {
@@ -1328,7 +1325,6 @@ pub fn build_session(
13281325
miri_unleashed_features: Lock::new(Default::default()),
13291326
asm_arch,
13301327
target_features: FxHashSet::default(),
1331-
if_let_suggestions: Default::default(),
13321328
};
13331329

13341330
validate_commandline_args_with_session_available(&sess);

compiler/rustc_typeck/src/check/expr.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -919,9 +919,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
919919
);
920920
}
921921

922-
if self.sess().if_let_suggestions.borrow().get(&expr.span).is_some() {
923-
// We already emitted an `if let` suggestion due to an identifier not found.
924-
err.delay_as_bug();
922+
// If the assignment expression itself is ill-formed, don't
923+
// bother emitting another error
924+
if lhs_ty.references_error() || rhs_ty.references_error() {
925+
err.delay_as_bug()
925926
} else {
926927
err.emit();
927928
}

src/test/ui/suggestions/if-let-typo.rs

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ fn main() {
1010
if Some(3) = foo {} //~ ERROR mismatched types
1111
//~^ ERROR destructuring assignments are unstable
1212
//~^^ ERROR invalid left-hand side of assignment
13+
if x = 5 {} //~ ERROR cannot find value `x` in this scope
1314
}

src/test/ui/suggestions/if-let-typo.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ help: you might have meant to use pattern matching
99
LL | if let Some(x) = foo {}
1010
| +++
1111

12+
error[E0425]: cannot find value `x` in this scope
13+
--> $DIR/if-let-typo.rs:13:8
14+
|
15+
LL | if x = 5 {}
16+
| ^ not found in this scope
17+
|
18+
help: you might have meant to use pattern matching
19+
|
20+
LL | if let x = 5 {}
21+
| +++
22+
1223
error[E0658]: destructuring assignments are unstable
1324
--> $DIR/if-let-typo.rs:4:16
1425
|
@@ -79,7 +90,7 @@ error[E0308]: mismatched types
7990
LL | if Some(3) = foo {}
8091
| ^^^^^^^^^^^^^ expected `bool`, found `()`
8192

82-
error: aborting due to 9 previous errors
93+
error: aborting due to 10 previous errors
8394

8495
Some errors have detailed explanations: E0070, E0308, E0425, E0658.
8596
For more information about an error, try `rustc --explain E0070`.

0 commit comments

Comments
 (0)