Skip to content

Commit 463c94a

Browse files
authored
Rollup merge of rust-lang#96236 - Aaron1011:constraint-debug, r=jackh726
Add an explicit `Span` field to `OutlivesConstraint` Previously, we would retrieve the span from the `Body` using the `locations` field. However, we may end up changing the `locations` field when moving a constraint from a promoted to a different body. We now store the original `Span` in a dedication field, so that changes to the `locations` do not affect the quality of our diagnostics.
2 parents e2543d7 + 611a06a commit 463c94a

16 files changed

+58
-64
lines changed

compiler/rustc_borrowck/src/constraints/graph.rs

+1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ impl<'s, 'tcx, D: ConstraintGraphDirecton> Iterator for Edges<'s, 'tcx, D> {
156156
sup: self.static_region,
157157
sub: next_static_idx.into(),
158158
locations: Locations::All(DUMMY_SP),
159+
span: DUMMY_SP,
159160
category: ConstraintCategory::Internal,
160161
variance_info: VarianceDiagInfo::default(),
161162
})

compiler/rustc_borrowck/src/constraints/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use rustc_data_structures::graph::scc::Sccs;
22
use rustc_index::vec::IndexVec;
33
use rustc_middle::mir::ConstraintCategory;
44
use rustc_middle::ty::{RegionVid, VarianceDiagInfo};
5+
use rustc_span::Span;
56
use std::fmt;
67
use std::ops::Index;
78

@@ -87,6 +88,12 @@ pub struct OutlivesConstraint<'tcx> {
8788
/// Where did this constraint arise?
8889
pub locations: Locations,
8990

91+
/// The `Span` associated with the creation of this constraint.
92+
/// This should be used in preference to obtaining the span from
93+
/// `locations`, since the `locations` may give a poor span
94+
/// in some cases (e.g. converting a constraint from a promoted).
95+
pub span: Span,
96+
9097
/// What caused this constraint?
9198
pub category: ConstraintCategory,
9299

compiler/rustc_borrowck/src/region_infer/dump_mir.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,18 @@ impl<'tcx> RegionInferenceContext<'tcx> {
7474
let mut constraints: Vec<_> = self.constraints.outlives().iter().collect();
7575
constraints.sort_by_key(|c| (c.sup, c.sub));
7676
for constraint in &constraints {
77-
let OutlivesConstraint { sup, sub, locations, category, variance_info: _ } = constraint;
77+
let OutlivesConstraint { sup, sub, locations, category, span, variance_info: _ } =
78+
constraint;
7879
let (name, arg) = match locations {
7980
Locations::All(span) => {
8081
("All", tcx.sess.source_map().span_to_embeddable_string(*span))
8182
}
8283
Locations::Single(loc) => ("Single", format!("{:?}", loc)),
8384
};
84-
with_msg(&format!("{:?}: {:?} due to {:?} at {}({})", sup, sub, category, name, arg))?;
85+
with_msg(&format!(
86+
"{:?}: {:?} due to {:?} at {}({}) ({:?}",
87+
sup, sub, category, name, arg, span
88+
))?;
8589
}
8690

8791
Ok(())

compiler/rustc_borrowck/src/region_infer/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1733,7 +1733,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17331733

17341734
crate fn retrieve_closure_constraint_info(
17351735
&self,
1736-
body: &Body<'tcx>,
1736+
_body: &Body<'tcx>,
17371737
constraint: &OutlivesConstraint<'tcx>,
17381738
) -> BlameConstraint<'tcx> {
17391739
let loc = match constraint.locations {
@@ -1760,7 +1760,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17601760
.unwrap_or(BlameConstraint {
17611761
category: constraint.category,
17621762
from_closure: false,
1763-
cause: ObligationCause::dummy_with_span(body.source_info(loc).span),
1763+
cause: ObligationCause::dummy_with_span(constraint.span),
17641764
variance_info: constraint.variance_info,
17651765
})
17661766
}
@@ -1869,6 +1869,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
18691869
sup: r,
18701870
sub: constraint.min_choice,
18711871
locations: Locations::All(p_c.definition_span),
1872+
span: p_c.definition_span,
18721873
category: ConstraintCategory::OpaqueType,
18731874
variance_info: ty::VarianceDiagInfo::default(),
18741875
};
@@ -2017,7 +2018,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
20172018
category: constraint.category,
20182019
from_closure: false,
20192020
cause: ObligationCause::new(
2020-
constraint.locations.span(body),
2021+
constraint.span,
20212022
CRATE_HIR_ID,
20222023
cause_code.clone(),
20232024
),

