]> git.lizzy.rs Git - rust.git/blob - src/test/codegen-units/partitioning/vtable-through-const.rs
0007eaae2897185c3dcff2c21b0a02921534dc3d
[rust.git] / src / test / codegen-units / partitioning / vtable-through-const.rs
1 // Copyright 2016 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 // ignore-tidy-linelength
12
13 // We specify -Z incremental here because we want to test the partitioning for
14 // incremental compilation
15 // compile-flags:-Zprint-trans-items=lazy -Zincremental=tmp/partitioning-tests/vtable-through-const
16
17 // This test case makes sure, that references made through constants are
18 // recorded properly in the InliningMap.
19
20 mod mod1 {
21     pub trait Trait1 {
22         fn do_something(&self) {}
23         fn do_something_else(&self) {}
24     }
25
26     impl Trait1 for u32 {}
27
28     pub trait Trait1Gen<T> {
29         fn do_something(&self, x: T) -> T;
30         fn do_something_else(&self, x: T) -> T;
31     }
32
33     impl<T> Trait1Gen<T> for u32 {
34         fn do_something(&self, x: T) -> T { x }
35         fn do_something_else(&self, x: T) -> T { x }
36     }
37
38     fn id<T>(x: T) -> T { x }
39
40     // These are referenced, so they produce trans-items (see main())
41     pub const TRAIT1_REF: &'static Trait1 = &0u32 as &Trait1;
42     pub const TRAIT1_GEN_REF: &'static Trait1Gen<u8> = &0u32 as &Trait1Gen<u8>;
43     pub const ID_CHAR: fn(char) -> char = id::<char>;
44
45
46
47     pub trait Trait2 {
48         fn do_something(&self) {}
49         fn do_something_else(&self) {}
50     }
51
52     impl Trait2 for u32 {}
53
54     pub trait Trait2Gen<T> {
55         fn do_something(&self, x: T) -> T;
56         fn do_something_else(&self, x: T) -> T;
57     }
58
59     impl<T> Trait2Gen<T> for u32 {
60         fn do_something(&self, x: T) -> T { x }
61         fn do_something_else(&self, x: T) -> T { x }
62     }
63
64     // These are not referenced, so they do not produce trans-items
65     pub const TRAIT2_REF: &'static Trait2 = &0u32 as &Trait2;
66     pub const TRAIT2_GEN_REF: &'static Trait2Gen<u8> = &0u32 as &Trait2Gen<u8>;
67     pub const ID_I64: fn(i64) -> i64 = id::<i64>;
68 }
69
70 //~ TRANS_ITEM fn vtable_through_const::main[0] @@ vtable_through_const[External]
71 fn main() {
72     //~ TRANS_ITEM drop-glue i8 @@ vtable_through_const[Internal]
73
74     // Since Trait1::do_something() is instantiated via its default implementation,
75     // it is considered a generic and is instantiated here only because it is
76     // referenced in this module.
77     //~ TRANS_ITEM fn vtable_through_const::mod1[0]::Trait1[0]::do_something_else[0]<u32> @@ vtable_through_const[Internal]
78
79     // Although it is never used, Trait1::do_something_else() has to be
80     // instantiated locally here too, otherwise the <&u32 as &Trait1> vtable
81     // could not be fully constructed.
82     //~ TRANS_ITEM fn vtable_through_const::mod1[0]::Trait1[0]::do_something[0]<u32> @@ vtable_through_const[Internal]
83     mod1::TRAIT1_REF.do_something();
84
85     // Same as above
86     //~ TRANS_ITEM fn vtable_through_const::mod1[0]::{{impl}}[1]::do_something[0]<u8> @@ vtable_through_const[Internal]
87     //~ TRANS_ITEM fn vtable_through_const::mod1[0]::{{impl}}[1]::do_something_else[0]<u8> @@ vtable_through_const[Internal]
88     mod1::TRAIT1_GEN_REF.do_something(0u8);
89
90     //~ TRANS_ITEM fn vtable_through_const::mod1[0]::id[0]<char> @@ vtable_through_const[Internal]
91     mod1::ID_CHAR('x');
92 }