Skip to content

Commit cab607e

Browse files
committed
Emit a single error on if expr with expectation and no else clause
1 parent fba38ac commit cab607e

File tree

5 files changed

+38
-108
lines changed

5 files changed

+38
-108
lines changed

src/librustc_typeck/check/_match.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,23 +112,36 @@ 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);
116-
all_arms_diverge &= self.diverges.get();
117-
118-
let span = expr.span;
119-
120-
if source_if {
115+
let arm_ty = if source_if {
121116
let then_expr = &arms[0].body;
122117
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),
118+
(0, _) => {
119+
let arm_ty = self.check_expr_with_expectation(&arm.body, expected);
120+
all_arms_diverge &= self.diverges.get();
121+
coercion.coerce(self, &self.misc(expr.span), &arm.body, arm_ty);
122+
arm_ty
123+
}
124+
(_, true) => {
125+
if self.if_fallback_coercion(expr.span, then_expr, &mut coercion) {
126+
tcx.types.err
127+
} else {
128+
let arm_ty = self.check_expr_with_expectation(&arm.body, expected);
129+
all_arms_diverge &= self.diverges.get();
130+
arm_ty
131+
}
132+
}
125133
(_, _) => {
134+
let arm_ty = self.check_expr_with_expectation(&arm.body, expected);
135+
all_arms_diverge &= self.diverges.get();
126136
let then_ty = prior_arm_ty.unwrap();
127-
let cause = self.if_cause(span, then_expr, &arm.body, then_ty, arm_ty);
137+
let cause = self.if_cause(expr.span, then_expr, &arm.body, then_ty, arm_ty);
128138
coercion.coerce(self, &cause, &arm.body, arm_ty);
139+
arm_ty
129140
}
130141
}
131142
} else {
143+
let arm_ty = self.check_expr_with_expectation(&arm.body, expected);
144+
all_arms_diverge &= self.diverges.get();
132145
let arm_span = if let hir::ExprKind::Block(blk, _) = &arm.body.node {
133146
// Point at the block expr instead of the entire block
134147
blk.expr.as_ref().map(|e| e.span).unwrap_or(arm.body.span)
@@ -139,7 +152,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
139152
// The reason for the first arm to fail is not that the match arms diverge,
140153
// but rather that there's a prior obligation that doesn't hold.
141154
0 => (arm_span, ObligationCauseCode::BlockTailExpression(arm.body.hir_id)),
142-
_ => (span, ObligationCauseCode::MatchExpressionArm {
155+
_ => (expr.span, ObligationCauseCode::MatchExpressionArm {
143156
arm_span,
144157
source: match_src,
145158
prior_arms: other_arms.clone(),
@@ -153,7 +166,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
153166
if other_arms.len() > 5 {
154167
other_arms.remove(0);
155168
}
156-
}
169+
arm_ty
170+
};
157171
prior_arm_ty = Some(arm_ty);
158172
}
159173

@@ -185,11 +199,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
185199
span: Span,
186200
then_expr: &'tcx hir::Expr,
187201
coercion: &mut CoerceMany<'tcx, '_, rustc::hir::Arm>,
188-
) {
202+
) -> bool {
189203
// If this `if` expr is the parent's function return expr,
190204
// the cause of the type coercion is the return type, point at it. (#25228)
191205
let ret_reason = self.maybe_get_coercion_reason(then_expr.hir_id, span);
192206
let cause = self.cause(span, ObligationCauseCode::IfExpressionWithNoElse);
207+
let mut error = false;
193208
coercion.coerce_forced_unit(self, &cause, &mut |err| {
194209
if let Some((span, msg)) = &ret_reason {
195210
err.span_label(*span, msg.as_str());
@@ -200,7 +215,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
200215
}
201216
err.note("`if` expressions without `else` evaluate to `()`");
202217
err.help("consider adding an `else` block that evaluates to the expected type");
218+
error = true;
203219
}, ret_reason.is_none());
220+
error
204221
}
205222

206223
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

Lines changed: 0 additions & 6 deletions
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.
Lines changed: 7 additions & 76 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 2 additions & 13 deletions
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)