diff --git a/src/librustc_mir/borrow_check.rs b/src/librustc_mir/borrow_check.rs index 85ecb9d6e36..d57bac7c85a 100644 --- a/src/librustc_mir/borrow_check.rs +++ b/src/librustc_mir/borrow_check.rs @@ -1090,20 +1090,13 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx> autoderef = true; ("", format!(""), Some(index)) }, - ProjectionElem::ConstantIndex { offset, from_end: false, .. } => { + ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => { autoderef = true; - ("", format!("[{}]", offset), None) + // Since it isn't possible to borrow an element on a particular index and + // then use another while the borrow is held, don't output indices details + // to avoid confusing the end-user + ("", format!("[..]"), None) }, - ProjectionElem::ConstantIndex { offset, from_end: true, .. } => { - autoderef = true; - ("", format!("[-{}]", offset), None) - }, - ProjectionElem::Subslice { from, to: 0 } => - ("", format!("[{}:]", from), None), - ProjectionElem::Subslice { from: 0, to } => - ("", format!("[:-{}]", to), None), - ProjectionElem::Subslice { from, to } => - ("", format!("[{}:-{}]", from, to), None), }; buf.push_str(prefix); self.append_lvalue_to_string(&proj.base, buf, Some(autoderef)); diff --git a/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs b/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs index 9d1b2b64d85..088b678efb5 100644 --- a/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs +++ b/src/test/compile-fail/borrowck/borrowck-describe-lvalue.rs @@ -8,9 +8,11 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// ignore-tidy-linelength // revisions: ast mir //[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir +#![feature(slice_patterns)] #![feature(advanced_slice_patterns)] pub struct Foo { @@ -173,29 +175,62 @@ fn main() { &[x, _, .., _, _] => println!("{}", x), //[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed //[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast) - //[mir]~| ERROR cannot use `v[0]` because it was mutably borrowed (Mir) + //[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir) _ => panic!("other case"), } match v { &[_, x, .., _, _] => println!("{}", x), //[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed //[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast) - //[mir]~| ERROR cannot use `v[1]` because it was mutably borrowed (Mir) + //[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir) _ => panic!("other case"), } match v { &[_, _, .., x, _] => println!("{}", x), //[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed //[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast) - //[mir]~| ERROR cannot use `v[-2]` because it was mutably borrowed (Mir) + //[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir) _ => panic!("other case"), } match v { &[_, _, .., _, x] => println!("{}", x), //[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed //[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast) - //[mir]~| ERROR cannot use `v[-1]` because it was mutably borrowed (Mir) + //[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir) _ => panic!("other case"), } } + // Subslices + { + let mut v = &[1, 2, 3, 4, 5]; + let _v = &mut v; + match v { + &[x..] => println!("{:?}", x), + //[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed + //[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast) + //[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir) + _ => panic!("other case"), + } + match v { + &[_, x..] => println!("{:?}", x), + //[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed + //[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast) + //[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir) + _ => panic!("other case"), + } + match v { + &[x.., _] => println!("{:?}", x), + //[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed + //[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast) + //[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir) + _ => panic!("other case"), + } + match v { + &[_, x.., _] => println!("{:?}", x), + //[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed + //[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast) + //[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir) + _ => panic!("other case"), + } + } }