]> git.lizzy.rs Git - rust.git/blob - src/test/compile-fail/trait-item-privacy.rs
rustdoc: Hide `self: Box<Self>` in list of deref methods
[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); //~ ERROR no associated item named `a` found for type `S` in the current scope
90     S::b(&S); //~ ERROR no associated item named `b` found for type `S` in the current scope
91     S::c(&S); // OK
92     // a, b, c are resolved as inherent items, their traits don't need to be in scope
93     C::a(&S); //~ ERROR method `a` is private
94     C::b(&S); // OK
95     C::c(&S); // OK
96 }
97
98 fn check_assoc_const() {
99     // A is private
100     // B is pub, not in scope
101     // C : A + B is pub, in scope
102     use assoc_const::C;
103
104     // Associated constants
105     // A, B, C are resolved as trait items, their traits need to be in scope
106     S::A; //~ ERROR no associated item named `A` found for type `S` in the current scope
107     S::B; //~ ERROR no associated item named `B` found for type `S` in the current scope
108     S::C; // OK
109     // A, B, C are resolved as inherent items, their traits don't need to be in scope
110     C::A; //~ ERROR associated constant `A` is private
111           //~^ ERROR the trait `assoc_const::C` cannot be made into an object
112           //~| ERROR the trait bound `assoc_const::C: assoc_const::A` is not satisfied
113     C::B; // ERROR the trait `assoc_const::C` cannot be made into an object
114           //~^ ERROR the trait bound `assoc_const::C: assoc_const::B` is not satisfied
115     C::C; // OK
116 }
117
118 fn check_assoc_ty<T: assoc_ty::C>() {
119     // A is private
120     // B is pub, not in scope
121     // C : A + B is pub, in scope
122     use assoc_ty::C;
123
124     // Associated types
125     // A, B, C are resolved as trait items, their traits need to be in scope, not implemented yet
126     let _: S::A; //~ ERROR ambiguous associated type
127     let _: S::B; //~ ERROR ambiguous associated type
128     let _: S::C; //~ ERROR ambiguous associated type
129     // A, B, C are resolved as inherent items, their traits don't need to be in scope
130     let _: T::A; //~ ERROR associated type `A` is private
131     let _: T::B; // OK
132     let _: T::C; // OK
133 }
134
135 fn main() {}