]> git.lizzy.rs Git - rust.git/blob - src/test/ui/privacy/private-inferred-type.rs
Auto merge of #54624 - arielb1:evaluate-outlives, r=nikomatsakis
[rust.git] / src / test / ui / privacy / private-inferred-type.rs
1 // Copyright 2017 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(decl_macro)]
13 #![allow(private_in_public)]
14
15 mod m {
16     fn priv_fn() {}
17     static PRIV_STATIC: u8 = 0;
18     enum PrivEnum { Variant }
19     pub enum PubEnum { Variant }
20     trait PrivTrait { fn method() {} }
21     impl PrivTrait for u8 {}
22     pub trait PubTrait { fn method() {} }
23     impl PubTrait for u8 {}
24     struct PrivTupleStruct(u8);
25     pub struct PubTupleStruct(u8);
26     impl PubTupleStruct { fn method() {} }
27
28     struct Priv;
29     pub type Alias = Priv;
30     pub struct Pub<T = Alias>(pub T);
31
32     impl Pub<Priv> {
33         pub fn static_method() {}
34         pub const INHERENT_ASSOC_CONST: u8 = 0;
35     }
36     impl<T> Pub<T> {
37         pub fn static_method_generic_self() {}
38         pub const INHERENT_ASSOC_CONST_GENERIC_SELF: u8 = 0;
39     }
40     impl Pub<u8> {
41         fn priv_method(&self) {}
42         pub fn method_with_substs<T>(&self) {}
43         pub fn method_with_priv_params(&self, _: Priv) {}
44     }
45     impl TraitWithAssocConst for Priv {}
46     impl TraitWithAssocTy for Priv { type AssocTy = u8; }
47
48     pub macro m() {
49         priv_fn; //~ ERROR type `fn() {m::priv_fn}` is private
50         PRIV_STATIC; // OK, not cross-crate
51         PrivEnum::Variant; //~ ERROR type `m::PrivEnum` is private
52         PubEnum::Variant; // OK
53         <u8 as PrivTrait>::method; //~ ERROR type `fn() {<u8 as m::PrivTrait>::method}` is private
54         <u8 as PubTrait>::method; // OK
55         PrivTupleStruct;
56         //~^ ERROR type `fn(u8) -> m::PrivTupleStruct {m::PrivTupleStruct::{{constructor}}}` is priv
57         PubTupleStruct;
58         //~^ ERROR type `fn(u8) -> m::PubTupleStruct {m::PubTupleStruct::{{constructor}}}` is privat
59         Pub(0u8).priv_method();
60         //~^ ERROR type `for<'r> fn(&'r m::Pub<u8>) {<m::Pub<u8>>::priv_method}` is private
61     }
62
63     trait Trait {}
64     pub trait TraitWithTyParam<T> {}
65     pub trait TraitWithTyParam2<T> { fn pub_method() {} }
66     pub trait TraitWithAssocTy { type AssocTy; }
67     pub trait TraitWithAssocConst { const TRAIT_ASSOC_CONST: u8 = 0; }
68     impl Trait for u8 {}
69     impl<T> TraitWithTyParam<T> for u8 {}
70     impl TraitWithTyParam2<Priv> for u8 {}
71     impl TraitWithAssocTy for u8 { type AssocTy = Priv; }
72     //~^ ERROR private type `m::Priv` in public interface
73
74     pub fn leak_anon1() -> impl Trait + 'static { 0 }
75     pub fn leak_anon2() -> impl TraitWithTyParam<Alias> { 0 }
76     pub fn leak_anon3() -> impl TraitWithAssocTy<AssocTy = Alias> { 0 }
77
78     pub fn leak_dyn1() -> Box<Trait + 'static> { Box::new(0) }
79     pub fn leak_dyn2() -> Box<TraitWithTyParam<Alias>> { Box::new(0) }
80     pub fn leak_dyn3() -> Box<TraitWithAssocTy<AssocTy = Alias>> { Box::new(0) }
81 }
82
83 mod adjust {
84     // Construct a chain of derefs with a private type in the middle
85     use std::ops::Deref;
86
87     pub struct S1;
88     struct S2;
89     pub type S2Alias = S2;
90     pub struct S3;
91
92     impl Deref for S1 {
93         type Target = S2Alias; //~ ERROR private type `adjust::S2` in public interface
94         fn deref(&self) -> &Self::Target { loop {} }
95     }
96     impl Deref for S2 {
97         type Target = S3;
98         fn deref(&self) -> &Self::Target { loop {} }
99     }
100
101     impl S3 {
102         pub fn method_s3(&self) {}
103     }
104 }
105
106 fn main() {
107     let _: m::Alias; //~ ERROR type `m::Priv` is private
108                      //~^ ERROR type `m::Priv` is private
109     let _: <m::Alias as m::TraitWithAssocTy>::AssocTy; //~ ERROR type `m::Priv` is private
110     m::Alias {}; //~ ERROR type `m::Priv` is private
111     m::Pub { 0: m::Alias {} }; //~ ERROR type `m::Priv` is private
112     m::Pub { 0: loop {} }; // OK, `m::Pub` is in value context, so it means Pub<_>, not Pub<Priv>
113     m::Pub::static_method; //~ ERROR type `m::Priv` is private
114     m::Pub::INHERENT_ASSOC_CONST; //~ ERROR type `m::Priv` is private
115     m::Pub(0u8).method_with_substs::<m::Alias>(); //~ ERROR type `m::Priv` is private
116     m::Pub(0u8).method_with_priv_params(loop{}); //~ ERROR type `m::Priv` is private
117     <m::Alias as m::TraitWithAssocConst>::TRAIT_ASSOC_CONST; //~ ERROR type `m::Priv` is private
118     <m::Pub<m::Alias>>::INHERENT_ASSOC_CONST; //~ ERROR type `m::Priv` is private
119     <m::Pub<m::Alias>>::INHERENT_ASSOC_CONST_GENERIC_SELF; //~ ERROR type `m::Priv` is private
120     <m::Pub<m::Alias>>::static_method_generic_self; //~ ERROR type `m::Priv` is private
121     use m::TraitWithTyParam2;
122     u8::pub_method; //~ ERROR type `m::Priv` is private
123
124     adjust::S1.method_s3(); //~ ERROR type `adjust::S2` is private
125
126     m::m!();
127
128     m::leak_anon1(); //~ ERROR trait `m::Trait` is private
129     m::leak_anon2(); //~ ERROR type `m::Priv` is private
130     m::leak_anon3(); //~ ERROR type `m::Priv` is private
131
132     m::leak_dyn1(); //~ ERROR type `(dyn m::Trait + 'static)` is private
133     m::leak_dyn2(); //~ ERROR type `m::Priv` is private
134     m::leak_dyn3(); //~ ERROR type `m::Priv` is private
135
136     // Check that messages are not duplicated for various kinds of assignments
137     let a = m::Alias {}; //~ ERROR type `m::Priv` is private
138     let mut b = a; //~ ERROR type `m::Priv` is private
139     b = a; //~ ERROR type `m::Priv` is private
140     match a { //~ ERROR type `m::Priv` is private
141         _ => {}
142     }
143 }