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