-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix for issue #8636 #51894
Merged
Merged
fix for issue #8636 #51894
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/run-pass/borrowck/borrowck-slice-pattern-element-loan.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//compile-flags: -Z borrowck=mir | ||
|
||
#![feature(slice_patterns)] | ||
|
||
fn mut_head_tail<'a, A>(v: &'a mut [A]) -> Option<(&'a mut A, &'a mut [A])> { | ||
match *v { | ||
[ref mut head, ref mut tail..] => { | ||
Some((head, tail)) | ||
} | ||
[] => None | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut v = [1,2,3,4]; | ||
match mut_head_tail(&mut v) { | ||
None => {}, | ||
Some((h,t)) => { | ||
*h = 1000; | ||
t.reverse(); | ||
} | ||
} | ||
} |
137 changes: 137 additions & 0 deletions
137
src/test/ui/borrowck/borrowck-slice-pattern-element-loan.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
//compile-flags: -Z borrowck=mir | ||
|
||
#![feature(slice_patterns)] | ||
|
||
fn nop(_s: &[& i32]) {} | ||
fn nop_subslice(_s: &[i32]) {} | ||
|
||
fn const_index_ok(s: &mut [i32]) { | ||
if let [ref first, ref second, _, ref fourth, ..] = *s { | ||
if let [_, _, ref mut third, ..] = *s { | ||
nop(&[first, second, third, fourth]); | ||
} | ||
} | ||
} | ||
|
||
fn const_index_err(s: &mut [i32]) { | ||
if let [ref first, ref second, ..] = *s { | ||
if let [_, ref mut second2, ref mut third, ..] = *s { //~ERROR | ||
nop(&[first, second, second2, third]); | ||
} | ||
} | ||
} | ||
|
||
fn const_index_from_end_ok(s: &mut [i32]) { | ||
if let [.., ref fourth, ref third, _, ref first] = *s { | ||
if let [.., ref mut second, _] = *s { | ||
nop(&[first, second, third, fourth]); | ||
} | ||
} | ||
} | ||
|
||
fn const_index_from_end_err(s: &mut [i32]) { | ||
if let [.., ref fourth, ref third, _, ref first] = *s { | ||
if let [.., ref mut third2, _, _] = *s { //~ERROR | ||
nop(&[first, third, third2, fourth]); | ||
} | ||
} | ||
} | ||
|
||
fn const_index_mixed(s: &mut [i32]) { | ||
if let [.., _, ref from_end4, ref from_end3, _, ref from_end1] = *s { | ||
if let [ref mut from_begin0, ..] = *s { | ||
nop(&[from_begin0, from_end1, from_end3, from_end4]); | ||
} | ||
if let [_, ref mut from_begin1, ..] = *s { //~ERROR | ||
nop(&[from_begin1, from_end1, from_end3, from_end4]); | ||
} | ||
if let [_, _, ref mut from_begin2, ..] = *s { //~ERROR | ||
nop(&[from_begin2, from_end1, from_end3, from_end4]); | ||
} | ||
if let [_, _, _, ref mut from_begin3, ..] = *s { //~ERROR | ||
nop(&[from_begin3, from_end1, from_end3, from_end4]); | ||
} | ||
} | ||
if let [ref from_begin0, ref from_begin1, _, ref from_begin3, _, ..] = *s { | ||
if let [.., ref mut from_end1] = *s { | ||
nop(&[from_begin0, from_begin1, from_begin3, from_end1]); | ||
} | ||
if let [.., ref mut from_end2, _] = *s { //~ERROR | ||
nop(&[from_begin0, from_begin1, from_begin3, from_end2]); | ||
} | ||
if let [.., ref mut from_end3, _, _] = *s { //~ERROR | ||
nop(&[from_begin0, from_begin1, from_begin3, from_end3]); | ||
} | ||
if let [.., ref mut from_end4, _, _, _] = *s { //~ERROR | ||
nop(&[from_begin0, from_begin1, from_begin3, from_end4]); | ||
} | ||
} | ||
} | ||
|
||
fn const_index_and_subslice_ok(s: &mut [i32]) { | ||
if let [ref first, ref second, ..] = *s { | ||
if let [_, _, ref mut tail..] = *s { | ||
nop(&[first, second]); | ||
nop_subslice(tail); | ||
} | ||
} | ||
} | ||
|
||
fn const_index_and_subslice_err(s: &mut [i32]) { | ||
if let [ref first, ref second, ..] = *s { | ||
if let [_, ref mut tail..] = *s { //~ERROR | ||
nop(&[first, second]); | ||
nop_subslice(tail); | ||
} | ||
} | ||
} | ||
|
||
fn const_index_and_subslice_from_end_ok(s: &mut [i32]) { | ||
if let [.., ref second, ref first] = *s { | ||
if let [ref mut tail.., _, _] = *s { | ||
nop(&[first, second]); | ||
nop_subslice(tail); | ||
} | ||
} | ||
} | ||
|
||
fn const_index_and_subslice_from_end_err(s: &mut [i32]) { | ||
if let [.., ref second, ref first] = *s { | ||
if let [ref mut tail.., _] = *s { //~ERROR | ||
nop(&[first, second]); | ||
nop_subslice(tail); | ||
} | ||
} | ||
} | ||
|
||
fn subslices(s: &mut [i32]) { | ||
if let [_, _, _, ref s1..] = *s { | ||
if let [ref mut s2.., _, _, _] = *s { //~ERROR | ||
nop_subslice(s1); | ||
nop_subslice(s2); | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut v = [1,2,3,4]; | ||
const_index_ok(&mut v); | ||
const_index_err(&mut v); | ||
const_index_from_end_ok(&mut v); | ||
const_index_from_end_err(&mut v); | ||
const_index_and_subslice_ok(&mut v); | ||
const_index_and_subslice_err(&mut v); | ||
const_index_and_subslice_from_end_ok(&mut v); | ||
const_index_and_subslice_from_end_err(&mut v); | ||
subslices(&mut v); | ||
} |
119 changes: 119 additions & 0 deletions
119
src/test/ui/borrowck/borrowck-slice-pattern-element-loan.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:28:20 | ||
| | ||
LL | if let [ref first, ref second, ..] = *s { | ||
| ---------- immutable borrow occurs here | ||
LL | if let [_, ref mut second2, ref mut third, ..] = *s { //~ERROR | ||
| ^^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop(&[first, second, second2, third]); | ||
| ------ borrow later used here | ||
|
||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:44:21 | ||
| | ||
LL | if let [.., ref fourth, ref third, _, ref first] = *s { | ||
| --------- immutable borrow occurs here | ||
LL | if let [.., ref mut third2, _, _] = *s { //~ERROR | ||
| ^^^^^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop(&[first, third, third2, fourth]); | ||
| ----- borrow later used here | ||
|
||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:55:20 | ||
| | ||
LL | if let [.., _, ref from_end4, ref from_end3, _, ref from_end1] = *s { | ||
| ------------- immutable borrow occurs here | ||
... | ||
LL | if let [_, ref mut from_begin1, ..] = *s { //~ERROR | ||
| ^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop(&[from_begin1, from_end1, from_end3, from_end4]); | ||
| --------- borrow later used here | ||
|
||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:58:23 | ||
| | ||
LL | if let [.., _, ref from_end4, ref from_end3, _, ref from_end1] = *s { | ||
| ------------- immutable borrow occurs here | ||
... | ||
LL | if let [_, _, ref mut from_begin2, ..] = *s { //~ERROR | ||
| ^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop(&[from_begin2, from_end1, from_end3, from_end4]); | ||
| --------- borrow later used here | ||
|
||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:61:26 | ||
| | ||
LL | if let [.., _, ref from_end4, ref from_end3, _, ref from_end1] = *s { | ||
| ------------- immutable borrow occurs here | ||
... | ||
LL | if let [_, _, _, ref mut from_begin3, ..] = *s { //~ERROR | ||
| ^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop(&[from_begin3, from_end1, from_end3, from_end4]); | ||
| --------- borrow later used here | ||
|
||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:69:21 | ||
| | ||
LL | if let [ref from_begin0, ref from_begin1, _, ref from_begin3, _, ..] = *s { | ||
| --------------- immutable borrow occurs here | ||
... | ||
LL | if let [.., ref mut from_end2, _] = *s { //~ERROR | ||
| ^^^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop(&[from_begin0, from_begin1, from_begin3, from_end2]); | ||
| ----------- borrow later used here | ||
|
||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:72:21 | ||
| | ||
LL | if let [ref from_begin0, ref from_begin1, _, ref from_begin3, _, ..] = *s { | ||
| --------------- immutable borrow occurs here | ||
... | ||
LL | if let [.., ref mut from_end3, _, _] = *s { //~ERROR | ||
| ^^^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop(&[from_begin0, from_begin1, from_begin3, from_end3]); | ||
| ----------- borrow later used here | ||
|
||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:75:21 | ||
| | ||
LL | if let [ref from_begin0, ref from_begin1, _, ref from_begin3, _, ..] = *s { | ||
| --------------- immutable borrow occurs here | ||
... | ||
LL | if let [.., ref mut from_end4, _, _, _] = *s { //~ERROR | ||
| ^^^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop(&[from_begin0, from_begin1, from_begin3, from_end4]); | ||
| ----------- borrow later used here | ||
|
||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:92:20 | ||
| | ||
LL | if let [ref first, ref second, ..] = *s { | ||
| ---------- immutable borrow occurs here | ||
LL | if let [_, ref mut tail..] = *s { //~ERROR | ||
| ^^^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop(&[first, second]); | ||
| ------ borrow later used here | ||
|
||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:110:17 | ||
| | ||
LL | if let [.., ref second, ref first] = *s { | ||
| ---------- immutable borrow occurs here | ||
LL | if let [ref mut tail.., _] = *s { //~ERROR | ||
| ^^^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop(&[first, second]); | ||
| ------ borrow later used here | ||
|
||
error[E0502]: cannot borrow `s[..]` as mutable because it is also borrowed as immutable | ||
--> $DIR/borrowck-slice-pattern-element-loan.rs:119:17 | ||
| | ||
LL | if let [_, _, _, ref s1..] = *s { | ||
| ------ immutable borrow occurs here | ||
LL | if let [ref mut s2.., _, _, _] = *s { //~ERROR | ||
| ^^^^^^^^^^ mutable borrow occurs here | ||
LL | nop_subslice(s1); | ||
| -- borrow later used here | ||
|
||
error: aborting due to 11 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0502`. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: it is not -1 but rather just 1...right?