Skip to content

Commit 035e07f

Browse files
committed
Invalid ? suggestion on mismatched Ok(T)
1 parent 7849162 commit 035e07f

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

compiler/rustc_hir_typeck/src/demand.rs

+5
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
983983
} else {
984984
return false;
985985
}
986+
let e_ok = args_e.type_at(0);
987+
let f_ok = args_f.type_at(0);
988+
if !self.infcx.can_eq(self.param_env, f_ok, e_ok) {
989+
return false;
990+
}
986991
let e = args_e.type_at(1);
987992
let f = args_f.type_at(1);
988993
if self
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn foo() -> Result<String, ()> {
2+
let out: Result<(), ()> = Ok(());
3+
out //~ ERROR mismatched types
4+
}
5+
6+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-116967-cannot-coerce-return-value.rs:3:5
3+
|
4+
LL | fn foo() -> Result<String, ()> {
5+
| ------------------ expected `Result<String, ()>` because of return type
6+
LL | let out: Result<(), ()> = Ok(());
7+
LL | out
8+
| ^^^ expected `Result<String, ()>`, found `Result<(), ()>`
9+
|
10+
= note: expected enum `Result<String, _>`
11+
found enum `Result<(), _>`
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)