Skip to content

Commit 16e74c7

Browse files
authored
Rollup merge of #103255 - oli-obk:opaque_wrong_eq_relation, r=compiler-errors
Clean up hidden type registration work on #101186 Actually passing down the relation and using it instead of `eq` for the hidden type comparison has *no* effect whatsoever and allows for no further improvements at the call sites. I decided the increased complexity was not worth it and thus did not include that change in this PR. r? `@compiler-errors`
2 parents 6d43dfb + de5517c commit 16e74c7

File tree

6 files changed

+41
-75
lines changed

6 files changed

+41
-75
lines changed

compiler/rustc_borrowck/src/region_infer/opaque_types.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,11 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
263263

264264
// Require that the hidden type actually fulfills all the bounds of the opaque type, even without
265265
// the bounds that the function supplies.
266-
match infcx.register_hidden_type(
267-
OpaqueTypeKey { def_id, substs: id_substs },
268-
ObligationCause::misc(instantiated_ty.span, body_id),
269-
param_env,
270-
definition_ty,
271-
origin,
272-
) {
266+
let opaque_ty = self.tcx.mk_opaque(def_id.to_def_id(), id_substs);
267+
match infcx
268+
.at(&ObligationCause::misc(instantiated_ty.span, body_id), param_env)
269+
.eq(opaque_ty, definition_ty)
270+
{
273271
Ok(infer_ok) => {
274272
for obligation in infer_ok.obligations {
275273
fulfillment_cx.register_predicate_obligation(&infcx, obligation);
@@ -280,7 +278,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
280278
.err_ctxt()
281279
.report_mismatched_types(
282280
&ObligationCause::misc(instantiated_ty.span, body_id),
283-
self.tcx.mk_opaque(def_id.to_def_id(), id_substs),
281+
opaque_ty,
284282
definition_ty,
285283
err,
286284
)

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_infer::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelatingDelegate};
22
use rustc_infer::infer::NllRegionVariableOrigin;
3-
use rustc_infer::traits::ObligationCause;
3+
use rustc_infer::traits::PredicateObligations;
44
use rustc_middle::mir::ConstraintCategory;
55
use rustc_middle::ty::error::TypeError;
66
use rustc_middle::ty::relate::TypeRelation;
@@ -155,27 +155,16 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
155155
true
156156
}
157157

158-
fn register_opaque_type(
158+
fn register_opaque_type_obligations(
159159
&mut self,
160-
a: Ty<'tcx>,
161-
b: Ty<'tcx>,
162-
a_is_expected: bool,
160+
obligations: PredicateObligations<'tcx>,
163161
) -> Result<(), TypeError<'tcx>> {
164-
let param_env = self.param_env();
165-
let span = self.span();
166-
let def_id = self.type_checker.body.source.def_id().expect_local();
167-
let body_id = self.type_checker.tcx().hir().local_def_id_to_hir_id(def_id);
168-
let cause = ObligationCause::misc(span, body_id);
169162
self.type_checker
170163
.fully_perform_op(
171164
self.locations,
172165
self.category,
173166
InstantiateOpaqueType {
174-
obligations: self
175-
.type_checker
176-
.infcx
177-
.handle_opaque_type(a, b, a_is_expected, &cause, param_env)?
178-
.obligations,
167+
obligations,
179168
// These fields are filled in during execution of the operation
180169
base_universe: None,
181170
region_constraints: None,

compiler/rustc_infer/src/infer/canonical/query_response.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use crate::infer::nll_relate::{NormalizationStrategy, TypeRelating, TypeRelating
1616
use crate::infer::region_constraints::{Constraint, RegionConstraintData};
1717
use crate::infer::{InferCtxt, InferOk, InferResult, NllRegionVariableOrigin};
1818
use crate::traits::query::{Fallible, NoSolution};
19-
use crate::traits::TraitEngine;
2019
use crate::traits::{Obligation, ObligationCause, PredicateObligation};
20+
use crate::traits::{PredicateObligations, TraitEngine};
2121
use rustc_data_structures::captures::Captures;
2222
use rustc_index::vec::Idx;
2323
use rustc_index::vec::IndexVec;
@@ -509,7 +509,7 @@ impl<'tcx> InferCtxt<'tcx> {
509509
for &(a, b) in &query_response.value.opaque_types {
510510
let a = substitute_value(self.tcx, &result_subst, a);
511511
let b = substitute_value(self.tcx, &result_subst, b);
512-
obligations.extend(self.handle_opaque_type(a, b, true, cause, param_env)?.obligations);
512+
obligations.extend(self.at(cause, param_env).eq(a, b)?.obligations);
513513
}
514514

515515
Ok(InferOk { value: result_subst, obligations })
@@ -741,17 +741,11 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> {
741741
true
742742
}
743743

744-
fn register_opaque_type(
744+
fn register_opaque_type_obligations(
745745
&mut self,
746-
a: Ty<'tcx>,
747-
b: Ty<'tcx>,
748-
a_is_expected: bool,
746+
obligations: PredicateObligations<'tcx>,
749747
) -> Result<(), TypeError<'tcx>> {
750-
self.obligations.extend(
751-
self.infcx
752-
.handle_opaque_type(a, b, a_is_expected, &self.cause, self.param_env)?
753-
.obligations,
754-
);
748+
self.obligations.extend(obligations);
755749
Ok(())
756750
}
757751
}

compiler/rustc_infer/src/infer/nll_relate/mod.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ use crate::infer::combine::ConstEquateRelation;
2525
use crate::infer::InferCtxt;
2626
use crate::infer::{ConstVarValue, ConstVariableValue};
2727
use crate::infer::{TypeVariableOrigin, TypeVariableOriginKind};
28+
use crate::traits::PredicateObligation;
2829
use rustc_data_structures::fx::FxHashMap;
30+
use rustc_middle::traits::ObligationCause;
2931
use rustc_middle::ty::error::TypeError;
3032
use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation};
3133
use rustc_middle::ty::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitor};
@@ -91,11 +93,9 @@ pub trait TypeRelatingDelegate<'tcx> {
9193
);
9294

9395
fn const_equate(&mut self, a: ty::Const<'tcx>, b: ty::Const<'tcx>);
94-
fn register_opaque_type(
96+
fn register_opaque_type_obligations(
9597
&mut self,
96-
a: Ty<'tcx>,
97-
b: Ty<'tcx>,
98-
a_is_expected: bool,
98+
obligations: Vec<PredicateObligation<'tcx>>,
9999
) -> Result<(), TypeError<'tcx>>;
100100

101101
/// Creates a new universe index. Used when instantiating placeholders.
@@ -414,7 +414,12 @@ where
414414
(_, &ty::Opaque(..)) => (generalize(a, true)?, b),
415415
_ => unreachable!(),
416416
};
417-
self.delegate.register_opaque_type(a, b, true)?;
417+
let cause = ObligationCause::dummy_with_span(self.delegate.span());
418+
let obligations = self
419+
.infcx
420+
.handle_opaque_type(a, b, true, &cause, self.delegate.param_env())?
421+
.obligations;
422+
self.delegate.register_opaque_type_obligations(obligations)?;
418423
trace!(a = ?a.kind(), b = ?b.kind(), "opaque type instantiated");
419424
Ok(a)
420425
}

compiler/rustc_infer/src/infer/opaque_types.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl<'tcx> InferCtxt<'tcx> {
103103
return Ok(InferOk { value: (), obligations: vec![] });
104104
}
105105
let (a, b) = if a_is_expected { (a, b) } else { (b, a) };
106-
let process = |a: Ty<'tcx>, b: Ty<'tcx>| match *a.kind() {
106+
let process = |a: Ty<'tcx>, b: Ty<'tcx>, a_is_expected| match *a.kind() {
107107
ty::Opaque(def_id, substs) if def_id.is_local() => {
108108
let def_id = def_id.expect_local();
109109
let origin = match self.defining_use_anchor {
@@ -169,13 +169,14 @@ impl<'tcx> InferCtxt<'tcx> {
169169
param_env,
170170
b,
171171
origin,
172+
a_is_expected,
172173
))
173174
}
174175
_ => None,
175176
};
176-
if let Some(res) = process(a, b) {
177+
if let Some(res) = process(a, b, true) {
177178
res
178-
} else if let Some(res) = process(b, a) {
179+
} else if let Some(res) = process(b, a, false) {
179180
res
180181
} else {
181182
let (a, b) = self.resolve_vars_if_possible((a, b));
@@ -514,13 +515,14 @@ impl UseKind {
514515

515516
impl<'tcx> InferCtxt<'tcx> {
516517
#[instrument(skip(self), level = "debug")]
517-
pub fn register_hidden_type(
518+
fn register_hidden_type(
518519
&self,
519520
opaque_type_key: OpaqueTypeKey<'tcx>,
520521
cause: ObligationCause<'tcx>,
521522
param_env: ty::ParamEnv<'tcx>,
522523
hidden_ty: Ty<'tcx>,
523524
origin: hir::OpaqueTyOrigin,
525+
a_is_expected: bool,
524526
) -> InferResult<'tcx, ()> {
525527
let tcx = self.tcx;
526528
let OpaqueTypeKey { def_id, substs } = opaque_type_key;
@@ -539,7 +541,8 @@ impl<'tcx> InferCtxt<'tcx> {
539541
origin,
540542
);
541543
if let Some(prev) = prev {
542-
obligations = self.at(&cause, param_env).eq(prev, hidden_ty)?.obligations;
544+
obligations =
545+
self.at(&cause, param_env).eq_exp(a_is_expected, prev, hidden_ty)?.obligations;
543546
}
544547

545548
let item_bounds = tcx.bound_explicit_item_bounds(def_id.to_def_id());

compiler/rustc_infer/src/infer/sub.rs

+8-31
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@ use super::combine::{CombineFields, RelationDir};
22
use super::SubregionOrigin;
33

44
use crate::infer::combine::ConstEquateRelation;
5-
use crate::infer::{TypeVariableOrigin, TypeVariableOriginKind};
65
use crate::traits::Obligation;
7-
use rustc_middle::ty::error::{ExpectedFound, TypeError};
86
use rustc_middle::ty::relate::{Cause, Relate, RelateResult, TypeRelation};
97
use rustc_middle::ty::visit::TypeVisitable;
108
use rustc_middle::ty::TyVar;
@@ -130,39 +128,18 @@ impl<'tcx> TypeRelation<'tcx> for Sub<'_, '_, 'tcx> {
130128
(&ty::Opaque(did, ..), _) | (_, &ty::Opaque(did, ..))
131129
if self.fields.define_opaque_types && did.is_local() =>
132130
{
133-
let mut generalize = |ty, ty_is_expected| {
134-
let var = infcx.next_ty_var_id_in_universe(
135-
TypeVariableOrigin {
136-
kind: TypeVariableOriginKind::MiscVariable,
137-
span: self.fields.trace.cause.span,
138-
},
139-
ty::UniverseIndex::ROOT,
140-
);
141-
self.fields.instantiate(ty, RelationDir::SubtypeOf, var, ty_is_expected)?;
142-
Ok(infcx.tcx.mk_ty_var(var))
143-
};
144-
let (a, b) = if self.a_is_expected { (a, b) } else { (b, a) };
145-
let (ga, gb) = match (a.kind(), b.kind()) {
146-
(&ty::Opaque(..), _) => (a, generalize(b, true)?),
147-
(_, &ty::Opaque(..)) => (generalize(a, false)?, b),
148-
_ => unreachable!(),
149-
};
150131
self.fields.obligations.extend(
151132
infcx
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-
})?
133+
.handle_opaque_type(
134+
a,
135+
b,
136+
self.a_is_expected,
137+
&self.fields.trace.cause,
138+
self.param_env(),
139+
)?
163140
.obligations,
164141
);
165-
Ok(ga)
142+
Ok(a)
166143
}
167144
// Optimization of GeneratorWitness relation since we know that all
168145
// free regions are replaced with bound regions during construction.

0 commit comments

Comments
 (0)