From a1066130c07b864a1612c49b4b84dc34d5f113a1 Mon Sep 17 00:00:00 2001 From: Andreas Martens Date: Sun, 18 Aug 2013 20:21:23 +0200 Subject: [PATCH 1/4] Fix ICE when calling static and static function pointers Fixes #8588 --- src/librustc/middle/trans/callee.rs | 3 ++- src/test/run-pass/static-function-pointer.rs | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/test/run-pass/static-function-pointer.rs diff --git a/src/librustc/middle/trans/callee.rs b/src/librustc/middle/trans/callee.rs index 4caaf38487351..5072c879e2bdd 100644 --- a/src/librustc/middle/trans/callee.rs +++ b/src/librustc/middle/trans/callee.rs @@ -132,6 +132,7 @@ pub fn trans(bcx: @mut Block, expr: @ast::expr) -> Callee { ast::def_struct(def_id) => { fn_callee(bcx, trans_fn_ref(bcx, def_id, ref_expr.id)) } + ast::def_static(*) | ast::def_arg(*) | ast::def_local(*) | ast::def_binding(*) | @@ -140,7 +141,7 @@ pub fn trans(bcx: @mut Block, expr: @ast::expr) -> Callee { datum_callee(bcx, ref_expr) } ast::def_mod(*) | ast::def_foreign_mod(*) | ast::def_trait(*) | - ast::def_static(*) | ast::def_ty(*) | ast::def_prim_ty(*) | + ast::def_ty(*) | ast::def_prim_ty(*) | ast::def_use(*) | ast::def_typaram_binder(*) | ast::def_region(*) | ast::def_label(*) | ast::def_ty_param(*) | ast::def_self_ty(*) | ast::def_method(*) => { diff --git a/src/test/run-pass/static-function-pointer.rs b/src/test/run-pass/static-function-pointer.rs new file mode 100644 index 0000000000000..0bb8c4e2cc398 --- /dev/null +++ b/src/test/run-pass/static-function-pointer.rs @@ -0,0 +1,19 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn f(x: int) -> int { x } + +static F: extern fn(int) -> int = f; +static mut G: extern fn(int) -> int = f; + +fn main() { + F(42); + unsafe { G(7); } +} From cc0c6fd43cd1b38aade4c9264d4c05342e9694f5 Mon Sep 17 00:00:00 2001 From: Andreas Martens Date: Sun, 18 Aug 2013 22:17:47 +0200 Subject: [PATCH 2/4] Add assertions and cross crate tests --- .../auxiliary/static-function-pointer-aux.rs | 14 ++++++++++++++ src/test/run-pass/static-function-pointer.rs | 18 ++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/test/auxiliary/static-function-pointer-aux.rs diff --git a/src/test/auxiliary/static-function-pointer-aux.rs b/src/test/auxiliary/static-function-pointer-aux.rs new file mode 100644 index 0000000000000..fcf82b1360580 --- /dev/null +++ b/src/test/auxiliary/static-function-pointer-aux.rs @@ -0,0 +1,14 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn f(x: int) -> int { -x } + +pub static F: extern fn(int) -> int = f; +pub static mut MutF: extern fn(int) -> int = f; diff --git a/src/test/run-pass/static-function-pointer.rs b/src/test/run-pass/static-function-pointer.rs index 0bb8c4e2cc398..2e29ce1ef45ba 100644 --- a/src/test/run-pass/static-function-pointer.rs +++ b/src/test/run-pass/static-function-pointer.rs @@ -8,12 +8,26 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// aux-build:static-function-pointer-aux.rs +extern mod aux(name = "static-function-pointer-aux"); + fn f(x: int) -> int { x } +fn g(x: int) -> int { 2 * x } static F: extern fn(int) -> int = f; static mut G: extern fn(int) -> int = f; fn main() { - F(42); - unsafe { G(7); } + assert_eq!(F(42), 42); + unsafe { + assert_eq!(G(42), 42); + G = g; + assert_eq!(G(42), 84); + } + assert_eq!(aux::F(42), -42); + unsafe { + assert_eq!(aux::MutF(42), -42); + aux::MutF = f; + assert_eq!(aux::MutF(42), 42); + } } From 0f6dd539488039cacef9646786039eb5213e6444 Mon Sep 17 00:00:00 2001 From: Andreas Martens Date: Wed, 21 Aug 2013 17:29:47 +0200 Subject: [PATCH 3/4] Split cross-crate test into own test and xfail-fast it --- .../auxiliary/static-function-pointer-aux.rs | 4 +-- .../run-pass/static-function-pointer-xc.rs | 26 +++++++++++++++++++ src/test/run-pass/static-function-pointer.rs | 11 +------- 3 files changed, 29 insertions(+), 12 deletions(-) create mode 100644 src/test/run-pass/static-function-pointer-xc.rs diff --git a/src/test/auxiliary/static-function-pointer-aux.rs b/src/test/auxiliary/static-function-pointer-aux.rs index fcf82b1360580..85f01666a3fb7 100644 --- a/src/test/auxiliary/static-function-pointer-aux.rs +++ b/src/test/auxiliary/static-function-pointer-aux.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -fn f(x: int) -> int { -x } +pub fn f(x: int) -> int { -x } pub static F: extern fn(int) -> int = f; pub static mut MutF: extern fn(int) -> int = f; diff --git a/src/test/run-pass/static-function-pointer-xc.rs b/src/test/run-pass/static-function-pointer-xc.rs new file mode 100644 index 0000000000000..0ba47320a67b9 --- /dev/null +++ b/src/test/run-pass/static-function-pointer-xc.rs @@ -0,0 +1,26 @@ +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// xfail-fast +// aux-build:static-function-pointer-aux.rs +extern mod aux(name = "static-function-pointer-aux"); + +fn f(x: int) -> int { x } + +fn main() { + assert_eq!(aux::F(42), -42); + unsafe { + assert_eq!(aux::MutF(42), -42); + aux::MutF = f; + assert_eq!(aux::MutF(42), 42); + aux::MutF = aux::f; + assert_eq!(aux::MutF(42), -42); + } +} diff --git a/src/test/run-pass/static-function-pointer.rs b/src/test/run-pass/static-function-pointer.rs index 2e29ce1ef45ba..ee68afe8ad665 100644 --- a/src/test/run-pass/static-function-pointer.rs +++ b/src/test/run-pass/static-function-pointer.rs @@ -1,4 +1,4 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// Copyright 2013 The Rust Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution and at // http://rust-lang.org/COPYRIGHT. // @@ -8,9 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// aux-build:static-function-pointer-aux.rs -extern mod aux(name = "static-function-pointer-aux"); - fn f(x: int) -> int { x } fn g(x: int) -> int { 2 * x } @@ -24,10 +21,4 @@ fn main() { G = g; assert_eq!(G(42), 84); } - assert_eq!(aux::F(42), -42); - unsafe { - assert_eq!(aux::MutF(42), -42); - aux::MutF = f; - assert_eq!(aux::MutF(42), 42); - } } From 5ed9f60a9730e8e4b27db2baf3fea7af987ebfe3 Mon Sep 17 00:00:00 2001 From: Andreas Martens Date: Wed, 21 Aug 2013 18:32:04 +0200 Subject: [PATCH 4/4] Changed `fn main` to `pub fn main` --- src/test/run-pass/static-function-pointer.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/run-pass/static-function-pointer.rs b/src/test/run-pass/static-function-pointer.rs index ee68afe8ad665..f8a889113ac5c 100644 --- a/src/test/run-pass/static-function-pointer.rs +++ b/src/test/run-pass/static-function-pointer.rs @@ -14,7 +14,7 @@ fn g(x: int) -> int { 2 * x } static F: extern fn(int) -> int = f; static mut G: extern fn(int) -> int = f; -fn main() { +pub fn main() { assert_eq!(F(42), 42); unsafe { assert_eq!(G(42), 42);