diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin.rs b/compiler/rustc_hir_analysis/src/coherence/builtin.rs index 79cc43edff139..00842c494d510 100644 --- a/compiler/rustc_hir_analysis/src/coherence/builtin.rs +++ b/compiler/rustc_hir_analysis/src/coherence/builtin.rs @@ -160,7 +160,7 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef use rustc_type_ir::sty::TyKind::*; match (source.kind(), target.kind()) { (&Ref(r_a, _, mutbl_a), Ref(r_b, _, mutbl_b)) - if infcx.at(&cause, param_env).eq(DefineOpaqueTypes::No, r_a, *r_b).is_ok() + if infcx.at(&cause, param_env).eq(DefineOpaqueTypes::Yes, r_a, *r_b).is_ok() && mutbl_a == *mutbl_b => {} (&RawPtr(tm_a), &RawPtr(tm_b)) if tm_a.mutbl == tm_b.mutbl => (), (&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 } if let Ok(ok) = - infcx.at(&cause, param_env).eq(DefineOpaqueTypes::No, ty_a, ty_b) + infcx.at(&cause, param_env).eq(DefineOpaqueTypes::Yes, ty_a, ty_b) { if ok.obligations.is_empty() { create_err( @@ -427,7 +427,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: LocalDefId) -> Coe // we may have to evaluate constraint // expressions in the course of execution.) // See e.g., #41936. - if let Ok(ok) = infcx.at(&cause, param_env).eq(DefineOpaqueTypes::No, a, b) { + if let Ok(ok) = infcx.at(&cause, param_env).eq(DefineOpaqueTypes::Yes, a, b) { if ok.obligations.is_empty() { return None; } diff --git a/compiler/rustc_hir_typeck/src/callee.rs b/compiler/rustc_hir_typeck/src/callee.rs index f306653c1ab25..60e930b5f263d 100644 --- a/compiler/rustc_hir_typeck/src/callee.rs +++ b/compiler/rustc_hir_typeck/src/callee.rs @@ -787,7 +787,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if let Some(idx) = generics.host_effect_index { let param = callee_substs.const_at(idx); let cause = self.misc(span); - match self.at(&cause, self.param_env).eq(infer::DefineOpaqueTypes::No, effect, param) { + match self.at(&cause, self.param_env).eq(infer::DefineOpaqueTypes::Yes, effect, param) { Ok(infer::InferOk { obligations, value: () }) => { self.register_predicates(obligations); } diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs index dc58d99ed9d74..4a7947190476f 100644 --- a/compiler/rustc_hir_typeck/src/coercion.rs +++ b/compiler/rustc_hir_typeck/src/coercion.rs @@ -1127,7 +1127,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // are the same function and their parameters have a LUB. match self.commit_if_ok(|_| { self.at(cause, self.param_env).lub( - DefineOpaqueTypes::No, + DefineOpaqueTypes::Yes, prev_ty, new_ty, ) @@ -1181,7 +1181,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let sig = self .at(cause, self.param_env) .trace(prev_ty, new_ty) - .lub(DefineOpaqueTypes::No, a_sig, b_sig) + .lub(DefineOpaqueTypes::Yes, a_sig, b_sig) .map(|ok| self.register_infer_ok_obligations(ok))?; // Reify both sides and return the reified fn pointer type. @@ -1270,7 +1270,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { return self .commit_if_ok(|_| { - self.at(cause, self.param_env).lub(DefineOpaqueTypes::No, prev_ty, new_ty) + self.at(cause, self.param_env).lub(DefineOpaqueTypes::Yes, prev_ty, new_ty) }) .map(|ok| self.register_infer_ok_obligations(ok)); } @@ -1283,7 +1283,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { Err(e) } else { self.commit_if_ok(|_| { - self.at(cause, self.param_env).lub(DefineOpaqueTypes::No, prev_ty, new_ty) + self.at(cause, self.param_env).lub(DefineOpaqueTypes::Yes, prev_ty, new_ty) }) .map(|ok| self.register_infer_ok_obligations(ok)) } diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index cc8198aab2529..38c1520b745fb 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -387,7 +387,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // what our ideal rcvr ty would look like. let _ = self .at(&ObligationCause::dummy(), self.param_env) - .eq(DefineOpaqueTypes::No, method.sig.inputs()[idx + 1], arg_ty) + .eq(DefineOpaqueTypes::Yes, method.sig.inputs()[idx + 1], arg_ty) .ok()?; self.select_obligations_where_possible(|errs| { // Yeet the errors, we're already reporting errors. @@ -449,7 +449,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .ok() .and_then(|method| { let _ = self.at(&ObligationCause::dummy(), self.param_env) - .eq(DefineOpaqueTypes::No, ideal_rcvr_ty, expected_ty) + .eq(DefineOpaqueTypes::Yes, ideal_rcvr_ty, expected_ty) .ok()?; Some(method) }); diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index 72b29f7b6e906..9f9167e731d67 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -1736,7 +1736,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let target_ty = self.field_ty(base_expr.span, f, substs); let cause = self.misc(base_expr.span); match self.at(&cause, self.param_env).sup( - DefineOpaqueTypes::No, + DefineOpaqueTypes::Yes, target_ty, fru_ty, ) { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs index 9a80a9c9303e3..402c4dc980607 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs @@ -564,7 +564,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let span = self.tcx.hir().body(body_id).value.span; let ok = self .at(&self.misc(span), self.param_env) - .eq(DefineOpaqueTypes::No, interior, witness) + .eq(DefineOpaqueTypes::Yes, interior, witness) .expect("Failed to unify generator interior type"); let mut obligations = ok.obligations; @@ -1400,7 +1400,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let impl_ty = self.normalize(span, tcx.type_of(impl_def_id).subst(tcx, substs)); let self_ty = self.normalize(span, self_ty); match self.at(&self.misc(span), self.param_env).eq( - DefineOpaqueTypes::No, + DefineOpaqueTypes::Yes, impl_ty, self_ty, ) { diff --git a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs index 41f5fafe72f27..23ec51dcb0b67 100644 --- a/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs +++ b/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs @@ -296,7 +296,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // 3. Check if the formal type is a supertype of the checked one // and register any such obligations for future type checks let supertype_error = self.at(&self.misc(provided_arg.span), self.param_env).sup( - DefineOpaqueTypes::No, + DefineOpaqueTypes::Yes, formal_input_ty, coerced_ty, ); @@ -591,7 +591,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // Using probe here, since we don't want this subtyping to affect inference. let subtyping_error = self.probe(|_| { self.at(&self.misc(arg_span), self.param_env) - .sup(DefineOpaqueTypes::No, formal_input_ty, coerced_ty) + .sup(DefineOpaqueTypes::Yes, formal_input_ty, coerced_ty) .err() }); diff --git a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs index 86ea092bc4073..30147d7bdaea2 100644 --- a/compiler/rustc_hir_typeck/src/generator_interior/mod.rs +++ b/compiler/rustc_hir_typeck/src/generator_interior/mod.rs @@ -327,7 +327,7 @@ pub fn resolve_interior<'a, 'tcx>( // Unify the type variable inside the generator with the new witness match fcx.at(&fcx.misc(body.value.span), fcx.param_env).eq( - DefineOpaqueTypes::No, + DefineOpaqueTypes::Yes, interior, witness, ) { diff --git a/compiler/rustc_hir_typeck/src/method/confirm.rs b/compiler/rustc_hir_typeck/src/method/confirm.rs index 87edb80314805..7ef38674e602e 100644 --- a/compiler/rustc_hir_typeck/src/method/confirm.rs +++ b/compiler/rustc_hir_typeck/src/method/confirm.rs @@ -502,7 +502,7 @@ impl<'a, 'tcx> ConfirmContext<'a, 'tcx> { substs, })), ); - match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::No, method_self_ty, self_ty) { + match self.at(&cause, self.param_env).sup(DefineOpaqueTypes::Yes, method_self_ty, self_ty) { Ok(InferOk { obligations, value: () }) => { self.register_predicates(obligations); } diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 03a3eebbdf599..f12b28cf19df7 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -936,7 +936,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { if let Some(self_ty) = self_ty { if self .at(&ObligationCause::dummy(), self.param_env) - .sup(DefineOpaqueTypes::No, fty.inputs()[0], self_ty) + .sup(DefineOpaqueTypes::Yes, fty.inputs()[0], self_ty) .is_err() { return false; @@ -1455,7 +1455,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { } TraitCandidate(trait_ref) => self.probe(|_| { let _ = self.at(&ObligationCause::dummy(), self.param_env).sup( - DefineOpaqueTypes::No, + DefineOpaqueTypes::Yes, candidate.xform_self_ty, self_ty, ); @@ -1486,7 +1486,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { self.probe(|_| { // First check that the self type can be related. let sub_obligations = match self.at(&ObligationCause::dummy(), self.param_env).sup( - DefineOpaqueTypes::No, + DefineOpaqueTypes::Yes, probe.xform_self_ty, self_ty, ) { @@ -1698,7 +1698,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> { if let ProbeResult::Match = result && self .at(&ObligationCause::dummy(), self.param_env) - .sup(DefineOpaqueTypes::No, return_ty, xform_ret_ty) + .sup(DefineOpaqueTypes::Yes, return_ty, xform_ret_ty) .is_err() { result = ProbeResult::BadReturnType; diff --git a/compiler/rustc_infer/src/infer/at.rs b/compiler/rustc_infer/src/infer/at.rs index 433735e827bdd..7f0afbd4e6a41 100644 --- a/compiler/rustc_infer/src/infer/at.rs +++ b/compiler/rustc_infer/src/infer/at.rs @@ -30,10 +30,14 @@ use super::*; use rustc_middle::ty::relate::{Relate, TypeRelation}; use rustc_middle::ty::{Const, ImplSubject}; -/// Whether we should define opaque types or just treat them opaquely. +/// Whether we should define opaque types or just treat them opaquely with +/// the old trait solver. This is completely ignored with the new solver. /// -/// Currently only used to prevent predicate matching from matching anything -/// against opaque types. +/// Currently only used to prevent the the trait solver from matching anything +/// against opaque types. The issue there is that we may use the trait solver +/// with `DefiningAnchor::Bubble` which would cause us to unify opaque types +/// with any impl even outside of their defining scope, causing a lot of unexpected +/// errors. #[derive(Debug, PartialEq, Eq, Clone, Copy)] pub enum DefineOpaqueTypes { Yes, diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index fca32b73d1db6..67f6241d23728 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -847,7 +847,7 @@ impl<'tcx> InferCtxt<'tcx> { T: at::ToTrace<'tcx>, { let origin = &ObligationCause::dummy(); - self.probe(|_| self.at(origin, param_env).sub(DefineOpaqueTypes::No, a, b).is_ok()) + self.probe(|_| self.at(origin, param_env).sub(DefineOpaqueTypes::Yes, a, b).is_ok()) } pub fn can_eq(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool @@ -855,7 +855,7 @@ impl<'tcx> InferCtxt<'tcx> { T: at::ToTrace<'tcx>, { let origin = &ObligationCause::dummy(); - self.probe(|_| self.at(origin, param_env).eq(DefineOpaqueTypes::No, a, b).is_ok()) + self.probe(|_| self.at(origin, param_env).eq(DefineOpaqueTypes::Yes, a, b).is_ok()) } #[instrument(skip(self), level = "debug")] @@ -951,7 +951,7 @@ impl<'tcx> InferCtxt<'tcx> { self.instantiate_binder_with_placeholders(predicate); let ok = - self.at(cause, param_env).sub_exp(DefineOpaqueTypes::No, a_is_expected, a, b)?; + self.at(cause, param_env).sub_exp(DefineOpaqueTypes::Yes, a_is_expected, a, b)?; Ok(ok.unit()) })) diff --git a/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs b/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs index 74dfbdddbab95..488433be25412 100644 --- a/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs +++ b/compiler/rustc_trait_selection/src/solve/eval_ctxt.rs @@ -696,7 +696,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { ) -> Result<(), NoSolution> { self.infcx .at(&ObligationCause::dummy(), param_env) - .eq(DefineOpaqueTypes::No, lhs, rhs) + .eq(DefineOpaqueTypes::Yes, lhs, rhs) .map(|InferOk { value: (), obligations }| { self.add_goals(obligations.into_iter().map(|o| o.into())); }) @@ -715,7 +715,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { ) -> Result<(), NoSolution> { self.infcx .at(&ObligationCause::dummy(), param_env) - .sub(DefineOpaqueTypes::No, sub, sup) + .sub(DefineOpaqueTypes::Yes, sub, sup) .map(|InferOk { value: (), obligations }| { self.add_goals(obligations.into_iter().map(|o| o.into())); }) @@ -739,7 +739,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> { ) -> Result>>, NoSolution> { self.infcx .at(&ObligationCause::dummy(), param_env) - .eq(DefineOpaqueTypes::No, lhs, rhs) + .eq(DefineOpaqueTypes::Yes, lhs, rhs) .map(|InferOk { value: (), obligations }| { obligations.into_iter().map(|o| o.into()).collect() }) diff --git a/compiler/rustc_trait_selection/src/traits/auto_trait.rs b/compiler/rustc_trait_selection/src/traits/auto_trait.rs index cb38d0ac8475f..f0cd5902086ae 100644 --- a/compiler/rustc_trait_selection/src/traits/auto_trait.rs +++ b/compiler/rustc_trait_selection/src/traits/auto_trait.rs @@ -813,7 +813,7 @@ impl<'tcx> AutoTraitFinder<'tcx> { match (evaluate(c1), evaluate(c2)) { (Ok(c1), Ok(c2)) => { - match selcx.infcx.at(&obligation.cause, obligation.param_env).eq(DefineOpaqueTypes::No,c1, c2) + match selcx.infcx.at(&obligation.cause, obligation.param_env).eq(DefineOpaqueTypes::Yes,c1, c2) { Ok(_) => (), Err(_) => return false, diff --git a/compiler/rustc_trait_selection/src/traits/coherence.rs b/compiler/rustc_trait_selection/src/traits/coherence.rs index 1b1285e1b461a..0cfe85f7e455a 100644 --- a/compiler/rustc_trait_selection/src/traits/coherence.rs +++ b/compiler/rustc_trait_selection/src/traits/coherence.rs @@ -376,7 +376,7 @@ fn impl_intersection_has_negative_obligation( // do the impls unify? If not, then it's not currently possible to prove any // obligations about their intersection. let Ok(InferOk { obligations: equate_obligations, .. }) = - infcx.at(&ObligationCause::dummy(), impl_env).eq(DefineOpaqueTypes::No,subject1, subject2) + infcx.at(&ObligationCause::dummy(), impl_env).eq(DefineOpaqueTypes::Yes,subject1, subject2) else { debug!("explicit_disjoint: {:?} does not unify with {:?}", subject1, subject2); return false; diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index 9ac1ba0275c19..4b7c85091d779 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -3543,7 +3543,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> { self.probe(|_| { match self .at(&ObligationCause::misc(expr.span, body_id), param_env) - .eq(DefineOpaqueTypes::No, expected, actual) + .eq(DefineOpaqueTypes::Yes, expected, actual) { Ok(_) => (), // We ignore nested obligations here for now. Err(err) => type_diffs.push(err), diff --git a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs index 96f1287416fec..dd55f3c8eff3e 100644 --- a/compiler/rustc_trait_selection/src/traits/specialize/mod.rs +++ b/compiler/rustc_trait_selection/src/traits/specialize/mod.rs @@ -227,7 +227,7 @@ fn fulfill_implication<'tcx>( // do the impls unify? If not, no specialization. let Ok(InferOk { obligations: more_obligations, .. }) = - infcx.at(&ObligationCause::dummy(), param_env).eq(DefineOpaqueTypes::No, source_trait, target_trait) + infcx.at(&ObligationCause::dummy(), param_env).eq(DefineOpaqueTypes::Yes, source_trait, target_trait) else { debug!( "fulfill_implication: {:?} does not unify with {:?}", diff --git a/src/librustdoc/clean/blanket_impl.rs b/src/librustdoc/clean/blanket_impl.rs index a36041588510f..f283e28e938bc 100644 --- a/src/librustdoc/clean/blanket_impl.rs +++ b/src/librustdoc/clean/blanket_impl.rs @@ -47,7 +47,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> { // Require the type the impl is implemented on to match // our type, and ignore the impl if there was a mismatch. - let Ok(eq_result) = infcx.at(&traits::ObligationCause::dummy(), param_env).eq(DefineOpaqueTypes::No, impl_trait_ref.self_ty(), impl_ty) else { + let Ok(eq_result) = infcx.at(&traits::ObligationCause::dummy(), param_env).eq(DefineOpaqueTypes::Yes, impl_trait_ref.self_ty(), impl_ty) else { continue }; let InferOk { value: (), obligations } = eq_result;