Skip to content

Commit 02f78fd

Browse files
committed
use spans in TypeTest rather than mir::Location
Spans are independent of the body being borrow-checked, so they don't need remapping when promoting type-tests and they yield more specific error spans inside bodies of closures/inline consts.
1 parent df668b9 commit 02f78fd

21 files changed

+76
-112
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
181181
// Try to convert the lower-bound region into something named we can print for the user.
182182
let lower_bound_region = self.to_error_region(type_test.lower_bound);
183183

184-
let type_test_span = type_test.locations.span(&self.body);
184+
let type_test_span = type_test.span;
185185

186186
if let Some(lower_bound_region) = lower_bound_region {
187187
let generic_ty = type_test.generic_kind.to_ty(self.infcx.tcx);

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,8 @@ pub struct TypeTest<'tcx> {
214214
/// The region `'x` that the type must outlive.
215215
pub lower_bound: RegionVid,
216216

217-
/// Where did this constraint arise and why?
218-
pub locations: Locations,
217+
/// The span to blame.
218+
pub span: Span,
219219

220220
/// A test which, if met by the region `'x`, proves that this type
221221
/// constraint is satisfied.
@@ -870,13 +870,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
870870
if deduplicate_errors.insert((
871871
erased_generic_kind,
872872
type_test.lower_bound,
873-
type_test.locations,
873+
type_test.span,
874874
)) {
875875
debug!(
876876
"check_type_test: reporting error for erased_generic_kind={:?}, \
877877
lower_bound_region={:?}, \
878-
type_test.locations={:?}",
879-
erased_generic_kind, type_test.lower_bound, type_test.locations,
878+
type_test.span={:?}",
879+
erased_generic_kind, type_test.lower_bound, type_test.span,
880880
);
881881

882882
errors_buffer.push(RegionErrorKind::TypeTestError { type_test: type_test.clone() });
@@ -919,7 +919,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
919919
) -> bool {
920920
let tcx = infcx.tcx;
921921

922-
let TypeTest { generic_kind, lower_bound, locations, verify_bound: _ } = type_test;
922+
let TypeTest { generic_kind, lower_bound, span: _, verify_bound: _ } = type_test;
923923

924924
let generic_ty = generic_kind.to_ty(tcx);
925925
let Some(subject) = self.try_promote_type_test_subject(infcx, generic_ty) else {
@@ -947,7 +947,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
947947
propagated_outlives_requirements.push(ClosureOutlivesRequirement {
948948
subject,
949949
outlived_free_region: static_r,
950-
blame_span: locations.span(body),
950+
blame_span: type_test.span,
951951
category: ConstraintCategory::Boring,
952952
});
953953

@@ -999,7 +999,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
999999
let requirement = ClosureOutlivesRequirement {
10001000
subject,
10011001
outlived_free_region: upper_bound,
1002-
blame_span: locations.span(body),
1002+
blame_span: type_test.span,
10031003
category: ConstraintCategory::Boring,
10041004
};
10051005
debug!("try_promote_type_test: pushing {:#?}", requirement);

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
169169
.type_must_outlive(origin, t1, r2, constraint_category);
170170
}
171171

172-
GenericArgKind::Const(_) => {
173-
// Consts cannot outlive one another, so we
174-
// don't need to handle any relations here.
175-
}
172+
GenericArgKind::Const(_) => unreachable!(),
176173
}
177174
}
178175

@@ -202,7 +199,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
202199
verify_bound: VerifyBound<'tcx>,
203200
) -> TypeTest<'tcx> {
204201
let lower_bound = self.to_region_vid(region);
205-
TypeTest { generic_kind, lower_bound, locations: self.locations, verify_bound }
202+
TypeTest { generic_kind, lower_bound, span: self.span, verify_bound }
206203
}
207204

