]> git.proxmox.com Git - rustc.git/blame - src/test/compile-fail/private-inferred-type.rs
New upstream version 1.22.1+dfsg1
[rustc.git] / src / test / compile-fail / private-inferred-type.rs
CommitLineData
041b39d2
XL
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(conservative_impl_trait)]
13#![feature(decl_macro)]
3b2f2976 14#![allow(warnings)]
041b39d2
XL
15
16mod m {
17 fn priv_fn() {}
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 PrivEnum::Variant; //~ ERROR type `m::PrivEnum` is private
51 PubEnum::Variant; // OK
52 <u8 as PrivTrait>::method; //~ ERROR type `fn() {<u8 as m::PrivTrait>::method}` is private
53 <u8 as PubTrait>::method; // OK
54 PrivTupleStruct;
55 //~^ ERROR type `fn(u8) -> m::PrivTupleStruct {m::PrivTupleStruct::{{constructor}}}` is priv
56 PubTupleStruct;
57 //~^ ERROR type `fn(u8) -> m::PubTupleStruct {m::PubTupleStruct::{{constructor}}}` is privat
58 Pub(0u8).priv_method();
59 //~^ ERROR type `fn(&m::Pub<u8>) {<m::Pub<u8>>::priv_method}` is private
60 }
61
62 trait Trait {}
63 pub trait TraitWithTyParam<T> {}
64 pub trait TraitWithTyParam2<T> { fn pub_method() {} }
65 pub trait TraitWithAssocTy { type AssocTy; }
66 pub trait TraitWithAssocConst { const TRAIT_ASSOC_CONST: u8 = 0; }
67 impl Trait for u8 {}
68 impl<T> TraitWithTyParam<T> for u8 {}
69 impl TraitWithTyParam2<Priv> for u8 {}
70 impl TraitWithAssocTy for u8 { type AssocTy = Priv; }
71
72 pub fn leak_anon1() -> impl Trait + 'static { 0 }
73 pub fn leak_anon2() -> impl TraitWithTyParam<Alias> { 0 }
74 pub fn leak_anon3() -> impl TraitWithAssocTy<AssocTy = Alias> { 0 }
75
76 pub fn leak_dyn1() -> Box<Trait + 'static> { Box::new(0) }
77 pub fn leak_dyn2() -> Box<TraitWithTyParam<Alias>> { Box::new(0) }
78 pub fn leak_dyn3() -> Box<TraitWithAssocTy<AssocTy = Alias>> { Box::new(0) }
79}
80
81mod adjust {
82 // Construct a chain of derefs with a private type in the middle
83 use std::ops::Deref;
84
85 pub struct S1;
86 struct S2;
87 pub type S2Alias = S2;
88 pub struct S3;
89
90 impl Deref for S1 {
91 type Target = S2Alias;
92 fn deref(&self) -> &Self::Target { loop {} }
93 }
94 impl Deref for S2 {
95 type Target = S3;
96 fn deref(&self) -> &Self::Target { loop {} }
97 }
98
99 impl S3 {
100 pub fn method_s3(&self) {}
101 }
102}
103
104fn main() {
105 let _: m::Alias; //~ ERROR type `m::Priv` is private
ea8adc8c
XL
106 //~^ ERROR type `m::Priv` is private
107 let _: <m::Alias as m::TraitWithAssocTy>::AssocTy; //~ ERROR type `m::Priv` is private
041b39d2
XL
108 m::Alias {}; //~ ERROR type `m::Priv` is private
109 m::Pub { 0: m::Alias {} }; //~ ERROR type `m::Priv` is private
ea8adc8c 110 m::Pub { 0: loop {} }; // OK, `m::Pub` is in value context, so it means Pub<_>, not Pub<Priv>
041b39d2
XL
111 m::Pub::static_method; //~ ERROR type `m::Priv` is private
112 m::Pub::INHERENT_ASSOC_CONST; //~ ERROR type `m::Priv` is private
113 m::Pub(0u8).method_with_substs::<m::Alias>(); //~ ERROR type `m::Priv` is private
114 m::Pub(0u8).method_with_priv_params(loop{}); //~ ERROR type `m::Priv` is private
115 <m::Alias as m::TraitWithAssocConst>::TRAIT_ASSOC_CONST; //~ ERROR type `m::Priv` is private
116 <m::Pub<m::Alias>>::INHERENT_ASSOC_CONST; //~ ERROR type `m::Priv` is private
117 <m::Pub<m::Alias>>::INHERENT_ASSOC_CONST_GENERIC_SELF; //~ ERROR type `m::Priv` is private
118 <m::Pub<m::Alias>>::static_method_generic_self; //~ ERROR type `m::Priv` is private
119 use m::TraitWithTyParam2;
120 u8::pub_method; //~ ERROR type `m::Priv` is private
121
122 adjust::S1.method_s3(); //~ ERROR type `adjust::S2` is private
123
124 m::m!();
125
126 m::leak_anon1(); //~ ERROR trait `m::Trait` is private
127 m::leak_anon2(); //~ ERROR type `m::Priv` is private
128 m::leak_anon3(); //~ ERROR type `m::Priv` is private
129
130 m::leak_dyn1(); //~ ERROR type `m::Trait + 'static` is private
131 m::leak_dyn2(); //~ ERROR type `m::Priv` is private
132 m::leak_dyn3(); //~ ERROR type `m::Priv` is private
133
134 // Check that messages are not duplicated for various kinds of assignments
135 let a = m::Alias {}; //~ ERROR type `m::Priv` is private
136 let mut b = a; //~ ERROR type `m::Priv` is private
137 b = a; //~ ERROR type `m::Priv` is private
138 match a { //~ ERROR type `m::Priv` is private
139 _ => {}
140 }
141}