about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEthan Brierley <ethanboxx@gmail.com>2023-10-20 09:46:17 +0100
committerEthan Brierley <ethanboxx@gmail.com>2023-10-20 10:13:19 +0100
commit6c97f136126557a982990a1d0ef879996b268cba (patch)
treec7a16f11cf0b005efaa73067f5593f148f60b35c
parent7849162ace72b5793f23ad68ed985c247e3cab13 (diff)
downloadrust-6c97f136126557a982990a1d0ef879996b268cba.tar.gz
rust-6c97f136126557a982990a1d0ef879996b268cba.zip
Invalid `?` suggestion on mismatched `Ok(T)`
-rw-r--r--compiler/rustc_hir_typeck/src/demand.rs5
-rw-r--r--tests/ui/type/type-check/issue-116967-cannot-coerce-returned-result.rs6
-rw-r--r--tests/ui/type/type-check/issue-116967-cannot-coerce-returned-result.stderr15
3 files changed, 26 insertions, 0 deletions
diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs
index 65ec2f232ae..3b09fdda0ce 100644
--- a/compiler/rustc_hir_typeck/src/demand.rs
+++ b/compiler/rustc_hir_typeck/src/demand.rs
@@ -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
diff --git a/tests/ui/type/type-check/issue-116967-cannot-coerce-returned-result.rs b/tests/ui/type/type-check/issue-116967-cannot-coerce-returned-result.rs
new file mode 100644
index 00000000000..adf3049b4ba
--- /dev/null
+++ b/tests/ui/type/type-check/issue-116967-cannot-coerce-returned-result.rs
@@ -0,0 +1,6 @@
+fn foo() -> Result<String, ()> {
+    let out: Result<(), ()> = Ok(());
+    out //~ ERROR mismatched types
+}
+
+fn main() {}
diff --git a/tests/ui/type/type-check/issue-116967-cannot-coerce-returned-result.stderr b/tests/ui/type/type-check/issue-116967-cannot-coerce-returned-result.stderr
new file mode 100644
index 00000000000..447b22a152d
--- /dev/null
+++ b/tests/ui/type/type-check/issue-116967-cannot-coerce-returned-result.stderr
@@ -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`.