Skip to content

Commit d443f98

Browse files
committed
Apply borrowck to fns that appear in const declarations.
Fixes #22382.
1 parent f0f7ca2 commit d443f98

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

src/librustc_borrowck/borrowck/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ fn borrowck_item(this: &mut BorrowckCtxt, item: &ast::Item) {
105105
ast::ItemConst(_, ref ex) => {
106106
gather_loans::gather_loans_in_static_initializer(this, &**ex);
107107
}
108-
_ => {
109-
visit::walk_item(this, item);
110-
}
108+
_ => { }
111109
}
110+
111+
visit::walk_item(this, item);
112112
}
113113

114114
/// Collection of conclusions determined via borrow checker analyses.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
// Check that we check fns appearing in constant declarations.
12+
// Issue #22382.
13+
14+
const MOVE: fn(&String) -> String = {
15+
fn broken(x: &String) -> String {
16+
return *x //~ ERROR cannot move
17+
}
18+
broken
19+
};
20+
21+
fn main() {
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
// Check that we check fns appearing in constant declarations.
12+
// Issue #22382.
13+
14+
// How about mutating an immutable vector?
15+
const MUTATE: fn(&Vec<String>) = {
16+
fn broken(x: &Vec<String>) {
17+
x.push(format!("this is broken"));
18+
//~^ ERROR cannot borrow
19+
}
20+
broken
21+
};
22+
23+
fn main() {
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
// Check that we check fns appearing in constant declarations.
12+
// Issue #22382.
13+
14+
// Returning local references?
15+
struct DropString {
16+
inner: String
17+
}
18+
impl Drop for DropString {
19+
fn drop(&mut self) {
20+
self.inner.clear();
21+
self.inner.push_str("dropped");
22+
}
23+
}
24+
const LOCAL_REF: fn() -> &'static str = {
25+
fn broken() -> &'static str {
26+
let local = DropString { inner: format!("Some local string") };
27+
return &local.inner; //~ ERROR does not live long enough
28+
}
29+
broken
30+
};
31+
32+
fn main() {
33+
}

0 commit comments

Comments
 (0)