]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/method-self-arg-trait.rs
rollup merge of #23592: alexcrichton/tweak-at-exit
[rust.git] / src / test / run-pass / method-self-arg-trait.rs
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 // Test method calls with self as an argument
12
13 // pretty-expanded FIXME #23616
14
15 #![allow(unknown_features)]
16 #![feature(box_syntax)]
17
18 static mut COUNT: u64 = 1;
19
20 #[derive(Copy)]
21 struct Foo;
22
23 trait Bar : Sized {
24     fn foo1(&self);
25     fn foo2(self);
26     fn foo3(self: Box<Self>);
27
28     fn bar1(&self) {
29         unsafe { COUNT *= 7; }
30     }
31     fn bar2(self) {
32         unsafe { COUNT *= 11; }
33     }
34     fn bar3(self: Box<Self>) {
35         unsafe { COUNT *= 13; }
36     }
37 }
38
39 impl Bar for Foo {
40     fn foo1(&self) {
41         unsafe { COUNT *= 2; }
42     }
43
44     fn foo2(self) {
45         unsafe { COUNT *= 3; }
46     }
47
48     fn foo3(self: Box<Foo>) {
49         unsafe { COUNT *= 5; }
50     }
51 }
52
53 impl Foo {
54     fn baz(self) {
55         unsafe { COUNT *= 17; }
56         // Test internal call.
57         Bar::foo1(&self);
58         Bar::foo2(self);
59         Bar::foo3(box self);
60
61         Bar::bar1(&self);
62         Bar::bar2(self);
63         Bar::bar3(box self);
64     }
65 }
66
67 fn main() {
68     let x = Foo;
69     // Test external call.
70     Bar::foo1(&x);
71     Bar::foo2(x);
72     Bar::foo3(box x);
73
74     Bar::bar1(&x);
75     Bar::bar2(x);
76     Bar::bar3(box x);
77
78     x.baz();
79
80     unsafe { assert!(COUNT == 2*2*3*3*5*5*7*7*11*11*13*13*17); }
81 }