Skip to content

Commit

Permalink
Invalid ? suggestion on mismatched Ok(T)
Browse files Browse the repository at this point in the history
  • Loading branch information
eopb committed Oct 20, 2023
1 parent 7849162 commit 6c97f13
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_hir_typeck/src/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
} else {
return false;
}
let e_ok = args_e.type_at(0);
let f_ok = args_f.type_at(0);
if !self.infcx.can_eq(self.param_env, f_ok, e_ok) {
return false;
}
let e = args_e.type_at(1);
let f = args_f.type_at(1);
if self
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fn foo() -> Result<String, ()> {
let out: Result<(), ()> = Ok(());
out //~ ERROR mismatched types
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0308]: mismatched types
--> $DIR/issue-116967-cannot-coerce-returned-result.rs:3:5
|
LL | fn foo() -> Result<String, ()> {
| ------------------ expected `Result<String, ()>` because of return type
LL | let out: Result<(), ()> = Ok(());
LL | out
| ^^^ expected `Result<String, ()>`, found `Result<(), ()>`
|
= note: expected enum `Result<String, _>`
found enum `Result<(), _>`

error: aborting due to previous error

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

0 comments on commit 6c97f13

Please sign in to comment.