Skip to content

Commit c865d39

Browse files
committed
Auto merge of #51938 - zackmdavis:and_the_case_of_the_leaking_desugar, r=estebank
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 #51632.
2 parents b51ca20 + 6cc78bf commit c865d39

4 files changed

+82
-2
lines changed

src/librustc/infer/error_reporting/mod.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
518518
} else {
519519
err.span_label(arm_span, msg);
520520
}
521-
}
521+
},
522+
hir::MatchSource::TryDesugar => { // Issue #51632
523+
if let Ok(try_snippet) = self.tcx.sess.codemap().span_to_snippet(arm_span) {
524+
err.span_suggestion_with_applicability(
525+
arm_span,
526+
"try wrapping with a success variant",
527+
format!("Ok({})", try_snippet),
528+
Applicability::MachineApplicable
529+
);
530+
}
531+
},
522532
_ => {
523533
let msg = "match arm with an incompatible type";
524534
if self.tcx.sess.codemap().is_multiline(arm_span) {
@@ -1312,7 +1322,12 @@ impl<'tcx> ObligationCause<'tcx> {
13121322
match self.code {
13131323
CompareImplMethodObligation { .. } => Error0308("method not compatible with trait"),
13141324
MatchExpressionArm { source, .. } => Error0308(match source {
1315-
hir::MatchSource::IfLetDesugar { .. } => "`if let` arms have incompatible types",
1325+
hir::MatchSource::IfLetDesugar { .. } => {
1326+
"`if let` arms have incompatible types"
1327+
},
1328+
hir::MatchSource::TryDesugar => {
1329+
"try expression alternatives have incompatible types"
1330+
},
13161331
_ => "match arms have incompatible types",
13171332
}),
13181333
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)