Skip to content

Commit d1dcd19

Browse files
committed
librustc: Make references to functions not have static lifetime.
This breaks code like: struct A<'a> { func: &'a fn() -> Option<int> } fn foo() -> Option<int> { ... } fn create() -> A<'static> { A { func: &foo } } Change this code to not take functions by reference. For example: struct A { func: extern "Rust" fn() -> Option<int> } fn foo() -> Option<int> { ... } fn create() -> A { A { func: foo } } Closes #13595. [breaking-change]
1 parent d3f66bd commit d1dcd19

File tree

3 files changed

+95
-3
lines changed

3 files changed

+95
-3
lines changed

src/librustc/middle/mem_categorization.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,11 @@ impl<'t,TYPER:Typer> MemCategorizationContext<'t,TYPER> {
500500
id, expr_ty.repr(self.tcx()), def);
501501

502502
match def {
503-
def::DefStruct(..) | def::DefVariant(..) => {
503+
def::DefStruct(..) | def::DefVariant(..) | def::DefFn(..) |
504+
def::DefStaticMethod(..) => {
504505
Ok(self.cat_rvalue_node(id, span, expr_ty))
505506
}
506-
def::DefFn(..) | def::DefStaticMethod(..) | def::DefMod(_) |
507-
def::DefForeignMod(_) | def::DefStatic(_, false) |
507+
def::DefMod(_) | def::DefForeignMod(_) | def::DefStatic(_, false) |
508508
def::DefUse(_) | def::DefTrait(_) | def::DefTy(_) | def::DefPrimTy(_) |
509509
def::DefTyParam(..) | def::DefTyParamBinder(..) | def::DefRegion(_) |
510510
def::DefLabel(_) | def::DefSelfTy(..) | def::DefMethod(..) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
struct A<'a> {
12+
func: &'a fn() -> Option<int>
13+
}
14+
15+
impl<'a> A<'a> {
16+
fn call(&self) -> Option<int> {
17+
(*self.func)()
18+
}
19+
}
20+
21+
fn foo() -> Option<int> {
22+
None
23+
}
24+
25+
fn create() -> A<'static> {
26+
A {
27+
func: &foo, //~ ERROR borrowed value does not live long enough
28+
}
29+
}
30+
31+
fn main() {
32+
let a = create();
33+
a.call();
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
struct StateMachineIter<'a> {
12+
statefn: &'a fn(&mut StateMachineIter<'a>) -> Option<&'static str>
13+
}
14+
15+
impl<'a> Iterator<&'static str> for StateMachineIter<'a> {
16+
fn next(&mut self) -> Option<&'static str> {
17+
return (*self.statefn)(self);
18+
}
19+
}
20+
21+
fn state1(self_: &mut StateMachineIter) -> Option<&'static str> {
22+
self_.statefn = &state2;
23+
//~^ ERROR borrowed value does not live long enough
24+
return Some("state1");
25+
}
26+
27+
fn state2(self_: &mut StateMachineIter) -> Option<(&'static str)> {
28+
self_.statefn = &state3;
29+
//~^ ERROR borrowed value does not live long enough
30+
return Some("state2");
31+
}
32+
33+
fn state3(self_: &mut StateMachineIter) -> Option<(&'static str)> {
34+
self_.statefn = &finished;
35+
//~^ ERROR borrowed value does not live long enough
36+
return Some("state3");
37+
}
38+
39+
fn finished(_: &mut StateMachineIter) -> Option<(&'static str)> {
40+
return None;
41+
}
42+
43+
fn state_iter() -> StateMachineIter<'static> {
44+
StateMachineIter {
45+
statefn: &state1 //~ ERROR borrowed value does not live long enough
46+
}
47+
}
48+
49+
50+
fn main() {
51+
let mut it = state_iter();
52+
println!("{}",it.next());
53+
println!("{}",it.next());
54+
println!("{}",it.next());
55+
println!("{}",it.next());
56+
println!("{}",it.next());
57+
}
58+

0 commit comments

Comments
 (0)