]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/mir_trans_calls.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / test / run-pass / mir_trans_calls.rs
1 // Copyright 2015 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 #![feature(fn_traits, test)]
12
13 extern crate test;
14
15 fn test1(a: isize, b: (i32, i32), c: &[i32]) -> (isize, (i32, i32), &[i32]) {
16 // Test passing a number of arguments including a fat pointer.
17 // Also returning via an out pointer
18 fn callee(a: isize, b: (i32, i32), c: &[i32]) -> (isize, (i32, i32), &[i32]) {
19 (a, b, c)
20 }
21 callee(a, b, c)
22 }
23
24 fn test2(a: isize) -> isize {
25 // Test passing a single argument.
26 // Not using out pointer.
27 fn callee(a: isize) -> isize {
28 a
29 }
30 callee(a)
31 }
32
33 #[derive(PartialEq, Eq, Debug)]
34 struct Foo;
35 impl Foo {
36 fn inherent_method(&self, a: isize) -> isize { a }
37 }
38
39 fn test3(x: &Foo, a: isize) -> isize {
40 // Test calling inherent method
41 x.inherent_method(a)
42 }
43
44 trait Bar {
45 fn extension_method(&self, a: isize) -> isize { a }
46 }
47 impl Bar for Foo {}
48
49 fn test4(x: &Foo, a: isize) -> isize {
50 // Test calling extension method
51 x.extension_method(a)
52 }
53
54 fn test5(x: &Bar, a: isize) -> isize {
55 // Test calling method on trait object
56 x.extension_method(a)
57 }
58
59 fn test6<T: Bar>(x: &T, a: isize) -> isize {
60 // Test calling extension method on generic callee
61 x.extension_method(a)
62 }
63
64 trait One<T = Self> {
65 fn one() -> T;
66 }
67 impl One for isize {
68 fn one() -> isize { 1 }
69 }
70
71 fn test7() -> isize {
72 // Test calling trait static method
73 <isize as One>::one()
74 }
75
76 struct Two;
77 impl Two {
78 fn two() -> isize { 2 }
79 }
80
81 fn test8() -> isize {
82 // Test calling impl static method
83 Two::two()
84 }
85
86 extern fn simple_extern(x: u32, y: (u32, u32)) -> u32 {
87 x + y.0 * y.1
88 }
89
90 fn test9() -> u32 {
91 simple_extern(41, (42, 43))
92 }
93
94 fn test_closure<F>(f: &F, x: i32, y: i32) -> i32
95 where F: Fn(i32, i32) -> i32
96 {
97 f(x, y)
98 }
99
100 fn test_fn_object(f: &Fn(i32, i32) -> i32, x: i32, y: i32) -> i32 {
101 f(x, y)
102 }
103
104 fn test_fn_impl(f: &&Fn(i32, i32) -> i32, x: i32, y: i32) -> i32 {
105 // This call goes through the Fn implementation for &Fn provided in
106 // core::ops::impls. It expands to a static Fn::call() that calls the
107 // Fn::call() implementation of the object shim underneath.
108 f(x, y)
109 }
110
111 fn test_fn_direct_call<F>(f: &F, x: i32, y: i32) -> i32
112 where F: Fn(i32, i32) -> i32
113 {
114 f.call((x, y))
115 }
116
117 fn test_fn_const_call<F>(f: &F) -> i32
118 where F: Fn(i32, i32) -> i32
119 {
120 f.call((100, -1))
121 }
122
123 fn test_fn_nil_call<F>(f: &F) -> i32
124 where F: Fn() -> i32
125 {
126 f()
127 }
128
129 fn test_fn_transmute_zst(x: ()) -> [(); 1] {
130 fn id<T>(x: T) -> T {x}
131
132 id(unsafe {
133 std::mem::transmute(x)
134 })
135 }
136
137 fn test_fn_ignored_pair() -> ((), ()) {
138 ((), ())
139 }
140
141 fn test_fn_ignored_pair_0() {
142 test_fn_ignored_pair().0
143 }
144
145 fn id<T>(x: T) -> T { x }
146
147 fn ignored_pair_named() -> (Foo, Foo) {
148 (Foo, Foo)
149 }
150
151 fn test_fn_ignored_pair_named() -> (Foo, Foo) {
152 id(ignored_pair_named())
153 }
154
155 fn test_fn_nested_pair(x: &((f32, f32), u32)) -> (f32, f32) {
156 let y = *x;
157 let z = y.0;
158 (z.0, z.1)
159 }
160
161 fn test_fn_const_arg_by_ref(mut a: [u64; 4]) -> u64 {
162 // Mutate the by-reference argument, which won't work with
163 // a non-immediate constant unless it's copied to the stack.
164 let a = test::black_box(&mut a);
165 a[0] += a[1];
166 a[0] += a[2];
167 a[0] += a[3];
168 a[0]
169 }
170
171 fn main() {
172 assert_eq!(test1(1, (2, 3), &[4, 5, 6]), (1, (2, 3), &[4, 5, 6][..]));
173 assert_eq!(test2(98), 98);
174 assert_eq!(test3(&Foo, 42), 42);
175 assert_eq!(test4(&Foo, 970), 970);
176 assert_eq!(test5(&Foo, 8576), 8576);
177 assert_eq!(test6(&Foo, 12367), 12367);
178 assert_eq!(test7(), 1);
179 assert_eq!(test8(), 2);
180 assert_eq!(test9(), 41 + 42 * 43);
181
182 let r = 3;
183 let closure = |x: i32, y: i32| { r*(x + (y*2)) };
184 assert_eq!(test_fn_const_call(&closure), 294);
185 assert_eq!(test_closure(&closure, 100, 1), 306);
186 let function_object = &closure as &Fn(i32, i32) -> i32;
187 assert_eq!(test_fn_object(function_object, 100, 2), 312);
188 assert_eq!(test_fn_impl(&function_object, 100, 3), 318);
189 assert_eq!(test_fn_direct_call(&closure, 100, 4), 324);
190
191 assert_eq!(test_fn_nil_call(&(|| 42)), 42);
192 assert_eq!(test_fn_transmute_zst(()), [()]);
193
194 assert_eq!(test_fn_ignored_pair_0(), ());
195 assert_eq!(test_fn_ignored_pair_named(), (Foo, Foo));
196 assert_eq!(test_fn_nested_pair(&((1.0, 2.0), 0)), (1.0, 2.0));
197
198 const ARRAY: [u64; 4] = [1, 2, 3, 4];
199 assert_eq!(test_fn_const_arg_by_ref(ARRAY), 1 + 2 + 3 + 4);
200 }