Skip to content

Commit 54b3a11

Browse files
committed
connect NLL machinery to the NiceRegionError code
1 parent 23b2883 commit 54b3a11

File tree

43 files changed

+144
-82
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+144
-82
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ mod note;
7878

7979
mod need_type_info;
8080

81-
mod nice_region_error;
81+
pub mod nice_region_error;
8282

8383
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
8484
pub fn note_and_explain_region(self,

src/librustc_mir/borrow_check/nll/region_infer/mod.rs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ use rustc::infer::NLLRegionVariableOrigin;
1515
use rustc::infer::RegionObligation;
1616
use rustc::infer::RegionVariableOrigin;
1717
use rustc::infer::SubregionOrigin;
18+
use rustc::infer::error_reporting::nice_region_error::NiceRegionError;
1819
use rustc::infer::region_constraints::{GenericKind, VarOrigins};
1920
use rustc::mir::{ClosureOutlivesRequirement, ClosureOutlivesSubject, ClosureRegionRequirements,
2021
Local, Location, Mir};
2122
use rustc::traits::ObligationCause;
2223
use rustc::ty::{self, RegionVid, Ty, TypeFoldable};
24+
use rustc::util::common::ErrorReported;
2325
use rustc_data_structures::indexed_vec::IndexVec;
2426
use rustc_errors::DiagnosticBuilder;
2527
use std::fmt;
@@ -230,7 +232,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
230232
/// `num_region_variables` valid inference variables; the first N
231233
/// of those will be constant regions representing the free
232234
/// regions defined in `universal_regions`.
233-
pub fn new(
235+
pub(crate) fn new(
234236
var_origins: VarOrigins,
235237
universal_regions: UniversalRegions<'tcx>,
236238
mir: &Mir<'tcx>,
@@ -430,7 +432,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
430432

431433
self.check_type_tests(infcx, mir, outlives_requirements.as_mut());
432434

433-
self.check_universal_regions(infcx, mir, outlives_requirements.as_mut());
435+
self.check_universal_regions(infcx, mir, mir_def_id, outlives_requirements.as_mut());
434436

435437
let outlives_requirements = outlives_requirements.unwrap_or(vec![]);
436438

@@ -807,6 +809,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
807809
&self,
808810
infcx: &InferCtxt<'_, 'gcx, 'tcx>,
809811
mir: &Mir<'tcx>,
812+
mir_def_id: DefId,
810813
mut propagated_outlives_requirements: Option<&mut Vec<ClosureOutlivesRequirement<'gcx>>>,
811814
) {
812815
// The universal regions are always found in a prefix of the
@@ -819,7 +822,13 @@ impl<'tcx> RegionInferenceContext<'tcx> {
819822
// they did not grow too large, accumulating any requirements
820823
// for our caller into the `outlives_requirements` vector.
821824
for (fr, _) in universal_definitions {
822-
self.check_universal_region(infcx, mir, fr, &mut propagated_outlives_requirements);
825+
self.check_universal_region(
826+
infcx,
827+
mir,
828+
mir_def_id,
829+
fr,
830+
&mut propagated_outlives_requirements,
831+
);
823832
}
824833
}
825834

@@ -835,6 +844,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
835844
&self,
836845
infcx: &InferCtxt<'_, 'gcx, 'tcx>,
837846
mir: &Mir<'tcx>,
847+
mir_def_id: DefId,
838848
longer_fr: RegionVid,
839849
propagated_outlives_requirements: &mut Option<&mut Vec<ClosureOutlivesRequirement<'gcx>>>,
840850
) {
@@ -891,7 +901,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
891901
// Note: in this case, we use the unapproximated regions
892902
// to report the error. This gives better error messages
893903
// in some cases.
894-
self.report_error(infcx, mir, longer_fr, shorter_fr, blame_span);
904+
self.report_error(infcx, mir, mir_def_id, longer_fr, shorter_fr, blame_span);
895905
}
896906
}
897907

@@ -907,18 +917,30 @@ impl<'tcx> RegionInferenceContext<'tcx> {
907917
&self,
908918
infcx: &InferCtxt<'_, '_, 'tcx>,
909919
mir: &Mir<'tcx>,
920+
mir_def_id: DefId,
910921
fr: RegionVid,
911922
outlived_fr: RegionVid,
912923
blame_span: Span,
913924
) {
914925
// Obviously uncool error reporting.
915926

916-
let fr_string = match self.definitions[fr].external_name {
927+
let fr_name = self.definitions[fr].external_name;
928+
let outlived_fr_name = self.definitions[outlived_fr].external_name;
929+
930+
if let (Some(f), Some(o)) = (fr_name, outlived_fr_name) {
931+
let tables = infcx.tcx.typeck_tables_of(mir_def_id);
932+
let nice = NiceRegionError::new(infcx.tcx, blame_span, o, f, Some(tables));
933+
if let Some(ErrorReported) = nice.try_report() {
934+
return;
935+
}
936+
}
937+
938+
let fr_string = match fr_name {
917939
Some(r) => format!("free region `{}`", r),
918940
None => format!("free region `{:?}`", fr),
919941
};
920942

921-
let outlived_fr_string = match self.definitions[outlived_fr].external_name {
943+
let outlived_fr_string = match outlived_fr_name {
922944
Some(r) => format!("free region `{}`", r),
923945
None => format!("free region `{:?}`", outlived_fr),
924946
};

src/librustc_mir/borrow_check/nll/universal_regions.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,16 +445,20 @@ impl<'cx, 'gcx, 'tcx> UniversalRegionsBuilder<'cx, 'gcx, 'tcx> {
445445
let defining_ty = self.defining_ty();
446446
debug!("build: defining_ty={:?}", defining_ty);
447447

448-
let indices = self.compute_indices(fr_static, defining_ty);
448+
let mut indices = self.compute_indices(fr_static, defining_ty);
449449
debug!("build: indices={:?}", indices);
450450

451451
let bound_inputs_and_output = self.compute_inputs_and_output(&indices, defining_ty);
452452

453453
// "Liberate" the late-bound regions. These correspond to
454454
// "local" free regions.
455455
let first_local_index = self.infcx.num_region_vars();
456-
let inputs_and_output = self.infcx
457-
.replace_bound_regions_with_nll_infer_vars(FR, &bound_inputs_and_output);
456+
let inputs_and_output = self.infcx.replace_bound_regions_with_nll_infer_vars(
457+
FR,
458+
self.mir_def_id,
459+
&bound_inputs_and_output,
460+
&mut indices,
461+
);
458462
let fr_fn_body = self.infcx.next_nll_region_var(FR).to_region_vid();
459463
let num_universals = self.infcx.num_region_vars();
460464

@@ -717,7 +721,7 @@ impl UniversalRegionRelations {
717721
}
718722
}
719723

720-
pub(crate) trait InferCtxtExt<'tcx> {
724+
trait InferCtxtExt<'tcx> {
721725
fn replace_free_regions_with_nll_infer_vars<T>(
722726
&self,
723727
origin: NLLRegionVariableOrigin,
@@ -729,7 +733,9 @@ pub(crate) trait InferCtxtExt<'tcx> {
729733
fn replace_bound_regions_with_nll_infer_vars<T>(
730734
&self,
731735
origin: NLLRegionVariableOrigin,
736+
all_outlive_scope: DefId,
732737
value: &ty::Binder<T>,
738+
indices: &mut UniversalRegionIndices<'tcx>,
733739
) -> T
734740
where
735741
T: TypeFoldable<'tcx>;
@@ -752,18 +758,38 @@ impl<'cx, 'gcx, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'cx, 'gcx, 'tcx> {
752758
fn replace_bound_regions_with_nll_infer_vars<T>(
753759
&self,
754760
origin: NLLRegionVariableOrigin,
761+
all_outlive_scope: DefId,
755762
value: &ty::Binder<T>,
763+
indices: &mut UniversalRegionIndices<'tcx>,
756764
) -> T
757765
where
758766
T: TypeFoldable<'tcx>,
759767
{
760-
let (value, _map) = self.tcx
761-
.replace_late_bound_regions(value, |_br| self.next_nll_region_var(origin));
768+
let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| {
769+
let liberated_region = self.tcx.mk_region(ty::ReFree(ty::FreeRegion {
770+
scope: all_outlive_scope,
771+
bound_region: br,
772+
}));
773+
let region_vid = self.next_nll_region_var(origin);
774+
indices.insert_late_bound_region(liberated_region, region_vid.to_region_vid());
775+
region_vid
776+
});
762777
value
763778
}
764779
}
765780

766781
impl<'tcx> UniversalRegionIndices<'tcx> {
782+
/// Initially, the `UniversalRegionIndices` map contains only the
783+
/// early-bound regions in scope. Once that is all setup, we come
784+
/// in later and instantiate the late-bound regions, and then we
785+
/// insert the `ReFree` version of those into the map as
786+
/// well. These are used for error reporting.
787+
fn insert_late_bound_region(&mut self, r: ty::Region<'tcx>,
788+
vid: ty::RegionVid)
789+
{
790+
self.indices.insert(r, vid);
791+
}
792+
767793
/// Converts `r` into a local inference variable: `r` can either
768794
/// by a `ReVar` (i.e., already a reference to an inference
769795
/// variable) or it can be `'static` or some early-bound

src/test/compile-fail/mir_check_cast_reify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn bar<'a>(x: &'a u32) -> &'static u32 {
4545
// as part of checking the `ReifyFnPointer`.
4646
let f: fn(_) -> _ = foo;
4747
//~^ WARNING not reporting region error due to -Znll
48-
//~| ERROR free region `'_#1r` does not outlive free region `'static`
48+
//~| ERROR free region `'a` does not outlive free region `'static`
4949
f(x)
5050
}
5151

src/test/compile-fail/mir_check_cast_unsafe_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn bar<'a>(input: &'a u32, f: fn(&'a u32) -> &'a u32) -> &'static u32 {
1717
// in `g`. These are related via the `UnsafeFnPointer` cast.
1818
let g: unsafe fn(_) -> _ = f;
1919
//~^ WARNING not reporting region error due to -Znll
20-
//~| ERROR free region `'_#1r` does not outlive free region `'static`
20+
//~| ERROR free region `'a` does not outlive free region `'static`
2121
unsafe { g(input) }
2222
}
2323

src/test/compile-fail/mir_check_cast_unsize.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use std::fmt::Debug;
1717

1818
fn bar<'a>(x: &'a u32) -> &'static dyn Debug {
19-
//~^ ERROR free region `'_#1r` does not outlive free region `'static`
19+
//~^ ERROR free region `'a` does not outlive free region `'static`
2020
x
2121
//~^ WARNING not reporting region error due to -Znll
2222
}

