Skip to content

Commit

Permalink
Rollup merge of rust-lang#40556 - cramertj:stabilize-pub-restricted, …
Browse files Browse the repository at this point in the history
…r=petrochenkov

Stabilize pub(restricted)

Fix rust-lang#32409
  • Loading branch information
frewsxcv authored Mar 21, 2017
2 parents 03235cc + 60c1c96 commit 17656ab
Show file tree
Hide file tree
Showing 28 changed files with 78 additions and 107 deletions.
1 change: 0 additions & 1 deletion src/doc/unstable-book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
- [plugin_registrar](plugin-registrar.md)
- [prelude_import](prelude-import.md)
- [proc_macro](proc-macro.md)
- [pub_restricted](pub-restricted.md)
- [quote](quote.md)
- [relaxed_adts](relaxed-adts.md)
- [repr_simd](repr-simd.md)
Expand Down
10 changes: 0 additions & 10 deletions src/doc/unstable-book/src/pub-restricted.md

This file was deleted.

12 changes: 12 additions & 0 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,18 @@ pub enum Visibility {
Inherited,
}

impl Visibility {
pub fn is_pub_restricted(&self) -> bool {
use self::Visibility::*;
match self {
&Public |
&Inherited => false,
&Crate |
&Restricted { .. } => true,
}
}
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct StructField {
pub span: Span,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#![feature(libc)]
#![feature(loop_break_value)]
#![feature(nonzero)]
#![feature(pub_restricted)]
#![cfg_attr(stage0, feature(pub_restricted))]
#![feature(quote)]
#![feature(rustc_diagnostic_macros)]
#![feature(rustc_private)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#![feature(core_intrinsics)]
#![feature(conservative_impl_trait)]
#![cfg_attr(stage0,feature(field_init_shorthand))]
#![feature(pub_restricted)]
#![cfg_attr(stage0, feature(pub_restricted))]

extern crate graphviz;
#[macro_use] extern crate rustc;
Expand Down
38 changes: 36 additions & 2 deletions src/librustc_privacy/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ use std::mem::replace;

pub mod diagnostics;

////////////////////////////////////////////////////////////////////////////////
/// Visitor used to determine if pub(restricted) is used anywhere in the crate.
///
/// This is done so that `private_in_public` warnings can be turned into hard errors
/// in crates that have been updated to use pub(restricted).
////////////////////////////////////////////////////////////////////////////////
struct PubRestrictedVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
has_pub_restricted: bool,
}

impl<'a, 'tcx> Visitor<'tcx> for PubRestrictedVisitor<'a, 'tcx> {
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
NestedVisitorMap::All(&self.tcx.hir)
}
fn visit_vis(&mut self, vis: &'tcx hir::Visibility) {
self.has_pub_restricted = self.has_pub_restricted || vis.is_pub_restricted();
}
}

////////////////////////////////////////////////////////////////////////////////
/// The embargo visitor, used to determine the exports of the ast
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -891,6 +911,7 @@ struct SearchInterfaceForPrivateItemsVisitor<'a, 'tcx: 'a> {
required_visibility: ty::Visibility,
/// The visibility of the least visible component that has been visited
min_visibility: ty::Visibility,
has_pub_restricted: bool,
has_old_errors: bool,
}

