Skip to content

Commit 5fcefb1

Browse files
authored
Rollup merge of #87061 - FabianWolff:issue-87051, r=oli-obk
Do not suggest adding a semicolon after `?` Fixes #87051. I have only modified `report_return_mismatched_types()`, i.e. my changes only affect suggestions to add `;` for return type mismatches, but this never makes sense after `?`, because the function cannot return `()` if `?` is used (it has to return a `Result` or an `Option`), and a semicolon won't help if the expected and actual `Err` types differ, even if the expected one is `()`.
2 parents f3d5fde + 9946ff2 commit 5fcefb1

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

compiler/rustc_typeck/src/check/coercion.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1456,11 +1456,15 @@ impl<'tcx, 'exprs, E: AsCoercionSite> CoerceMany<'tcx, 'exprs, E> {
14561456
expected.is_unit(),
14571457
pointing_at_return_type,
14581458
) {
1459-
// If the block is from an external macro, then do not suggest
1460-
// adding a semicolon, because there's nowhere to put it.
1461-
// See issue #81943.
1459+
// If the block is from an external macro or try (`?`) desugaring, then
1460+
// do not suggest adding a semicolon, because there's nowhere to put it.
1461+
// See issues #81943 and #87051.
14621462
if cond_expr.span.desugaring_kind().is_none()
14631463
&& !in_external_macro(fcx.tcx.sess, cond_expr.span)
1464+
&& !matches!(
1465+
cond_expr.kind,
1466+
hir::ExprKind::Match(.., hir::MatchSource::TryDesugar)
1467+
)
14641468
{
14651469
err.span_label(cond_expr.span, "expected this to be `()`");
14661470
if expr.can_have_side_effects() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Regression test for #87051, where a double semicolon was erroneously
2+
// suggested after a `?` operator.
3+
4+
fn main() -> Result<(), ()> {
5+
a(|| {
6+
b()
7+
//~^ ERROR: mismatched types [E0308]
8+
//~| NOTE: expected `()`, found `i32`
9+
//~| HELP: consider using a semicolon here
10+
})?;
11+
12+
// Here, we do want to suggest a semicolon:
13+
let x = Ok(42);
14+
if true {
15+
//~^ NOTE: expected this to be `()`
16+
x?
17+
//~^ ERROR: mismatched types [E0308]
18+
//~| NOTE: expected `()`, found integer
19+
//~| HELP: consider using a semicolon here
20+
}
21+
//~^ HELP: consider using a semicolon here
22+
23+
Ok(())
24+
}
25+
26+
fn a<F>(f: F) -> Result<(), ()> where F: FnMut() { Ok(()) }
27+
fn b() -> i32 { 42 }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/try-operator-dont-suggest-semicolon.rs:6:9
3+
|
4+
LL | b()
5+
| ^^^- help: consider using a semicolon here: `;`
6+
| |
7+
| expected `()`, found `i32`
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/try-operator-dont-suggest-semicolon.rs:16:9
11+
|
12+
LL | / if true {
13+
LL | |
14+
LL | | x?
15+
| | ^^ expected `()`, found integer
16+
LL | |
17+
LL | |
18+
LL | |
19+
LL | | }
20+
| |_____- expected this to be `()`
21+
|
22+
help: consider using a semicolon here
23+
|
24+
LL | x?;
25+
| ^
26+
help: consider using a semicolon here
27+
|
28+
LL | };
29+
| ^
30+
31+
error: aborting due to 2 previous errors
32+
33+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)