Skip to content

Commit 17f29f4

Browse files
committed
Require all .at calls to provide a defining use anchor
1 parent e920c5d commit 17f29f4

File tree

28 files changed

+132
-88
lines changed

28 files changed

+132
-88
lines changed

compiler/rustc_hir_analysis/src/autoderef.rs

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

144144
let normalized_ty = self
145145
.infcx
146-
.at(&cause, self.param_env)
146+
.at(&cause, self.param_env, DefiningAnchor::Error)
147147
.normalize(tcx.mk_projection(tcx.lang_items().deref_target()?, trait_ref.substs));
148148
let mut fulfillcx = <dyn TraitEngine<'tcx>>::new_in_snapshot(tcx, self.defining_use_anchor);
149149
let normalized_ty =

compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use rustc_hir::def_id::{DefId, LocalDefId};
88
use rustc_hir::lang_items::LangItem;
99
use rustc_hir::ItemKind;
1010
use rustc_infer::infer::outlives::env::OutlivesEnvironment;
11-
use rustc_infer::infer::TyCtxtInferExt;
1211
use rustc_infer::infer::{self, RegionResolutionError};
12+
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
1313
use rustc_middle::ty::adjustment::CoerceUnsizedInfo;
1414
use rustc_middle::ty::{self, suggest_constraining_type_params, Ty, TyCtxt, TypeVisitableExt};
1515
use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt;
@@ -227,7 +227,8 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
227227
use rustc_type_ir::sty::TyKind::*;
228228
match (source.kind(), target.kind()) {
229229
(&Ref(r_a, _, mutbl_a), Ref(r_b, _, mutbl_b))
230-
if infcx.at(&cause, param_env).eq(r_a, *r_b).is_ok() && mutbl_a == *mutbl_b => {}
230+
if infcx.at(&cause, param_env, DefiningAnchor::Error).eq(r_a, *r_b).is_ok()
231+
&& mutbl_a == *mutbl_b => {}
231232
(&RawPtr(tm_a), &RawPtr(tm_b)) if tm_a.mutbl == tm_b.mutbl => (),
232233
(&Adt(def_a, substs_a), &Adt(def_b, substs_b))
233234
if def_a.is_struct() && def_b.is_struct() =>
@@ -270,7 +271,9 @@ fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDef
270271
}
271272
}
272273