Expand Down Expand Up @@ -951,7 +972,7 @@ impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'
self.min_visibility = vis;
}
if !vis.is_at_least(self.required_visibility, self.tcx) {
if self.tcx.sess.features.borrow().pub_restricted || self.has_old_errors {
if self.has_pub_restricted || self.has_old_errors {
let mut err = struct_span_err!(self.tcx.sess, self.span, E0446,
"private type `{}` in public interface", ty);
err.span_label(self.span, &format!("can't leak private type"));
Expand Down Expand Up @@ -986,7 +1007,7 @@ impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'
self.min_visibility = vis;
}
if !vis.is_at_least(self.required_visibility, self.tcx) {
if self.tcx.sess.features.borrow().pub_restricted || self.has_old_errors {
if self.has_pub_restricted || self.has_old_errors {
struct_span_err!(self.tcx.sess, self.span, E0445,
"private trait `{}` in public interface", trait_ref)
.span_label(self.span, &format!(
Expand All @@ -1008,6 +1029,7 @@ impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'

struct PrivateItemsInPublicInterfacesVisitor<'a, 'tcx: 'a> {
tcx: TyCtxt<'a, 'tcx, 'tcx>,
has_pub_restricted: bool,
old_error_set: &'a NodeSet,
inner_visibility: ty::Visibility,
}
Expand Down Expand Up @@ -1044,6 +1066,7 @@ impl<'a, 'tcx> PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
span: self.tcx.hir.span(item_id),
min_visibility: ty::Visibility::Public,
required_visibility: required_visibility,
has_pub_restricted: self.has_pub_restricted,
has_old_errors: has_old_errors,
}
}
Expand Down Expand Up @@ -1227,9 +1250,20 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
};
intravisit::walk_crate(&mut visitor, krate);


let has_pub_restricted = {
let mut pub_restricted_visitor = PubRestrictedVisitor {
tcx: tcx,
has_pub_restricted: false
};
intravisit::walk_crate(&mut pub_restricted_visitor, krate);
pub_restricted_visitor.has_pub_restricted
};

// Check for private types and traits in public interfaces
let mut visitor = PrivateItemsInPublicInterfacesVisitor {
tcx: tcx,
has_pub_restricted: has_pub_restricted,
old_error_set: &visitor.old_error_set,
inner_visibility: ty::Visibility::Public,
};
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@
#![feature(placement_in_syntax)]
#![feature(placement_new_protocol)]
#![feature(prelude_import)]
#![feature(pub_restricted)]
#![feature(rand)]
#![feature(raw)]
#![feature(repr_simd)]
Expand All @@ -309,6 +308,7 @@
#![feature(vec_push_all)]
#![feature(zero_one)]
#![cfg_attr(test, feature(update_panic_count))]
#![cfg_attr(stage0, feature(pub_restricted))]

// Explicitly import the prelude. The compiler uses this same unstable attribute
// to import the prelude implicitly when building crates that depend on std.
Expand Down
17 changes: 3 additions & 14 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,6 @@ declare_features! (
// impl specialization (RFC 1210)
(active, specialization, "1.7.0", Some(31844)),

// pub(restricted) visibilities (RFC 1422)
(active, pub_restricted, "1.9.0", Some(32409)),

// Allow Drop types in statics/const functions (RFC 1440)
(active, drop_types_in_const, "1.9.0", Some(33156)),

Expand Down Expand Up @@ -409,6 +406,9 @@ declare_features! (
(accepted, field_init_shorthand, "1.17.0", Some(37340)),
// Allows the definition recursive static items.
(accepted, static_recursion, "1.17.0", Some(29719)),
// pub(restricted) visibilities (RFC 1422)
(accepted, pub_restricted, "1.17.0", Some(32409)),

);
// If you change this, please modify src/doc/unstable-book as well. You must
// move that documentation into the relevant place in the other docs, and
Expand Down Expand Up @@ -1417,17 +1417,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
visit::walk_impl_item(self, ii);
}

fn visit_vis(&mut self, vis: &'a ast::Visibility) {
let span = match *vis {
ast::Visibility::Crate(span) => span,
ast::Visibility::Restricted { ref path, .. } => path.span,
_ => return,
};
gate_feature_post!(&self, pub_restricted, span, "`pub(restricted)` syntax is experimental");

visit::walk_vis(self, vis)
}

fn visit_generics(&mut self, g: &'a ast::Generics) {
for t in &g.ty_params {
if !t.attrs.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
// non-pub fields, marked with SILLY below)

#![feature(staged_api)]
#![feature(pub_restricted)]

#![stable(feature = "unit_test", since = "0.0.0")]

Expand Down
1 change: 0 additions & 1 deletion src/test/compile-fail/imports/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]
#![deny(unused)]

mod foo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// 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 {
Expand Down
27 changes: 0 additions & 27 deletions src/test/compile-fail/privacy/restricted/feature-gate.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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

mod foo {
Expand Down
2 changes: 0 additions & 2 deletions src/test/compile-fail/privacy/restricted/private-in-public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]

mod foo {
struct Priv;
mod bar {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

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

Expand Down
1 change: 0 additions & 1 deletion src/test/compile-fail/privacy/restricted/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

// aux-build:pub_restricted.rs

#![feature(pub_restricted)]
#![deny(private_in_public)]
#![allow(warnings)]
extern crate pub_restricted;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]

mod foo {
type T = ();
struct S1(pub(foo) (), pub(T), pub(crate) (), pub(((), T)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]

macro_rules! define_struct {
($t:ty) => {
struct S1(pub $t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]

macro_rules! define_struct {
($t:ty) => {
struct S1(pub($t));
Expand Down
2 changes: 0 additions & 2 deletions src/test/compile-fail/privacy/restricted/ty-params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]

macro_rules! m {
($p: path) => (pub(in $p) struct Z;)
}
Expand Down
1 change: 0 additions & 1 deletion src/test/compile-fail/privacy/union-field-privacy-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]
#![feature(untagged_unions)]

mod m {
Expand Down
1 change: 0 additions & 1 deletion src/test/compile-fail/privacy/union-field-privacy-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]
#![feature(untagged_unions)]

mod m {
Expand Down
2 changes: 0 additions & 2 deletions src/test/compile-fail/resolve-bad-visibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]

enum E {}
trait Tr {}

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/resolve/auxiliary/privacy-struct-ctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(pub_restricted)]

pub mod m {
pub struct S(u8);

Expand Down
2 changes: 0 additions & 2 deletions src/test/ui/resolve/privacy-struct-ctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

// aux-build:privacy-struct-ctor.rs

#![feature(pub_restricted)]

extern crate privacy_struct_ctor as xcrate;

mod m {
Expand Down
Loading

0 comments on commit 17656ab

Please sign in to comment.