Skip to content

Commit 463c94a

Browse files
authored
Rollup merge of #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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 0 deletions
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 4 additions & 3 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 2 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 8 additions & 8 deletions
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

0 commit comments

Comments
 (0)