Skip to content

Commit 0d6bd42

Browse files
committed
make blame_span deterministic
1 parent a118afe commit 0d6bd42

11 files changed

+71
-52
lines changed

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

+39-21
Original file line numberDiff line numberDiff line change
@@ -872,35 +872,50 @@ impl<'tcx> RegionInferenceContext<'tcx> {
872872
// be obvious to the user -- not to mention the naive notion
873873
// of dependencies, which doesn't account for the locations of
874874
// contraints at all. But it will do for now.
875-
for constraint in &self.constraints {
876-
if constraint.sub == fr2 && influenced_fr1[constraint.sup] {
877-
return constraint.span;
878-
}
879-
}
880-
881-
bug!(
882-
"could not find any constraint to blame for {:?}: {:?}",
883-
fr1,
884-
fr2
885-
);
875+
let relevant_constraint = self.constraints
876+
.iter()
877+
.filter_map(|constraint| {
878+
if constraint.sub != fr2 {
879+
None
880+
} else {
881+
influenced_fr1[constraint.sup]
882+
.map(|distance| (distance, constraint.span))
883+
}
884+
})
885+
.min() // constraining fr1 with fewer hops *ought* to be more obvious
886+
.map(|(_dist, span)| span);
887+
888+
relevant_constraint.unwrap_or_else(|| {
889+
bug!(
890+
"could not find any constraint to blame for {:?}: {:?}",
891+
fr1,
892+
fr2
893+
);
894+
})
886895
}
887896

888897
/// Finds all regions whose values `'a` may depend on in some way.
889-
/// Basically if there exists a constraint `'a: 'b @ P`, then `'b`
890-
/// and `dependencies('b)` will be in the final set.
898+
/// For each region, returns either `None` (does not influence
899+
/// `'a`) or `Some(d)` which indicates that it influences `'a`
900+
/// with distinct `d` (minimum number of edges that must be
901+
/// traversed).
891902
///
892903
/// Used during error reporting, extremely naive and inefficient.
893-
fn dependencies(&self, r0: RegionVid) -> IndexVec<RegionVid, bool> {
894-
let mut result_set = IndexVec::from_elem(false, &self.definitions);
904+
fn dependencies(&self, r0: RegionVid) -> IndexVec<RegionVid, Option<usize>> {
905+
let mut result_set = IndexVec::from_elem(None, &self.definitions);
895906
let mut changed = true;
896-
result_set[r0] = true;
907+
result_set[r0] = Some(0); // distance 0 from `r0`
897908

898909
while changed {
899910
changed = false;
900911
for constraint in &self.constraints {
901-
if result_set[constraint.sup] {
902-
if !result_set[constraint.sub] {
903-
result_set[constraint.sub] = true;
912+
if let Some(n) = result_set[constraint.sup] {
913+
let m = n + 1;
914+
if result_set[constraint.sub]
915+
.map(|distance| m < distance)
916+
.unwrap_or(true)
917+
{
918+
result_set[constraint.sub] = Some(m);
904919
changed = true;
905920
}
906921
}
@@ -1049,13 +1064,16 @@ impl<'gcx, 'tcx> ClosureRegionRequirementsExt<'gcx, 'tcx> for ClosureRegionRequi
10491064
value: &T,
10501065
) -> T
10511066
where
1052-
T: TypeFoldable<'tcx>
1067+
T: TypeFoldable<'tcx>,
10531068
{
10541069
infcx.tcx.fold_regions(value, &mut false, |r, _depth| {
10551070
if let ty::ReClosureBound(vid) = r {
10561071
closure_mapping[*vid]
10571072
} else {
1058-
bug!("subst_closure_mapping: encountered non-closure bound free region {:?}", r)
1073+
bug!(
1074+
"subst_closure_mapping: encountered non-closure bound free region {:?}",
1075+
r
1076+
)
10591077
}
10601078
})
10611079
}

src/test/compile-fail/mir_check_cast_closure.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414

1515
fn bar<'a, 'b>() -> fn(&'a u32, &'b u32) -> &'a u32 {
1616
let g: fn(_, _) -> _ = |_x, y| y;
17+
//~^ ERROR free region `'b` does not outlive free region `'a`
1718
g
1819
//~^ WARNING not reporting region error due to -Znll
19-
//~| ERROR free region `'b` does not outlive free region `'a`
2020
}
2121

2222
fn main() {}

src/test/compile-fail/mir_check_cast_reify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ 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`
4849
f(x)
49-
//~^ ERROR free region `'_#1r` does not outlive free region `'static`
5050
}
5151

5252
fn main() {}

src/test/compile-fail/mir_check_cast_unsafe_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ 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`
2021
unsafe { g(input) }
21-
//~^ ERROR free region `'_#1r` does not outlive free region `'static`
2222
}
2323

2424
fn main() {}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ 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`
5758
demand_y(x, y, p)
58-
//~^ ERROR free region `'_#5r` does not outlive free region `'_#6r`
5959
},
6060
);
6161
}

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ warning: not reporting region error due to -Znll
55
| ^^^^^^^
66

77
error: free region `'_#5r` does not outlive free region `'_#6r`
8-
--> $DIR/propagate-approximated-fail-no-postdom.rs:57:25
8+
--> $DIR/propagate-approximated-fail-no-postdom.rs:55:17
99
|
10-
57 | demand_y(x, y, p)
11-
| ^
10+
55 | let p = x.get();
11+
| ^
1212

