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.
This commit is contained in:
Aaron Hill 2022-04-19 20:53:35 -04:00
parent 4ca19e09d3
commit 611a06a375
No known key found for this signature in database
GPG key ID: B4087E510E98B164
16 changed files with 58 additions and 64 deletions

View file

@ -156,6 +156,7 @@ impl<'s, 'tcx, D: ConstraintGraphDirecton> Iterator for Edges<'s, 'tcx, D> {
sup: self.static_region,
sub: next_static_idx.into(),
locations: Locations::All(DUMMY_SP),
span: DUMMY_SP,
category: ConstraintCategory::Internal,
variance_info: VarianceDiagInfo::default(),
})

View file

@ -2,6 +2,7 @@ use rustc_data_structures::graph::scc::Sccs;
use rustc_index::vec::IndexVec;
use rustc_middle::mir::ConstraintCategory;
use rustc_middle::ty::{RegionVid, VarianceDiagInfo};
use rustc_span::Span;
use std::fmt;
use std::ops::Index;
@ -87,6 +88,12 @@ pub struct OutlivesConstraint<'tcx> {
/// Where did this constraint arise?
pub locations: Locations,
/// The `Span` associated with the creation of this constraint.
/// This should be used in preference to obtaining the span from
/// `locations`, since the `locations` may give a poor span
/// in some cases (e.g. converting a constraint from a promoted).
pub span: Span,
/// What caused this constraint?
pub category: ConstraintCategory,

View file

@ -74,14 +74,18 @@ impl<'tcx> RegionInferenceContext<'tcx> {
let mut constraints: Vec<_> = self.constraints.outlives().iter().collect();
constraints.sort_by_key(|c| (c.sup, c.sub));
for constraint in &constraints {
let OutlivesConstraint { sup, sub, locations, category, variance_info: _ } = constraint;
let OutlivesConstraint { sup, sub, locations, category, span, variance_info: _ } =
constraint;
let (name, arg) = match locations {
Locations::All(span) => {
("All", tcx.sess.source_map().span_to_embeddable_string(*span))
}
Locations::Single(loc) => ("Single", format!("{:?}", loc)),
};
with_msg(&format!("{:?}: {:?} due to {:?} at {}({})", sup, sub, category, name, arg))?;
with_msg(&format!(
"{:?}: {:?} due to {:?} at {}({}) ({:?}",
sup, sub, category, name, arg, span
))?;
}
Ok(())

View file

@ -1733,7 +1733,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
crate fn retrieve_closure_constraint_info(
&self,
body: &Body<'tcx>,
_body: &Body<'tcx>,
constraint: &OutlivesConstraint<'tcx>,
) -> BlameConstraint<'tcx> {
let loc = match constraint.locations {
@ -1760,7 +1760,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
.unwrap_or(BlameConstraint {
category: constraint.category,
from_closure: false,
cause: ObligationCause::dummy_with_span(body.source_info(loc).span),
cause: ObligationCause::dummy_with_span(constraint.span),
variance_info: constraint.variance_info,
})
}
@ -1869,6 +1869,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
sup: r,
sub: constraint.min_choice,
locations: Locations::All(p_c.definition_span),
span: p_c.definition_span,
category: ConstraintCategory::OpaqueType,
variance_info: ty::VarianceDiagInfo::default(),
};
@ -2017,7 +2018,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
category: constraint.category,
from_closure: false,
cause: ObligationCause::new(
constraint.locations.span(body),
constraint.span,
CRATE_HIR_ID,
cause_code.clone(),
),

View file

@ -8,7 +8,7 @@ use rustc_middle::mir::ConstraintCategory;
use rustc_middle::ty::subst::GenericArgKind;
use rustc_middle::ty::TypeFoldable;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::DUMMY_SP;
use rustc_span::{Span, DUMMY_SP};
use crate::{
constraints::OutlivesConstraint,
@ -26,6 +26,7 @@ crate struct ConstraintConversion<'a, 'tcx> {
implicit_region_bound: Option<ty::Region<'tcx>>,
param_env: ty::ParamEnv<'tcx>,
locations: Locations,
span: Span,
category: ConstraintCategory,
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
}
@ -38,6 +39,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
implicit_region_bound: Option<ty::Region<'tcx>>,
param_env: ty::ParamEnv<'tcx>,
locations: Locations,
span: Span,
category: ConstraintCategory,
constraints: &'a mut MirTypeckRegionConstraints<'tcx>,
) -> Self {
@ -49,6 +51,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
implicit_region_bound,
param_env,
locations,
span,
category,
constraints,
}
@ -153,6 +156,7 @@ impl<'a, 'tcx> ConstraintConversion<'a, 'tcx> {
self.constraints.outlives_constraints.push(OutlivesConstraint {
locations: self.locations,
category: self.category,
span: self.span,
sub,
sup,
variance_info: ty::VarianceDiagInfo::default(),

View file

@ -316,6 +316,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
self.implicit_region_bound,
self.param_env,
Locations::All(DUMMY_SP),
DUMMY_SP,
ConstraintCategory::Internal,
&mut self.constraints,
)

View file

@ -235,6 +235,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Some(self.implicit_region_bound),
self.param_env,
Locations::All(DUMMY_SP),
DUMMY_SP,
ConstraintCategory::Internal,
&mut self.borrowck_context.constraints,
)

View file

@ -1141,6 +1141,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
Some(self.implicit_region_bound),
self.param_env,
locations,
locations.span(self.body),
category,
&mut self.borrowck_context.constraints,
)
@ -2401,6 +2402,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
sup: ref_region.to_region_vid(),
sub: borrow_region.to_region_vid(),
locations: location.to_locations(),
span: location.to_locations().span(body),
category,
variance_info: ty::VarianceDiagInfo::default(),
});

