Skip to content

Commit b9bacc4

Browse files
Rollup merge of #136269 - compiler-errors:spanned, r=lcnr
Pass spans around new solver ...so that when we instantiate canonical responses, we can actually have region obligations with the right span. Within the solver itself, we still use dummy spans everywhere.
2 parents cc0f3ef + 4e763c2 commit b9bacc4

File tree

16 files changed

+139
-61
lines changed

16 files changed

+139
-61
lines changed

compiler/rustc_infer/src/infer/at.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ impl<'a, 'tcx> At<'a, 'tcx> {
137137
expected,
138138
ty::Contravariant,
139139
actual,
140+
self.cause.span,
140141
)
141142
.map(|goals| self.goals_to_obligations(goals))
142143
} else {
@@ -163,8 +164,15 @@ impl<'a, 'tcx> At<'a, 'tcx> {
163164
T: ToTrace<'tcx>,
164165
{
165166
if self.infcx.next_trait_solver {
166-
NextSolverRelate::relate(self.infcx, self.param_env, expected, ty::Covariant, actual)
167-
.map(|goals| self.goals_to_obligations(goals))
167+
NextSolverRelate::relate(
168+
self.infcx,
169+
self.param_env,
170+
expected,
171+
ty::Covariant,
172+
actual,
173+
self.cause.span,
174+
)
175+
.map(|goals| self.goals_to_obligations(goals))
168176
} else {
169177
let mut op = TypeRelating::new(
170178
self.infcx,
@@ -208,8 +216,15 @@ impl<'a, 'tcx> At<'a, 'tcx> {
208216
T: Relate<TyCtxt<'tcx>>,
209217
{
210218
if self.infcx.next_trait_solver {
211-
NextSolverRelate::relate(self.infcx, self.param_env, expected, ty::Invariant, actual)
212-
.map(|goals| self.goals_to_obligations(goals))
219+
NextSolverRelate::relate(
220+
self.infcx,
221+
self.param_env,
222+
expected,
223+
ty::Invariant,
224+
actual,
225+
self.cause.span,
226+
)
227+
.map(|goals| self.goals_to_obligations(goals))
213228
} else {
214229
let mut op = TypeRelating::new(
215230
self.infcx,

compiler/rustc_infer/src/infer/context.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_middle::ty::fold::TypeFoldable;
55
use rustc_middle::ty::relate::RelateResult;
66
use rustc_middle::ty::relate::combine::PredicateEmittingRelation;
77
use rustc_middle::ty::{self, Ty, TyCtxt};
8-
use rustc_span::{DUMMY_SP, ErrorGuaranteed};
8+
use rustc_span::{DUMMY_SP, ErrorGuaranteed, Span};
99

1010
use super::{BoundRegionConversionTime, InferCtxt, RegionVariableOrigin, SubregionOrigin};
1111

@@ -203,23 +203,23 @@ impl<'tcx> rustc_type_ir::InferCtxtLike for InferCtxt<'tcx> {
203203
self.probe(|_| probe())
204204
}
205205

206-
fn sub_regions(&self, sub: ty::Region<'tcx>, sup: ty::Region<'tcx>) {
206+
fn sub_regions(&self, sub: ty::Region<'tcx>, sup: ty::Region<'tcx>, span: Span) {
207207
self.inner.borrow_mut().unwrap_region_constraints().make_subregion(
208-
SubregionOrigin::RelateRegionParamBound(DUMMY_SP, None),
208+
SubregionOrigin::RelateRegionParamBound(span, None),
209209
sub,
210210
sup,
211211
);
212212
}
213213

214-
fn equate_regions(&self, a: ty::Region<'tcx>, b: ty::Region<'tcx>) {
214+
fn equate_regions(&self, a: ty::Region<'tcx>, b: ty::Region<'tcx>, span: Span) {
215215
self.inner.borrow_mut().unwrap_region_constraints().make_eqregion(
216-
SubregionOrigin::RelateRegionParamBound(DUMMY_SP, None),
216+
SubregionOrigin::RelateRegionParamBound(span, None),
217217
a,
218218
b,
219219
);
220220
}
221221

222-
fn register_ty_outlives(&self, ty: Ty<'tcx>, r: ty::Region<'tcx>) {
223-
self.register_region_obligation_with_cause(ty, r, &ObligationCause::dummy());
222+
fn register_ty_outlives(&self, ty: Ty<'tcx>, r: ty::Region<'tcx>, span: Span) {
223+
self.register_region_obligation_with_cause(ty, r, &ObligationCause::dummy_with_span(span));
224224
}
225225
}

compiler/rustc_next_trait_solver/src/delegate.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ use rustc_type_ir::fold::TypeFoldable;
44
use rustc_type_ir::solve::{Certainty, Goal, NoSolution};
55
use rustc_type_ir::{self as ty, InferCtxtLike, Interner};
66

7-
pub trait SolverDelegate: Deref<Target = <Self as SolverDelegate>::Infcx> + Sized {
8-
type Infcx: InferCtxtLike<Interner = <Self as SolverDelegate>::Interner>;
7+
pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
8+
type Infcx: InferCtxtLike<Interner = Self::Interner>;
99
type Interner: Interner;
1010
fn cx(&self) -> Self::Interner {
1111
(**self).cx()
1212
}
1313

14-
type Span: Copy;
15-
1614
fn build_with_canonical<V>(
1715
cx: Self::Interner,
1816
canonical: &ty::CanonicalQueryInput<Self::Interner, V>,
@@ -23,7 +21,7 @@ pub trait SolverDelegate: Deref<Target = <Self as SolverDelegate>::Infcx> + Size
2321
fn fresh_var_for_kind_with_span(
2422
&self,
2523
arg: <Self::Interner as Interner>::GenericArg,
26-
span: Self::Span,
24+
span: <Self::Interner as Interner>::Span,
2725
) -> <Self::Interner as Interner>::GenericArg;
2826

2927
// FIXME: Uplift the leak check into this crate.
@@ -61,6 +59,7 @@ pub trait SolverDelegate: Deref<Target = <Self as SolverDelegate>::Infcx> + Size
6159
fn instantiate_canonical_var_with_infer(
6260
&self,
6361
cv_info: ty::CanonicalVarInfo<Self::Interner>,
62+
span: <Self::Interner as Interner>::Span,
6463
universe_map: impl Fn(ty::UniverseIndex) -> ty::UniverseIndex,
6564
) -> <Self::Interner as Interner>::GenericArg;
6665

@@ -86,6 +85,7 @@ pub trait SolverDelegate: Deref<Target = <Self as SolverDelegate>::Infcx> + Size
8685
&self,
8786
key: ty::OpaqueTypeKey<Self::Interner>,
8887
hidden_ty: <Self::Interner as Interner>::Ty,
88+
span: <Self::Interner as Interner>::Span,
8989
);
9090

9191
fn reset_opaque_types(&self);

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs

+19-8
Original file line numberDiff line numberDiff line change
@@ -255,20 +255,29 @@ where
255255
self.delegate,
256256
&original_values,
257257
&response,
258+
self.origin_span,
258259
);
259260

260261
let Response { var_values, external_constraints, certainty } =
261262
self.delegate.instantiate_canonical(response, instantiation);
262263

263-
Self::unify_query_var_values(self.delegate, param_env, &original_values, var_values);
264+
Self::unify_query_var_values(
265+
self.delegate,
266+
param_env,
267+
&original_values,
268+
var_values,
269+
self.origin_span,
270+
);
264271

265272
let ExternalConstraintsData {
266273
region_constraints,
267274
opaque_types,
268275
normalization_nested_goals,
269276
} = &*external_constraints;
277+
270278
self.register_region_constraints(region_constraints);
271279
self.register_new_opaque_types(opaque_types);
280+
272281
(normalization_nested_goals.clone(), certainty)
273282
}
274283

@@ -279,6 +288,7 @@ where
279288
delegate: &D,
280289
original_values: &[I::GenericArg],
281290
response: &Canonical<I, T>,
291+
span: I::Span,
282292
) -> CanonicalVarValues<I> {
283293
// FIXME: Longterm canonical queries should deal with all placeholders
284294
// created inside of the query directly instead of returning them to the
@@ -331,7 +341,7 @@ where
331341
// A variable from inside a binder of the query. While ideally these shouldn't
332342
// exist at all (see the FIXME at the start of this method), we have to deal with
333343
// them for now.
334-
delegate.instantiate_canonical_var_with_infer(info, |idx| {
344+
delegate.instantiate_canonical_var_with_infer(info, span, |idx| {
335345
ty::UniverseIndex::from(prev_universe.index() + idx.index())
336346
})
337347
} else if info.is_existential() {
@@ -345,7 +355,7 @@ where
345355
if let Some(v) = opt_values[ty::BoundVar::from_usize(index)] {
346356
v
347357
} else {
348-
delegate.instantiate_canonical_var_with_infer(info, |_| prev_universe)
358+
delegate.instantiate_canonical_var_with_infer(info, span, |_| prev_universe)
349359
}
350360
} else {
351361
// For placeholders which were already part of the input, we simply map this
@@ -376,12 +386,13 @@ where
376386
param_env: I::ParamEnv,
377387
original_values: &[I::GenericArg],
378388
var_values: CanonicalVarValues<I>,
389+
span: I::Span,
379390
) {
380391
assert_eq!(original_values.len(), var_values.len());
381392

382393
for (&orig, response) in iter::zip(original_values, var_values.var_values.iter()) {
383394
let goals =
384-
delegate.eq_structurally_relating_aliases(param_env, orig, response).unwrap();
395+
delegate.eq_structurally_relating_aliases(param_env, orig, response, span).unwrap();
385396
assert!(goals.is_empty());
386397
}
387398
}
@@ -401,7 +412,7 @@ where
401412

402413
fn register_new_opaque_types(&mut self, opaque_types: &[(ty::OpaqueTypeKey<I>, I::Ty)]) {
403414
for &(key, ty) in opaque_types {
404-
self.delegate.inject_new_hidden_type_unchecked(key, ty);
415+
self.delegate.inject_new_hidden_type_unchecked(key, ty, self.origin_span);
405416
}
406417
}
407418
}
@@ -431,7 +442,7 @@ where
431442
// `rustc_trait_selection::solve::inspect::analyse`.
432443
pub fn instantiate_canonical_state<D, I, T: TypeFoldable<I>>(
433444
delegate: &D,
434-
span: D::Span,
445+
span: I::Span,
435446
param_env: I::ParamEnv,
436447
orig_values: &mut Vec<I::GenericArg>,
437448
state: inspect::CanonicalState<I, T>,
@@ -451,10 +462,10 @@ where
451462
}
452463

453464
let instantiation =
454-
EvalCtxt::compute_query_response_instantiation_values(delegate, orig_values, &state);
465+
EvalCtxt::compute_query_response_instantiation_values(delegate, orig_values, &state, span);
455466

456467
let inspect::State { var_values, data } = delegate.instantiate_canonical(state, instantiation);
457468

458-
EvalCtxt::unify_query_var_values(delegate, param_env, orig_values, var_values);
469+
EvalCtxt::unify_query_var_values(delegate, param_env, orig_values, var_values, span);
459470
data
460471
}

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

+33-13
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ where
7878

7979
nested_goals: NestedGoals<I>,
8080

81+
pub(super) origin_span: I::Span,
82+
8183
// Has this `EvalCtxt` errored out with `NoSolution` in `try_evaluate_added_goals`?
8284
//
8385
// If so, then it can no longer be used to make a canonical query response,
@@ -134,6 +136,7 @@ pub trait SolverDelegateEvalExt: SolverDelegate {
134136
&self,
135137
goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
136138
generate_proof_tree: GenerateProofTree,
139+
span: <Self::Interner as Interner>::Span,
137140
) -> (
138141
Result<(HasChanged, Certainty), NoSolution>,
139142
Option<inspect::GoalEvaluation<Self::Interner>>,
@@ -174,8 +177,9 @@ where
174177
&self,
175178
goal: Goal<I, I::Predicate>,
176179
generate_proof_tree: GenerateProofTree,
180+
span: I::Span,
177181
) -> (Result<(HasChanged, Certainty), NoSolution>, Option<inspect::GoalEvaluation<I>>) {
178-
EvalCtxt::enter_root(self, self.cx().recursion_limit(), generate_proof_tree, |ecx| {
182+
EvalCtxt::enter_root(self, self.cx().recursion_limit(), generate_proof_tree, span, |ecx| {
179183
ecx.evaluate_goal(GoalEvaluationKind::Root, GoalSource::Misc, goal)
180184
})
181185
}
@@ -186,7 +190,7 @@ where
186190
goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
187191
) -> bool {
188192
self.probe(|| {
189-
EvalCtxt::enter_root(self, root_depth, GenerateProofTree::No, |ecx| {
193+
EvalCtxt::enter_root(self, root_depth, GenerateProofTree::No, I::Span::dummy(), |ecx| {
190194
ecx.evaluate_goal(GoalEvaluationKind::Root, GoalSource::Misc, goal)
191195
})
192196
.0
@@ -203,9 +207,13 @@ where
203207
Result<(NestedNormalizationGoals<I>, HasChanged, Certainty), NoSolution>,
204208
Option<inspect::GoalEvaluation<I>>,
205209
) {
206-
EvalCtxt::enter_root(self, self.cx().recursion_limit(), generate_proof_tree, |ecx| {
207-
ecx.evaluate_goal_raw(GoalEvaluationKind::Root, GoalSource::Misc, goal)
208-
})
210+
EvalCtxt::enter_root(
211+
self,
212+
self.cx().recursion_limit(),
213+
generate_proof_tree,
214+
I::Span::dummy(),
215+
|ecx| ecx.evaluate_goal_raw(GoalEvaluationKind::Root, GoalSource::Misc, goal),
216+
)
209217
}
210218
}
211219

@@ -229,6 +237,7 @@ where
229237
delegate: &D,
230238
root_depth: usize,
231239
generate_proof_tree: GenerateProofTree,
240+
origin_span: I::Span,
232241
f: impl FnOnce(&mut EvalCtxt<'_, D>) -> R,
233242
) -> (R, Option<inspect::GoalEvaluation<I>>) {
234243
let mut search_graph = SearchGraph::new(root_depth);
@@ -248,6 +257,7 @@ where
248257
variables: Default::default(),
249258
var_values: CanonicalVarValues::dummy(),
250259
is_normalizes_to_goal: false,
260+
origin_span,
251261
tainted: Ok(()),
252262
};
253263
let result = f(&mut ecx);
@@ -289,12 +299,13 @@ where
289299
max_input_universe: canonical_input.canonical.max_universe,
290300
search_graph,
291301
nested_goals: NestedGoals::new(),
302+
origin_span: I::Span::dummy(),
292303
tainted: Ok(()),
293304
inspect: canonical_goal_evaluation.new_goal_evaluation_step(var_values),
294305
};
295306

296307
for &(key, ty) in &input.predefined_opaques_in_body.opaque_types {
297-
ecx.delegate.inject_new_hidden_type_unchecked(key, ty);
308+
ecx.delegate.inject_new_hidden_type_unchecked(key, ty, ecx.origin_span);
298309
}
299310

300311
if !ecx.nested_goals.is_empty() {
@@ -822,8 +833,12 @@ where
822833
let identity_args = self.fresh_args_for_item(alias.def_id);
823834
let rigid_ctor = ty::AliasTerm::new_from_args(cx, alias.def_id, identity_args);
824835
let ctor_term = rigid_ctor.to_term(cx);
825-
let obligations =
826-
self.delegate.eq_structurally_relating_aliases(param_env, term, ctor_term)?;
836+
let obligations = self.delegate.eq_structurally_relating_aliases(
837+
param_env,
838+
term,
839+
ctor_term,
840+
self.origin_span,
841+
)?;
827842
debug_assert!(obligations.is_empty());
828843
self.relate(param_env, alias, variance, rigid_ctor)
829844
} else {
@@ -841,7 +856,12 @@ where
841856
lhs: T,
842857
rhs: T,
843858
) -> Result<(), NoSolution> {
844-
let result = self.delegate.eq_structurally_relating_aliases(param_env, lhs, rhs)?;
859+
let result = self.delegate.eq_structurally_relating_aliases(
860+
param_env,
861+
lhs,
862+
rhs,
863+
self.origin_span,
864+
)?;
845865
assert_eq!(result, vec![]);
846866
Ok(())
847867
}
@@ -864,7 +884,7 @@ where
864884
variance: ty::Variance,
865885
rhs: T,
866886
) -> Result<(), NoSolution> {
867-
let goals = self.delegate.relate(param_env, lhs, variance, rhs)?;
887+
let goals = self.delegate.relate(param_env, lhs, variance, rhs, self.origin_span)?;
868888
self.add_goals(GoalSource::Misc, goals);
869889
Ok(())
870890
}
@@ -881,7 +901,7 @@ where
881901
lhs: T,
882902
rhs: T,
883903
) -> Result<Vec<Goal<I, I::Predicate>>, NoSolution> {
884-
Ok(self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs)?)
904+
Ok(self.delegate.relate(param_env, lhs, ty::Variance::Invariant, rhs, self.origin_span)?)
885905
}
886906

887907
pub(super) fn instantiate_binder_with_infer<T: TypeFoldable<I> + Copy>(
@@ -917,12 +937,12 @@ where
917937
}
918938

919939
pub(super) fn register_ty_outlives(&self, ty: I::Ty, lt: I::Region) {
920-
self.delegate.register_ty_outlives(ty, lt);
940+
self.delegate.register_ty_outlives(ty, lt, self.origin_span);
921941
}
922942

923943
pub(super) fn register_region_outlives(&self, a: I::Region, b: I::Region) {
924944
// `b : a` ==> `a <= b`
925-
self.delegate.sub_regions(b, a);
945+
self.delegate.sub_regions(b, a, self.origin_span);
926946
}
927947

928948
/// Computes the list of goals required for `arg` to be well-formed

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/probe.rs

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ where
3939
max_input_universe,
4040
search_graph: outer_ecx.search_graph,
4141
nested_goals: outer_ecx.nested_goals.clone(),
42+
origin_span: outer_ecx.origin_span,
4243
tainted: outer_ecx.tainted,
4344
inspect: outer_ecx.inspect.take_and_enter_probe(),
4445
};

0 commit comments

Comments
 (0)