Do not leak type variables from opaque type relation
This commit is contained in:
parent
9de7474830
commit
fb12f40f3b
3 changed files with 48 additions and 3 deletions
|
@ -4,6 +4,7 @@ use super::SubregionOrigin;
|
||||||
use crate::infer::combine::ConstEquateRelation;
|
use crate::infer::combine::ConstEquateRelation;
|
||||||
use crate::infer::{TypeVariableOrigin, TypeVariableOriginKind};
|
use crate::infer::{TypeVariableOrigin, TypeVariableOriginKind};
|
||||||
use crate::traits::Obligation;
|
use crate::traits::Obligation;
|
||||||
|
use rustc_middle::ty::error::{ExpectedFound, TypeError};
|
||||||
use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
|
use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
|
||||||
use rustc_middle::ty::visit::TypeVisitable;
|
use rustc_middle::ty::visit::TypeVisitable;
|
||||||
use rustc_middle::ty::TyVar;
|
use rustc_middle::ty::TyVar;
|
||||||
|
@ -141,17 +142,27 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
|
||||||
Ok(infcx.tcx.mk_ty_var(var))
|
Ok(infcx.tcx.mk_ty_var(var))
|
||||||
};
|
};
|
||||||
let (a, b) = if self.a_is_expected { (a, b) } else { (b, a) };
|
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(..), _) => (a, generalize(b, true)?),
|
||||||
(_, &ty::Opaque(..)) => (generalize(a, false)?, b),
|
(_, &ty::Opaque(..)) => (generalize(a, false)?, b),
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
self.fields.obligations.extend(
|
self.fields.obligations.extend(
|
||||||
infcx
|
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,
|
.obligations,
|
||||||
);
|
);
|
||||||
Ok(a)
|
Ok(ga)
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {
|
_ => {
|
||||||
|
|
13
src/test/ui/impl-trait/issue-99914.rs
Normal file
13
src/test/ui/impl-trait/issue-99914.rs
Normal file
|
@ -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) {}
|
21
src/test/ui/impl-trait/issue-99914.stderr
Normal file
21
src/test/ui/impl-trait/issue-99914.stderr
Normal file
|
@ -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`.
|
Loading…
Add table
Add a link
Reference in a new issue