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

+1-1
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

+7-4
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

+1-1
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

+1-1
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

+1-1
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

+2-1
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

+13-11
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

+4-3
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

+1-2
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

+1-1
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()

compiler/rustc_trait_selection/src/solve/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ impl<'a, 'tcx> EvalCtxt<'a, 'tcx> {
374374
} else {
375375
let InferOk { value: (), obligations } = self
376376
.infcx
377-
.at(&ObligationCause::dummy(), goal.param_env)
377+
.at(&ObligationCause::dummy(), goal.param_env, DefiningAnchor::Error)
378378
.sub(goal.predicate.a, goal.predicate.b)?;
379379
self.evaluate_all_and_make_canonical_response(
380380
obligations.into_iter().map(|pred| pred.into()).collect(),

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
814814

815815
match (evaluate(c1), evaluate(c2)) {
816816
(Ok(c1), Ok(c2)) => {
817-
match selcx.infcx.at(&obligation.cause, obligation.param_env).eq(c1, c2)
817+
match selcx.infcx.at(&obligation.cause, obligation.param_env, DefiningAnchor::Error).eq(c1, c2)
818818
{
819819
Ok(_) => (),
820820
Err(_) => return false,

compiler/rustc_trait_selection/src/traits/coherence.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ fn with_fresh_ty_vars<'cx, 'tcx>(
127127
predicates: tcx.predicates_of(impl_def_id).instantiate(tcx, impl_substs).predicates,
128128
};
129129

130-
let InferOk { value: mut header, obligations } =
131-
selcx.infcx.at(&ObligationCause::dummy(), param_env).normalize(header);
130+
let InferOk { value: mut header, obligations } = selcx
131+
.infcx
132+
.at(&ObligationCause::dummy(), param_env, DefiningAnchor::Error)
133+
.normalize(header);
132134

133135
header.predicates.extend(obligations.into_iter().map(|o| o.predicate));
134136
header
@@ -214,7 +216,7 @@ fn equate_impl_headers<'cx, 'tcx>(
214216
debug!("equate_impl_headers(impl1_header={:?}, impl2_header={:?}", impl1_header, impl2_header);
215217
selcx
216218
.infcx
217-
.at(&ObligationCause::dummy(), ty::ParamEnv::empty())
219+
.at(&ObligationCause::dummy(), ty::ParamEnv::empty(), DefiningAnchor::Error)
218220
.define_opaque_types(DefiningAnchor::Bubble)
219221
.eq_impl_headers(impl1_header, impl2_header)
220222
.map(|infer_ok| infer_ok.obligations)
@@ -323,7 +325,7 @@ fn equate<'tcx>(
323325
) -> bool {
324326
// do the impls unify? If not, not disjoint.
325327
let Ok(InferOk { obligations: more_obligations, .. }) =
326-
infcx.at(&ObligationCause::dummy(), impl_env).eq(subject1, subject2)
328+
infcx.at(&ObligationCause::dummy(), impl_env, DefiningAnchor::Error).eq(subject1, subject2)
327329
else {
328330
debug!("explicit_disjoint: {:?} does not unify with {:?}", subject1, subject2);
329331
return true;

compiler/rustc_trait_selection/src/traits/engine.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
132132
param_env: ty::ParamEnv<'tcx>,
133133
value: T,
134134
) -> T {
135-
let infer_ok = self.infcx.at(&cause, param_env).normalize(value);
135+
let infer_ok = self.infcx.at(&cause, param_env, DefiningAnchor::Error).normalize(value);
136136
self.register_infer_ok_obligations(infer_ok)
137137
}
138138

@@ -149,7 +149,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
149149
T: ToTrace<'tcx>,
150150
{
151151
self.infcx
152-
.at(cause, param_env)
152+
.at(cause, param_env, DefiningAnchor::Error)
153153
.define_opaque_types(self.defining_use_anchor())
154154
.eq_exp(a_is_expected, a, b)
155155
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
@@ -163,7 +163,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
163163
actual: T,
164164
) -> Result<(), TypeError<'tcx>> {
165165
self.infcx
166-
.at(cause, param_env)
166+
.at(cause, param_env, DefiningAnchor::Error)
167167
.define_opaque_types(self.defining_use_anchor())
168168
.eq(expected, actual)
169169
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
@@ -178,7 +178,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
178178
actual: T,
179179
) -> Result<(), TypeError<'tcx>> {
180180
self.infcx
181-
.at(cause, param_env)
181+
.at(cause, param_env, DefiningAnchor::Error)
182182
.define_opaque_types(self.defining_use_anchor())
183183
.sup(expected, actual)
184184
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))
@@ -193,7 +193,7 @@ impl<'a, 'tcx> ObligationCtxt<'a, 'tcx> {
193193
actual: T,
194194
) -> Result<(), TypeError<'tcx>> {
195195
self.infcx
196-
.at(cause, param_env)
196+
.at(cause, param_env, DefiningAnchor::Error)
197197
.define_opaque_types(self.defining_use_anchor())
198198
.sup(expected, actual)
199199
.map(|infer_ok| self.register_infer_ok_obligations(infer_ok))

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

+5-3
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
21422142
.map(|ImplCandidate { trait_ref, similarity }| {
21432143
// FIXME(compiler-errors): This should be using `NormalizeExt::normalize`
21442144
let normalized = self
2145-
.at(&ObligationCause::dummy(), ty::ParamEnv::empty())
2145+
.at(&ObligationCause::dummy(), ty::ParamEnv::empty(), DefiningAnchor::Error)
21462146
.query_normalize(trait_ref)
21472147
.map_or(trait_ref, |normalized| normalized.value);
21482148
(similarity, normalized)
@@ -2706,8 +2706,10 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
27062706
let cleaned_pred =
27072707
pred.fold_with(&mut ParamToVarFolder { infcx: self, var_map: Default::default() });
27082708

2709-
let InferOk { value: cleaned_pred, .. } =
2710-
self.infcx.at(&ObligationCause::dummy(), param_env).normalize(cleaned_pred);
2709+
let InferOk { value: cleaned_pred, .. } = self
2710+
.infcx
2711+
.at(&ObligationCause::dummy(), param_env, DefiningAnchor::Error)
2712+
.normalize(cleaned_pred);
27112713

27122714
let obligation =
27132715
Obligation::new(self.tcx, ObligationCause::dummy(), param_env, cleaned_pred);

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
12111211
// implied by wf, but also because that would possibly result in
12121212
// erroneous errors later on.
12131213
let InferOk { value: output, obligations: _ } =
1214-
self.at(&ObligationCause::dummy(), param_env).normalize(output);
1214+
self.at(&ObligationCause::dummy(), param_env, DefiningAnchor::Error).normalize(output);
12151215

12161216
if output.is_ty_var() { None } else { Some((def_id_or_name, output, inputs)) }
12171217
}
@@ -3388,8 +3388,9 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
33883388
[trait_pred.self_ty()],
33893389
)
33903390
});
3391-
let InferOk { value: projection_ty, .. } =
3392-
self.at(&obligation.cause, obligation.param_env).normalize(projection_ty);
3391+
let InferOk { value: projection_ty, .. } = self
3392+
.at(&obligation.cause, obligation.param_env, DefiningAnchor::Error)
3393+
.normalize(projection_ty);
33933394

33943395
debug!(
33953396
normalized_projection_type = ?self.resolve_vars_if_possible(projection_ty)

compiler/rustc_trait_selection/src/traits/fulfill.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,11 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
521521
&& tcx.def_kind(a.def.did) == DefKind::AssocConst =>
522522
{
523523
if let Ok(new_obligations) = infcx
524-
.at(&obligation.cause, obligation.param_env)
524+
.at(
525+
&obligation.cause,
526+
obligation.param_env,
527+
DefiningAnchor::Error,
528+
)
525529
.trace(c1, c2)
526530
.eq(a.substs, b.substs)
527531
{
@@ -532,8 +536,13 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
532536
}
533537
(_, Unevaluated(_)) | (Unevaluated(_), _) => (),
534538
(_, _) => {
535-
if let Ok(new_obligations) =
536-
infcx.at(&obligation.cause, obligation.param_env).eq(c1, c2)
539+
if let Ok(new_obligations) = infcx
540+
.at(
541+
&obligation.cause,
542+
obligation.param_env,
543+
DefiningAnchor::Error,
544+
)
545+
.eq(c1, c2)
537546
{
538547
return ProcessResult::Changed(mk_pending(
539548
new_obligations.into_obligations(),
@@ -576,7 +585,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
576585
match self
577586
.selcx
578587
.infcx
579-
.at(&obligation.cause, obligation.param_env)
588+
.at(&obligation.cause, obligation.param_env, DefiningAnchor::Error)
580589
.eq(c1, c2)
581590
{
582591
Ok(inf_ok) => {
@@ -621,7 +630,7 @@ impl<'a, 'tcx> ObligationProcessor for FulfillProcessor<'a, 'tcx> {
621630
match self
622631
.selcx
623632
.infcx
624-
.at(&obligation.cause, obligation.param_env)
633+
.at(&obligation.cause, obligation.param_env, DefiningAnchor::Error)
625634
.eq(ct.ty(), ty)
626635
{
627636
Ok(inf_ok) => ProcessResult::Changed(mk_pending(inf_ok.into_obligations())),

compiler/rustc_trait_selection/src/traits/project.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,8 @@ fn project_and_unify_type<'cx, 'tcx>(
289289
obligations.extend(new);
290290

291291
match infcx
292-
.at(&obligation.cause, obligation.param_env)
293-
// This is needed to support nested opaque types like `impl Fn() -> impl Trait`
294-
.define_opaque_types(selcx.defining_use_anchor())
292+
// This anchor is needed to support nested opaque types like `impl Fn() -> impl Trait`
293+
.at(&obligation.cause, obligation.param_env, selcx.defining_use_anchor())
295294
.eq(normalized, actual)
296295
{
297296
Ok(InferOk { obligations: inferred_obligations, value: () }) => {
@@ -2067,7 +2066,10 @@ fn confirm_param_env_candidate<'cx, 'tcx>(
20672066

20682067
debug!(?cache_projection, ?obligation_projection);
20692068

2070-
match infcx.at(cause, param_env).eq(cache_projection, obligation_projection) {
2069+
match infcx
2070+
.at(cause, param_env, DefiningAnchor::Error)
2071+
.eq(cache_projection, obligation_projection)
2072+
{
20712073
Ok(InferOk { value: _, obligations }) => {
20722074
nested_obligations.extend(obligations);
20732075
assoc_ty_own_obligations(selcx, obligation, &mut nested_obligations);

0 commit comments

Comments
 (0)