Skip to content

Commit 028b5f9

Browse files
committed
Report error for assignment in if condition
For code like `if x = 3 {}`, output: ``` error[E0308]: mismatched types --> $DIR/issue-17283.rs:25:8 | 25 | if x = x { | ^^^^^ | | | help: did you mean to compare equality? `x == x` | expected bool, found () | = note: expected type `bool` found type `()` ```
1 parent 554c685 commit 028b5f9

File tree

4 files changed

+118
-27
lines changed

4 files changed

+118
-27
lines changed

src/librustc_typeck/check/demand.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
2626
// Requires that the two types unify, and prints an error message if
2727
// they don't.
2828
pub fn demand_suptype(&self, sp: Span, expected: Ty<'tcx>, actual: Ty<'tcx>) {
29+
self.demand_suptype_diag(sp, expected, actual).map(|mut e| e.emit());
30+
}
31+
32+
pub fn demand_suptype_diag(&self, sp: Span,
33+
expected: Ty<'tcx>,
34+
actual: Ty<'tcx>) -> Option<DiagnosticBuilder<'tcx>> {
2935
let cause = &self.misc(sp);
3036
match self.at(cause, self.param_env).sup(expected, actual) {
3137
Ok(InferOk { obligations, value: () }) => {
3238
self.register_predicates(obligations);
39+
None
3340
},
3441
Err(e) => {
35-
self.report_mismatched_types(&cause, expected, actual, e).emit();
42+
Some(self.report_mismatched_types(&cause, expected, actual, e))
3643
}
3744
}
3845
}

src/librustc_typeck/check/mod.rs

+51-17
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ pub enum Expectation<'tcx> {
232232
/// We know nothing about what type this expression should have.
233233
NoExpectation,
234234

235+
/// This expression is an `if` condition, it must resolve to `bool`.
236+
ExpectIfCondition,
237+
235238
/// This expression should have the type given (or some subtype)
236239
ExpectHasType(Ty<'tcx>),
237240

@@ -310,9 +313,8 @@ impl<'a, 'gcx, 'tcx> Expectation<'tcx> {
310313
// no constraints yet present), just returns `None`.
311314
fn resolve(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Expectation<'tcx> {
312315
match self {
313-
NoExpectation => {
314-
NoExpectation
315-
}
316+
NoExpectation => NoExpectation,
317+
ExpectIfCondition => ExpectIfCondition,
316318
ExpectCastableToType(t) => {
317319
ExpectCastableToType(fcx.resolve_type_vars_if_possible(&t))
318320
}
@@ -328,6 +330,7 @@ impl<'a, 'gcx, 'tcx> Expectation<'tcx> {
328330
fn to_option(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Option<Ty<'tcx>> {
329331
match self.resolve(fcx) {
330332
NoExpectation => None,
333+
ExpectIfCondition => Some(fcx.tcx.types.bool),
331334
ExpectCastableToType(ty) |
332335
ExpectHasType(ty) |
333336
ExpectRvalueLikeUnsized(ty) => Some(ty),
@@ -341,6 +344,7 @@ impl<'a, 'gcx, 'tcx> Expectation<'tcx> {
341344
fn only_has_type(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Option<Ty<'tcx>> {
342345
match self.resolve(fcx) {
343346
ExpectHasType(ty) => Some(ty),
347+
ExpectIfCondition => Some(fcx.tcx.types.bool),
344348
_ => None
345349
}
346350
}
@@ -2646,7 +2650,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
26462650
pub fn check_expr_has_type(&self,
26472651
expr: &'gcx hir::Expr,
26482652
expected: Ty<'tcx>) -> Ty<'tcx> {
2649-
let mut ty = self.check_expr_with_hint(expr, expected);
2653+
self.check_expr_expect_type(expr, ExpectHasType(expected))
2654+
}
2655+
2656+
fn check_expr_expect_type(&self,
2657+
expr: &'gcx hir::Expr,
2658+
expected: Expectation<'tcx>) -> Ty<'tcx> {
2659+
let expected_ty = expected.to_option(&self).unwrap_or(self.tcx.types.bool);
2660+
let mut ty = self.check_expr_with_expectation(expr, expected);
26502661

26512662
// While we don't allow *arbitrary* coercions here, we *do* allow
26522663
// coercions from ! to `expected`.
@@ -2662,7 +2673,24 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
26622673
ty = adj_ty;
26632674
}
26642675

2665-
self.demand_suptype(expr.span, expected, ty);
2676+
if let Some(mut err) = self.demand_suptype_diag(expr.span, expected_ty, ty) {
2677+
// Add help to type error if this is an `if` condition with an assignment
2678+
match (expected, &expr.node) {
2679+
(ExpectIfCondition, &hir::ExprAssign(ref lhs, ref rhs)) => {
2680+
let msg = "did you mean to compare equality?";
2681+
if let (Ok(left), Ok(right)) = (
2682+
self.tcx.sess.codemap().span_to_snippet(lhs.span),
2683+
self.tcx.sess.codemap().span_to_snippet(rhs.span))
2684+
{
2685+
err.span_suggestion(expr.span, msg, format!("{} == {}", left, right));
2686+
} else {
2687+
err.help(msg);
2688+
}
2689+
}
2690+
_ => (),
2691+
}
2692+
err.emit();
2693+
}
26662694
ty
26672695
}
26682696

@@ -2837,7 +2865,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
28372865
opt_else_expr: Option<&'gcx hir::Expr>,
28382866
sp: Span,
28392867
expected: Expectation<'tcx>) -> Ty<'tcx> {
2840-
let cond_ty = self.check_expr_has_type(cond_expr, self.tcx.types.bool);
2868+
let cond_ty = self.check_expr_expect_type(cond_expr, ExpectIfCondition);
28412869
let cond_diverges = self.diverges.get();
28422870
self.diverges.set(Diverges::Maybe);
28432871

@@ -3637,19 +3665,25 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
36373665
hir::ExprAssign(ref lhs, ref rhs) => {
36383666
let lhs_ty = self.check_expr_with_lvalue_pref(&lhs, PreferMutLvalue);
36393667

3640-
let tcx = self.tcx;
3641-
if !tcx.expr_is_lval(&lhs) {
3642-
struct_span_err!(
3643-
tcx.sess, expr.span, E0070,
3644-
"invalid left-hand side expression")
3645-
.span_label(
3646-
expr.span,
3647-
"left-hand of expression not valid")
3648-
.emit();
3649-
}
3650-
36513668
let rhs_ty = self.check_expr_coercable_to_type(&rhs, lhs_ty);
36523669

3670+
match expected {
3671+
ExpectIfCondition => (),
3672+
_ => {
3673+
// Only check this if not in an `if` condition, as the
3674+
// mistyped comparison help is more appropriate.
3675+
if !self.tcx.expr_is_lval(&lhs) {
3676+
struct_span_err!(
3677+
self.tcx.sess, expr.span, E0070,
3678+
"invalid left-hand side expression")
3679+
.span_label(
3680+
expr.span,
3681+
"left-hand of expression not valid")
3682+
.emit();
3683+
}
3684+
}
3685+
}
3686+
36533687
self.require_type_is_sized(lhs_ty, lhs.span, traits::AssignmentLhsSized);
36543688

36553689
if lhs_ty.references_error() || rhs_ty.references_error() {

src/test/compile-fail/issue-17283.rs renamed to src/test/ui/type-check/issue-17283.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,25 +24,25 @@ fn main() {
2424
// `x { ... }` should not be interpreted as a struct literal here
2525
if x = x {
2626
//~^ ERROR mismatched types
27-
//~| expected type `bool`
28-
//~| found type `()`
29-
//~| expected bool, found ()
27+
//~| HELP did you mean to compare equality?
3028
println!("{}", x);
3129
}
3230
// Explicit parentheses on the left should match behavior of above
3331
if (x = x) {
3432
//~^ ERROR mismatched types
35-
//~| expected type `bool`
36-
//~| found type `()`
37-
//~| expected bool, found ()
33+
//~| HELP did you mean to compare equality?
3834
println!("{}", x);
3935
}
4036
// The struct literal interpretation is fine with explicit parentheses on the right
4137
if y = (Foo { foo: x }) {
4238
//~^ ERROR mismatched types
43-
//~| expected type `bool`
44-
//~| found type `()`
45-
//~| expected bool, found ()
39+
//~| HELP did you mean to compare equality?
40+
println!("{}", x);
41+
}
42+
// "invalid left-hand side expression" error is suppresed
43+
if 3 = x {
44+
//~^ ERROR mismatched types
45+
//~| HELP did you mean to compare equality?
4646
println!("{}", x);
4747
}
4848
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-17283.rs:25:8
3+
|
4+
25 | if x = x {
5+
| ^^^^^
6+
| |
7+
| help: did you mean to compare equality? `x == x`
8+
| expected bool, found ()
9+
|
10+
= note: expected type `bool`
11+
found type `()`
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/issue-17283.rs:31:8
15+
|
16+
31 | if (x = x) {
17+
| ^^^^^^^
18+
| |
19+
| help: did you mean to compare equality? `x == x`
20+
| expected bool, found ()
21+
|
22+
= note: expected type `bool`
23+
found type `()`
24+
25+
error[E0308]: mismatched types
26+
--> $DIR/issue-17283.rs:37:8
27+
|
28+
37 | if y = (Foo { foo: x }) {
29+
| ^^^^^^^^^^^^^^^^^^^^
30+
| |
31+
| help: did you mean to compare equality? `y == (Foo { foo: x })`
32+
| expected bool, found ()
33+
|
34+
= note: expected type `bool`
35+
found type `()`
36+
37+
error[E0308]: mismatched types
38+
--> $DIR/issue-17283.rs:43:8
39+
|
40+
43 | if 3 = x {
41+
| ^^^^^
42+
| |
43+
| help: did you mean to compare equality? `3 == x`
44+
| expected bool, found ()
45+
|
46+
= note: expected type `bool`
47+
found type `()`
48+
49+
error: aborting due to previous error(s)
50+

0 commit comments

Comments
 (0)