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