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

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

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

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

-9
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

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

+6-18
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

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

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

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

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

src/test/ui/nll/issue-98589-closures-relate-named-regions.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,21 @@ LL | || { None::<&'a &'b ()>; };
3535
= help: consider adding the following bound: `'b: 'a`
3636

3737
error[E0309]: the parameter type `T` may not live long enough
38-
--> $DIR/issue-98589-closures-relate-named-regions.rs:26:5
38+
--> $DIR/issue-98589-closures-relate-named-regions.rs:26:10
3939
|
4040
LL | || { None::<&'a T>; };
41-
| ^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
41+
| ^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
4242
|
4343
help: consider adding an explicit lifetime bound...
4444
|
4545
LL | fn test_early_type<'a: 'a, T: 'a>() {
4646
| ++++
4747

4848
error[E0309]: the parameter type `T` may not live long enough
49-
--> $DIR/issue-98589-closures-relate-named-regions.rs:32:5
49+
--> $DIR/issue-98589-closures-relate-named-regions.rs:32:10
5050
|
5151
LL | || { None::<&'a T>; };
52-
| ^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
52+
| ^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
5353
|
5454
help: consider adding an explicit lifetime bound...
5555
|

src/test/ui/nll/issue-98693.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ where
1313

1414
fn test<T>() {
1515
|| {
16-
//~^ ERROR the parameter type `T` may not live long enough
1716
assert_static::<T>();
17+
//~^ ERROR the parameter type `T` may not live long enough
1818
};
1919
}
2020

src/test/ui/nll/issue-98693.stderr

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
error[E0310]: the parameter type `T` may not live long enough
2-
--> $DIR/issue-98693.rs:15:5
2+
--> $DIR/issue-98693.rs:16:9
33
|
4-
LL | / || {
5-
LL | |
6-
LL | | assert_static::<T>();
7-
LL | | };
8-
| |_____^ ...so that the type `T` will meet its required lifetime bounds
4+
LL | assert_static::<T>();
5+
| ^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
96
|
107
help: consider adding an explicit lifetime bound...
118
|

src/test/ui/nll/ty-outlives/projection-implied-bounds.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0310]: the parameter type `T` may not live long enough
2-
--> $DIR/projection-implied-bounds.rs:30:18
2+
--> $DIR/projection-implied-bounds.rs:30:36
33
|
44
LL | twice(value, |value_ref, item| invoke2(value_ref, item));
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
66
|
77
help: consider adding an explicit lifetime bound...
88
|

src/test/ui/nll/ty-outlives/projection-no-regions-closure.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ LL | | T: Iterator,
2323
= note: defining type: no_region::<'_#1r, T>
2424

2525
error[E0309]: the associated type `<T as Iterator>::Item` may not live long enough
26-
--> $DIR/projection-no-regions-closure.rs:25:23
26+
--> $DIR/projection-no-regions-closure.rs:25:31
2727
|
2828
LL | with_signature(x, |mut y| Box::new(y.next()))
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
| ^^^^^^^^^^^^^^^^^^
3030
|
3131
= help: consider adding an explicit lifetime bound `<T as Iterator>::Item: 'a`...
3232
= note: ...so that the type `<T as Iterator>::Item` will meet its required lifetime bounds
@@ -80,10 +80,10 @@ LL | | T: 'b + Iterator,
8080
= note: defining type: wrong_region::<'_#1r, '_#2r, T>
8181

8282
error[E0309]: the associated type `<T as Iterator>::Item` may not live long enough
83-
--> $DIR/projection-no-regions-closure.rs:42:23
83+
--> $DIR/projection-no-regions-closure.rs:42:31
8484
|
8585
LL | with_signature(x, |mut y| Box::new(y.next()))
86-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
86+
| ^^^^^^^^^^^^^^^^^^
8787
|
8888
= help: consider adding an explicit lifetime bound `<T as Iterator>::Item: 'a`...
8989
= note: ...so that the type `<T as Iterator>::Item` will meet its required lifetime bounds

src/test/ui/nll/ty-outlives/projection-one-region-closure.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ LL | | T: Anything<'b>,
2525
= note: defining type: no_relationships_late::<'_#1r, T>
2626

2727
error[E0309]: the parameter type `T` may not live long enough
28-
--> $DIR/projection-one-region-closure.rs:45:29
28+
--> $DIR/projection-one-region-closure.rs:45:39
2929
|
3030
LL | with_signature(cell, t, |cell, t| require(cell, t));
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
31+
| ^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
3232
|
3333
help: consider adding an explicit lifetime bound...
3434
|
@@ -75,10 +75,10 @@ LL | | 'a: 'a,
7575
= note: defining type: no_relationships_early::<'_#1r, '_#2r, T>
7676

7777
error[E0309]: the parameter type `T` may not live long enough
78-
--> $DIR/projection-one-region-closure.rs:56:29
78+
--> $DIR/projection-one-region-closure.rs:56:39
7979
|
8080
LL | with_signature(cell, t, |cell, t| require(cell, t));
81-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
81+
| ^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
8282
|
8383
help: consider adding an explicit lifetime bound...
8484
|

src/test/ui/nll/ty-outlives/projection-two-region-trait-bound-closure.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ LL | | T: Anything<'b, 'c>,
2424
= note: defining type: no_relationships_late::<'_#1r, '_#2r, T>
2525

2626
error[E0309]: the associated type `<T as Anything<ReEarlyBound(0, 'b), ReEarlyBound(1, 'c)>>::AssocType` may not live long enough
27-
--> $DIR/projection-two-region-trait-bound-closure.rs:38:29
27+
--> $DIR/projection-two-region-trait-bound-closure.rs:38:39
2828
|
2929
LL | with_signature(cell, t, |cell, t| require(cell, t));
30-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
30+
| ^^^^^^^^^^^^^^^^
3131
|
3232
= help: consider adding an explicit lifetime bound `<T as Anything<ReEarlyBound(0, 'b), ReEarlyBound(1, 'c)>>::AssocType: 'a`...
3333
= note: ...so that the type `<T as Anything<ReEarlyBound(0, 'b), ReEarlyBound(1, 'c)>>::AssocType` will meet its required lifetime bounds
@@ -58,10 +58,10 @@ LL | | 'a: 'a,
5858
= note: defining type: no_relationships_early::<'_#1r, '_#2r, '_#3r, T>
5959

6060
error[E0309]: the associated type `<T as Anything<ReEarlyBound(1, 'b), ReEarlyBound(2, 'c)>>::AssocType` may not live long enough
61-
--> $DIR/projection-two-region-trait-bound-closure.rs:48:29
61+
--> $DIR/projection-two-region-trait-bound-closure.rs:48:39
6262
|
6363
LL | with_signature(cell, t, |cell, t| require(cell, t));
64-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
64+
| ^^^^^^^^^^^^^^^^
6565
|
6666
= help: consider adding an explicit lifetime bound `<T as Anything<ReEarlyBound(1, 'b), ReEarlyBound(2, 'c)>>::AssocType: 'a`...
6767
= note: ...so that the type `<T as Anything<ReEarlyBound(1, 'b), ReEarlyBound(2, 'c)>>::AssocType` will meet its required lifetime bounds

src/test/ui/nll/ty-outlives/ty-param-closure-approximate-lower-bound.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ LL | fn generic_fail<'a, T>(cell: Cell<&'a ()>, value: T) {
4444
= note: defining type: generic_fail::<T>
4545

4646
error[E0309]: the parameter type `T` may not live long enough
47-
--> $DIR/ty-param-closure-approximate-lower-bound.rs:29:24
47+
--> $DIR/ty-param-closure-approximate-lower-bound.rs:29:31
4848
|
4949
LL | twice(cell, value, |a, b| invoke(a, b));
50-
| ^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
50+
| ^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
5151
|
5252
help: consider adding an explicit lifetime bound...
5353
|

0 commit comments

Comments
 (0)