Skip to content

Commit

Permalink
Add tests for pub(restricted)
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Apr 16, 2016
1 parent daec3fe commit e14504a
Show file tree
Hide file tree
Showing 6 changed files with 205 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/test/auxiliary/pub_restricted.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]

pub(crate) struct Crate;
#[derive(Default)]
pub struct Universe {
pub x: i32,
pub(crate) y: i32
}

impl Universe {
pub fn f(&self) {}
pub(crate) fn g(&self) {}
}
25 changes: 25 additions & 0 deletions src/test/compile-fail/privacy/restricted/feature-gate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

pub(crate) //~ ERROR experimental
mod foo {}

pub(self) //~ ERROR experimental
mod bar {}

struct S {
pub(self) x: i32, //~ ERROR experimental
}
impl S {
pub(self) fn f() {} //~ ERROR experimental
}
extern {
pub(self) fn f(); //~ ERROR experimental
}
44 changes: 44 additions & 0 deletions src/test/compile-fail/privacy/restricted/lookup-ignores-private.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(rustc_attrs, pub_restricted)]
#![allow(warnings)]

mod foo {
pub use foo::bar::S;
mod bar {
#[derive(Default)]
pub struct S {
pub(foo) x: i32,
}
impl S {
pub(foo) fn f(&self) -> i32 { 0 }
}

pub struct S2 {
pub(crate) x: bool,
}
impl S2 {
pub(crate) fn f(&self) -> bool { false }
}

impl ::std::ops::Deref for S {
type Target = S2;
fn deref(&self) -> &S2 { unimplemented!() }
}
}
}

#[rustc_error]
fn main() { //~ ERROR compilation successful
let s = foo::S::default();
let _: bool = s.x;
let _: bool = s.f();
}
20 changes: 20 additions & 0 deletions src/test/compile-fail/privacy/restricted/private-in-public.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]

mod foo {
struct Priv;
mod bar {
use foo::Priv;
pub(super) fn f(_: Priv) {}
pub(crate) fn f(_: Priv) {} //~ ERROR private
}
}
31 changes: 31 additions & 0 deletions src/test/compile-fail/privacy/restricted/struct-literal-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]
#![deny(private_in_public)]
#![allow(warnings)]

mod foo {
pub mod bar {
pub struct S {
pub(foo) x: i32,
}
}

fn f() {
use foo::bar::S;
S { x: 0 }; // ok
}
}

fn main() {
use foo::bar::S;
S { x: 0 }; //~ ERROR private
}
62 changes: 62 additions & 0 deletions src/test/compile-fail/privacy/restricted/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:pub_restricted.rs

#![feature(pub_restricted)]
#![deny(private_in_public)]
#![allow(warnings)]
extern crate pub_restricted;

mod foo {
pub mod bar {
pub(super) fn f() {}
#[derive(Default)]
pub struct S {
pub(super) x: i32,
}
impl S {
pub(super) fn f(&self) {}
pub(super) fn g() {}
}
}
fn f() {
use foo::bar::S;
pub(self) use foo::bar::f; // ok
pub(super) use foo::bar::f as g; //~ ERROR cannot be reexported
S::default().x; // ok
S::default().f(); // ok
S::g(); // ok
}
}

fn f() {
use foo::bar::S;
use foo::bar::f; //~ ERROR private
S::default().x; //~ ERROR private
S::default().f(); //~ ERROR private
S::g(); //~ ERROR private
}

fn main() {
use pub_restricted::Universe;
use pub_restricted::Crate; //~ ERROR private

let u = Universe::default();
let _ = u.x;
let _ = u.y; //~ ERROR private
u.f();
u.g(); //~ ERROR private
}

mod pathological {
pub(bad::path) mod m1 {} //~ ERROR failed to resolve module path
pub(foo) mod m2 {} //~ ERROR visibilities can only be restricted to ancestor modules
}

0 comments on commit e14504a

Please sign in to comment.