Skip to content

Commit ef2f42d

Browse files
committed
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.
1 parent 456e12e commit ef2f42d

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

src/librustc_mir/borrow_check.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,10 +1090,14 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
10901090
autoderef = true;
10911091
("", format!(""), Some(index))
10921092
},
1093-
ProjectionElem::ConstantIndex { offset, min_length, from_end: true } =>
1094-
("", format!("[{} of {}]", offset, min_length), None),
1095-
ProjectionElem::ConstantIndex { offset, min_length, from_end: false } =>
1096-
("", format!("[-{} of {}]", offset, min_length), None),
1093+
ProjectionElem::ConstantIndex { offset, from_end: false, .. } => {
1094+
autoderef = true;
1095+
("", format!("[{}]", offset), None)
1096+
},
1097+
ProjectionElem::ConstantIndex { offset, from_end: true, .. } => {
1098+
autoderef = true;
1099+
("", format!("[-{}]", offset), None)
1100+
},
10971101
ProjectionElem::Subslice { from, to: 0 } =>
10981102
("", format!("[{}:]", from), None),
10991103
ProjectionElem::Subslice { from: 0, to } =>

src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// revisions: ast mir
1212
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
1313

14+
#![feature(advanced_slice_patterns)]
15+
1416
pub struct Foo {
1517
x: u32
1618
}
@@ -163,4 +165,37 @@ fn main() {
163165
//[mir]~^ ERROR cannot use `u.a` because it was mutably borrowed (Ast)
164166
//[mir]~| ERROR cannot use `u.a` because it was mutably borrowed (Mir)
165167
}
168+
// Constant index
169+
{
170+
let mut v = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
171+
let _v = &mut v;
172+
match v {
173+
&[x, _, .., _, _] => println!("{}", x),
174+
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
175+
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
176+
//[mir]~| ERROR cannot use `v[0]` because it was mutably borrowed (Mir)
177+
_ => panic!("other case"),
178+
}
179+
match v {
180+
&[_, x, .., _, _] => println!("{}", x),
181+
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
182+
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
183+
//[mir]~| ERROR cannot use `v[1]` because it was mutably borrowed (Mir)
184+
_ => panic!("other case"),
185+
}
186+
match v {
187+
&[_, _, .., x, _] => println!("{}", x),
188+
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
189+
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
190+
//[mir]~| ERROR cannot use `v[-2]` because it was mutably borrowed (Mir)
191+
_ => panic!("other case"),
192+
}
193+
match v {
194+
&[_, _, .., _, x] => println!("{}", x),
195+
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
196+
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
197+
//[mir]~| ERROR cannot use `v[-1]` because it was mutably borrowed (Mir)
198+
_ => panic!("other case"),
199+
}
200+
}
166201
}

0 commit comments

Comments
 (0)