src/test/compile-fail/nll/where_clauses_in_functions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ where
2121

2222
fn bar<'a, 'b>(x: &'a u32, y: &'b u32) -> (&'a u32, &'b u32) {
2323
foo(x, y)
24-
//~^ ERROR free region `'_#1r` does not outlive free region `'_#2r`
24+
//~^ ERROR lifetime mismatch [E0623]
2525
//~| WARNING not reporting region error due to -Znll
2626
}
2727

src/test/compile-fail/nll/where_clauses_in_structs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ struct Foo<'a: 'b, 'b> {
2121

2222
fn bar<'a, 'b>(x: Cell<&'a u32>, y: Cell<&'b u32>) {
2323
Foo { x, y };
24-
//~^ ERROR free region `'_#1r` does not outlive free region `'_#2r`
24+
//~^ ERROR lifetime mismatch [E0623]
2525
//~| WARNING not reporting region error due to -Znll
2626
}
2727

src/test/compile-fail/regions-static-bound.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ fn static_id_wrong_way<'a>(t: &'a ()) -> &'static () where 'static: 'a {
2424
fn error(u: &(), v: &()) {
2525
static_id(&u); //[ll]~ ERROR cannot infer an appropriate lifetime
2626
//[nll]~^ WARNING not reporting region error due to -Znll
27-
//[nll]~| ERROR free region `'_#1r` does not outlive free region `'static`
27+
//[nll]~| ERROR free region `` does not outlive free region `'static`
2828
static_id_indirect(&v); //[ll]~ ERROR cannot infer an appropriate lifetime
2929
//[nll]~^ WARNING not reporting region error due to -Znll
30-
//[nll]~| ERROR free region `'_#2r` does not outlive free region `'static`
30+
//[nll]~| ERROR free region `` does not outlive free region `'static`
3131
}
3232