1313
note: No external requirements
1414
--> $DIR/propagate-approximated-fail-no-postdom.rs:53:9
@@ -17,8 +17,8 @@ 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 | | demand_y(x, y, p)
21-
58 | | //~^ ERROR free region `'_#5r` does not outlive free region `'_#6r`
20+
57 | | //~| ERROR free region `'_#5r` does not outlive free region `'_#6r`
21+
58 | | demand_y(x, y, p)
2222
59 | | },
2323
| |_________^
2424
|

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ note: External requirements
2424
= note: where '_#1r: '_#2r
2525

2626
error: free region `'_#1r` does not outlive free region `'_#2r`
27-
--> $DIR/propagate-approximated-ref.rs:53:38
27+
--> $DIR/propagate-approximated-ref.rs:53:29
2828
|
2929
53 | establish_relationships(&cell_a, &cell_b, |_outlives1, _outlives2, x, y| {
30-
| ^^^^^^^
30+
| ^^^^^^^
3131

3232
note: No external requirements
3333
--> $DIR/propagate-approximated-ref.rs:52:1

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ warning: not reporting region error due to -Znll
55
| ^^^^^^^^^^^^^^^^^^^^^^^
66

77
error: free region `'_#6r` does not outlive free region `'_#4r`
8-
--> $DIR/propagate-approximated-to-empty.rs:41:21
8+
--> $DIR/propagate-approximated-to-empty.rs:41:18
99
|
1010
41 | demand_y(x, y, x.get())
11-
| ^
11+
| ^
1212

1313
note: No external requirements
1414
--> $DIR/propagate-approximated-to-empty.rs:39:47

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ note: External requirements
2424
= note: where '_#1r: '_#2r
2525

2626
error: free region `'_#1r` does not outlive free region `'_#2r`
27-
--> $DIR/propagate-approximated-val.rs:46:37
27+
--> $DIR/propagate-approximated-val.rs:46:29
2828
|
2929
46 | establish_relationships(cell_a, cell_b, |outlives1, outlives2, x, y| {
30-
| ^^^^^^
30+
| ^^^^^^
3131

3232
note: No external requirements
3333
--> $DIR/propagate-approximated-val.rs:45:1

src/test/ui/nll/closure-requirements/propagate-from-trait-match.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ where
4040
T: Trait<'a>,
4141
{
4242
establish_relationships(value, |value| {
43+
//~^ ERROR failed type test
44+
4345
// This function call requires that
4446
//
4547
// (a) T: Trait<'a>
@@ -52,7 +54,6 @@ where
5254

5355
require(value);
5456
//~^ WARNING not reporting region error due to -Znll
55-
//~| ERROR failed type test
5657
});
5758
}
5859

src/test/ui/nll/closure-requirements/propagate-from-trait-match.stderr

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
warning: not reporting region error due to -Znll
2-
--> $DIR/propagate-from-trait-match.rs:53:9
2+
--> $DIR/propagate-from-trait-match.rs:55:9
33
|
4-
53 | require(value);
4+
55 | require(value);
55
| ^^^^^^^
66

77
note: External requirements
88
--> $DIR/propagate-from-trait-match.rs:42:36
99
|
1010
42 | establish_relationships(value, |value| {
1111
| ____________________________________^
12-
43 | | // This function call requires that
13-
44 | | //
14-
45 | | // (a) T: Trait<'a>
12+
43 | | //~^ ERROR failed type test
13+
44 | |
14+
45 | | // This function call requires that
1515
... |
16-
55 | | //~| ERROR failed type test
17-
56 | | });
16+
56 | | //~^ WARNING not reporting region error due to -Znll
17+
57 | | });
1818
| |_____^
1919
|
2020
= note: defining type: DefId(0/1:16 ~ propagate_from_trait_match[317d]::supply[0]::{{closure}}[0]) with closure substs [
@@ -26,17 +26,17 @@ note: External requirements
2626
= note: number of external vids: 2
2727
= note: where T: '_#1r
2828

29-
error: failed type test: TypeTest { generic_kind: T/#1, lower_bound: '_#3r, point: bb0[3], span: $DIR/propagate-from-trait-match.rs:42:36: 56:6, test: IsOutlivedByAnyRegionIn(['_#2r]) }
29+
error: failed type test: TypeTest { generic_kind: T/#1, lower_bound: '_#3r, point: bb0[3], span: $DIR/propagate-from-trait-match.rs:42:36: 57:6, test: IsOutlivedByAnyRegionIn(['_#2r]) }
3030
--> $DIR/propagate-from-trait-match.rs:42:36
3131
|
3232
42 | establish_relationships(value, |value| {
3333
| ____________________________________^
34-
43 | | // This function call requires that
35-
44 | | //
36-
45 | | // (a) T: Trait<'a>
34+
43 | | //~^ ERROR failed type test
35+
44 | |
36+
45 | | // This function call requires that
3737
... |
38-
55 | | //~| ERROR failed type test
39-
56 | | });
38+
56 | | //~^ WARNING not reporting region error due to -Znll
39+
57 | | });
4040
| |_____^
4141

4242
note: No external requirements
@@ -47,8 +47,8 @@ note: No external requirements
4747
40 | | T: Trait<'a>,
4848
41 | | {
4949
... |
50-
56 | | });
51-
57 | | }
50+
57 | | });
51+
58 | | }
5252
| |_^
5353
|
5454
= note: defining type: DefId(0/0:6 ~ propagate_from_trait_match[317d]::supply[0]) with substs [

0 commit comments

Comments
 (0)