Skip to content

Commit f247a3e

Browse files
committed
fix for issue #8636
1 parent 6fc21e5 commit f247a3e

File tree

4 files changed

+356
-16
lines changed

4 files changed

+356
-16
lines changed

src/librustc_mir/borrow_check/places_conflict.rs

+67-16
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc::hir;
1515
use rustc::mir::{Mir, Place};
1616
use rustc::mir::{Projection, ProjectionElem};
1717
use rustc::ty::{self, TyCtxt};
18+
use std::cmp::max;
1819

1920
pub(super) fn places_conflict<'gcx, 'tcx>(
2021
tcx: TyCtxt<'_, 'gcx, 'tcx>,
@@ -394,27 +395,77 @@ fn place_element_conflict<'a, 'gcx: 'tcx, 'tcx>(
394395
| (ProjectionElem::Index(..), ProjectionElem::ConstantIndex { .. })
395396
| (ProjectionElem::Index(..), ProjectionElem::Subslice { .. })
396397
| (ProjectionElem::ConstantIndex { .. }, ProjectionElem::Index(..))
397-
| (ProjectionElem::ConstantIndex { .. }, ProjectionElem::ConstantIndex { .. })
398-
| (ProjectionElem::ConstantIndex { .. }, ProjectionElem::Subslice { .. })
399-
| (ProjectionElem::Subslice { .. }, ProjectionElem::Index(..))
400-
| (ProjectionElem::Subslice { .. }, ProjectionElem::ConstantIndex { .. })
401-
| (ProjectionElem::Subslice { .. }, ProjectionElem::Subslice { .. }) => {
398+
| (ProjectionElem::Subslice { .. }, ProjectionElem::Index(..)) => {
402399
// Array indexes (`a[0]` vs. `a[i]`). These can either be disjoint
403400
// (if the indexes differ) or equal (if they are the same), so this
404401
// is the recursive case that gives "equal *or* disjoint" its meaning.
405-
//
406-
// Note that by construction, MIR at borrowck can't subdivide
407-
// `Subslice` accesses (e.g. `a[2..3][i]` will never be present) - they
408-
// are only present in slice patterns, and we "merge together" nested
409-
// slice patterns. That means we don't have to think about these. It's
410-
// probably a good idea to assert this somewhere, but I'm too lazy.
411-
//
412-
// FIXME(#8636) we might want to return Disjoint if
413-
// both projections are constant and disjoint.
414-
debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY");
402+
debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-INDEX");
415403
Overlap::EqualOrDisjoint
416404
}
417-
405+
(ProjectionElem::ConstantIndex { offset: o1, min_length: _, from_end: false },
406+
ProjectionElem::ConstantIndex { offset: o2, min_length: _, from_end: false })
407+
| (ProjectionElem::ConstantIndex { offset: o1, min_length: _, from_end: true },
408+
ProjectionElem::ConstantIndex {
409+
offset: o2, min_length: _, from_end: true }) => {
410+
if o1 == o2 {
411+
debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX");
412+
Overlap::EqualOrDisjoint
413+
} else {
414+
debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX");
415+
Overlap::Disjoint
416+
}
417+
}
418+
(ProjectionElem::ConstantIndex {
419+
offset: offset_from_begin, min_length: min_length1, from_end: false },
420+
ProjectionElem::ConstantIndex {
421+
offset: offset_from_end, min_length: min_length2, from_end: true })
422+
| (ProjectionElem::ConstantIndex {
423+
offset: offset_from_end, min_length: min_length1, from_end: true },
424+
ProjectionElem::ConstantIndex {
425+
offset: offset_from_begin, min_length: min_length2, from_end: false }) => {
426+
// both patterns matched so it must be at least the greater of the two
427+
let min_length = max(min_length1, min_length2);
428+
// offset_from_end can be in range [1..min_length], -1 for last and min_length
429+
// for first, min_length - offset_from_end gives minimal possible offset from
430+
// the beginning
431+
if *offset_from_begin >= min_length - offset_from_end {
432+
debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX-FE");
433+
Overlap::EqualOrDisjoint
434+
} else {
435+
debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX-FE");
436+
Overlap::Disjoint
437+
}
438+
}
439+
(ProjectionElem::ConstantIndex { offset, min_length: _, from_end: false },
440+
ProjectionElem::Subslice {from, .. })
441+
| (ProjectionElem::Subslice {from, .. },
442+
ProjectionElem::ConstantIndex { offset, min_length: _, from_end: false }) => {
443+
if offset >= from {
444+
debug!(
445+
"place_element_conflict: DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX-SUBSLICE");
446+
Overlap::EqualOrDisjoint
447+
} else {
448+
debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX-SUBSLICE");
449+
Overlap::Disjoint
450+
}
451+
}
452+
(ProjectionElem::ConstantIndex { offset, min_length: _, from_end: true },
453+
ProjectionElem::Subslice {from: _, to })
454+
| (ProjectionElem::Subslice {from: _, to },
455+
ProjectionElem::ConstantIndex { offset, min_length: _, from_end: true }) => {
456+
if offset > to {
457+
debug!("place_element_conflict: \
458+
DISJOINT-OR-EQ-ARRAY-CONSTANT-INDEX-SUBSLICE-FE");
459+
Overlap::EqualOrDisjoint
460+
} else {
461+
debug!("place_element_conflict: DISJOINT-ARRAY-CONSTANT-INDEX-SUBSLICE-FE");
462+
Overlap::Disjoint
463+
}
464+
}
465+
(ProjectionElem::Subslice { .. }, ProjectionElem::Subslice { .. }) => {
466+
debug!("place_element_conflict: DISJOINT-OR-EQ-ARRAY-SUBSLICES");
467+
Overlap::EqualOrDisjoint
468+
}
418469
(ProjectionElem::Deref, _)
419470
| (ProjectionElem::Field(..), _)
420471
| (ProjectionElem::Index(..), _)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//compile-flags: -Z borrowck=mir
12+
13+
#![feature(slice_patterns)]
14+
15+
fn mut_head_tail<'a, A>(v: &'a mut [A]) -> Option<(&'a mut A, &'a mut [A])> {
16+
match *v {
17+
[ref mut head, ref mut tail..] => {
18+
Some((head, tail))
19+
}
20+
[] => None
21+
}
22+
}
23+
24+
fn main() {
25+
let mut v = [1,2,3,4];
26+
match mut_head_tail(&mut v) {
27+
None => {},
28+
Some((h,t)) => {
29+
*h = 1000;
30+
t.reverse();
31+
}
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//compile-flags: -Z borrowck=mir
12+
13+
#![feature(slice_patterns)]
14+
15+
fn nop(_s: &[& i32]) {}
16+
fn nop_subslice(_s: &[i32]) {}
17+
18+
fn const_index_ok(s: &mut [i32]) {
19+
if let [ref first, ref second, _, ref fourth, ..] = *s {
20+
if let [_, _, ref mut third, ..] = *s {
21+
nop(&[first, second, third, fourth]);
22+
}
23+
}
24+
}
25+
26+
fn const_index_err(s: &mut [i32]) {
27+
if let [ref first, ref second, ..] = *s {
28+
if let [_, ref mut second2, ref mut third, ..] = *s { //~ERROR
29+
nop(&[first, second, second2, third]);
30+
}
31+
}
32+
}
33+
34+
fn const_index_from_end_ok(s: &mut [i32]) {
35+
if let [.., ref fourth, ref third, _, ref first] = *s {
36+
if let [.., ref mut second, _] = *s {
37+
nop(&[first, second, third, fourth]);
38+
}
39+
}
40+
}
41+
42+
fn const_index_from_end_err(s: &mut [i32]) {
43+
if let [.., ref fourth, ref third, _, ref first] = *s {
44+
if let [.., ref mut third2, _, _] = *s { //~ERROR
45+
nop(&[first, third, third2, fourth]);
46+
}
47+
}
48+
}
49+
50+
fn const_index_mixed(s: &mut [i32]) {
51+
if let [.., _, ref from_end4, ref from_end3, _, ref from_end1] = *s {
52+
if let [ref mut from_begin0, ..] = *s {
53+
nop(&[from_begin0, from_end1, from_end3, from_end4]);
54+
}
55+
if let [_, ref mut from_begin1, ..] = *s { //~ERROR
56+
nop(&[from_begin1, from_end1, from_end3, from_end4]);
57+
}
58+
if let [_, _, ref mut from_begin2, ..] = *s { //~ERROR
59+
nop(&[from_begin2, from_end1, from_end3, from_end4]);
60+
}
61+
if let [_, _, _, ref mut from_begin3, ..] = *s { //~ERROR
62+
nop(&[from_begin3, from_end1, from_end3, from_end4]);
63+
}
64+
}
65+
if let [ref from_begin0, ref from_begin1, _, ref from_begin3, _, ..] = *s {
66+
if let [.., ref mut from_end1] = *s {
67+
nop(&[from_begin0, from_begin1, from_begin3, from_end1]);
68+
}
69+
if let [.., ref mut from_end2, _] = *s { //~ERROR
70+
nop(&[from_begin0, from_begin1, from_begin3, from_end2]);
71+
}
72+
if let [.., ref mut from_end3, _, _] = *s { //~ERROR
73+
nop(&[from_begin0, from_begin1, from_begin3, from_end3]);
74+
}
75+
if let [.., ref mut from_end4, _, _, _] = *s { //~ERROR
76+
nop(&[from_begin0, from_begin1, from_begin3, from_end4]);
77+
}
78+
}
79+
}
80+
81+
fn const_index_and_subslice_ok(s: &mut [i32]) {
82+
if let [ref first, ref second, ..] = *s {
83+
if let [_, _, ref mut tail..] = *s {
84+
nop(&[first, second]);
85+
nop_subslice(tail);
86+
}
87+
}
88+
}
89+
90+
fn const_index_and_subslice_err(s: &mut [i32]) {
91+
if let [ref first, ref second, ..] = *s {
92+
if let [_, ref mut tail..] = *s { //~ERROR
93+
nop(&[first, second]);
94+
nop_subslice(tail);
95+
}
96+
}
97+
}
98+
99+
fn const_index_and_subslice_from_end_ok(s: &mut [i32]) {
100+
if let [.., ref second, ref first] = *s {
101+
if let [ref mut tail.., _, _] = *s {
102+
nop(&[first, second]);
103+
nop_subslice(tail);
104+
}
105+
}
106+
}
107+
108+
fn const_index_and_subslice_from_end_err(s: &mut [i32]) {
109+
if let [.., ref second, ref first] = *s {
110+
if let [ref mut tail.., _] = *s { //~ERROR
111+
nop(&[first, second]);
112+
nop_subslice(tail);
113+
}
114+
}
115+
}
116+
117+
fn subslices(s: &mut [i32]) {
118+
if let [_, _, _, ref s1..] = *s {
119+
if let [ref mut s2.., _, _, _] = *s { //~ERROR
120+
nop_subslice(s1);
121+
nop_subslice(s2);
122+
}
123+
}
124+
}
125+
126+
fn main() {
127+
let mut v = [1,2,3,4];
128+
const_index_ok(&mut v);
129+
const_index_err(&mut v);
130+
const_index_from_end_ok(&mut v);
131+
const_index_from_end_err(&mut v);
132+
const_index_and_subslice_ok(&mut v);
133+
const_index_and_subslice_err(&mut v);
134+
const_index_and_subslice_from_end_ok(&mut v);
135+
const_index_and_subslice_from_end_err(&mut v);
136+
subslices(&mut v);
137+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
2+
--> $DIR/borrowck-slice-pattern-element-loan.rs:28:20
3+
|
4+
LL | if let [ref first, ref second, ..] = *s {
5+
| ---------- immutable borrow occurs here
6+
LL | if let [_, ref mut second2, ref mut third, ..] = *s { //~ERROR
7+
| ^^^^^^^^^^^^^^^^ mutable borrow occurs here
8+
LL | nop(&[first, second, second2, third]);
9+
| ------ borrow later used here
10+
11+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
12+
--> $DIR/borrowck-slice-pattern-element-loan.rs:44:21
13+
|
14+
LL | if let [.., ref fourth, ref third, _, ref first] = *s {
15+
| --------- immutable borrow occurs here
16+
LL | if let [.., ref mut third2, _, _] = *s { //~ERROR
17+
| ^^^^^^^^^^^^^^ mutable borrow occurs here
18+
LL | nop(&[first, third, third2, fourth]);
19+
| ----- borrow later used here
20+
21+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
22+
--> $DIR/borrowck-slice-pattern-element-loan.rs:55:20
23+
|
24+
LL | if let [.., _, ref from_end4, ref from_end3, _, ref from_end1] = *s {
25+
| ------------- immutable borrow occurs here
26+
...
27+
LL | if let [_, ref mut from_begin1, ..] = *s { //~ERROR
28+
| ^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
29+
LL | nop(&[from_begin1, from_end1, from_end3, from_end4]);
30+
| --------- borrow later used here
31+
32+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
33+
--> $DIR/borrowck-slice-pattern-element-loan.rs:58:23
34+
|
35+
LL | if let [.., _, ref from_end4, ref from_end3, _, ref from_end1] = *s {
36+
| ------------- immutable borrow occurs here
37+
...
38+
LL | if let [_, _, ref mut from_begin2, ..] = *s { //~ERROR
39+
| ^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
40+
LL | nop(&[from_begin2, from_end1, from_end3, from_end4]);
41+
| --------- borrow later used here
42+
43+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
44+
--> $DIR/borrowck-slice-pattern-element-loan.rs:61:26
45+
|
46+
LL | if let [.., _, ref from_end4, ref from_end3, _, ref from_end1] = *s {
47+
| ------------- immutable borrow occurs here
48+
...
49+
LL | if let [_, _, _, ref mut from_begin3, ..] = *s { //~ERROR
50+
| ^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here
51+
LL | nop(&[from_begin3, from_end1, from_end3, from_end4]);
52+
| --------- borrow later used here
53+
54+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
55+
--> $DIR/borrowck-slice-pattern-element-loan.rs:69:21
56+
|
57+
LL | if let [ref from_begin0, ref from_begin1, _, ref from_begin3, _, ..] = *s {
58+
| --------------- immutable borrow occurs here
59+
...
60+
LL | if let [.., ref mut from_end2, _] = *s { //~ERROR
61+
| ^^^^^^^^^^^^^^^^^ mutable borrow occurs here
62+
LL | nop(&[from_begin0, from_begin1, from_begin3, from_end2]);
63+
| ----------- borrow later used here
64+
65+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
66+
--> $DIR/borrowck-slice-pattern-element-loan.rs:72:21
67+
|
68+
LL | if let [ref from_begin0, ref from_begin1, _, ref from_begin3, _, ..] = *s {
69+
| --------------- immutable borrow occurs here
70+
...
71+
LL | if let [.., ref mut from_end3, _, _] = *s { //~ERROR
72+
| ^^^^^^^^^^^^^^^^^ mutable borrow occurs here
73+
LL | nop(&[from_begin0, from_begin1, from_begin3, from_end3]);
74+
| ----------- borrow later used here
75+
76+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
77+
--> $DIR/borrowck-slice-pattern-element-loan.rs:75:21
78+
|
79+
LL | if let [ref from_begin0, ref from_begin1, _, ref from_begin3, _, ..] = *s {
80+
| --------------- immutable borrow occurs here
81+
...
82+
LL | if let [.., ref mut from_end4, _, _, _] = *s { //~ERROR
83+
| ^^^^^^^^^^^^^^^^^ mutable borrow occurs here
84+
LL | nop(&[from_begin0, from_begin1, from_begin3, from_end4]);
85+
| ----------- borrow later used here
86+
87+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
88+
--> $DIR/borrowck-slice-pattern-element-loan.rs:92:20
89+
|
90+
LL | if let [ref first, ref second, ..] = *s {
91+
| ---------- immutable borrow occurs here
92+
LL | if let [_, ref mut tail..] = *s { //~ERROR
93+
| ^^^^^^^^^^^^ mutable borrow occurs here
94+
LL | nop(&[first, second]);
95+
| ------ borrow later used here
96+
97+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
98+
--> $DIR/borrowck-slice-pattern-element-loan.rs:110:17
99+
|
100+
LL | if let [.., ref second, ref first] = *s {
101+
| ---------- immutable borrow occurs here
102+
LL | if let [ref mut tail.., _] = *s { //~ERROR
103+
| ^^^^^^^^^^^^ mutable borrow occurs here
104+
LL | nop(&[first, second]);
105+
| ------ borrow later used here
106+
107+
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable
108+
--> $DIR/borrowck-slice-pattern-element-loan.rs:119:17
109+
|
110+
LL | if let [_, _, _, ref s1..] = *s {
111+
| ------ immutable borrow occurs here
112+
LL | if let [ref mut s2.., _, _, _] = *s { //~ERROR
113+
| ^^^^^^^^^^ mutable borrow occurs here
114+
LL | nop_subslice(s1);
115+
| -- borrow later used here
116+
117+
error: aborting due to 11 previous errors
118+
119+
For more information about this error, try `rustc --explain E0502`.

0 commit comments

Comments
 (0)