3333
fn main() {}

src/test/ui/nll/closure-requirements/escape-argument-callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn test() {
3434
{
3535
let y = 22;
3636
let mut closure = expect_sig(|p, y| *p = y);
37-
//~^ ERROR free region `'_#4r` does not outlive free region `'_#3r`
37+
//~^ ERROR does not outlive free region
3838
//~| WARNING not reporting region error due to -Znll
3939
closure(&mut p, &y);
4040
}

src/test/ui/nll/closure-requirements/escape-argument-callee.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: not reporting region error due to -Znll
44
36 | let mut closure = expect_sig(|p, y| *p = y);
55
| ^
66

7-
error: free region `'_#4r` does not outlive free region `'_#3r`
7+
error: free region `ReFree(DefId(0/1:9 ~ escape_argument_callee[317d]::test[0]::{{closure}}[0]), BrAnon(3))` does not outlive free region `ReFree(DefId(0/1:9 ~ escape_argument_callee[317d]::test[0]::{{closure}}[0]), BrAnon(2))`
88
--> $DIR/escape-argument-callee.rs:36:45
99
|
1010
36 | let mut closure = expect_sig(|p, y| *p = y);

src/test/ui/nll/closure-requirements/propagate-approximated-fail-no-postdom.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn supply<'a, 'b, 'c>(cell_a: Cell<&'a u32>, cell_b: Cell<&'b u32>, cell_c: Cell
5454
// Only works if 'x: 'y:
5555
let p = x.get();
5656
//~^ WARN not reporting region error due to -Znll
57-
//~| ERROR free region `'_#5r` does not outlive free region `'_#6r`
57+
//~| ERROR does not outlive free region
5858
demand_y(x, y, p)
5959
},
6060
);

