Skip to content

Commit 01aacd4

Browse files
committed
Require SelectionContext to have a DefiningAnchor from the start
1 parent a5f9af2 commit 01aacd4

File tree

17 files changed

+31
-34
lines changed

17 files changed

+31
-34
lines changed

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
744744

745745
let implsrc = {
746746
let infcx = tcx.infer_ctxt().build();
747-
let mut selcx = SelectionContext::new(&infcx);
747+
let mut selcx = SelectionContext::new(&infcx, DefiningAnchor::Error);
748748
selcx.select(&obligation)
749749
};
750750

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_errors::{
77
};
88
use rustc_hir as hir;
99
use rustc_hir::def_id::DefId;
10-
use rustc_infer::infer::TyCtxtInferExt;
10+
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
1111
use rustc_infer::traits::{ImplSource, Obligation, ObligationCause};
1212
use rustc_middle::mir;
1313
use rustc_middle::ty::print::with_no_trimmed_paths;
@@ -149,7 +149,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
149149
);
150150

151151
let infcx = tcx.infer_ctxt().build();
152-
let mut selcx = SelectionContext::new(&infcx);
152+
let mut selcx = SelectionContext::new(&infcx, DefiningAnchor::Error);
153153
let implsrc = selcx.select(&obligation);
154154

155155
if let Ok(Some(ImplSource::UserDefined(data))) = implsrc {

compiler/rustc_const_eval/src/transform/check_consts/qualifs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
use rustc_errors::ErrorGuaranteed;
66
use rustc_hir::LangItem;
7-
use rustc_infer::infer::TyCtxtInferExt;
7+
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
88
use rustc_middle::mir;
99
use rustc_middle::mir::*;
1010
use rustc_middle::ty::{self, subst::SubstsRef, AdtDef, Ty};
@@ -162,7 +162,7 @@ impl Qualif for NeedsNonConstDrop {
162162
);
163163

164164
let infcx = cx.tcx.infer_ctxt().build();
165-
let mut selcx = SelectionContext::new(&infcx);
165+
let mut selcx = SelectionContext::new(&infcx, DefiningAnchor::Error);
166166
let Some(impl_src) = selcx.select(&obligation).ok().flatten() else {
167167
// If we couldn't select a const destruct candidate, then it's bad
168168
return true;

compiler/rustc_hir_typeck/src/coercion.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -585,8 +585,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
585585
}
586586
})?;
587587

588-
let mut selcx = traits::SelectionContext::new(self)
589-
.with_defining_use_anchor(self.defining_use_anchor());
588+
let mut selcx = traits::SelectionContext::new(self, self.defining_use_anchor());
590589

