Skip to content

Commit 3ca53d3

Browse files
committed
librustc: Make sure lifetimes in for loop heads outlive the for loop
itself. This breaks code like: for &x in my_vector.iter() { my_vector[2] = "wibble"; ... } Change this code to not invalidate iterators. For example: for i in range(0, my_vector.len()) { my_vector[2] = "wibble"; ... } The `for-loop-does-not-borrow-iterators` test for #8372 was incorrect and has been removed. Closes #16820. [breaking-change]
1 parent d8a2618 commit 3ca53d3

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

src/librustc/middle/typeck/check/regionck.rs

+4
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,10 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) {
769769
}
770770

771771
rcx.visit_expr(&**head, ());
772+
type_of_node_must_outlive(rcx,
773+
infer::AddrOf(expr.span),
774+
head.id,
775+
ty::ReScope(expr.id));
772776

773777
let repeating_scope = rcx.set_repeating_scope(body.id);
774778
rcx.visit_block(&**body, ());
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let mut vector = vec![1u, 2];
13+
for &x in vector.iter() {
14+
let cap = vector.capacity();
15+
vector.grow(cap, &0u); //~ ERROR cannot borrow
16+
*vector.get_mut(1u) = 5u; //~ ERROR cannot borrow
17+
}
18+
}
19+

src/test/run-pass/for-loop-does-not-borrow-iterators.rs

-30
This file was deleted.

0 commit comments

Comments
 (0)