208205
fn to_region_vid(&mut self, r: ty::Region<'tcx>) -> ty::RegionVid {

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
583583
// modify their locations.
584584
let all_facts = &mut None;
585585
let mut constraints = Default::default();
586-
let mut type_tests = Default::default();
587586
let mut liveness_constraints =
588587
LivenessValues::new(Rc::new(RegionValueElements::new(&promoted_body)));
589588
// Don't try to add borrow_region facts for the promoted MIR
@@ -594,7 +593,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
594593
&mut this.cx.borrowck_context.constraints.outlives_constraints,
595594
&mut constraints,
596595
);
597-
mem::swap(&mut this.cx.borrowck_context.constraints.type_tests, &mut type_tests);
598596
mem::swap(
599597
&mut this.cx.borrowck_context.constraints.liveness_constraints,
600598
&mut liveness_constraints,
@@ -615,13 +613,6 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
615613
swap_constraints(self);
616614

617615
let locations = location.to_locations();
618-
619-
// Use location of promoted const in collected constraints
620-
for type_test in type_tests.iter() {
621-
let mut type_test = type_test.clone();
622-
type_test.locations = locations;
623-
self.cx.borrowck_context.constraints.type_tests.push(type_test)
624-
}
625616
for constraint in constraints.outlives().iter() {
626617
let mut constraint = constraint.clone();
627618
constraint.locations = locations;

src/test/ui/consts/issue-102117.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ pub struct VTable {
1414
impl VTable {
1515
pub fn new<T>() -> &'static Self {
1616
const {
17-
//~^ ERROR the parameter type `T` may not live long enough
18-
//~| ERROR the parameter type `T` may not live long enough
1917
&VTable {
2018
layout: Layout::new::<T>(),
2119
type_id: TypeId::of::<T>(),
20+
//~^ ERROR the parameter type `T` may not live long enough
21+
//~| ERROR the parameter type `T` may not live long enough
2222
drop_in_place: unsafe {
2323
transmute::<unsafe fn(*mut T), unsafe fn(*mut ())>(drop_in_place::<T>)
2424
},

src/test/ui/consts/issue-102117.stderr

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,19 @@
11
error[E0310]: the parameter type `T` may not live long enough
2-
--> $DIR/issue-102117.rs:16:9
2+
--> $DIR/issue-102117.rs:19:26
33
|
4-
LL | / const {
5-
LL | |
6-
LL | |
7-
LL | | &VTable {
8-
... |
9-
LL | | }
10-
LL | | }
11-
| |_________^ ...so that the type `T` will meet its required lifetime bounds
4+
LL | type_id: TypeId::of::<T>(),
5+
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
126
|
137
help: consider adding an explicit lifetime bound...
148
|
159
LL | pub fn new<T: 'static>() -> &'static Self {
1610
| +++++++++
1711

1812
error[E0310]: the parameter type `T` may not live long enough
19-
--> $DIR/issue-102117.rs:16:9
13+
--> $DIR/issue-102117.rs:19:26
2014
|
21-
LL | / const {
22-
LL | |
23-
LL | |
24-
LL | | &VTable {
25-
... |
26-
LL | | }
27-
LL | | }
28-
| |_________^ ...so that the type `T` will meet its required lifetime bounds
15+
LL | type_id: TypeId::of::<T>(),
16+
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
2917
|
3018
help: consider adding an explicit lifetime bound...
3119
|

src/test/ui/generic-associated-types/issue-91139.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn foo<T>() {
2121
//~| ERROR `T` does not live long enough
2222
//~| ERROR `T` does not live long enough
2323
//~| ERROR `T` may not live long enough
24+
//~| ERROR `T` may not live long enough
2425
//
2526
// FIXME: This error is bogus, but it arises because we try to validate
2627
// that `<() as Foo<T>>::Type<'a>` is valid, which requires proving

src/test/ui/generic-associated-types/issue-91139.stderr

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ error: `T` does not live long enough
2222
LL | let _: for<'a> fn(<() as Foo<T>>::Type<'a>, &'a T) = |_, _| ();
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

25+
error[E0310]: the parameter type `T` may not live long enough
26+
--> $DIR/issue-91139.rs:14:58
27+
|
28+
LL | let _: for<'a> fn(<() as Foo<T>>::Type<'a>, &'a T) = |_, _| ();
29+
| ^^^^^^ ...so that the type `T` will meet its required lifetime bounds
30+
|
31+
help: consider adding an explicit lifetime bound...
32+
|
33+
LL | fn foo<T: 'static>() {
34+
| +++++++++
35+
2536
error: `T` does not live long enough
2637
--> $DIR/issue-91139.rs:14:58
2738
|
@@ -57,6 +68,6 @@ error: `T` does not live long enough
5768
LL | let _: for<'a> fn(<() as Foo<T>>::Type<'a>, &'a T) = |_, _| ();
5869
| ^^^^^^^^^
5970

60-
error: aborting due to 9 previous errors
71+
error: aborting due to 10 previous errors
6172

6273
For more information about this error, try `rustc --explain E0310`.

src/test/ui/nll/closure-requirements/propagate-from-trait-match.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ where
3030
T: Trait<'a>,
3131
{
3232
establish_relationships(value, |value| {
33-
//~^ ERROR the parameter type `T` may not live long enough
34-
3533
// This function call requires that
3634
//
3735
// (a) T: Trait<'a>
@@ -43,6 +41,7 @@ where
4341
// The latter does not hold.
4442

4543
require(value);
44+
//~^ ERROR the parameter type `T` may not live long enough
4645
});
4746
}
4847

src/test/ui/nll/closure-requirements/propagate-from-trait-match.stderr

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,10 @@ LL | | T: Trait<'a>,
2323
= note: defining type: supply::<'_#1r, T>
2424

2525
error[E0309]: the parameter type `T` may not live long enough
26-
--> $DIR/propagate-from-trait-match.rs:32:36
26+
--> $DIR/propagate-from-trait-match.rs:43:9
2727
|
28-
LL | establish_relationships(value, |value| {
29-
| ____________________________________^
30-
LL | |
31-
LL | |
32-
LL | | // This function call requires that
33-
... |
34-
LL | | require(value);
35-
LL | | });
36-
| |_____^ ...so that the type `T` will meet its required lifetime bounds
28+
LL | require(value);
29+
| ^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
3730
|
3831
help: consider adding an explicit lifetime bound...
3932
|

0 commit comments

Comments
 (0)