Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in which we plug the crack where ?-desugaring leaked into errors #51938

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,17 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
} else {
err.span_label(arm_span, msg);
}
}
},
hir::MatchSource::TryDesugar => { // Issue #51632
if let Ok(try_snippet) = self.tcx.sess.codemap().span_to_snippet(arm_span) {
err.span_suggestion_with_applicability(
arm_span,
"try wrapping with a success variant",
format!("Ok({})", try_snippet),
Applicability::MachineApplicable
);
}
},
_ => {
let msg = "match arm with an incompatible type";
if self.tcx.sess.codemap().is_multiline(arm_span) {
Expand Down Expand Up @@ -1294,7 +1304,12 @@ impl<'tcx> ObligationCause<'tcx> {
match self.code {
CompareImplMethodObligation { .. } => Error0308("method not compatible with trait"),
MatchExpressionArm { source, .. } => Error0308(match source {
hir::MatchSource::IfLetDesugar { .. } => "`if let` arms have incompatible types",
hir::MatchSource::IfLetDesugar { .. } => {
"`if let` arms have incompatible types"
},
hir::MatchSource::TryDesugar => {
"try expression alternatives have incompatible types"
},
_ => "match arms have incompatible types",
}),
IfExpression => Error0308("if and else have incompatible types"),
Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/issue-51632-try-desugar-incompatible-types.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// run-rustfix

#![allow(dead_code)]

fn missing_discourses() -> Result<isize, ()> {
Ok(1)
}

fn forbidden_narratives() -> Result<isize, ()> {
Ok(missing_discourses()?)
//~^ ERROR try expression alternatives have incompatible types
//~| HELP try wrapping with a success variant
}

fn main() {}
25 changes: 25 additions & 0 deletions src/test/ui/issue-51632-try-desugar-incompatible-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// run-rustfix

#![allow(dead_code)]

fn missing_discourses() -> Result<isize, ()> {
Ok(1)
}

fn forbidden_narratives() -> Result<isize, ()> {
missing_discourses()?
//~^ ERROR try expression alternatives have incompatible types
//~| HELP try wrapping with a success variant
}

fn main() {}
15 changes: 15 additions & 0 deletions src/test/ui/issue-51632-try-desugar-incompatible-types.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0308]: try expression alternatives have incompatible types
--> $DIR/issue-51632-try-desugar-incompatible-types.rs:20:5
|
LL | missing_discourses()?
| ^^^^^^^^^^^^^^^^^^^^^
| |
| expected enum `std::result::Result`, found isize
| help: try wrapping with a success variant: `Ok(missing_discourses()?)`
|
= note: expected type `std::result::Result<isize, ()>`
found type `isize`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0308`.