Skip to content

Commit 0241ea4

Browse files
committed
mir-borrowck: Replace all constant index and sublices output with [..] to match the AST borrowck output
1 parent ef2f42d commit 0241ea4

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

src/librustc_mir/borrow_check.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,20 +1090,13 @@ 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, from_end: false, .. } => {
1093+
ProjectionElem::ConstantIndex { .. } | ProjectionElem::Subslice { .. } => {
10941094
autoderef = true;
1095-
("", format!("[{}]", offset), None)
1095+
// Since it isn't possible to borrow an element on a particular index and
1096+
// then use another while the borrow is held, don't output indices details
1097+
// to avoid confusing the end-user
1098+
("", format!("[..]"), None)
10961099
},
1097-
ProjectionElem::ConstantIndex { offset, from_end: true, .. } => {
1098-
autoderef = true;
1099-
("", format!("[-{}]", offset), None)
1100-
},
1101-
ProjectionElem::Subslice { from, to: 0 } =>
1102-
("", format!("[{}:]", from), None),
1103-
ProjectionElem::Subslice { from: 0, to } =>
1104-
("", format!("[:-{}]", to), None),
1105-
ProjectionElem::Subslice { from, to } =>
1106-
("", format!("[{}:-{}]", from, to), None),
11071100
};
11081101
buf.push_str(prefix);
11091102
self.append_lvalue_to_string(&proj.base, buf, Some(autoderef));

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

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// ignore-tidy-linelength
1112
// revisions: ast mir
1213
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
1314

15+
#![feature(slice_patterns)]
1416
#![feature(advanced_slice_patterns)]
1517

1618
pub struct Foo {
@@ -173,29 +175,62 @@ fn main() {
173175
&[x, _, .., _, _] => println!("{}", x),
174176
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
175177
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
176-
//[mir]~| ERROR cannot use `v[0]` because it was mutably borrowed (Mir)
178+
//[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir)
177179
_ => panic!("other case"),
178180
}
179181
match v {
180182
&[_, x, .., _, _] => println!("{}", x),
181183
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
182184
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
183-
//[mir]~| ERROR cannot use `v[1]` because it was mutably borrowed (Mir)
185+
//[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir)
184186
_ => panic!("other case"),
185187
}
186188
match v {
187189
&[_, _, .., x, _] => println!("{}", x),
188190
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
189191
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
190-
//[mir]~| ERROR cannot use `v[-2]` because it was mutably borrowed (Mir)
192+
//[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir)
191193
_ => panic!("other case"),
192194
}
193195
match v {
194196
&[_, _, .., _, x] => println!("{}", x),
195197
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
196198
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
197-
//[mir]~| ERROR cannot use `v[-1]` because it was mutably borrowed (Mir)
199+
//[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir)
198200
_ => panic!("other case"),
199201
}
200202
}
203+
// Subslices
204+
{
205+
let mut v = &[1, 2, 3, 4, 5];
206+
let _v = &mut v;
207+
match v {
208+
&[x..] => println!("{:?}", x),
209+
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
210+
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
211+
//[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir)
212+
_ => panic!("other case"),
213+
}
214+
match v {
215+
&[_, x..] => println!("{:?}", x),
216+
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
217+
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
218+
//[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir)
219+
_ => panic!("other case"),
220+
}
221+
match v {
222+
&[x.., _] => println!("{:?}", x),
223+
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
224+
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
225+
//[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir)
226+
_ => panic!("other case"),
227+
}
228+
match v {
229+
&[_, x.., _] => println!("{:?}", x),
230+
//[ast]~^ ERROR cannot use `v[..]` because it was mutably borrowed
231+
//[mir]~^^ ERROR cannot use `v[..]` because it was mutably borrowed (Ast)
232+
//[mir]~| ERROR cannot use `v[..]` because it was mutably borrowed (Mir)
233+
_ => panic!("other case"),
234+
}
235+
}
201236
}

0 commit comments

Comments
 (0)