]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/trait-item-privacy.rs
e915ca05f6751c1cf849a1dca2fb6aff3229caf1
[rust.git] / src / test / compile-fail / trait-item-privacy.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 #![feature(associated_consts)]
12 #![feature(associated_type_defaults)]
13
14 struct S;
15
16 mod method {
17     trait A {
18         fn a(&self) { }
19     }
20
21     pub trait B {
22         fn b(&self) { }
23     }
24
25     pub trait C: A + B {
26         fn c(&self) { }
27     }
28
29     impl A for ::S {}
30     impl B for ::S {}
31     impl C for ::S {}
32 }
33
34 mod assoc_const {
35     trait A {
36         const A: u8 = 0;
37     }
38
39     pub trait B {
40         const B: u8 = 0;
41     }
42
43     pub trait C: A + B {
44         const C: u8 = 0;
45     }
46
47     impl A for ::S {}
48     impl B for ::S {}
49     impl C for ::S {}
50 }
51
52 mod assoc_ty {
53     trait A {
54         type A = u8;
55     }
56
57     pub trait B {
58         type B = u8;
59     }
60
61     pub trait C: A + B {
62         type C = u8;
63     }
64
65     impl A for ::S {}
66     impl B for ::S {}
67     impl C for ::S {}
68 }
69
70 fn check_method() {
71     // A is private
72     // B is pub, not in scope
73     // C : A + B is pub, in scope
74     use method::C;
75
76     // Methods, method call
77     // a, b, c are resolved as trait items, their traits need to be in scope
78     S.a(); //~ ERROR no method named `a` found for type `S` in the current scope
79     S.b(); //~ ERROR no method named `b` found for type `S` in the current scope
80     S.c(); // OK
81     // a, b, c are resolved as inherent items, their traits don't need to be in scope
82     let c = &S as &C;
83     c.a(); //~ ERROR method `a` is private
84     c.b(); // OK
85     c.c(); // OK
86
87     // Methods, UFCS
88     // a, b, c are resolved as trait items, their traits need to be in scope
89     S::a(&S);
90     //~^ ERROR no function or associated item named `a` found for type `S`
91     S::b(&S);
92     //~^ ERROR no function or associated item named `b` found for type `S`
93     S::c(&S); // OK
94     // a, b, c are resolved as inherent items, their traits don't need to be in scope
95     C::a(&S); //~ ERROR method `a` is private
96     C::b(&S); // OK
97     C::c(&S); // OK
98 }
99
100 fn check_assoc_const() {
101     // A is private
102     // B is pub, not in scope
103     // C : A + B is pub, in scope
104     use assoc_const::C;
105
106     // Associated constants
107     // A, B, C are resolved as trait items, their traits need to be in scope
108     S::A; //~ ERROR no associated item named `A` found for type `S` in the current scope
109     S::B; //~ ERROR no associated item named `B` found for type `S` in the current scope
110     S::C; // OK
111     // A, B, C are resolved as inherent items, their traits don't need to be in scope
112     C::A; //~ ERROR associated constant `A` is private
113           //~^ ERROR the trait `assoc_const::C` cannot be made into an object
114           //~| ERROR the trait bound `assoc_const::C: assoc_const::A` is not satisfied
115     C::B; // ERROR the trait `assoc_const::C` cannot be made into an object
116           //~^ ERROR the trait bound `assoc_const::C: assoc_const::B` is not satisfied
117     C::C; // OK
118 }
119
120 fn check_assoc_ty<T: assoc_ty::C>() {
121     // A is private
122     // B is pub, not in scope
123     // C : A + B is pub, in scope
124     use assoc_ty::C;
125
126     // Associated types
127     // A, B, C are resolved as trait items, their traits need to be in scope, not implemented yet
128     let _: S::A; //~ ERROR ambiguous associated type
129     let _: S::B; //~ ERROR ambiguous associated type
130     let _: S::C; //~ ERROR ambiguous associated type
131     // A, B, C are resolved as inherent items, their traits don't need to be in scope
132     let _: T::A; //~ ERROR associated type `A` is private
133     let _: T::B; // OK
134     let _: T::C; // OK
135 }
136
137 fn main() {}