1 // compile-flags: -O --target=avr-unknown-gnu-atmega328 --crate-type=rlib
2 // needs-llvm-components: avr
4 // This test validates that function pointers can be stored in global variables
5 // and called upon. It ensures that Rust emits function pointers in the correct
6 // address space to LLVM so that an assertion error relating to casting is
9 // It also validates that functions can be called through function pointers
12 #![feature(no_core, lang_items, unboxed_closures, arbitrary_self_types)]
13 #![crate_type = "lib"]
21 pub trait Receiver { }
22 #[lang = "tuple_trait"]
25 pub struct Result<T, E> { _a: T, _b: E }
27 impl Copy for usize {}
28 impl Copy for &usize {}
30 #[lang = "drop_in_place"]
31 pub unsafe fn drop_in_place<T: ?Sized>(_: *mut T) {}
34 pub trait FnOnce<Args: Tuple> {
35 #[lang = "fn_once_output"]
38 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
42 pub trait FnMut<Args: Tuple> : FnOnce<Args> {
43 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
47 pub trait Fn<Args: Tuple>: FnOnce<Args> {
48 /// Performs the call operation.
49 extern "rust-call" fn call(&self, args: Args) -> Self::Output;
52 pub static mut STORAGE_FOO: fn(&usize, &mut u32) -> Result<(), ()> = arbitrary_black_box;
53 pub static mut STORAGE_BAR: u32 = 12;
55 fn arbitrary_black_box(ptr: &usize, _: &mut u32) -> Result<(), ()> {
56 let raw_ptr = ptr as *const usize;
57 let _v: usize = unsafe { *raw_ptr };
63 fn call_through_fn_trait(a: &mut impl Fn<(), Output=()>) {
68 fn update_bar_value() {
74 // CHECK: define dso_local void @test(){{.+}}addrspace(1)
76 pub extern "C" fn test() {
79 // A call through the Fn trait must use address space 1.
81 // CHECK: call{{.+}}addrspace(1) void @call_through_fn_trait()
82 call_through_fn_trait(&mut update_bar_value);
84 // A call through a global variable must use address space 1.
85 // CHECK: load {{.*}}addrspace(1){{.+}}FOO
87 STORAGE_FOO(&1, &mut buf);