src/test/ui/nll/closure-requirements/propagate-approximated-fail-no-postdom.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: not reporting region error due to -Znll
44
55 | let p = x.get();
55
| ^^^^^^^
66

7-
error: free region `'_#5r` does not outlive free region `'_#6r`
7+
error: free region `ReFree(DefId(0/1:20 ~ propagate_approximated_fail_no_postdom[317d]::supply[0]::{{closure}}[0]), BrAnon(1))` does not outlive free region `ReFree(DefId(0/1:20 ~ propagate_approximated_fail_no_postdom[317d]::supply[0]::{{closure}}[0]), BrAnon(2))`
88
--> $DIR/propagate-approximated-fail-no-postdom.rs:55:17
99
|
1010
55 | let p = x.get();
@@ -17,7 +17,7 @@ note: No external requirements
1717
54 | | // Only works if 'x: 'y:
1818
55 | | let p = x.get();
1919
56 | | //~^ WARN not reporting region error due to -Znll
20-
57 | | //~| ERROR free region `'_#5r` does not outlive free region `'_#6r`
20+
57 | | //~| ERROR does not outlive free region
2121
58 | | demand_y(x, y, p)
2222
59 | | },
2323
| |_________^

src/test/ui/nll/closure-requirements/propagate-approximated-ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ fn demand_y<'x, 'y>(_cell_x: &Cell<&'x u32>, _cell_y: &Cell<&'y u32>, _y: &'y u3
5151
#[rustc_regions]
5252
fn supply<'a, 'b>(cell_a: Cell<&'a u32>, cell_b: Cell<&'b u32>) {
5353
establish_relationships(&cell_a, &cell_b, |_outlives1, _outlives2, x, y| {
54-
//~^ ERROR free region `'_#1r` does not outlive free region `'_#2r`
54+
//~^ ERROR lifetime mismatch
5555

5656
// Only works if 'x: 'y:
5757
demand_y(x, y, x.get()) //~ WARNING not reporting region error due to -Znll

src/test/ui/nll/closure-requirements/propagate-approximated-ref.stderr

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: External requirements
99
|
1010
53 | establish_relationships(&cell_a, &cell_b, |_outlives1, _outlives2, x, y| {
1111
| _______________________________________________^
12-
54 | | //~^ ERROR free region `'_#1r` does not outlive free region `'_#2r`
12+
54 | | //~^ ERROR lifetime mismatch
1313
55 | |
1414
56 | | // Only works if 'x: 'y:
1515
57 | | demand_y(x, y, x.get()) //~ WARNING not reporting region error due to -Znll
@@ -23,18 +23,22 @@ note: External requirements
2323
= note: number of external vids: 3
2424
= note: where '_#1r: '_#2r
2525

26-
error: free region `'_#1r` does not outlive free region `'_#2r`
26+
error[E0623]: lifetime mismatch
2727
--> $DIR/propagate-approximated-ref.rs:53:29
2828
|
29+
52 | fn supply<'a, 'b>(cell_a: Cell<&'a u32>, cell_b: Cell<&'b u32>) {
30+
| ------- -------
31+
| |
32+
| these two types are declared with different lifetimes...
2933
53 | establish_relationships(&cell_a, &cell_b, |_outlives1, _outlives2, x, y| {
30-
| ^^^^^^^
34+
| ^^^^^^^ ...but data from `cell_a` flows into `cell_b` here
3135

3236
note: No external requirements
3337
--> $DIR/propagate-approximated-ref.rs:52:1
3438
|
3539
52 | / fn supply<'a, 'b>(cell_a: Cell<&'a u32>, cell_b: Cell<&'b u32>) {
3640
53 | | establish_relationships(&cell_a, &cell_b, |_outlives1, _outlives2, x, y| {
37-
54 | | //~^ ERROR free region `'_#1r` does not outlive free region `'_#2r`
41+
54 | | //~^ ERROR lifetime mismatch
3842
55 | |
3943
... |
4044
58 | | });

src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-comparing-against-free.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn case1() {
3131
foo(cell, |cell_a, cell_x| {
3232
//~^ WARNING not reporting region error due to -Znll
3333
cell_a.set(cell_x.get()); // forces 'x: 'a, error in closure
34-
//~^ ERROR free region `'_#2r` does not outlive free region `'_#1r`
34+
//~^ ERROR does not outlive free region
3535
})
3636
}
3737

src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-comparing-against-free.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ warning: not reporting region error due to -Znll
44
31 | foo(cell, |cell_a, cell_x| {
55
| ^^^
66

7-
error: free region `'_#2r` does not outlive free region `'_#1r`
7+
error: free region `ReFree(DefId(0/1:12 ~ propagate_approximated_shorter_to_static_comparing_against_free[317d]::case1[0]::{{closure}}[0]), BrAnon(1))` does not outlive free region `'_#1r`
88
--> $DIR/propagate-approximated-shorter-to-static-comparing-against-free.rs:33:9
99
|
1010
33 | cell_a.set(cell_x.get()); // forces 'x: 'a, error in closure
@@ -17,7 +17,7 @@ note: No external requirements
1717
| _______________^
1818
32 | | //~^ WARNING not reporting region error due to -Znll
1919
33 | | cell_a.set(cell_x.get()); // forces 'x: 'a, error in closure
20-
34 | | //~^ ERROR free region `'_#2r` does not outlive free region `'_#1r`
20+
34 | | //~^ ERROR does not outlive free region
2121
35 | | })
2222
| |_____^
2323
|

src/test/ui/nll/closure-requirements/propagate-approximated-shorter-to-static-no-bound.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn demand_y<'x, 'y>(_cell_x: &Cell<&'x u32>, _cell_y: &Cell<&'y u32>, _y: &'y u3
4343
#[rustc_regions]
4444
fn supply<'a, 'b>(cell_a: Cell<&'a u32>, cell_b: Cell<&'b u32>) {
4545
establish_relationships(&cell_a, &cell_b, |_outlives, x, y| {
46-
//~^ ERROR free region `'_#1r` does not outlive free region `ReStatic`
46+
//~^ ERROR does not outlive free region
4747

4848
// Only works if 'x: 'y:
4949
demand_y(x, y, x.get()) //~ WARNING not reporting region error due to -Znll

0 commit comments

Comments
 (0)