Skip to content

Commit 9feaf1d

Browse files
committed
auto merge of #8594 : bytewiseand/rust/static-fn-ptr, r=pcwalton
Fixes #8588
2 parents 18144b1 + 5ed9f60 commit 9feaf1d

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

src/librustc/middle/trans/callee.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ pub fn trans(bcx: @mut Block, expr: @ast::expr) -> Callee {
135135
ast::def_struct(def_id) => {
136136
fn_callee(bcx, trans_fn_ref(bcx, def_id, ref_expr.id))
137137
}
138+
ast::def_static(*) |
138139
ast::def_arg(*) |
139140
ast::def_local(*) |
140141
ast::def_binding(*) |
@@ -143,7 +144,7 @@ pub fn trans(bcx: @mut Block, expr: @ast::expr) -> Callee {
143144
datum_callee(bcx, ref_expr)
144145
}
145146
ast::def_mod(*) | ast::def_foreign_mod(*) | ast::def_trait(*) |
146-
ast::def_static(*) | ast::def_ty(*) | ast::def_prim_ty(*) |
147+
ast::def_ty(*) | ast::def_prim_ty(*) |
147148
ast::def_use(*) | ast::def_typaram_binder(*) |
148149
ast::def_region(*) | ast::def_label(*) | ast::def_ty_param(*) |
149150
ast::def_self_ty(*) | ast::def_method(*) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2013 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+
pub fn f(x: int) -> int { -x }
12+
13+
pub static F: extern fn(int) -> int = f;
14+
pub static mut MutF: extern fn(int) -> int = f;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Copyright 2013 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+
// xfail-fast
12+
// aux-build:static-function-pointer-aux.rs
13+
extern mod aux(name = "static-function-pointer-aux");
14+
15+
fn f(x: int) -> int { x }
16+
17+
fn main() {
18+
assert_eq!(aux::F(42), -42);
19+
unsafe {
20+
assert_eq!(aux::MutF(42), -42);
21+
aux::MutF = f;
22+
assert_eq!(aux::MutF(42), 42);
23+
aux::MutF = aux::f;
24+
assert_eq!(aux::MutF(42), -42);
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2013 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 f(x: int) -> int { x }
12+
fn g(x: int) -> int { 2 * x }
13+
14+
static F: extern fn(int) -> int = f;
15+
static mut G: extern fn(int) -> int = f;
16+
17+
pub fn main() {
18+
assert_eq!(F(42), 42);
19+
unsafe {
20+
assert_eq!(G(42), 42);
21+
G = g;
22+
assert_eq!(G(42), 84);
23+
}
24+
}

0 commit comments

Comments
 (0)