about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDylan DPC <99973273+Dylan-DPC@users.noreply.github.com>2022-08-30 16:56:07 +0530
committerGitHub <noreply@github.com>2022-08-30 16:56:07 +0530
commit9cfd161cd5b2e3b53c488086f8000aea0c21b0b2 (patch)
tree1a05c18d4790c085dcab0deb7a156a2a632291ad
parent548ed409af7fc161859ffd864cedacd7af8748d6 (diff)
parentfb12f40f3b157b0f7dafe393b09bbdcf2e156c11 (diff)
downloadrust-9cfd161cd5b2e3b53c488086f8000aea0c21b0b2.tar.gz
rust-9cfd161cd5b2e3b53c488086f8000aea0c21b0b2.zip
Rollup merge of #99928 - compiler-errors:issue-99914, r=oli-obk
Do not leak type variables from opaque type relation

The "root cause" is that we call `InferCtxt::resolve_vars_if_possible` (3d9dd681f520d1d59f38aed0056cf9474894cc74) on the types we get back in `TypeError::Sorts` since I added a call to it in `InferCtxt::same_type_modulo_infer`. However if this `TypeError` comes from a `InferCtxt::commit_if_ok`, then it may reference type variables that do not exist anymore, which is problematic.

We avoid this by substituting the `TypeError` with the types we had before being generalized while handling opaques.

This is kinda gross, and I feel like we can get the same issue from other places where we generalize type/const inference variables. Maybe not? I don't know.

Fixes #99914
Fixes #99970
Fixes #100463
-rw-r--r--compiler/rustc_infer/src/infer/sub.rs17
-rw-r--r--src/test/ui/impl-trait/issue-99914.rs13
-rw-r--r--src/test/ui/impl-trait/issue-99914.stderr21
3 files changed, 48 insertions, 3 deletions
diff --git a/compiler/rustc_infer/src/infer/sub.rs b/compiler/rustc_infer/src/infer/sub.rs
index b27571275b7..b7eab5d4328 100644
--- a/compiler/rustc_infer/src/infer/sub.rs
+++ b/compiler/rustc_infer/src/infer/sub.rs
@@ -4,6 +4,7 @@ use super::SubregionOrigin;
 use crate::infer::combine::ConstEquateRelation;
 use crate::infer::{TypeVariableOrigin, TypeVariableOriginKind};
 use crate::traits::Obligation;
+use rustc_middle::ty::error::{ExpectedFound, TypeError};
 use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
 use rustc_middle::ty::visit::TypeVisitable;
 use rustc_middle::ty::TyVar;
@@ -141,17 +142,27 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
                     Ok(infcx.tcx.mk_ty_var(var))
                 };
                 let (a, b) = if self.a_is_expected { (a, b) } else { (b, a) };
-                let (a, b) = match (a.kind(), b.kind()) {
+                let (ga, gb) = match (a.kind(), b.kind()) {
                     (&ty::Opaque(..), _) => (a, generalize(b, true)?),
                     (_, &ty::Opaque(..)) => (generalize(a, false)?, b),
                     _ => unreachable!(),
                 };
                 self.fields.obligations.extend(
                     infcx
-                        .handle_opaque_type(a, b, true, &self.fields.trace.cause, self.param_env())?
+                        .handle_opaque_type(ga, gb, true, &self.fields.trace.cause, self.param_env())
+                        // Don't leak any generalized type variables out of this
+                        // subtyping relation in the case of a type error.
+                        .map_err(|err| {
+                            let (ga, gb) = self.fields.infcx.resolve_vars_if_possible((ga, gb));
+                            if let TypeError::Sorts(sorts) = err && sorts.expected == ga && sorts.found == gb {
+                                TypeError::Sorts(ExpectedFound { expected: a, found: b })
+                            } else {
+                                err
+                            }
+                        })?
                         .obligations,
                 );
-                Ok(a)
+                Ok(ga)
             }
 
             _ => {
diff --git a/src/test/ui/impl-trait/issue-99914.rs b/src/test/ui/impl-trait/issue-99914.rs
new file mode 100644
index 00000000000..4324a0229a6
--- /dev/null
+++ b/src/test/ui/impl-trait/issue-99914.rs
@@ -0,0 +1,13 @@
+// edition:2021
+
+fn main() {}
+
+struct Error;
+struct Okay;
+
+fn foo(t: Result<Okay, Error>) {
+    t.and_then(|t| -> _ { bar(t) });
+    //~^ ERROR mismatched types
+}
+
+async fn bar(t: Okay) {}
diff --git a/src/test/ui/impl-trait/issue-99914.stderr b/src/test/ui/impl-trait/issue-99914.stderr
new file mode 100644
index 00000000000..074d5d58d9a
--- /dev/null
+++ b/src/test/ui/impl-trait/issue-99914.stderr
@@ -0,0 +1,21 @@
+error[E0308]: mismatched types
+  --> $DIR/issue-99914.rs:9:27
+   |
+LL |     t.and_then(|t| -> _ { bar(t) });
+   |                           ^^^^^^ expected enum `Result`, found opaque type
+   |
+note: while checking the return type of the `async fn`
+  --> $DIR/issue-99914.rs:13:23
+   |
+LL | async fn bar(t: Okay) {}
+   |                       ^ checked the `Output` of this `async fn`, found opaque type
+   = note:     expected enum `Result<_, Error>`
+           found opaque type `impl Future<Output = ()>`
+help: try wrapping the expression in `Ok`
+   |
+LL |     t.and_then(|t| -> _ { Ok(bar(t)) });
+   |                           +++      +
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0308`.