Skip to content

Commit e14504a

Browse files
committed
Add tests for pub(restricted)
1 parent daec3fe commit e14504a

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

src/test/auxiliary/pub_restricted.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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(pub_restricted)]
12+
13+
pub(crate) struct Crate;
14+
#[derive(Default)]
15+
pub struct Universe {
16+
pub x: i32,
17+
pub(crate) y: i32
18+
}
19+
20+
impl Universe {
21+
pub fn f(&self) {}
22+
pub(crate) fn g(&self) {}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
pub(crate) //~ ERROR experimental
12+
mod foo {}
13+
14+
pub(self) //~ ERROR experimental
15+
mod bar {}
16+
17+
struct S {
18+
pub(self) x: i32, //~ ERROR experimental
19+
}
20+
impl S {
21+
pub(self) fn f() {} //~ ERROR experimental
22+
}
23+
extern {
24+
pub(self) fn f(); //~ ERROR experimental
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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(rustc_attrs, pub_restricted)]
12+
#![allow(warnings)]
13+
14+
mod foo {
15+
pub use foo::bar::S;
16+
mod bar {
17+
#[derive(Default)]
18+
pub struct S {
19+
pub(foo) x: i32,
20+
}
21+
impl S {
22+
pub(foo) fn f(&self) -> i32 { 0 }
23+
}
24+
25+
pub struct S2 {
26+
pub(crate) x: bool,
27+
}
28+
impl S2 {
29+
pub(crate) fn f(&self) -> bool { false }
30+
}
31+
32+
impl ::std::ops::Deref for S {
33+
type Target = S2;
34+
fn deref(&self) -> &S2 { unimplemented!() }
35+
}
36+
}
37+
}
38+
39+
#[rustc_error]
40+
fn main() { //~ ERROR compilation successful
41+
let s = foo::S::default();
42+
let _: bool = s.x;
43+
let _: bool = s.f();
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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(pub_restricted)]
12+
13+
mod foo {
14+
struct Priv;
15+
mod bar {
16+
use foo::Priv;
17+
pub(super) fn f(_: Priv) {}
18+
pub(crate) fn f(_: Priv) {} //~ ERROR private
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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(pub_restricted)]
12+
#![deny(private_in_public)]
13+
#![allow(warnings)]
14+
15+
mod foo {
16+
pub mod bar {
17+
pub struct S {
18+
pub(foo) x: i32,
19+
}
20+
}
21+
22+
fn f() {
23+
use foo::bar::S;
24+
S { x: 0 }; // ok
25+
}
26+
}
27+
28+
fn main() {
29+
use foo::bar::S;
30+
S { x: 0 }; //~ ERROR private
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
// aux-build:pub_restricted.rs
12+
13+
#![feature(pub_restricted)]
14+
#![deny(private_in_public)]
15+
#![allow(warnings)]
16+
extern crate pub_restricted;
17+
18+
mod foo {
19+
pub mod bar {
20+
pub(super) fn f() {}
21+
#[derive(Default)]
22+
pub struct S {
23+
pub(super) x: i32,
24+
}
25+
impl S {
26+
pub(super) fn f(&self) {}
27+
pub(super) fn g() {}
28+
}
29+
}
30+
fn f() {
31+
use foo::bar::S;
32+
pub(self) use foo::bar::f; // ok
33+
pub(super) use foo::bar::f as g; //~ ERROR cannot be reexported
34+
S::default().x; // ok
35+
S::default().f(); // ok
36+
S::g(); // ok
37+
}
38+
}
39+
40+
fn f() {
41+
use foo::bar::S;
42+
use foo::bar::f; //~ ERROR private
43+
S::default().x; //~ ERROR private
44+
S::default().f(); //~ ERROR private
45+
S::g(); //~ ERROR private
46+
}
47+
48+
fn main() {
49+
use pub_restricted::Universe;
50+
use pub_restricted::Crate; //~ ERROR private
51+
52+
let u = Universe::default();
53+
let _ = u.x;
54+
let _ = u.y; //~ ERROR private
55+
u.f();
56+
u.g(); //~ ERROR private
57+
}
58+
59+
mod pathological {
60+
pub(bad::path) mod m1 {} //~ ERROR failed to resolve module path
61+
pub(foo) mod m2 {} //~ ERROR visibilities can only be restricted to ancestor modules
62+
}

0 commit comments

Comments
 (0)