Rollup merge of #111450 - compiler-errors:opaque-ty-key, r=oli-obk

Use `OpaqueTypeKey` in query response

Makes it a bit clearer that at least one of the types being returned from a canonical query is an opaque.
This commit is contained in:
Guillaume Gomez 2023-05-20 15:37:23 +02:00 committed by GitHub
commit de074242eb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 8 deletions

View file

@ -153,20 +153,22 @@ impl<'tcx> InferCtxt<'tcx> {
/// Used by the new solver as that one takes the opaque types at the end of a probe
/// to deal with multiple candidates without having to recompute them.
pub fn clone_opaque_types_for_query_response(&self) -> Vec<(Ty<'tcx>, Ty<'tcx>)> {
pub fn clone_opaque_types_for_query_response(
&self,
) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
self.inner
.borrow()
.opaque_type_storage
.opaque_types
.iter()
.map(|(k, v)| (self.tcx.mk_opaque(k.def_id.to_def_id(), k.substs), v.hidden_type.ty))
.map(|(k, v)| (*k, v.hidden_type.ty))
.collect()
}
fn take_opaque_types_for_query_response(&self) -> Vec<(Ty<'tcx>, Ty<'tcx>)> {
fn take_opaque_types_for_query_response(&self) -> Vec<(ty::OpaqueTypeKey<'tcx>, Ty<'tcx>)> {
std::mem::take(&mut self.inner.borrow_mut().opaque_type_storage.opaque_types)
.into_iter()
.map(|(k, v)| (self.tcx.mk_opaque(k.def_id.to_def_id(), k.substs), v.hidden_type.ty))
.map(|(k, v)| (k, v.hidden_type.ty))
.collect()
}
@ -507,8 +509,22 @@ impl<'tcx> InferCtxt<'tcx> {
let a = substitute_value(self.tcx, &result_subst, a);
let b = substitute_value(self.tcx, &result_subst, b);
debug!(?a, ?b, "constrain opaque type");
obligations
.extend(self.at(cause, param_env).eq(DefineOpaqueTypes::Yes, a, b)?.obligations);
// We use equate here instead of, for example, just registering the
// opaque type's hidden value directly, because we may be instantiating
// a query response that was canonicalized in an InferCtxt that had
// a different defining anchor. In that case, we may have inferred
// `NonLocalOpaque := LocalOpaque` but can only instantiate it in
// the other direction as `LocalOpaque := NonLocalOpaque`. Using eq
// here allows us to try both directions (in `InferCtxt::handle_opaque_type`).
obligations.extend(
self.at(cause, param_env)
.eq(
DefineOpaqueTypes::Yes,
self.tcx.mk_opaque(a.def_id.to_def_id(), a.substs),
b,
)?
.obligations,
);
}
Ok(InferOk { value: result_subst, obligations })