compiler/rustc_borrowck/src/type_check/constraint_conversion.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::mir::ConstraintCategory;
88
use rustc_middle::ty::subst::GenericArgKind;
99
use rustc_middle::ty::TypeFoldable;
1010
use rustc_middle::ty::{self, TyCtxt};
11-
use rustc_span::DUMMY_SP;
11+
use rustc_span::{Span, DUMMY_SP};
1212

1313
use crate::{
1414
constraints::OutlivesConstraint,
@@ -26,6 +26,7 @@ crate struct ConstraintConversion<'a, 'tcx> {
2626
implicit_region_bound: Option<ty::Region<'tcx>>,
2727
param_env: ty::ParamEnv<'tcx>,
2828
locations: Locations,
29+
span: Span,
2930
category: ConstraintCategory,
3031
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
3132
}
@@ -38,6 +39,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
3839
implicit_region_bound: Option<ty::Region<'tcx>>,
3940
param_env: ty::ParamEnv<'tcx>,
4041
locations: Locations,
42+
span: Span,
4143
category: ConstraintCategory,
4244
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
4345
) -> Self {
@@ -49,6 +51,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
4951
implicit_region_bound,
5052
param_env,
5153
locations,
54+
span,
5255
category,
5356
constraints,
5457
}
@@ -153,6 +156,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
153156
self.constraints.outlives_constraints.push(OutlivesConstraint {
154157
locations: self.locations,
155158
category: self.category,
159+
span: self.span,
156160
sub,
157161
sup,
158162
variance_info: ty::VarianceDiagInfo::default(),

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

+1
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
316316
self.implicit_region_bound,
317317
self.param_env,
318318
Locations::All(DUMMY_SP),
319+
DUMMY_SP,
319320
ConstraintCategory::Internal,
320321
&mut self.constraints,
321322
)

compiler/rustc_borrowck/src/type_check/input_output.rs

+1
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
235235
Some(self.implicit_region_bound),
236236
self.param_env,
237237
Locations::All(DUMMY_SP),
238+
DUMMY_SP,
238239
ConstraintCategory::Internal,
239240
&mut self.borrowck_context.constraints,
240241
)

compiler/rustc_borrowck/src/type_check/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
11411141
Some(self.implicit_region_bound),
11421142
self.param_env,
11431143
locations,
1144+
locations.span(self.body),
11441145
category,
11451146
&mut self.borrowck_context.constraints,
11461147
)
@@ -2401,6 +2402,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24012402
sup: ref_region.to_region_vid(),
24022403
sub: borrow_region.to_region_vid(),
24032404
locations: location.to_locations(),
2405+
span: location.to_locations().span(body),
24042406
category,
24052407
variance_info: ty::VarianceDiagInfo::default(),
24062408
});

compiler/rustc_borrowck/src/type_check/relate_tys.rs

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
116116
sup,
117117
sub,
118118
locations: self.locations,
119+
span: self.locations.span(self.type_checker.body),
119120
category: self.category,
120121
variance_info: info,
121122
},

src/test/mir-opt/nll/named_lifetimes_basic.use_x.nll.0.mir