View file

@ -116,6 +116,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
sup,
sub,
locations: self.locations,
span: self.locations.span(self.type_checker.body),
category: self.category,
variance_info: info,
},

View file

@ -25,14 +25,14 @@
| '_#2r live at {bb0[0..=1]}
| '_#3r live at {bb0[0..=1]}
| '_#4r live at {bb0[0..=1]}
| '_#1r: '_#6r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:26: 12:27)
| '_#1r: '_#8r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:54: 12:55)
| '_#2r: '_#7r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:42: 12:43)
| '_#3r: '_#9r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:66: 12:67)
| '_#6r: '_#1r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:26: 12:27)
| '_#7r: '_#2r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:42: 12:43)
| '_#8r: '_#1r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:54: 12:55)
| '_#9r: '_#3r due to BoringNoLocation at All($DIR/named-lifetimes-basic.rs:12:66: 12:67)
| '_#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)
| '_#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)
| '_#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)
| '_#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)
| '_#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)
| '_#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)
| '_#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)
| '_#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)
|
fn use_x(_1: &'_#6r mut i32, _2: &'_#7r u32, _3: &'_#8r u32, _4: &'_#9r u32) -> bool {
debug w => _1; // in scope 0 at $DIR/named-lifetimes-basic.rs:12:26: 12:27

View file

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

View file

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

View file

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

View file

@ -1,5 +1,5 @@
error: implementation of `FnOnce` is not general enough
--> $DIR/rfc1623.rs:36:8
--> $DIR/rfc1623.rs:32:8
|
LL | f: &id,
| ^^^ implementation of `FnOnce` is not general enough

View file

@ -1,63 +1,35 @@
error[E0308]: mismatched types
--> $DIR/rfc1623.rs:29:35
--> $DIR/rfc1623.rs:32:8
|
LL | static SOME_STRUCT: &SomeStruct = &SomeStruct {
| ___________________________________^
LL | |
LL | |
LL | |
... |
LL | |
LL | | };
| |_^ one type is more general than the other
LL | f: &id,
| ^^^ one type is more general than the other
|
= note: expected type `for<'a, 'b> Fn<(&'a Foo<'b>,)>`
found type `Fn<(&Foo<'_>,)>`
error[E0308]: mismatched types
--> $DIR/rfc1623.rs:29:35
--> $DIR/rfc1623.rs:32:8
|
LL | static SOME_STRUCT: &SomeStruct = &SomeStruct {
| ___________________________________^
LL | |
LL | |
LL | |
... |
LL | |
LL | | };
| |_^ one type is more general than the other
LL | f: &id,
| ^^^ one type is more general than the other
|
= note: expected type `for<'a, 'b> Fn<(&'a Foo<'b>,)>`
found type `Fn<(&Foo<'_>,)>`
error: implementation of `FnOnce` is not general enough
--> $DIR/rfc1623.rs:29:35
--> $DIR/rfc1623.rs:32:8
|
LL | static SOME_STRUCT: &SomeStruct = &SomeStruct {
| ___________________________________^
LL | |
LL | |
LL | |
... |
LL | |
LL | | };
| |_^ implementation of `FnOnce` is not general enough
LL | f: &id,
| ^^^ implementation of `FnOnce` is not general enough
|
= note: `fn(&'2 Foo<'_>) -> &'2 Foo<'_> {id::<&'2 Foo<'_>>}` must implement `FnOnce<(&'1 Foo<'b>,)>`, for any lifetime `'1`...
= note: ...but it actually implements `FnOnce<(&'2 Foo<'_>,)>`, for some specific lifetime `'2`
error: implementation of `FnOnce` is not general enough
--> $DIR/rfc1623.rs:29:35
--> $DIR/rfc1623.rs:32:8
|
LL | static SOME_STRUCT: &SomeStruct = &SomeStruct {
| ___________________________________^
LL | |
LL | |
LL | |
... |
LL | |
LL | | };
| |_^ implementation of `FnOnce` is not general enough
LL | f: &id,
| ^^^ implementation of `FnOnce` is not general enough
|
= note: `fn(&Foo<'2>) -> &Foo<'2> {id::<&Foo<'2>>}` must implement `FnOnce<(&'a Foo<'1>,)>`, for any lifetime `'1`...
= note: ...but it actually implements `FnOnce<(&Foo<'2>,)>`, for some specific lifetime `'2`

View file

@ -27,14 +27,14 @@ fn id<T>(t: T) -> T {
}
static SOME_STRUCT: &SomeStruct = &SomeStruct {
//[nll]~^ ERROR mismatched types
//[nll]~| ERROR mismatched types
//[nll]~| ERROR implementation of `FnOnce` is not general enough
//[nll]~| ERROR implementation of `FnOnce` is not general enough
foo: &Foo { bools: &[false, true] },
bar: &Bar { bools: &[true, true] },
f: &id,
//[base]~^ ERROR implementation of `FnOnce` is not general enough
//[nll]~^^ ERROR mismatched types
//[nll]~| ERROR mismatched types
//[nll]~| ERROR implementation of `FnOnce` is not general enough
//[nll]~| ERROR implementation of `FnOnce` is not general enough
};
// very simple test for a 'static static with default lifetime