591590
// Create an obligation for `Source: CoerceUnsized<Target>`.
592591
let cause = ObligationCause::new(

compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1903,7 +1903,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19031903
self.param_env,
19041904
ty::Binder::dummy(trait_ref),
19051905
);
1906-
match SelectionContext::new(&self).with_defining_use_anchor(self.defining_use_anchor()).select(&obligation) {
1906+
match SelectionContext::new(&self,self.defining_use_anchor()).select(&obligation) {
19071907
Ok(Some(traits::ImplSource::UserDefined(impl_source))) => {
19081908
Some(impl_source.impl_def_id)
19091909
}

compiler/rustc_hir_typeck/src/method/probe.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1428,9 +1428,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
14281428
let cause = traits::ObligationCause::misc(self.span, self.body_id);
14291429
let predicate = ty::Binder::dummy(trait_ref);
14301430
let obligation = traits::Obligation::new(self.tcx, cause, self.param_env, predicate);
1431-
traits::SelectionContext::new(self)
1432-
.with_defining_use_anchor(self.defining_use_anchor())
1433-
.select(&obligation)
1431+
traits::SelectionContext::new(self, self.defining_use_anchor()).select(&obligation)
14341432
}
14351433

14361434
fn candidate_source(&self, candidate: &Candidate<'tcx>, self_ty: Ty<'tcx>) -> CandidateSource {

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
8989
let trait_ref = tcx.mk_trait_ref(trait_did, [ty]);
9090

9191
let infcx = tcx.infer_ctxt().build();
92-
let mut selcx = SelectionContext::new(&infcx);
92+
let mut selcx = SelectionContext::new(&infcx, DefiningAnchor::Error);
9393
for polarity in [true, false] {
9494
let result = selcx.select(&Obligation::new(
9595
tcx,
@@ -256,7 +256,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
256256
fresh_preds.insert(self.clean_pred(infcx, predicate));
257257
}
258258

259-
let mut select = SelectionContext::new(&infcx);
259+
let mut select = SelectionContext::new(&infcx, DefiningAnchor::Error);
260260

261261
let mut already_visited = FxHashSet::default();
262262
let mut predicates = VecDeque::new();

compiler/rustc_trait_selection/src/traits/coherence.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub fn overlapping_impls(
9696
}
9797

9898
let infcx = tcx.infer_ctxt().intercrate().build();
99-
let selcx = &mut SelectionContext::new(&infcx);
99+
let selcx = &mut SelectionContext::new(&infcx, DefiningAnchor::Error);
100100
let overlaps =
101101
overlap(selcx, skip_leak_check, impl1_def_id, impl2_def_id, overlap_mode).is_some();
102102
if !overlaps {
@@ -107,7 +107,7 @@ pub fn overlapping_impls(
107107
// this time tracking intercrate ambiguity causes for better
108108
// diagnostics. (These take time and can lead to false errors.)
109109
let infcx = tcx.infer_ctxt().intercrate().build();
110-
let selcx = &mut SelectionContext::new(&infcx);
110+
let selcx = &mut SelectionContext::new(&infcx, DefiningAnchor::Error);
111111
selcx.enable_tracking_intercrate_ambiguity_causes();
112112
Some(overlap(selcx, skip_leak_check, impl1_def_id, impl2_def_id, overlap_mode).unwrap())
113113
}
@@ -305,7 +305,7 @@ fn negative_impl(tcx: TyCtxt<'_>, impl1_def_id: DefId, impl2_def_id: DefId) -> b
305305
};
306306

307307
// Attempt to prove that impl2 applies, given all of the above.
308-
let selcx = &mut SelectionContext::new(&infcx);
308+
let selcx = &mut SelectionContext::new(&infcx, DefiningAnchor::Error);
309309
let impl2_substs = infcx.fresh_substs_for_item(DUMMY_SP, impl2_def_id);
310310
let (subject2, obligations) =
311311
impl_subject_and_oblig(selcx, impl_env, impl2_def_id, impl2_substs);

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -2316,8 +2316,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
23162316
};
23172317

23182318
let obligation = obligation.with(self.tcx, trait_ref);
2319-
let mut selcx =
2320-
SelectionContext::new(&self).with_defining_use_anchor(DefiningAnchor::Bubble);
2319+
let mut selcx = SelectionContext::new(&self, DefiningAnchor::Bubble);
23212320
match selcx.select_from_obligation(&obligation) {
23222321
Ok(None) => {
23232322
let ambiguities =

compiler/rustc_trait_selection/src/traits/fulfill.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentContext<'tcx> {
149149
}
150150

151151
fn select_where_possible(&mut self, infcx: &InferCtxt<'tcx>) -> Vec<FulfillmentError<'tcx>> {
152-
let selcx = SelectionContext::new(infcx).with_defining_use_anchor(self.defining_use_anchor);
152+
let selcx = SelectionContext::new(infcx, self.defining_use_anchor);
153153
self.select(selcx)
154154
}
155155

compiler/rustc_trait_selection/src/traits/project.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use rustc_hir::def::DefKind;
2828
use rustc_hir::lang_items::LangItem;
2929
use rustc_infer::infer::at::At;
3030
use rustc_infer::infer::resolve::OpportunisticRegionResolver;
31+
use rustc_infer::infer::DefiningAnchor;
3132
use rustc_infer::traits::ImplSourceBuiltinData;
3233
use rustc_middle::traits::select::OverflowError;
3334
use rustc_middle::ty::fold::{TypeFoldable, TypeFolder, TypeSuperFoldable};
@@ -58,7 +59,7 @@ pub trait NormalizeExt<'tcx> {
5859

5960
impl<'tcx> NormalizeExt<'tcx> for At<'_, 'tcx> {
6061
fn normalize<T: TypeFoldable<TyCtxt<'tcx>>>(&self, value: T) -> InferOk<'tcx, T> {
61-
let mut selcx = SelectionContext::new(self.infcx);
62+
let mut selcx = SelectionContext::new(self.infcx, DefiningAnchor::Error);
6263
let Normalized { value, obligations } =
6364
normalize_with_depth(&mut selcx, self.param_env, self.cause.clone(), 0, value);
6465
InferOk { value, obligations }

compiler/rustc_trait_selection/src/traits/query/evaluate_obligation.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_infer::infer::DefiningAnchor;
12
use rustc_middle::ty;
23
use rustc_session::config::TraitSolver;
34

@@ -127,7 +128,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
127128
match self.evaluate_obligation(obligation) {
128129
Ok(result) => result,
129130
Err(OverflowError::Canonical) => {
130-
let mut selcx = SelectionContext::new(&self);
131+
let mut selcx = SelectionContext::new(&self, DefiningAnchor::Error);
131132
selcx.evaluate_root_obligation(obligation).unwrap_or_else(|r| match r {
132133
OverflowError::Canonical => {
133134
span_bug!(

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

+6-8
Original file line numberDiff line numberDiff line change
@@ -211,27 +211,25 @@ enum BuiltinImplConditions<'tcx> {
211211
}
212212

213213
impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
214-
pub fn new(infcx: &'cx InferCtxt<'tcx>) -> SelectionContext<'cx, 'tcx> {
214+
pub fn new(
215+
infcx: &'cx InferCtxt<'tcx>,
216+
defining_use_anchor: impl Into<DefiningAnchor>,
217+
) -> SelectionContext<'cx, 'tcx> {
215218
SelectionContext {
216219
infcx,
217220
freshener: infcx.freshener_keep_static(),
218221
intercrate_ambiguity_causes: None,
219222
query_mode: TraitQueryMode::Standard,
220-
defining_use_anchor: DefiningAnchor::Error,
223+
defining_use_anchor: defining_use_anchor.into(),
221224
}
222225
}
223226

224-
pub fn with_defining_use_anchor(self, defining_use_anchor: DefiningAnchor) -> Self {
225-
Self { defining_use_anchor, ..self }
226-
}
227-
228227
pub fn new_in_canonical_query(infcx: &'cx InferCtxt<'tcx>) -> SelectionContext<'cx, 'tcx> {
229228
SelectionContext {
230229
query_mode: TraitQueryMode::Canonical,
231230
// This bubble is required for this tests to pass:
232231
// impl-trait/issue99642.rs
233-
defining_use_anchor: DefiningAnchor::Bubble,
234-
..SelectionContext::new(infcx)
232+
..SelectionContext::new(infcx, DefiningAnchor::Bubble)
235233
}
236234
}
237235

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn fulfill_implication<'tcx>(
187187

188188
let source_trait = ImplSubject::Trait(source_trait_ref);
189189

190-
let selcx = &mut SelectionContext::new(&infcx);
190+
let selcx = &mut SelectionContext::new(&infcx, DefiningAnchor::Error);
191191
let target_substs = infcx.fresh_substs_for_item(DUMMY_SP, target_impl);
192192
let (target_trait, obligations) =
193193
util::impl_subject_and_oblig(selcx, param_env, target_impl, target_substs);

compiler/rustc_trait_selection/src/traits/wf.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::infer::InferCtxt;
22
use crate::traits;
33
use rustc_hir as hir;
44
use rustc_hir::lang_items::LangItem;
5+
use rustc_infer::infer::DefiningAnchor;
56
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, SubstsRef};
67
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
78
use rustc_span::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
@@ -314,7 +315,7 @@ impl<'tcx> WfPredicates<'tcx> {
314315
let mut obligations = Vec::with_capacity(self.out.len());
315316
for mut obligation in self.out {
316317
assert!(!obligation.has_escaping_bound_vars());
317-
let mut selcx = traits::SelectionContext::new(infcx);
318+
let mut selcx = traits::SelectionContext::new(infcx, DefiningAnchor::Error);
318319
// Don't normalize the whole obligation, the param env is either
319320
// already normalized, or we're currently normalizing the
320321
// param_env. Either way we should only normalize the predicate.

compiler/rustc_traits/src/codegen.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn codegen_select_candidate<'tcx>(
3232
let infcx = tcx.infer_ctxt().ignoring_regions().build();
3333
// HACK `Bubble` is required for
3434
// this test to pass: type-alias-impl-trait/assoc-projection-ice.rs
35-
let mut selcx = SelectionContext::new(&infcx).with_defining_use_anchor(DefiningAnchor::Bubble);
35+
let mut selcx = SelectionContext::new(&infcx, DefiningAnchor::Bubble);
3636

3737
let obligation_cause = ObligationCause::dummy();
3838
let obligation = Obligation::new(tcx, obligation_cause, param_env, trait_ref);

compiler/rustc_traits/src/normalize_projection_ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_infer::infer::canonical::{Canonical, QueryResponse};
2-
use rustc_infer::infer::TyCtxtInferExt;
2+
use rustc_infer::infer::{DefiningAnchor, TyCtxtInferExt};
33
use rustc_middle::ty::query::Providers;
44
use rustc_middle::ty::{ParamEnvAnd, TyCtxt};
55
use rustc_trait_selection::infer::InferCtxtBuilderExt;
@@ -23,7 +23,7 @@ fn normalize_projection_ty<'tcx>(
2323
tcx.infer_ctxt().enter_canonical_trait_query(
2424
&goal,
2525
|ocx, ParamEnvAnd { param_env, value: goal }| {
26-
let selcx = &mut SelectionContext::new(ocx.infcx);
26+
let selcx = &mut SelectionContext::new(ocx.infcx, DefiningAnchor::Error);
2727
let cause = ObligationCause::dummy();
2828
let mut obligations = vec![];
2929
let answer = traits::normalize_projection_type(

0 commit comments

Comments
 (0)