Skip to content

Commit 6cc78bf

Browse files
committed
in which we plug the crack where ?-desugaring leaked into errors
Most of the time, it's not a problem that the types of the arm bodies in a desugared-from-`?` match are different (that is, specifically: in `x?` where x is a `Result<A, B>`, the `Ok` arm body is an `A`, whereas the `Err` arm diverges to return a `Result<A, B>`), because they're being assigned to different places. But in tail position, the types do need to match, and our error message was explicitly referring to "match arms", which is confusing when there's no `match` in the sweetly sugared source. It is not without some misgivings that we pollute the clarity-of-purpose of `note_error_origin` with the suggestion to wrap with `Ok` (the other branches are pointing out the odd-arm-out in the HIR that is the origin of the error; the new branch that issues the `Ok` suggestion is serving a different purpose), but it's the natural place to do it given that we're already matching on `ObligationCauseCode::MatchExpressionArm { arm_span, source }` there. Resolves rust-lang#51632.
1 parent 1029775 commit 6cc78bf

4 files changed

+82
-2
lines changed

src/librustc/infer/error_reporting/mod.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
500500
} else {
501501
err.span_label(arm_span, msg);
502502
}
503-
}
503+
},
504+
hir::MatchSource::TryDesugar => { // Issue #51632
505+
if let Ok(try_snippet) = self.tcx.sess.codemap().span_to_snippet(arm_span) {
506+
err.span_suggestion_with_applicability(
507+
arm_span,
508+
"try wrapping with a success variant",
509+
format!("Ok({})", try_snippet),
510+
Applicability::MachineApplicable
511+
);
512+
}
513+
},
504514
_ => {
505515
let msg = "match arm with an incompatible type";
506516
if self.tcx.sess.codemap().is_multiline(arm_span) {
@@ -1294,7 +1304,12 @@ impl<'tcx> ObligationCause<'tcx> {
12941304
match self.code {
12951305
CompareImplMethodObligation { .. } => Error0308("method not compatible with trait"),
12961306
MatchExpressionArm { source, .. } => Error0308(match source {
1297-
hir::MatchSource::IfLetDesugar { .. } => "`if let` arms have incompatible types",
1307+
hir::MatchSource::IfLetDesugar { .. } => {
1308+
"`if let` arms have incompatible types"
1309+
},
1310+
hir::MatchSource::TryDesugar => {
1311+
"try expression alternatives have incompatible types"
1312+
},
12981313
_ => "match arms have incompatible types",
12991314
}),
13001315
IfExpression => Error0308("if and else have incompatible types"),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// run-rustfix
12+
13+
#![allow(dead_code)]
14+
15+
fn missing_discourses() -> Result<isize, ()> {
16+
Ok(1)
17+
}
18+
19+
fn forbidden_narratives() -> Result<isize, ()> {
20+
Ok(missing_discourses()?)
21+
//~^ ERROR try expression alternatives have incompatible types
22+
//~| HELP try wrapping with a success variant
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// run-rustfix
12+
13+
#![allow(dead_code)]
14+
15+
fn missing_discourses() -> Result<isize, ()> {
16+
Ok(1)
17+
}
18+
19+
fn forbidden_narratives() -> Result<isize, ()> {
20+
missing_discourses()?
21+
//~^ ERROR try expression alternatives have incompatible types
22+
//~| HELP try wrapping with a success variant
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: try expression alternatives have incompatible types
2+
--> $DIR/issue-51632-try-desugar-incompatible-types.rs:20:5
3+
|
4+
LL | missing_discourses()?
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| expected enum `std::result::Result`, found isize
8+
| help: try wrapping with a success variant: `Ok(missing_discourses()?)`
9+
|
10+
= note: expected type `std::result::Result<isize, ()>`
11+
found type `isize`
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)