273-
if let Ok(ok) = infcx.at(&cause, param_env).eq(ty_a, ty_b) {
274+
if let Ok(ok) =
275+
infcx.at(&cause, param_env, DefiningAnchor::Error).eq(ty_a, ty_b)
276+
{
274277
if ok.obligations.is_empty() {
275278
create_err(
276279
"the trait `DispatchFromDyn` may only be implemented \
@@ -497,7 +500,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
497500
// we may have to evaluate constraint
498501
// expressions in the course of execution.)
499502
// See e.g., #41936.
500-
if let Ok(ok) = infcx.at(&cause, param_env).eq(a, b) {
503+
if let Ok(ok) = infcx.at(&cause, param_env, DefiningAnchor::Error).eq(a, b) {
501504
if ok.obligations.is_empty() {
502505
return None;
503506
}

compiler/rustc_hir_analysis/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ fn require_same_types<'tcx>(
165165
) -> bool {
166166
let infcx = &tcx.infer_ctxt().build();
167167
let param_env = ty::ParamEnv::empty();
168-
let errors = match infcx.at(cause, param_env).eq(expected, actual) {
168+
let errors = match infcx.at(cause, param_env, DefiningAnchor::Error).eq(expected, actual) {
169169
Ok(InferOk { obligations, .. }) => traits::fully_solve_obligations(
170170
infcx,
171171
obligations,

compiler/rustc_hir_typeck/src/coercion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
142142
}
143143

144144
pub fn at(&self) -> At<'_, 'tcx> {
145-
self.infcx.at(&self.cause, self.param_env)
145+
self.fcx.at(&self.cause)
146146
}
147147

148148
fn unify(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> InferResult<'tcx, Ty<'tcx>> {

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
147147
}
148148

149149
pub fn at(&'a self, cause: &'a ObligationCause<'tcx>) -> At<'a, 'tcx> {
150-
self.infcx.at(cause, self.param_env)
150+
self.infcx.at(cause, self.param_env, self.defining_use_anchor())
151151
}
152152

153153
pub fn sess(&self) -> &Session {

compiler/rustc_infer/src/infer/at.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ impl<'tcx> InferCtxt<'tcx> {
5151
&'a self,
5252
cause: &'a ObligationCause<'tcx>,
5353
param_env: ty::ParamEnv<'tcx>,
54+
define_opaque_types: impl Into<DefiningAnchor>,
5455
) -> At<'a, 'tcx> {
55-
At { infcx: self, cause, param_env, define_opaque_types: DefiningAnchor::Error }
56+
At { infcx: self, cause, param_env, define_opaque_types: define_opaque_types.into() }
5657
}
5758

5859
/// Forks the inference context, creating a new inference context with the same inference

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -515,12 +515,8 @@ impl<'tcx> InferCtxt<'tcx> {
515515
let a = substitute_value(self.tcx, &result_subst, a);
516516
let b = substitute_value(self.tcx, &result_subst, b);
517517
debug!(?a, ?b, "constrain opaque type");
518-
obligations.extend(
519-
self.at(cause, param_env)
520-
.define_opaque_types(defining_use_anchor)
521-
.eq(a, b)?
522-
.obligations,
523-
);
518+
obligations
519+
.extend(self.at(cause, param_env, defining_use_anchor).eq(a, b)?.obligations);
524520
}
525521

526522
Ok(InferOk { value: result_subst, obligations })
@@ -613,20 +609,26 @@ impl<'tcx> InferCtxt<'tcx> {
613609

614610
match (value1.unpack(), value2.unpack()) {
615611
(GenericArgKind::Type(v1), GenericArgKind::Type(v2)) => {
616-
obligations
617-
.extend(self.at(cause, param_env).eq(v1, v2)?.into_obligations());
612+
obligations.extend(
613+
self.at(cause, param_env, DefiningAnchor::Error)
614+
.eq(v1, v2)?
615+
.into_obligations(),
616+
);
618617
}
619618
(GenericArgKind::Lifetime(re1), GenericArgKind::Lifetime(re2))
620619
if re1.is_erased() && re2.is_erased() =>
621620
{
622621
// no action needed
623622
}
624623
(GenericArgKind::Lifetime(v1), GenericArgKind::Lifetime(v2)) => {
625-
obligations
626-
.extend(self.at(cause, param_env).eq(v1, v2)?.into_obligations());
624+
obligations.extend(
625+
self.at(cause, param_env, DefiningAnchor::Error)
626+
.eq(v1, v2)?
627+
.into_obligations(),
628+
);
627629
}
628630
(GenericArgKind::Const(v1), GenericArgKind::Const(v2)) => {
629-
let ok = self.at(cause, param_env).eq(v1, v2)?;
631+
let ok = self.at(cause, param_env, DefiningAnchor::Error).eq(v1, v2)?;
630632
obligations.extend(ok.into_obligations());
631633
}
632634
_ => {

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -837,15 +837,15 @@ impl<'tcx> InferCtxt<'tcx> {
837837
T: at::ToTrace<'tcx>,
838838
{
839839
let origin = &ObligationCause::dummy();
840-
self.probe(|_| self.at(origin, param_env).sub(a, b).is_ok())
840+
self.probe(|_| self.at(origin, param_env, DefiningAnchor::Error).sub(a, b).is_ok())
841841
}
842842

843843
pub fn can_eq<T>(&self, param_env: ty::ParamEnv<'tcx>, a: T, b: T) -> bool
844844
where
845845
T: at::ToTrace<'tcx>,
846846
{
847847
let origin = &ObligationCause::dummy();
848-
self.probe(|_| self.at(origin, param_env).eq(a, b).is_ok())
848+
self.probe(|_| self.at(origin, param_env, DefiningAnchor::Error).eq(a, b).is_ok())
849849
}
850850

851851
#[instrument(skip(self), level = "debug")]
@@ -940,7 +940,8 @@ impl<'tcx> InferCtxt<'tcx> {
940940
let ty::SubtypePredicate { a_is_expected, a, b } =
941941
self.instantiate_binder_with_placeholders(predicate);
942942

943-
let ok = self.at(cause, param_env).sub_exp(a_is_expected, a, b)?;
943+
let ok =
944+
self.at(cause, param_env, DefiningAnchor::Error).sub_exp(a_is_expected, a, b)?;
944945

945946
Ok(ok.unit())
946947
}))

compiler/rustc_infer/src/infer/opaque_types.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,7 @@ impl<'tcx> InferCtxt<'tcx> {
564564
);
565565
if let Some(prev) = prev {
566566
obligations = self
567-
.at(&cause, param_env)
568-
.define_opaque_types(defining_use_anchor)
567+
.at(&cause, param_env, defining_use_anchor)
569568
.eq_exp(a_is_expected, prev, hidden_ty)?
570569
.obligations;
571570
}

compiler/rustc_trait_selection/src/solve/eval_ctxt.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'tcx> EvalCtxt<'_, 'tcx> {
130130
rhs: T,
131131
) -> Result<Vec<Goal<'tcx, ty::Predicate<'tcx>>>, NoSolution> {
132132
self.infcx
133-
.at(&ObligationCause::dummy(), param_env)
133+
.at(&ObligationCause::dummy(), param_env, DefiningAnchor::Error)
134134
.eq(lhs, rhs)
135135
.map(|InferOk { value: (), obligations }| {
136136
obligations.into_iter().map(|o| o.into()).collect()

0 commit comments

Comments
 (0)