2017-10-02 12:39:43 -05:00
|
|
|
pub struct Foo {
|
|
|
|
x: u32
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Bar(u32);
|
|
|
|
|
|
|
|
pub enum Baz {
|
|
|
|
X(u32)
|
|
|
|
}
|
|
|
|
|
|
|
|
union U {
|
|
|
|
a: u8,
|
|
|
|
b: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn x(&mut self) -> &mut u32 { &mut self.x }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Bar {
|
|
|
|
fn x(&mut self) -> &mut u32 { &mut self.0 }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Baz {
|
|
|
|
fn x(&mut self) -> &mut u32 {
|
|
|
|
match *self {
|
|
|
|
Baz::X(ref mut value) => value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
// Local and field from struct
|
|
|
|
{
|
|
|
|
let mut f = Foo { x: 22 };
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = f.x();
|
2019-04-22 02:40:08 -05:00
|
|
|
f.x; //~ ERROR cannot use `f.x` because it was mutably borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|
|
|
|
// Local and field from tuple-struct
|
|
|
|
{
|
|
|
|
let mut g = Bar(22);
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = g.x();
|
2019-04-22 02:40:08 -05:00
|
|
|
g.0; //~ ERROR cannot use `g.0` because it was mutably borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|
|
|
|
// Local and field from tuple
|
|
|
|
{
|
|
|
|
let mut h = (22, 23);
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = &mut h.0;
|
2019-04-22 02:40:08 -05:00
|
|
|
h.0; //~ ERROR cannot use `h.0` because it was mutably borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|
|
|
|
// Local and field from enum
|
|
|
|
{
|
|
|
|
let mut e = Baz::X(2);
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = e.x();
|
2018-09-13 16:04:09 -05:00
|
|
|
match e {
|
2019-04-22 02:40:08 -05:00
|
|
|
Baz::X(value) => value //~ ERROR cannot use `e.0` because it was mutably borrowed
|
2017-10-02 12:39:43 -05:00
|
|
|
};
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|
|
|
|
// Local and field from union
|
|
|
|
unsafe {
|
|
|
|
let mut u = U { b: 0 };
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = &mut u.a;
|
2019-04-22 02:40:08 -05:00
|
|
|
u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|
|
|
|
// Deref and field from struct
|
|
|
|
{
|
|
|
|
let mut f = Box::new(Foo { x: 22 });
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = f.x();
|
2019-04-22 02:40:08 -05:00
|
|
|
f.x; //~ ERROR cannot use `f.x` because it was mutably borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|
|
|
|
// Deref and field from tuple-struct
|
|
|
|
{
|
|
|
|
let mut g = Box::new(Bar(22));
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = g.x();
|
2019-04-22 02:40:08 -05:00
|
|
|
g.0; //~ ERROR cannot use `g.0` because it was mutably borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|
|
|
|
// Deref and field from tuple
|
|
|
|
{
|
|
|
|
let mut h = Box::new((22, 23));
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = &mut h.0;
|
2019-04-22 02:40:08 -05:00
|
|
|
h.0; //~ ERROR cannot use `h.0` because it was mutably borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|
|
|
|
// Deref and field from enum
|
|
|
|
{
|
|
|
|
let mut e = Box::new(Baz::X(3));
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = e.x();
|
2018-09-13 16:04:09 -05:00
|
|
|
match *e {
|
2017-10-02 12:39:43 -05:00
|
|
|
Baz::X(value) => value
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot use `e.0` because it was mutably borrowed
|
2017-10-02 12:39:43 -05:00
|
|
|
};
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|
|
|
|
// Deref and field from union
|
|
|
|
unsafe {
|
|
|
|
let mut u = Box::new(U { b: 0 });
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = &mut u.a;
|
2019-04-22 02:40:08 -05:00
|
|
|
u.a; //~ ERROR cannot use `u.a` because it was mutably borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|
mir-borrowck: Autoderef values followed by a constant index, and fix reported lvalue for constant index
Previously the constant index was reported as `[x of y]` or `[-x of y]` where
`x` was the offset and `y` the minimum length of the slice. The minus sign
wasn't in the right case since for `&[_, x, .., _, _]`, the error reported was
`[-1 of 4]`, and for `&[_, _, .., x, _]`, the error reported was `[2 of 4]`.
This commit fixes the sign so that the indexes 1 and -2 are reported, and
remove the ` of y` part of the message to make it more succinct.
2017-10-05 04:03:32 -05:00
|
|
|
// Constant index
|
|
|
|
{
|
|
|
|
let mut v = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = &mut v;
|
2018-09-13 16:04:09 -05:00
|
|
|
match v {
|
mir-borrowck: Autoderef values followed by a constant index, and fix reported lvalue for constant index
Previously the constant index was reported as `[x of y]` or `[-x of y]` where
`x` was the offset and `y` the minimum length of the slice. The minus sign
wasn't in the right case since for `&[_, x, .., _, _]`, the error reported was
`[-1 of 4]`, and for `&[_, _, .., x, _]`, the error reported was `[2 of 4]`.
This commit fixes the sign so that the indexes 1 and -2 are reported, and
remove the ` of y` part of the message to make it more succinct.
2017-10-05 04:03:32 -05:00
|
|
|
&[x, _, .., _, _] => println!("{}", x),
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot use `v[..]` because it was mutably borrowed
|
mir-borrowck: Autoderef values followed by a constant index, and fix reported lvalue for constant index
Previously the constant index was reported as `[x of y]` or `[-x of y]` where
`x` was the offset and `y` the minimum length of the slice. The minus sign
wasn't in the right case since for `&[_, x, .., _, _]`, the error reported was
`[-1 of 4]`, and for `&[_, _, .., x, _]`, the error reported was `[2 of 4]`.
This commit fixes the sign so that the indexes 1 and -2 are reported, and
remove the ` of y` part of the message to make it more succinct.
2017-10-05 04:03:32 -05:00
|
|
|
_ => panic!("other case"),
|
|
|
|
}
|
2018-09-13 16:04:09 -05:00
|
|
|
match v {
|
mir-borrowck: Autoderef values followed by a constant index, and fix reported lvalue for constant index
Previously the constant index was reported as `[x of y]` or `[-x of y]` where
`x` was the offset and `y` the minimum length of the slice. The minus sign
wasn't in the right case since for `&[_, x, .., _, _]`, the error reported was
`[-1 of 4]`, and for `&[_, _, .., x, _]`, the error reported was `[2 of 4]`.
This commit fixes the sign so that the indexes 1 and -2 are reported, and
remove the ` of y` part of the message to make it more succinct.
2017-10-05 04:03:32 -05:00
|
|
|
&[_, x, .., _, _] => println!("{}", x),
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot use `v[..]` because it was mutably borrowed
|
mir-borrowck: Autoderef values followed by a constant index, and fix reported lvalue for constant index
Previously the constant index was reported as `[x of y]` or `[-x of y]` where
`x` was the offset and `y` the minimum length of the slice. The minus sign
wasn't in the right case since for `&[_, x, .., _, _]`, the error reported was
`[-1 of 4]`, and for `&[_, _, .., x, _]`, the error reported was `[2 of 4]`.
This commit fixes the sign so that the indexes 1 and -2 are reported, and
remove the ` of y` part of the message to make it more succinct.
2017-10-05 04:03:32 -05:00
|
|
|
_ => panic!("other case"),
|
|
|
|
}
|
2018-09-13 16:04:09 -05:00
|
|
|
match v {
|
mir-borrowck: Autoderef values followed by a constant index, and fix reported lvalue for constant index
Previously the constant index was reported as `[x of y]` or `[-x of y]` where
`x` was the offset and `y` the minimum length of the slice. The minus sign
wasn't in the right case since for `&[_, x, .., _, _]`, the error reported was
`[-1 of 4]`, and for `&[_, _, .., x, _]`, the error reported was `[2 of 4]`.
This commit fixes the sign so that the indexes 1 and -2 are reported, and
remove the ` of y` part of the message to make it more succinct.
2017-10-05 04:03:32 -05:00
|
|
|
&[_, _, .., x, _] => println!("{}", x),
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot use `v[..]` because it was mutably borrowed
|
mir-borrowck: Autoderef values followed by a constant index, and fix reported lvalue for constant index
Previously the constant index was reported as `[x of y]` or `[-x of y]` where
`x` was the offset and `y` the minimum length of the slice. The minus sign
wasn't in the right case since for `&[_, x, .., _, _]`, the error reported was
`[-1 of 4]`, and for `&[_, _, .., x, _]`, the error reported was `[2 of 4]`.
This commit fixes the sign so that the indexes 1 and -2 are reported, and
remove the ` of y` part of the message to make it more succinct.
2017-10-05 04:03:32 -05:00
|
|
|
_ => panic!("other case"),
|
|
|
|
}
|
2018-09-13 16:04:09 -05:00
|
|
|
match v {
|
mir-borrowck: Autoderef values followed by a constant index, and fix reported lvalue for constant index
Previously the constant index was reported as `[x of y]` or `[-x of y]` where
`x` was the offset and `y` the minimum length of the slice. The minus sign
wasn't in the right case since for `&[_, x, .., _, _]`, the error reported was
`[-1 of 4]`, and for `&[_, _, .., x, _]`, the error reported was `[2 of 4]`.
This commit fixes the sign so that the indexes 1 and -2 are reported, and
remove the ` of y` part of the message to make it more succinct.
2017-10-05 04:03:32 -05:00
|
|
|
&[_, _, .., _, x] => println!("{}", x),
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot use `v[..]` because it was mutably borrowed
|
mir-borrowck: Autoderef values followed by a constant index, and fix reported lvalue for constant index
Previously the constant index was reported as `[x of y]` or `[-x of y]` where
`x` was the offset and `y` the minimum length of the slice. The minus sign
wasn't in the right case since for `&[_, x, .., _, _]`, the error reported was
`[-1 of 4]`, and for `&[_, _, .., x, _]`, the error reported was `[2 of 4]`.
This commit fixes the sign so that the indexes 1 and -2 are reported, and
remove the ` of y` part of the message to make it more succinct.
2017-10-05 04:03:32 -05:00
|
|
|
_ => panic!("other case"),
|
|
|
|
}
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
mir-borrowck: Autoderef values followed by a constant index, and fix reported lvalue for constant index
Previously the constant index was reported as `[x of y]` or `[-x of y]` where
`x` was the offset and `y` the minimum length of the slice. The minus sign
wasn't in the right case since for `&[_, x, .., _, _]`, the error reported was
`[-1 of 4]`, and for `&[_, _, .., x, _]`, the error reported was `[2 of 4]`.
This commit fixes the sign so that the indexes 1 and -2 are reported, and
remove the ` of y` part of the message to make it more succinct.
2017-10-05 04:03:32 -05:00
|
|
|
}
|
2017-10-06 10:21:06 -05:00
|
|
|
// Subslices
|
|
|
|
{
|
|
|
|
let mut v = &[1, 2, 3, 4, 5];
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = &mut v;
|
2018-09-13 16:04:09 -05:00
|
|
|
match v {
|
2019-07-07 18:47:46 -05:00
|
|
|
&[x @ ..] => println!("{:?}", x),
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot use `v[..]` because it was mutably borrowed
|
2017-10-06 10:21:06 -05:00
|
|
|
_ => panic!("other case"),
|
|
|
|
}
|
2018-09-13 16:04:09 -05:00
|
|
|
match v {
|
2019-07-07 18:47:46 -05:00
|
|
|
&[_, x @ ..] => println!("{:?}", x),
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot use `v[..]` because it was mutably borrowed
|
2017-10-06 10:21:06 -05:00
|
|
|
_ => panic!("other case"),
|
|
|
|
}
|
2018-09-13 16:04:09 -05:00
|
|
|
match v {
|
2019-07-07 18:47:46 -05:00
|
|
|
&[x @ .., _] => println!("{:?}", x),
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot use `v[..]` because it was mutably borrowed
|
2017-10-06 10:21:06 -05:00
|
|
|
_ => panic!("other case"),
|
|
|
|
}
|
2018-09-13 16:04:09 -05:00
|
|
|
match v {
|
2019-07-07 18:47:46 -05:00
|
|
|
&[_, x @ .., _] => println!("{:?}", x),
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot use `v[..]` because it was mutably borrowed
|
2017-10-06 10:21:06 -05:00
|
|
|
_ => panic!("other case"),
|
|
|
|
}
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-06 10:21:06 -05:00
|
|
|
}
|
2017-10-06 10:24:23 -05:00
|
|
|
// Downcasted field
|
|
|
|
{
|
|
|
|
enum E<X> { A(X), B { x: X } }
|
|
|
|
|
|
|
|
let mut e = E::A(3);
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = &mut e;
|
2018-09-13 16:04:09 -05:00
|
|
|
match e {
|
2021-07-23 17:55:36 -05:00
|
|
|
//~^ ERROR cannot use `e` because it was mutably borrowed
|
2017-10-06 10:24:23 -05:00
|
|
|
E::A(ref ax) =>
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot borrow `e.0` as immutable because it is also borrowed as mutable
|
2017-10-06 10:24:23 -05:00
|
|
|
println!("e.ax: {:?}", ax),
|
|
|
|
E::B { x: ref bx } =>
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot borrow `e.x` as immutable because it is also borrowed as mutable
|
2017-10-06 10:24:23 -05:00
|
|
|
println!("e.bx: {:?}", bx),
|
|
|
|
}
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-06 10:24:23 -05:00
|
|
|
}
|
2017-10-06 10:25:41 -05:00
|
|
|
// Field in field
|
|
|
|
{
|
|
|
|
struct F { x: u32, y: u32 };
|
|
|
|
struct S { x: F, y: (u32, u32), };
|
|
|
|
let mut s = S { x: F { x: 1, y: 2}, y: (999, 998) };
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = &mut s;
|
2018-09-13 16:04:09 -05:00
|
|
|
match s {
|
2017-10-06 10:25:41 -05:00
|
|
|
S { y: (ref y0, _), .. } =>
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot borrow `s.y.0` as immutable because it is also borrowed as mutable
|
2017-10-06 10:25:41 -05:00
|
|
|
println!("y0: {:?}", y0),
|
|
|
|
_ => panic!("other case"),
|
|
|
|
}
|
2018-09-13 16:04:09 -05:00
|
|
|
match s {
|
2017-10-06 10:25:41 -05:00
|
|
|
S { x: F { y: ref x0, .. }, .. } =>
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot borrow `s.x.y` as immutable because it is also borrowed as mutable
|
2017-10-06 10:25:41 -05:00
|
|
|
println!("x0: {:?}", x0),
|
|
|
|
_ => panic!("other case"),
|
|
|
|
}
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-06 10:25:41 -05:00
|
|
|
}
|
2017-10-06 10:26:53 -05:00
|
|
|
// Field of ref
|
|
|
|
{
|
|
|
|
struct Block<'a> {
|
|
|
|
current: &'a u8,
|
|
|
|
unrelated: &'a u8,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn bump<'a>(mut block: &mut Block<'a>) {
|
|
|
|
let x = &mut block;
|
|
|
|
let p: &'a u8 = &*block.current;
|
2019-09-06 08:47:50 -05:00
|
|
|
//~^ ERROR cannot borrow `*block.current` as immutable because it is also borrowed as mutable
|
|
|
|
// See issue rust#38899
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-06 10:26:53 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Field of ptr
|
|
|
|
{
|
|
|
|
struct Block2 {
|
|
|
|
current: *const u8,
|
|
|
|
unrelated: *const u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe fn bump2(mut block: *mut Block2) {
|
|
|
|
let x = &mut block;
|
|
|
|
let p : *const u8 = &*(*block).current;
|
2019-09-06 08:47:50 -05:00
|
|
|
//~^ ERROR cannot borrow `*block.current` as immutable because it is also borrowed as mutable
|
|
|
|
// See issue rust#38899
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-06 10:26:53 -05:00
|
|
|
}
|
|
|
|
}
|
2017-10-06 10:26:14 -05:00
|
|
|
// Field of index
|
|
|
|
{
|
|
|
|
struct F {x: u32, y: u32};
|
|
|
|
let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = &mut v;
|
2017-10-06 10:26:14 -05:00
|
|
|
v[0].y;
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot use `v[_].y` because it was mutably borrowed
|
|
|
|
//~| ERROR cannot use `*v` because it was mutably borrowed
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-06 10:26:14 -05:00
|
|
|
}
|
|
|
|
// Field of constant index
|
|
|
|
{
|
|
|
|
struct F {x: u32, y: u32};
|
|
|
|
let mut v = &[F{x: 1, y: 2}, F{x: 3, y: 4}];
|
2018-04-09 04:28:00 -05:00
|
|
|
let x = &mut v;
|
2018-09-13 16:04:09 -05:00
|
|
|
match v {
|
2017-10-06 10:26:14 -05:00
|
|
|
&[_, F {x: ref xf, ..}] => println!("{}", xf),
|
2019-04-22 02:40:08 -05:00
|
|
|
//~^ ERROR cannot borrow `v[..].x` as immutable because it is also borrowed as mutable
|
2017-10-06 10:26:14 -05:00
|
|
|
_ => panic!("other case")
|
|
|
|
}
|
2018-04-09 04:28:00 -05:00
|
|
|
drop(x);
|
2017-10-06 10:26:14 -05:00
|
|
|
}
|
2017-11-12 10:19:02 -06:00
|
|
|
// Field from upvar
|
|
|
|
{
|
|
|
|
let mut x = 0;
|
|
|
|
|| {
|
|
|
|
let y = &mut x;
|
2019-04-22 02:40:08 -05:00
|
|
|
&mut x; //~ ERROR cannot borrow `x` as mutable more than once at a time
|
2017-11-12 10:19:02 -06:00
|
|
|
*y = 1;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// Field from upvar nested
|
|
|
|
{
|
|
|
|
let mut x = 0;
|
|
|
|
|| {
|
2019-04-22 02:40:08 -05:00
|
|
|
|| { //~ ERROR captured variable cannot escape `FnMut` closure body
|
2018-04-09 04:28:00 -05:00
|
|
|
let y = &mut x;
|
2019-04-22 02:40:08 -05:00
|
|
|
&mut x; //~ ERROR cannot borrow `x` as mutable more than once at a time
|
2018-04-09 04:28:00 -05:00
|
|
|
*y = 1;
|
|
|
|
drop(y);
|
2017-11-12 10:19:02 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-11-18 16:32:05 -06:00
|
|
|
{
|
|
|
|
fn foo(x: Vec<i32>) {
|
|
|
|
let c = || {
|
|
|
|
drop(x);
|
2019-04-22 02:40:08 -05:00
|
|
|
drop(x); //~ ERROR use of moved value: `x`
|
2017-11-18 16:32:05 -06:00
|
|
|
};
|
|
|
|
c();
|
|
|
|
}
|
|
|
|
}
|
2017-10-02 12:39:43 -05:00
|
|
|
}
|