Skip to content

Commit 3811f6e

Browse files
committed
always define opaque types outside of trait solver
1 parent 63ef74b commit 3811f6e

File tree

18 files changed

+39
-35
lines changed

18 files changed

+39
-35
lines changed

compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
160160
use rustc_type_ir::sty::TyKind::*;
161161
match (source.kind(), target.kind()) {
162162
(&Ref(r_a, _, mutbl_a), Ref(r_b, _, mutbl_b))
163-
if infcx.at(&cause, param_env).eq(DefineOpaqueTypes::No, r_a, *r_b).is_ok()
163+
if infcx.at(&cause, param_env).eq(DefineOpaqueTypes::Yes, r_a, *r_b).is_ok()
164164
&& mutbl_a == *mutbl_b => {}
165165
(&RawPtr(tm_a), &RawPtr(tm_b)) if tm_a.mutbl == tm_b.mutbl => (),
166166
(&Adt(def_a, substs_a), &Adt(def_b, substs_b))
@@ -205,7 +205,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
205205
}
206206

207207
if let Ok(ok) =
208-
infcx.at(&cause, param_env).eq(DefineOpaqueTypes::No, ty_a, ty_b)
208+
infcx.at(&cause, param_env).eq(DefineOpaqueTypes::Yes, ty_a, ty_b)
209209
{
210210
if ok.obligations.is_empty() {
211211
create_err(
@@ -427,7 +427,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) -> Coe
427427
// we may have to evaluate constraint
428428
// expressions in the course of execution.)
429429
// See e.g., #41936.
430-
if let Ok(ok) = infcx.at(&cause, param_env).eq(DefineOpaqueTypes::No, a, b) {
430+
if let Ok(ok) = infcx.at(&cause, param_env).eq(DefineOpaqueTypes::Yes, a, b) {
431431
if ok.obligations.is_empty() {
432432
return None;
433433
}

compiler/rustc_hir_typeck/src/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
787787
if let Some(idx) = generics.host_effect_index {
788788
let param = callee_substs.const_at(idx);
789789
let cause = self.misc(span);
790-
match self.at(&cause, self.param_env).eq(infer::DefineOpaqueTypes::No, effect, param) {
790+
match self.at(&cause, self.param_env).eq(infer::DefineOpaqueTypes::Yes, effect, param) {
791791
Ok(infer::InferOk { obligations, value: () }) => {
792792
self.register_predicates(obligations);
793793
}

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,7 +1127,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11271127
// are the same function and their parameters have a LUB.
11281128
match self.commit_if_ok(|_| {
11291129
self.at(cause, self.param_env).lub(
1130-
DefineOpaqueTypes::No,
1130+
DefineOpaqueTypes::Yes,
11311131
prev_ty,
11321132
new_ty,
11331133
)
@@ -1181,7 +1181,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
11811181
let sig = self
11821182
.at(cause, self.param_env)
11831183
.trace(prev_ty, new_ty)
1184-
.lub(DefineOpaqueTypes::No, a_sig, b_sig)
1184+
.lub(DefineOpaqueTypes::Yes, a_sig, b_sig)
11851185
.map(|ok| self.register_infer_ok_obligations(ok))?;
11861186

11871187
// Reify both sides and return the reified fn pointer type.
@@ -1270,7 +1270,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12701270

12711271
return self
12721272
.commit_if_ok(|_| {
1273-
self.at(cause, self.param_env).lub(DefineOpaqueTypes::No, prev_ty, new_ty)
1273+
self.at(cause, self.param_env).lub(DefineOpaqueTypes::Yes, prev_ty, new_ty)
12741274
})
12751275
.map(|ok| self.register_infer_ok_obligations(ok));
12761276
}
@@ -1283,7 +1283,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12831283
Err(e)
12841284
} else {
12851285
self.commit_if_ok(|_| {
1286-
self.at(cause, self.param_env).lub(DefineOpaqueTypes::No, prev_ty, new_ty)
1286+
self.at(cause, self.param_env).lub(DefineOpaqueTypes::Yes, prev_ty, new_ty)
12871287
})
12881288
.map(|ok| self.register_infer_ok_obligations(ok))
12891289
}

compiler/rustc_hir_typeck/src/demand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
387387
// what our ideal rcvr ty would look like.
388388
let _ = self
389389
.at(&ObligationCause::dummy(), self.param_env)
390-
.eq(DefineOpaqueTypes::No, method.sig.inputs()[idx + 1], arg_ty)
390+
.eq(DefineOpaqueTypes::Yes, method.sig.inputs()[idx + 1], arg_ty)
391391
.ok()?;
392392
self.select_obligations_where_possible(|errs| {
393393
// Yeet the errors, we're already reporting errors.
@@ -449,7 +449,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
449449
.ok()
450450
.and_then(|method| {
451451
let _ = self.at(&ObligationCause::dummy(), self.param_env)
452-
.eq(DefineOpaqueTypes::No, ideal_rcvr_ty, expected_ty)
452+
.eq(DefineOpaqueTypes::Yes, ideal_rcvr_ty, expected_ty)
453453
.ok()?;
454454
Some(method)
455455
});

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17361736
let target_ty = self.field_ty(base_expr.span, f, substs);
17371737
let cause = self.misc(base_expr.span);
17381738
match self.at(&cause, self.param_env).sup(
1739-
DefineOpaqueTypes::No,
1739+
DefineOpaqueTypes::Yes,
17401740
target_ty,
17411741
fru_ty,
17421742
) {

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
564564
let span = self.tcx.hir().body(body_id).value.span;
565565
let ok = self
566566
.at(&self.misc(span), self.param_env)
567-
.eq(DefineOpaqueTypes::No, interior, witness)
567+
.eq(DefineOpaqueTypes::Yes, interior, witness)
568568
.expect("Failed to unify generator interior type");
569569
let mut obligations = ok.obligations;
570570

@@ -1400,7 +1400,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
14001400
let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).subst(tcx, substs));
14011401
let self_ty = self.normalize(span, self_ty);
14021402
match self.at(&self.misc(span), self.param_env).eq(
1403-
DefineOpaqueTypes::No,
1403+
DefineOpaqueTypes::Yes,
14041404
impl_ty,
14051405
self_ty,
14061406
) {

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
296296
// 3. Check if the formal type is a supertype of the checked one
297297
// and register any such obligations for future type checks
298298
let supertype_error = self.at(&self.misc(provided_arg.span), self.param_env).sup(
299-
DefineOpaqueTypes::No,
299+
DefineOpaqueTypes::Yes,
300300
formal_input_ty,
301301
coerced_ty,
302302
);
@@ -591,7 +591,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
591591
// Using probe here, since we don't want this subtyping to affect inference.
592592
let subtyping_error = self.probe(|_| {
593593
self.at(&self.misc(arg_span), self.param_env)
594-
.sup(DefineOpaqueTypes::No, formal_input_ty, coerced_ty)
594+
.sup(DefineOpaqueTypes::Yes, formal_input_ty, coerced_ty)
595595
.err()
596596
});
597597

compiler/rustc_hir_typeck/src/generator_interior/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ pub fn resolve_interior<'a, 'tcx>(
327327

328328
// Unify the type variable inside the generator with the new witness
329329
match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq(
330-
DefineOpaqueTypes::No,
330+
DefineOpaqueTypes::Yes,
331331
interior,
332332
witness,
333333
) {

compiler/rustc_hir_typeck/src/method/confirm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> {
502502
substs,
503503
})),
504504
);
505-
match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::No, method_self_ty, self_ty) {
505+
match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::Yes, method_self_ty, self_ty) {
506506
Ok(InferOk { obligations, value: () }) => {
507507
self.register_predicates(obligations);
508508
}

compiler/rustc_hir_typeck/src/method/probe.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
936936
if let Some(self_ty) = self_ty {
937937
if self
938938
.at(&ObligationCause::dummy(), self.param_env)
939-
.sup(DefineOpaqueTypes::No, fty.inputs()[0], self_ty)
939+
.sup(DefineOpaqueTypes::Yes, fty.inputs()[0], self_ty)
940940
.is_err()
941941
{
942942
return false;
@@ -1455,7 +1455,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14551455
}
14561456
TraitCandidate(trait_ref) => self.probe(|_| {
14571457
let _ = self.at(&ObligationCause::dummy(), self.param_env).sup(
1458-
DefineOpaqueTypes::No,
1458+
DefineOpaqueTypes::Yes,
14591459
candidate.xform_self_ty,
14601460
self_ty,
14611461
);
@@ -1486,7 +1486,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14861486
self.probe(|_| {
14871487
// First check that the self type can be related.
14881488
let sub_obligations = match self.at(&ObligationCause::dummy(), self.param_env).sup(
1489-
DefineOpaqueTypes::No,
1489+
DefineOpaqueTypes::Yes,
14901490
probe.xform_self_ty,
14911491
self_ty,
14921492
) {
@@ -1698,7 +1698,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
16981698
if let ProbeResult::Match = result
16991699
&& self
17001700
.at(&ObligationCause::dummy(), self.param_env)
1701-
.sup(DefineOpaqueTypes::No, return_ty, xform_ret_ty)
1701+
.sup(DefineOpaqueTypes::Yes, return_ty, xform_ret_ty)
17021702
.is_err()
17031703
{
17041704
result = ProbeResult::BadReturnType;

compiler/rustc_infer/src/infer/at.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ use super::*;
3030
use rustc_middle::ty::relate::{Relate, TypeRelation};
3131
use rustc_middle::ty::{Const, ImplSubject};
3232

33-
/// Whether we should define opaque types or just treat them opaquely.
33+
/// Whether we should define opaque types or just treat them opaquely with
34+
/// the old trait solver. This is completely ignored with the new solver.
3435
///
35-
/// Currently only used to prevent predicate matching from matching anything
36-
/// against opaque types.
36+
/// Currently only used to prevent the the trait solver from matching anything
37+
/// against opaque types. The issue there is that we may use the trait solver
38+
/// with `DefiningAnchor::Bubble` which would cause us to unify opaque types
39+
/// with any impl even outside of their defining scope, causing a lot of unexpected
40+
/// errors.
3741
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
3842
pub enum DefineOpaqueTypes {
3943
Yes,

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -847,15 +847,15 @@ impl<'tcx> InferCtxt<'tcx> {
847847
T: at::ToTrace<'tcx>,
848848
{
849849
let origin = &ObligationCause::dummy();
850-
self.probe(|_| self.at(origin, param_env).sub(DefineOpaqueTypes::No, a, b).is_ok())
850+
self.probe(|_| self.at(origin, param_env).sub(DefineOpaqueTypes::Yes, a, b).is_ok())
851851
}
852852

853853
pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
854854
where
855855
T: at::ToTrace<'tcx>,
856856
{
857857
let origin = &ObligationCause::dummy();
858-
self.probe(|_| self.at(origin, param_env).eq(DefineOpaqueTypes::No, a, b).is_ok())
858+
self.probe(|_| self.at(origin, param_env).eq(DefineOpaqueTypes::Yes, a, b).is_ok())
859859
}
860860

861861
#[instrument(skip(self), level = "debug")]
@@ -951,7 +951,7 @@ impl<'tcx> InferCtxt<'tcx> {
951951
self.instantiate_binder_with_placeholders(predicate);
952952

953953
let ok =
954-
self.at(cause, param_env).sub_exp(DefineOpaqueTypes::No, a_is_expected, a, b)?;
954+
self.at(cause, param_env).sub_exp(DefineOpaqueTypes::Yes, a_is_expected, a, b)?;
955955

956956
Ok(ok.unit())
957957
}))

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
696696
) -> Result<(), NoSolution> {
697697
self.infcx
698698
.at(&ObligationCause::dummy(), param_env)
699-
.eq(DefineOpaqueTypes::No, lhs, rhs)
699+
.eq(DefineOpaqueTypes::Yes, lhs, rhs)
700700
.map(|InferOk { value: (), obligations }| {
701701
self.add_goals(obligations.into_iter().map(|o| o.into()));
702702
})
@@ -715,7 +715,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
715715
) -> Result<(), NoSolution> {
716716
self.infcx
717717
.at(&ObligationCause::dummy(), param_env)
718-
.sub(DefineOpaqueTypes::No, sub, sup)
718+
.sub(DefineOpaqueTypes::Yes, sub, sup)
719719
.map(|InferOk { value: (), obligations }| {
720720
self.add_goals(obligations.into_iter().map(|o| o.into()));
721721
})
@@ -739,7 +739,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
739739
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
740740
self.infcx
741741
.at(&ObligationCause::dummy(), param_env)
742-
.eq(DefineOpaqueTypes::No, lhs, rhs)
742+
.eq(DefineOpaqueTypes::Yes, lhs, rhs)
743743
.map(|InferOk { value: (), obligations }| {
744744
obligations.into_iter().map(|o| o.into()).collect()
745745
})

compiler/rustc_trait_selection/src/traits/auto_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
813813

814814
match (evaluate(c1), evaluate(c2)) {
815815
(Ok(c1), Ok(c2)) => {
816-
match selcx.infcx.at(&obligation.cause, obligation.param_env).eq(DefineOpaqueTypes::No,c1, c2)
816+
match selcx.infcx.at(&obligation.cause, obligation.param_env).eq(DefineOpaqueTypes::Yes,c1, c2)
817817
{
818818
Ok(_) => (),
819819
Err(_) => return false,

compiler/rustc_trait_selection/src/traits/coherence.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ fn impl_intersection_has_negative_obligation(
376376
// do the impls unify? If not, then it's not currently possible to prove any
377377
// obligations about their intersection.
378378
let Ok(InferOk { obligations: equate_obligations, .. }) =
379-
infcx.at(&ObligationCause::dummy(), impl_env).eq(DefineOpaqueTypes::No,subject1, subject2)
379+
infcx.at(&ObligationCause::dummy(), impl_env).eq(DefineOpaqueTypes::Yes,subject1, subject2)
380380
else {
381381
debug!("explicit_disjoint: {:?} does not unify with {:?}", subject1, subject2);
382382
return false;

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3543,7 +3543,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
35433543
self.probe(|_| {
35443544
match self
35453545
.at(&ObligationCause::misc(expr.span, body_id), param_env)
3546-
.eq(DefineOpaqueTypes::No, expected, actual)
3546+
.eq(DefineOpaqueTypes::Yes, expected, actual)
35473547
{
35483548
Ok(_) => (), // We ignore nested obligations here for now.
35493549
Err(err) => type_diffs.push(err),

compiler/rustc_trait_selection/src/traits/specialize/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ fn fulfill_implication<'tcx>(
227227

228228
// do the impls unify? If not, no specialization.
229229
let Ok(InferOk { obligations: more_obligations, .. }) =
230-
infcx.at(&ObligationCause::dummy(), param_env).eq(DefineOpaqueTypes::No, source_trait, target_trait)
230+
infcx.at(&ObligationCause::dummy(), param_env).eq(DefineOpaqueTypes::Yes, source_trait, target_trait)
231231
else {
232232
debug!(
233233
"fulfill_implication: {:?} does not unify with {:?}",

src/librustdoc/clean/blanket_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
4747

4848
// Require the type the impl is implemented on to match
4949
// our type, and ignore the impl if there was a mismatch.
50-
let Ok(eq_result) = infcx.at(&traits::ObligationCause::dummy(), param_env).eq(DefineOpaqueTypes::No, impl_trait_ref.self_ty(), impl_ty) else {
50+
let Ok(eq_result) = infcx.at(&traits::ObligationCause::dummy(), param_env).eq(DefineOpaqueTypes::Yes, impl_trait_ref.self_ty(), impl_ty) else {
5151
continue
5252
};
5353
let InferOk { value: (), obligations } = eq_result;

0 commit comments

Comments
 (0)