Skip to content

Commit 94543b5

Browse files
committedSep 2, 2019
Auto merge of #64049 - estebank:if-else-type-err, r=Centril
Emit a single error on if expr with expectation and no else clause Fix #60254. r? @Centril
2 parents fdaf594 + f53c217 commit 94543b5

File tree

5 files changed

+30
-105
lines changed

5 files changed

+30
-105
lines changed
 

‎src/librustc_typeck/check/_match.rs

+21-9
Original file line numberDiff line numberDiff line change
@@ -112,19 +112,26 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
112112
}
113113

114114
self.diverges.set(pats_diverge);
115-
let arm_ty = self.check_expr_with_expectation(&arm.body, expected);
115+
let arm_ty = if source_if && if_no_else && i != 0 && self.if_fallback_coercion(
116+
expr.span,
117+
&arms[0].body,
118+
&mut coercion,
119+
) {
120+
tcx.types.err
121+
} else {
122+
// Only call this if this is not an `if` expr with an expected type and no `else`
123+
// clause to avoid duplicated type errors. (#60254)
124+
self.check_expr_with_expectation(&arm.body, expected)
125+
};
116126
all_arms_diverge &= self.diverges.get();
117-
118-
let span = expr.span;
119-
120127
if source_if {
121128
let then_expr = &arms[0].body;
122129
match (i, if_no_else) {
123-
(0, _) => coercion.coerce(self, &self.misc(span), &arm.body, arm_ty),
124-
(_, true) => self.if_fallback_coercion(span, then_expr, &mut coercion),
130+
(0, _) => coercion.coerce(self, &self.misc(expr.span), &arm.body, arm_ty),
131+
(_, true) => {} // Handled above to avoid duplicated type errors (#60254).
125132
(_, _) => {
126133
let then_ty = prior_arm_ty.unwrap();
127-
let cause = self.if_cause(span, then_expr, &arm.body, then_ty, arm_ty);
134+
let cause = self.if_cause(expr.span, then_expr, &arm.body, then_ty, arm_ty);
128135
coercion.coerce(self, &cause, &arm.body, arm_ty);
129136
}
130137
}
@@ -139,7 +146,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
139146
// The reason for the first arm to fail is not that the match arms diverge,
140147
// but rather that there's a prior obligation that doesn't hold.
141148
0 => (arm_span, ObligationCauseCode::BlockTailExpression(arm.body.hir_id)),
142-
_ => (span, ObligationCauseCode::MatchExpressionArm {
149+
_ => (expr.span, ObligationCauseCode::MatchExpressionArm {
143150
arm_span,
144151
source: match_src,
145152
prior_arms: other_arms.clone(),
@@ -180,16 +187,19 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
180187
}
181188

182189
/// Handle the fallback arm of a desugared if(-let) like a missing else.
190+
///
191+
/// Returns `true` if there was an error forcing the coercion to the `()` type.
183192
fn if_fallback_coercion(
184193
&self,
185194
span: Span,
186195
then_expr: &'tcx hir::Expr,
187196
coercion: &mut CoerceMany<'tcx, '_, rustc::hir::Arm>,
188-
) {
197+
) -> bool {
189198
// If this `if` expr is the parent's function return expr,
190199
// the cause of the type coercion is the return type, point at it. (#25228)
191200
let ret_reason = self.maybe_get_coercion_reason(then_expr.hir_id, span);
192201
let cause = self.cause(span, ObligationCauseCode::IfExpressionWithNoElse);
202+
let mut error = false;
193203
coercion.coerce_forced_unit(self, &cause, &mut |err| {
194204
if let Some((span, msg)) = &ret_reason {
195205
err.span_label(*span, msg.as_str());
@@ -200,7 +210,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
200210
}
201211
err.note("`if` expressions without `else` evaluate to `()`");
202212
err.help("consider adding an `else` block that evaluates to the expected type");
213+
error = true;
203214
}, ret_reason.is_none());
215+
error
204216
}
205217

206218
fn maybe_get_coercion_reason(&self, hir_id: hir::HirId, span: Span) -> Option<(Span, String)> {

‎src/test/ui/if/if-without-else-as-fn-expr.rs

-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,13 @@ fn foo(bar: usize) -> usize {
33
return 3;
44
}
55
//~^^^ ERROR if may be missing an else clause
6-
//~| ERROR mismatched types [E0308]
76
}
87

98
fn foo2(bar: usize) -> usize {
109
let x: usize = if bar % 5 == 0 {
1110
return 3;
1211
};
1312
//~^^^ ERROR if may be missing an else clause
14-
//~| ERROR mismatched types [E0308]
1513
x
1614
}
1715

@@ -20,23 +18,20 @@ fn foo3(bar: usize) -> usize {
2018
3
2119
}
2220
//~^^^ ERROR if may be missing an else clause
23-
//~| ERROR mismatched types [E0308]
2421
}
2522

2623
fn foo_let(bar: usize) -> usize {
2724
if let 0 = 1 {
2825
return 3;
2926
}
3027
//~^^^ ERROR if may be missing an else clause
31-
//~| ERROR mismatched types [E0308]
3228
}
3329

3430
fn foo2_let(bar: usize) -> usize {
3531
let x: usize = if let 0 = 1 {
3632
return 3;
3733
};
3834
//~^^^ ERROR if may be missing an else clause
39-
//~| ERROR mismatched types [E0308]
4035
x
4136
}
4237

@@ -45,7 +40,6 @@ fn foo3_let(bar: usize) -> usize {
4540
3
4641
}
4742
//~^^^ ERROR if may be missing an else clause
48-
//~| ERROR mismatched types [E0308]
4943
}
5044

5145
// FIXME(60254): deduplicate first error in favor of second.
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/if-without-else-as-fn-expr.rs:2:5
3-
|
4-
LL | / if bar % 5 == 0 {
5-
LL | | return 3;
6-
LL | | }
7-
| |_____^ expected usize, found ()
8-
|
9-
= note: expected type `usize`
10-
found type `()`
11-
121
error[E0317]: if may be missing an else clause
132
--> $DIR/if-without-else-as-fn-expr.rs:2:5
143
|
@@ -24,20 +13,8 @@ LL | | }
2413
= note: `if` expressions without `else` evaluate to `()`
2514
= help: consider adding an `else` block that evaluates to the expected type
2615

27-
error[E0308]: mismatched types
28-
--> $DIR/if-without-else-as-fn-expr.rs:10:20
29-
|
30-
LL | let x: usize = if bar % 5 == 0 {
31-
| ____________________^
32-
LL | | return 3;
33-
LL | | };
34-
| |_____^ expected usize, found ()
35-
|
36-
= note: expected type `usize`
37-
found type `()`
38-
3916
error[E0317]: if may be missing an else clause
40-
--> $DIR/if-without-else-as-fn-expr.rs:10:20
17+
--> $DIR/if-without-else-as-fn-expr.rs:9:20
4118
|
4219
LL | let x: usize = if bar % 5 == 0 {
4320
| _________-__________^
@@ -52,19 +29,8 @@ LL | | };
5229
= note: `if` expressions without `else` evaluate to `()`
5330
= help: consider adding an `else` block that evaluates to the expected type
5431

55-
error[E0308]: mismatched types
56-
--> $DIR/if-without-else-as-fn-expr.rs:19:5
57-
|
58-
LL | / if bar % 5 == 0 {
59-
LL | | 3
60-
LL | | }
61-
| |_____^ expected usize, found ()
62-
|
63-
= note: expected type `usize`
64-
found type `()`
65-
6632
error[E0317]: if may be missing an else clause
67-
--> $DIR/if-without-else-as-fn-expr.rs:19:5
33+
--> $DIR/if-without-else-as-fn-expr.rs:17:5
6834
|
6935
LL | fn foo3(bar: usize) -> usize {
7036
| ----- expected `usize` because of this return type
@@ -78,19 +44,8 @@ LL | | }
7844
= note: `if` expressions without `else` evaluate to `()`
7945
= help: consider adding an `else` block that evaluates to the expected type
8046

81-
error[E0308]: mismatched types
82-
--> $DIR/if-without-else-as-fn-expr.rs:27:5
83-
|
84-
LL | / if let 0 = 1 {
85-
LL | | return 3;
86-
LL | | }
87-
| |_____^ expected usize, found ()
88-
|
89-
= note: expected type `usize`
90-
found type `()`
91-
9247
error[E0317]: if may be missing an else clause
93-
--> $DIR/if-without-else-as-fn-expr.rs:27:5
48+
--> $DIR/if-without-else-as-fn-expr.rs:24:5
9449
|
9550
LL | fn foo_let(bar: usize) -> usize {
9651
| ----- expected `usize` because of this return type
@@ -104,20 +59,8 @@ LL | | }
10459
= note: `if` expressions without `else` evaluate to `()`
10560
= help: consider adding an `else` block that evaluates to the expected type
10661

107-
error[E0308]: mismatched types
108-
--> $DIR/if-without-else-as-fn-expr.rs:35:20
109-
|
110-
LL | let x: usize = if let 0 = 1 {
111-
| ____________________^
112-
LL | | return 3;
113-
LL | | };
114-
| |_____^ expected usize, found ()
115-
|
116-
= note: expected type `usize`
117-
found type `()`
118-
11962
error[E0317]: if may be missing an else clause
120-
--> $DIR/if-without-else-as-fn-expr.rs:35:20
63+
--> $DIR/if-without-else-as-fn-expr.rs:31:20
12164
|
12265
LL | let x: usize = if let 0 = 1 {
12366
| _________-__________^
@@ -132,19 +75,8 @@ LL | | };
13275
= note: `if` expressions without `else` evaluate to `()`
13376
= help: consider adding an `else` block that evaluates to the expected type
13477

135-
error[E0308]: mismatched types
136-
--> $DIR/if-without-else-as-fn-expr.rs:44:5
137-
|
138-
LL | / if let 0 = 1 {
139-
LL | | 3
140-
LL | | }
141-
| |_____^ expected usize, found ()
142-
|
143-
= note: expected type `usize`
144-
found type `()`
145-
14678
error[E0317]: if may be missing an else clause
147-
--> $DIR/if-without-else-as-fn-expr.rs:44:5
79+
--> $DIR/if-without-else-as-fn-expr.rs:39:5
14880
|
14981
LL | fn foo3_let(bar: usize) -> usize {
15082
| ----- expected `usize` because of this return type
@@ -158,7 +90,6 @@ LL | | }
15890
= note: `if` expressions without `else` evaluate to `()`
15991
= help: consider adding an `else` block that evaluates to the expected type
16092

161-
error: aborting due to 12 previous errors
93+
error: aborting due to 6 previous errors
16294

163-
Some errors have detailed explanations: E0308, E0317.
164-
For more information about an error, try `rustc --explain E0308`.
95+
For more information about this error, try `rustc --explain E0317`.

‎src/test/ui/issues/issue-50577.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@ fn main() {
22
enum Foo {
33
Drop = assert_eq!(1, 1)
44
//~^ ERROR if may be missing an else clause
5-
//~| ERROR mismatched types [E0308]
65
}
76
}

‎src/test/ui/issues/issue-50577.stderr

+2-13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/issue-50577.rs:3:16
3-
|
4-
LL | Drop = assert_eq!(1, 1)
5-
| ^^^^^^^^^^^^^^^^ expected isize, found ()
6-
|
7-
= note: expected type `isize`
8-
found type `()`
9-
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
10-
111
error[E0317]: if may be missing an else clause
122
--> $DIR/issue-50577.rs:3:16
133
|
@@ -23,7 +13,6 @@ LL | Drop = assert_eq!(1, 1)
2313
= help: consider adding an `else` block that evaluates to the expected type
2414
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
2515

26-
error: aborting due to 2 previous errors
16+
error: aborting due to previous error
2717

28-
Some errors have detailed explanations: E0308, E0317.
29-
For more information about an error, try `rustc --explain E0308`.
18+
For more information about this error, try `rustc --explain E0317`.

0 commit comments

Comments
 (0)