Skip to content

Commit 9cfd161

Browse files
authored
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` (3d9dd68) 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
2 parents 548ed40 + fb12f40 commit 9cfd161

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

compiler/rustc_infer/src/infer/sub.rs

+14-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::SubregionOrigin;
44
use crate::infer::combine::ConstEquateRelation;
55
use crate::infer::{TypeVariableOrigin, TypeVariableOriginKind};
66
use crate::traits::Obligation;
7+
use rustc_middle::ty::error::{ExpectedFound, TypeError};
78
use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
89
use rustc_middle::ty::visit::TypeVisitable;
910
use rustc_middle::ty::TyVar;
@@ -141,17 +142,27 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
141142
Ok(infcx.tcx.mk_ty_var(var))
142143
};
143144
let (a, b) = if self.a_is_expected { (a, b) } else { (b, a) };
144-
let (a, b) = match (a.kind(), b.kind()) {
145+
let (ga, gb) = match (a.kind(), b.kind()) {
145146
(&ty::Opaque(..), _) => (a, generalize(b, true)?),
146147
(_, &ty::Opaque(..)) => (generalize(a, false)?, b),
147148
_ => unreachable!(),
148149
};
149150
self.fields.obligations.extend(
150151
infcx
151-
.handle_opaque_type(a, b, true, &self.fields.trace.cause, self.param_env())?
152+
.handle_opaque_type(ga, gb, true, &self.fields.trace.cause, self.param_env())
153+
// Don't leak any generalized type variables out of this
154+
// subtyping relation in the case of a type error.
155+
.map_err(|err| {
156+
let (ga, gb) = self.fields.infcx.resolve_vars_if_possible((ga, gb));
157+
if let TypeError::Sorts(sorts) = err && sorts.expected == ga && sorts.found == gb {
158+
TypeError::Sorts(ExpectedFound { expected: a, found: b })
159+
} else {
160+
err
161+
}
162+
})?
152163
.obligations,
153164
);
154-
Ok(a)
165+
Ok(ga)
155166
}
156167

157168
_ => {

src/test/ui/impl-trait/issue-99914.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// edition:2021
2+
3+
fn main() {}
4+
5+
struct Error;
6+
struct Okay;
7+
8+
fn foo(t: Result<Okay, Error>) {
9+
t.and_then(|t| -> _ { bar(t) });
10+
//~^ ERROR mismatched types
11+
}
12+
13+
async fn bar(t: Okay) {}
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-99914.rs:9:27
3+
|
4+
LL | t.and_then(|t| -> _ { bar(t) });
5+
| ^^^^^^ expected enum `Result`, found opaque type
6+
|
7+
note: while checking the return type of the `async fn`
8+
--> $DIR/issue-99914.rs:13:23
9+
|
10+
LL | async fn bar(t: Okay) {}
11+
| ^ checked the `Output` of this `async fn`, found opaque type
12+
= note: expected enum `Result<_, Error>`
13+
found opaque type `impl Future<Output = ()>`
14+
help: try wrapping the expression in `Ok`
15+
|
16+
LL | t.and_then(|t| -> _ { Ok(bar(t)) });
17+
| +++ +
18+
19+
error: aborting due to previous error
20+
21+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)