+8-8
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525
| '_#2r live at {bb0[0..=1]}
2626
| '_#3r live at {bb0[0..=1]}
2727
| '_#4r live at {bb0[0..=1]}
28-
| '_#1r: '_#6r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:26: 12:27)
29-
| '_#1r: '_#8r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:54: 12:55)
30-
| '_#2r: '_#7r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:42: 12:43)
31-
| '_#3r: '_#9r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:66: 12:67)
32-
| '_#6r: '_#1r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:26: 12:27)
33-
| '_#7r: '_#2r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:42: 12:43)
34-
| '_#8r: '_#1r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:54: 12:55)
35-
| '_#9r: '_#3r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:66: 12:67)
28+
| '_#1r: '_#6r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:26: 12:27) ($DIR/named-lifetimes-basic.rs:12:26: 12:27 (#0)
29+
| '_#1r: '_#8r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:54: 12:55) ($DIR/named-lifetimes-basic.rs:12:54: 12:55 (#0)
30+
| '_#2r: '_#7r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:42: 12:43) ($DIR/named-lifetimes-basic.rs:12:42: 12:43 (#0)
31+
| '_#3r: '_#9r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:66: 12:67) ($DIR/named-lifetimes-basic.rs:12:66: 12:67 (#0)
32+
| '_#6r: '_#1r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:26: 12:27) ($DIR/named-lifetimes-basic.rs:12:26: 12:27 (#0)
33+
| '_#7r: '_#2r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:42: 12:43) ($DIR/named-lifetimes-basic.rs:12:42: 12:43 (#0)
34+
| '_#8r: '_#1r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:54: 12:55) ($DIR/named-lifetimes-basic.rs:12:54: 12:55 (#0)
35+
| '_#9r: '_#3r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:66: 12:67) ($DIR/named-lifetimes-basic.rs:12:66: 12:67 (#0)
3636
|
3737
fn use_x(_1: &'_#6r mut i32, _2: &'_#7r u32, _3: &'_#8r u32, _4: &'_#9r u32) -> bool {
3838
debug w => _1; // in scope 0 at $DIR/named-lifetimes-basic.rs:12:26: 12:27

src/test/mir-opt/nll/region_subtyping_basic.main.nll.0.32bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
| '_#3r live at {bb1[0]}
1919
| '_#4r live at {bb1[1..=3]}
2020
| '_#5r live at {bb1[4..=7], bb2[0..=2]}
21-
| '_#3r: '_#4r due to Assignment at Single(bb1[0])
22-
| '_#4r: '_#5r due to Assignment at Single(bb1[3])
21+
| '_#3r: '_#4r due to Assignment at Single(bb1[0]) ($DIR/region-subtyping-basic.rs:18:13: 18:18 (#0)
22+
| '_#4r: '_#5r due to Assignment at Single(bb1[3]) ($DIR/region-subtyping-basic.rs:19:13: 19:14 (#0)
2323
|
2424
fn main() -> () {
2525
let mut _0: (); // return place in scope 0 at $DIR/region-subtyping-basic.rs:16:11: 16:11

src/test/mir-opt/nll/region_subtyping_basic.main.nll.0.64bit.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
| '_#3r live at {bb1[0]}
1919
| '_#4r live at {bb1[1..=3]}
2020
| '_#5r live at {bb1[4..=7], bb2[0..=2]}
21-
| '_#3r: '_#4r due to Assignment at Single(bb1[0])
22-
| '_#4r: '_#5r due to Assignment at Single(bb1[3])
21+
| '_#3r: '_#4r due to Assignment at Single(bb1[0]) ($DIR/region-subtyping-basic.rs:18:13: 18:18 (#0)
22+
| '_#4r: '_#5r due to Assignment at Single(bb1[3]) ($DIR/region-subtyping-basic.rs:19:13: 19:14 (#0)
2323
|
2424
fn main() -> () {
2525
let mut _0: (); // return place in scope 0 at $DIR/region-subtyping-basic.rs:16:11: 16:11

src/test/mir-opt/storage_ranges.main.nll.0.mir

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
| '_#1r live at {bb0[0..=22]}
1717
| '_#3r live at {bb0[10]}
1818
| '_#4r live at {bb0[11]}
19-
| '_#3r: '_#4r due to Assignment at Single(bb0[10])
19+
| '_#3r: '_#4r due to Assignment at Single(bb0[10]) ($DIR/storage_ranges.rs:6:17: 6:25 (#0)
2020
|
2121
fn main() -> () {
2222
let mut _0: (); // return place in scope 0 at $DIR/storage_ranges.rs:3:11: 3:11

src/test/ui/rfc1623.base.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: implementation of `FnOnce` is not general enough
2-
--> $DIR/rfc1623.rs:36:8
2+
--> $DIR/rfc1623.rs:32:8
33
|
44
LL | f: &id,
55
| ^^^ implementation of `FnOnce` is not general enough

src/test/ui/rfc1623.nll.stderr

+12-40
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,35 @@
11
error[E0308]: mismatched types
2-
--> $DIR/rfc1623.rs:29:35
2+
--> $DIR/rfc1623.rs:32:8
33
|
4-
LL | static SOME_STRUCT: &SomeStruct = &SomeStruct {
5-
| ___________________________________^
6-
LL | |
7-
LL | |
8-
LL | |
9-
... |
10-
LL | |
11-
LL | | };
12-
| |_^ one type is more general than the other
4+
LL | f: &id,
5+
| ^^^ one type is more general than the other
136
|
147
= note: expected type `for<'a, 'b> Fn<(&'a Foo<'b>,)>`
158
found type `Fn<(&Foo<'_>,)>`
169

1710
error[E0308]: mismatched types
18-
--> $DIR/rfc1623.rs:29:35
11+
--> $DIR/rfc1623.rs:32:8
1912
|
20-
LL | static SOME_STRUCT: &SomeStruct = &SomeStruct {
21-
| ___________________________________^
22-
LL | |
23-
LL | |
24-
LL | |
25-
... |
26-
LL | |
27-
LL | | };
28-
| |_^ one type is more general than the other
13+
LL | f: &id,
14+
| ^^^ one type is more general than the other
2915
|
3016
= note: expected type `for<'a, 'b> Fn<(&'a Foo<'b>,)>`
3117
found type `Fn<(&Foo<'_>,)>`
3218

3319
error: implementation of `FnOnce` is not general enough
34-
--> $DIR/rfc1623.rs:29:35
20+
--> $DIR/rfc1623.rs:32:8
3521
|
36-
LL | static SOME_STRUCT: &SomeStruct = &SomeStruct {
37-
| ___________________________________^
38-
LL | |
39-
LL | |
40-
LL | |
41-
... |
42-
LL | |
43-
LL | | };
44-
| |_^ implementation of `FnOnce` is not general enough
22+
LL | f: &id,
23+
| ^^^ implementation of `FnOnce` is not general enough
4524
|
4625
= note: `fn(&'2 Foo<'_>) -> &'2 Foo<'_> {id::<&'2 Foo<'_>>}` must implement `FnOnce<(&'1 Foo<'b>,)>`, for any lifetime `'1`...
4726
= note: ...but it actually implements `FnOnce<(&'2 Foo<'_>,)>`, for some specific lifetime `'2`
4827

4928
error: implementation of `FnOnce` is not general enough
50-
--> $DIR/rfc1623.rs:29:35
29+
--> $DIR/rfc1623.rs:32:8
5130
|
52-
LL | static SOME_STRUCT: &SomeStruct = &SomeStruct {
53-
| ___________________________________^
54-
LL | |
55-
LL | |
56-
LL | |
57-
... |
58-
LL | |
59-
LL | | };
60-
| |_^ implementation of `FnOnce` is not general enough
31+
LL | f: &id,
32+
| ^^^ implementation of `FnOnce` is not general enough
6133
|
6234
= note: `fn(&Foo<'2>) -> &Foo<'2> {id::<&Foo<'2>>}` must implement `FnOnce<(&'a Foo<'1>,)>`, for any lifetime `'1`...
6335
= note: ...but it actually implements `FnOnce<(&Foo<'2>,)>`, for some specific lifetime `'2`

src/test/ui/rfc1623.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ fn id<T>(t: T) -> T {
2727
}
2828

2929
static SOME_STRUCT: &SomeStruct = &SomeStruct {
30-
//[nll]~^ ERROR mismatched types
31-
//[nll]~| ERROR mismatched types
32-
//[nll]~| ERROR implementation of `FnOnce` is not general enough
33-
//[nll]~| ERROR implementation of `FnOnce` is not general enough
3430
foo: &Foo { bools: &[false, true] },
3531
bar: &Bar { bools: &[true, true] },
3632
f: &id,
3733
//[base]~^ ERROR implementation of `FnOnce` is not general enough
34+
//[nll]~^^ ERROR mismatched types
35+
//[nll]~| ERROR mismatched types
36+
//[nll]~| ERROR implementation of `FnOnce` is not general enough
37+
//[nll]~| ERROR implementation of `FnOnce` is not general enough
3838
};
3939

4040
// very simple test for a 'static static with default lifetime

0 commit comments

Comments
 (0)