Skip to content

Commit 06b23b7

Browse files
committed
Add some tests for public-private dependencies.
1 parent c8813dd commit 06b23b7

16 files changed

+360
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@ aux-crate:priv:c=c.rs
2+
//@ compile-flags: -Zunstable-options
3+
4+
extern crate c;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
//@ aux-crate:shared=shared.rs
2+
3+
// This is public.
4+
extern crate shared;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ aux-crate:shared=shared.rs
2+
3+
extern crate shared;
4+
5+
pub use shared::Shared;
6+
7+
pub struct SharedInType {
8+
pub f: Shared
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ aux-crate:shared=shared.rs
2+
3+
extern crate shared;
4+
5+
pub use shared::Shared;
6+
7+
pub struct SharedInType {
8+
pub f: Shared
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ force-host
2+
//@ no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
use proc_macro::TokenStream;
8+
9+
#[proc_macro]
10+
pub fn fn_like(input: TokenStream) -> TokenStream {
11+
"".parse().unwrap()
12+
}
13+
14+
#[proc_macro_derive(PmDerive)]
15+
pub fn pm_derive(item: TokenStream) -> TokenStream {
16+
"".parse().unwrap()
17+
}
18+
19+
#[proc_macro_attribute]
20+
pub fn pm_attr(attr: TokenStream, item: TokenStream) -> TokenStream {
21+
"".parse().unwrap()
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
11
pub struct OtherType;
22
pub trait OtherTrait {}
3+
4+
#[macro_export]
5+
macro_rules! m {
6+
() => {};
7+
}
8+
9+
pub enum E {
10+
V1
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
//@ aux-crate:shared=shared.rs
2+
3+
extern crate shared;
4+
5+
pub use shared::Shared;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub struct Shared;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//@ aux-crate:priv:diamond_priv_dep=diamond_priv_dep.rs
2+
//@ aux-crate:diamond_pub_dep=diamond_pub_dep.rs
3+
//@ compile-flags: -Zunstable-options
4+
5+
// A diamond dependency:
6+
//
7+
// diamond_reepxort
8+
// /\
9+
// (public) / \ (PRIVATE)
10+
// / \
11+
// diamond_pub_dep diamond_priv_dep
12+
// \ /
13+
// (public) \ / (public)
14+
// \/
15+
// shared
16+
//
17+
// Where the pub and private crates reexport something from the shared crate.
18+
//
19+
// Checks the behavior when the same shared item appears in the public API,
20+
// depending on whether it comes from the public side or the private side.
21+
//
22+
// NOTE: compiletest does not support deduplicating shared dependencies.
23+
// However, it should work well enough for this test, the only downside is
24+
// that diamond_shared gets built twice.
25+
26+
#![crate_type = "lib"]
27+
#![deny(exported_private_dependencies)]
28+
29+
extern crate diamond_priv_dep;
30+
extern crate diamond_pub_dep;
31+
32+
// FIXME: This should trigger.
33+
pub fn leaks_priv() -> diamond_priv_dep::Shared {
34+
diamond_priv_dep::Shared
35+
}
36+
37+
pub fn leaks_pub() -> diamond_pub_dep::Shared {
38+
diamond_pub_dep::Shared
39+
}
40+
41+
pub struct PrivInStruct {
42+
pub f: diamond_priv_dep::SharedInType
43+
//~^ ERROR type `diamond_priv_dep::SharedInType` from private dependency 'diamond_priv_dep' in public interface
44+
}
45+
46+
pub struct PubInStruct {
47+
pub f: diamond_pub_dep::SharedInType
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: type `diamond_priv_dep::SharedInType` from private dependency 'diamond_priv_dep' in public interface
2+
--> $DIR/diamond_deps.rs:42:5
3+
|
4+
LL | pub f: diamond_priv_dep::SharedInType
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/diamond_deps.rs:27:9
9+
|
10+
LL | #![deny(exported_private_dependencies)]
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+

tests/ui/privacy/pub-priv-dep/pub-priv1.rs

+65-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
//@ aux-crate:priv:priv_dep=priv_dep.rs
22
//@ aux-build:pub_dep.rs
3+
//@ aux-crate:priv:pm=pm.rs
34
//@ compile-flags: -Zunstable-options
5+
6+
// Basic behavior check of exported_private_dependencies from either a public
7+
// dependency or a private one.
8+
49
#![deny(exported_private_dependencies)]
510

611
// This crate is a private dependency
7-
extern crate priv_dep;
12+
// FIXME: This should trigger.
13+
pub extern crate priv_dep;
814
// This crate is a public dependency
915
extern crate pub_dep;
16+
// This crate is a private dependency
17+
extern crate pm;
1018

1119
use priv_dep::{OtherTrait, OtherType};
1220
use pub_dep::PubType;
@@ -25,7 +33,10 @@ pub struct PublicType {
2533
}
2634

2735
impl PublicType {
28-
pub fn pub_fn(param: OtherType) {}
36+
pub fn pub_fn_param(param: OtherType) {}
37+
//~^ ERROR type `OtherType` from private dependency 'priv_dep' in public interface
38+
39+
pub fn pub_fn_return() -> OtherType { OtherType }
2940
//~^ ERROR type `OtherType` from private dependency 'priv_dep' in public interface
3041

3142
fn priv_fn(param: OtherType) {}
@@ -36,9 +47,61 @@ pub trait MyPubTrait {
3647
}
3748
//~^^ ERROR trait `OtherTrait` from private dependency 'priv_dep' in public interface
3849

50+
pub trait WithSuperTrait: OtherTrait {}
51+
//~^ ERROR trait `OtherTrait` from private dependency 'priv_dep' in public interface
52+
53+
pub trait PubLocalTraitWithAssoc {
54+
type X;
55+
}
56+
57+
pub struct PrivateAssoc;
58+
impl PubLocalTraitWithAssoc for PrivateAssoc {
59+
type X = OtherType;
60+
//~^ ERROR type `OtherType` from private dependency 'priv_dep' in public interface
61+
}
62+
63+
pub fn in_bounds<T: OtherTrait>(x: T) { unimplemented!() }
64+
//~^ ERROR trait `OtherTrait` from private dependency 'priv_dep' in public interface
65+
66+
pub fn private_in_generic() -> std::num::Saturating<OtherType> { unimplemented!() }
67+
//~^ ERROR type `OtherType` from private dependency 'priv_dep' in public interface
68+
69+
pub static STATIC: OtherType = OtherType;
70+
//~^ ERROR type `OtherType` from private dependency 'priv_dep' in public interface
71+
72+
pub const CONST: OtherType = OtherType;
73+
//~^ ERROR type `OtherType` from private dependency 'priv_dep' in public interface
74+
75+
pub type Alias = OtherType;
76+
//~^ ERROR type `OtherType` from private dependency 'priv_dep' in public interface
77+
78+
pub struct PublicWithPrivateImpl;
79+
80+
// FIXME: This should trigger.
81+
// See https://github.com/rust-lang/rust/issues/71043
82+
impl OtherTrait for PublicWithPrivateImpl {}
83+
84+
pub trait PubTraitOnPrivate {}
85+
86+
// FIXME: This should trigger.
87+
// See https://github.com/rust-lang/rust/issues/71043
88+
impl PubTraitOnPrivate for OtherType {}
89+
3990
pub struct AllowedPrivType {
4091
#[allow(exported_private_dependencies)]
4192
pub allowed: OtherType,
4293
}
4394

95+
// FIXME: This should trigger.
96+
pub use priv_dep::m;
97+
// FIXME: This should trigger.
98+
pub use pm::fn_like;
99+
// FIXME: This should trigger.
100+
pub use pm::PmDerive;
101+
// FIXME: This should trigger.
102+
pub use pm::pm_attr;
103+
104+
// FIXME: This should trigger.
105+
pub use priv_dep::E::V1;
106+
44107
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,74 @@
11
error: type `OtherType` from private dependency 'priv_dep' in public interface
2-
--> $DIR/pub-priv1.rs:21:5
2+
--> $DIR/pub-priv1.rs:29:5
33
|
44
LL | pub field: OtherType,
55
| ^^^^^^^^^^^^^^^^^^^^
66
|
77
note: the lint level is defined here
8-
--> $DIR/pub-priv1.rs:4:9
8+
--> $DIR/pub-priv1.rs:9:9
99
|
1010
LL | #![deny(exported_private_dependencies)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: type `OtherType` from private dependency 'priv_dep' in public interface
14-
--> $DIR/pub-priv1.rs:28:5
14+
--> $DIR/pub-priv1.rs:36:5
1515
|
16-
LL | pub fn pub_fn(param: OtherType) {}
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
LL | pub fn pub_fn_param(param: OtherType) {}
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
19+
error: type `OtherType` from private dependency 'priv_dep' in public interface
20+
--> $DIR/pub-priv1.rs:39:5
21+
|
22+
LL | pub fn pub_fn_return() -> OtherType { OtherType }
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1824

1925
error: trait `OtherTrait` from private dependency 'priv_dep' in public interface
20-
--> $DIR/pub-priv1.rs:35:5
26+
--> $DIR/pub-priv1.rs:46:5
2127
|
2228
LL | type Foo: OtherTrait;
2329
| ^^^^^^^^^^^^^^^^^^^^
2430

25-
error: aborting due to 3 previous errors
31+
error: trait `OtherTrait` from private dependency 'priv_dep' in public interface
32+
--> $DIR/pub-priv1.rs:50:1
33+
|
34+
LL | pub trait WithSuperTrait: OtherTrait {}
35+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
37+
error: type `OtherType` from private dependency 'priv_dep' in public interface
38+
--> $DIR/pub-priv1.rs:59:5
39+
|
40+
LL | type X = OtherType;
41+
| ^^^^^^
42+
43+
error: trait `OtherTrait` from private dependency 'priv_dep' in public interface
44+
--> $DIR/pub-priv1.rs:63:1
45+
|
46+
LL | pub fn in_bounds<T: OtherTrait>(x: T) { unimplemented!() }
47+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
48+
49+
error: type `OtherType` from private dependency 'priv_dep' in public interface
50+
--> $DIR/pub-priv1.rs:66:1
51+
|
52+
LL | pub fn private_in_generic() -> std::num::Saturating<OtherType> { unimplemented!() }
53+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54+
55+
error: type `OtherType` from private dependency 'priv_dep' in public interface
56+
--> $DIR/pub-priv1.rs:69:1
57+
|
58+
LL | pub static STATIC: OtherType = OtherType;
59+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
60+
61+
error: type `OtherType` from private dependency 'priv_dep' in public interface
62+
--> $DIR/pub-priv1.rs:72:1
63+
|
64+
LL | pub const CONST: OtherType = OtherType;
65+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66+
67+
error: type `OtherType` from private dependency 'priv_dep' in public interface
68+
--> $DIR/pub-priv1.rs:75:1
69+
|
70+
LL | pub type Alias = OtherType;
71+
| ^^^^^^^^^^^^^^
72+
73+
error: aborting due to 11 previous errors
2674

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//@ aux-crate:priv:reexport=reexport.rs
2+
//@ compile-flags: -Zunstable-options
3+
//@ check-pass
4+
5+
// Checks the behavior of a reexported item from a private dependency.
6+
7+
#![crate_type = "lib"]
8+
#![deny(exported_private_dependencies)]
9+
10+
extern crate reexport;
11+
12+
// FIXME: This should trigger.
13+
pub fn leaks_priv() -> reexport::Shared {
14+
reexport::Shared
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//@ aux-crate:priv:shared=shared.rs
2+
//@ aux-crate:reexport=reexport.rs
3+
//@ compile-flags: -Zunstable-options
4+
//@ check-pass
5+
6+
// A shared dependency, where a private dependency reexports a public dependency.
7+
//
8+
// shared_both_private
9+
// /\
10+
// (PRIVATE) / | (PRIVATE)
11+
// / |
12+
// reexport |
13+
// \ |
14+
// (public) \ /
15+
// \/
16+
// shared
17+
18+
#![crate_type = "lib"]
19+
#![deny(exported_private_dependencies)]
20+
21+
extern crate shared;
22+
extern crate reexport;
23+
24+
// FIXME: This should trigger.
25+
pub fn leaks_priv() -> shared::Shared {
26+
shared::Shared
27+
}
28+
29+
// FIXME: This should trigger.
30+
pub fn leaks_priv_reexport() -> reexport::Shared {
31+
reexport::Shared
32+
}

0 commit comments

Comments
 (0)