Skip to content

Commit 17656ab

Browse files
authoredMar 21, 2017
Rollup merge of rust-lang#40556 - cramertj:stabilize-pub-restricted, r=petrochenkov
Stabilize pub(restricted) Fix rust-lang#32409
2 parents 03235cc + 60c1c96 commit 17656ab

28 files changed

+78
-107
lines changed
 

‎src/doc/unstable-book/src/SUMMARY.md

-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
- [plugin_registrar](plugin-registrar.md)
6666
- [prelude_import](prelude-import.md)
6767
- [proc_macro](proc-macro.md)
68-
- [pub_restricted](pub-restricted.md)
6968
- [quote](quote.md)
7069
- [relaxed_adts](relaxed-adts.md)
7170
- [repr_simd](repr-simd.md)

‎src/doc/unstable-book/src/pub-restricted.md

-10
This file was deleted.

‎src/librustc/hir/mod.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,18 @@ pub enum Visibility {
14871487
Inherited,
14881488
}
14891489

1490+
impl Visibility {
1491+
pub fn is_pub_restricted(&self) -> bool {
1492+
use self::Visibility::*;
1493+
match self {
1494+
&Public |
1495+
&Inherited => false,
1496+
&Crate |
1497+
&Restricted { .. } => true,
1498+
}
1499+
}
1500+
}
1501+
14901502
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
14911503
pub struct StructField {
14921504
pub span: Span,

‎src/librustc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#![feature(libc)]
3535
#![feature(loop_break_value)]
3636
#![feature(nonzero)]
37-
#![feature(pub_restricted)]
37+
#![cfg_attr(stage0, feature(pub_restricted))]
3838
#![feature(quote)]
3939
#![feature(rustc_diagnostic_macros)]
4040
#![feature(rustc_private)]

‎src/librustc_incremental/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#![feature(core_intrinsics)]
2626
#![feature(conservative_impl_trait)]
2727
#![cfg_attr(stage0,feature(field_init_shorthand))]
28-
#![feature(pub_restricted)]
28+
#![cfg_attr(stage0, feature(pub_restricted))]
2929

3030
extern crate graphviz;
3131
#[macro_use] extern crate rustc;

‎src/librustc_privacy/lib.rs

+36-2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ use std::mem::replace;
4545

4646
pub mod diagnostics;
4747

48+
////////////////////////////////////////////////////////////////////////////////
49+
/// Visitor used to determine if pub(restricted) is used anywhere in the crate.
50+
///
51+
/// This is done so that `private_in_public` warnings can be turned into hard errors
52+
/// in crates that have been updated to use pub(restricted).
53+
////////////////////////////////////////////////////////////////////////////////
54+
struct PubRestrictedVisitor<'a, 'tcx: 'a> {
55+
tcx: TyCtxt<'a, 'tcx, 'tcx>,
56+
has_pub_restricted: bool,
57+
}
58+
59+
impl<'a, 'tcx> Visitor<'tcx> for PubRestrictedVisitor<'a, 'tcx> {
60+
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
61+
NestedVisitorMap::All(&self.tcx.hir)
62+
}
63+
fn visit_vis(&mut self, vis: &'tcx hir::Visibility) {
64+
self.has_pub_restricted = self.has_pub_restricted || vis.is_pub_restricted();
65+
}
66+
}
67+
4868
////////////////////////////////////////////////////////////////////////////////
4969
/// The embargo visitor, used to determine the exports of the ast
5070
////////////////////////////////////////////////////////////////////////////////
@@ -891,6 +911,7 @@ struct SearchInterfaceForPrivateItemsVisitor<'a, 'tcx: 'a> {
891911
required_visibility: ty::Visibility,
892912
/// The visibility of the least visible component that has been visited
893913
min_visibility: ty::Visibility,
914+
has_pub_restricted: bool,
894915
has_old_errors: bool,
895916
}
896917

@@ -951,7 +972,7 @@ impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'
951972
self.min_visibility = vis;
952973
}
953974
if !vis.is_at_least(self.required_visibility, self.tcx) {
954-
if self.tcx.sess.features.borrow().pub_restricted || self.has_old_errors {
975+
if self.has_pub_restricted || self.has_old_errors {
955976
let mut err = struct_span_err!(self.tcx.sess, self.span, E0446,
956977
"private type `{}` in public interface", ty);
957978
err.span_label(self.span, &format!("can't leak private type"));
@@ -986,7 +1007,7 @@ impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'
9861007
self.min_visibility = vis;
9871008
}
9881009
if !vis.is_at_least(self.required_visibility, self.tcx) {
989-
if self.tcx.sess.features.borrow().pub_restricted || self.has_old_errors {
1010+
if self.has_pub_restricted || self.has_old_errors {
9901011
struct_span_err!(self.tcx.sess, self.span, E0445,
9911012
"private trait `{}` in public interface", trait_ref)
9921013
.span_label(self.span, &format!(
@@ -1008,6 +1029,7 @@ impl<'a, 'tcx: 'a> TypeVisitor<'tcx> for SearchInterfaceForPrivateItemsVisitor<'
10081029

10091030
struct PrivateItemsInPublicInterfacesVisitor<'a, 'tcx: 'a> {
10101031
tcx: TyCtxt<'a, 'tcx, 'tcx>,
1032+
has_pub_restricted: bool,
10111033
old_error_set: &'a NodeSet,
10121034
inner_visibility: ty::Visibility,
10131035
}
@@ -1044,6 +1066,7 @@ impl<'a, 'tcx> PrivateItemsInPublicInterfacesVisitor<'a, 'tcx> {
10441066
span: self.tcx.hir.span(item_id),
10451067
min_visibility: ty::Visibility::Public,
10461068
required_visibility: required_visibility,
1069+
has_pub_restricted: self.has_pub_restricted,
10471070
has_old_errors: has_old_errors,
10481071
}
10491072
}
@@ -1227,9 +1250,20 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
12271250
};
12281251
intravisit::walk_crate(&mut visitor, krate);
12291252

1253+
1254+
let has_pub_restricted = {
1255+
let mut pub_restricted_visitor = PubRestrictedVisitor {
1256+
tcx: tcx,
1257+
has_pub_restricted: false
1258+
};
1259+
intravisit::walk_crate(&mut pub_restricted_visitor, krate);
1260+
pub_restricted_visitor.has_pub_restricted
1261+
};
1262+
12301263
// Check for private types and traits in public interfaces
12311264
let mut visitor = PrivateItemsInPublicInterfacesVisitor {
12321265
tcx: tcx,
1266+
has_pub_restricted: has_pub_restricted,
12331267
old_error_set: &visitor.old_error_set,
12341268
inner_visibility: ty::Visibility::Public,
12351269
};

‎src/libstd/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@
283283
#![feature(placement_in_syntax)]
284284
#![feature(placement_new_protocol)]
285285
#![feature(prelude_import)]
286-
#![feature(pub_restricted)]
287286
#![feature(rand)]
288287
#![feature(raw)]
289288
#![feature(repr_simd)]
@@ -309,6 +308,7 @@
309308
#![feature(vec_push_all)]
310309
#![feature(zero_one)]
311310
#![cfg_attr(test, feature(update_panic_count))]
311+
#![cfg_attr(stage0, feature(pub_restricted))]
312312

313313
// Explicitly import the prelude. The compiler uses this same unstable attribute
314314
// to import the prelude implicitly when building crates that depend on std.

‎src/libsyntax/feature_gate.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,6 @@ declare_features! (
260260
// impl specialization (RFC 1210)
261261
(active, specialization, "1.7.0", Some(31844)),
262262

263-
// pub(restricted) visibilities (RFC 1422)
264-
(active, pub_restricted, "1.9.0", Some(32409)),
265-
266263
// Allow Drop types in statics/const functions (RFC 1440)
267264
(active, drop_types_in_const, "1.9.0", Some(33156)),
268265

@@ -409,6 +406,9 @@ declare_features! (
409406
(accepted, field_init_shorthand, "1.17.0", Some(37340)),
410407
// Allows the definition recursive static items.
411408
(accepted, static_recursion, "1.17.0", Some(29719)),
409+
// pub(restricted) visibilities (RFC 1422)
410+
(accepted, pub_restricted, "1.17.0", Some(32409)),
411+
412412
);
413413
// If you change this, please modify src/doc/unstable-book as well. You must
414414
// move that documentation into the relevant place in the other docs, and
@@ -1417,17 +1417,6 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
14171417
visit::walk_impl_item(self, ii);
14181418
}
14191419

1420-
fn visit_vis(&mut self, vis: &'a ast::Visibility) {
1421-
let span = match *vis {
1422-
ast::Visibility::Crate(span) => span,
1423-
ast::Visibility::Restricted { ref path, .. } => path.span,
1424-
_ => return,
1425-
};
1426-
gate_feature_post!(&self, pub_restricted, span, "`pub(restricted)` syntax is experimental");
1427-
1428-
visit::walk_vis(self, vis)
1429-
}
1430-
14311420
fn visit_generics(&mut self, g: &'a ast::Generics) {
14321421
for t in &g.ty_params {
14331422
if !t.attrs.is_empty() {

‎src/test/compile-fail-fulldeps/auxiliary/pub_and_stability.rs

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
// non-pub fields, marked with SILLY below)
3636

3737
#![feature(staged_api)]
38-
#![feature(pub_restricted)]
3938

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

‎src/test/compile-fail/imports/unused.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
1211
#![deny(unused)]
1312

1413
mod foo {

‎src/test/compile-fail/privacy/restricted/auxiliary/pub_restricted.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
12-
1311
pub(crate) struct Crate;
1412
#[derive(Default)]
1513
pub struct Universe {

‎src/test/compile-fail/privacy/restricted/feature-gate.rs

-27
This file was deleted.

‎src/test/compile-fail/privacy/restricted/lookup-ignores-private.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(rustc_attrs, pub_restricted)]
11+
#![feature(rustc_attrs)]
1212
#![allow(warnings)]
1313

1414
mod foo {

‎src/test/compile-fail/privacy/restricted/private-in-public.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
12-
1311
mod foo {
1412
struct Priv;
1513
mod bar {

‎src/test/compile-fail/privacy/restricted/struct-literal-field.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
1211
#![deny(private_in_public)]
1312
#![allow(warnings)]
1413

‎src/test/compile-fail/privacy/restricted/test.rs

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
// aux-build:pub_restricted.rs
1212

13-
#![feature(pub_restricted)]
1413
#![deny(private_in_public)]
1514
#![allow(warnings)]
1615
extern crate pub_restricted;

‎src/test/compile-fail/privacy/restricted/tuple-struct-fields/test.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
12-
1311
mod foo {
1412
type T = ();
1513
struct S1(pub(foo) (), pub(T), pub(crate) (), pub(((), T)));

‎src/test/compile-fail/privacy/restricted/tuple-struct-fields/test2.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
12-
1311
macro_rules! define_struct {
1412
($t:ty) => {
1513
struct S1(pub $t);

‎src/test/compile-fail/privacy/restricted/tuple-struct-fields/test3.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
12-
1311
macro_rules! define_struct {
1412
($t:ty) => {
1513
struct S1(pub($t));

‎src/test/compile-fail/privacy/restricted/ty-params.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
12-
1311
macro_rules! m {
1412
($p: path) => (pub(in $p) struct Z;)
1513
}

‎src/test/compile-fail/privacy/union-field-privacy-1.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
1211
#![feature(untagged_unions)]
1312

1413
mod m {

‎src/test/compile-fail/privacy/union-field-privacy-2.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
1211
#![feature(untagged_unions)]
1312

1413
mod m {

‎src/test/compile-fail/resolve-bad-visibility.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
12-
1311
enum E {}
1412
trait Tr {}
1513

‎src/test/ui/resolve/auxiliary/privacy-struct-ctor.rs

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
#![feature(pub_restricted)]
12-
1311
pub mod m {
1412
pub struct S(u8);
1513

‎src/test/ui/resolve/privacy-struct-ctor.rs

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

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

13-
#![feature(pub_restricted)]
14-
1513
extern crate privacy_struct_ctor as xcrate;
1614

1715
mod m {

‎src/test/ui/resolve/privacy-struct-ctor.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
error[E0423]: expected value, found struct `Z`
2-
--> $DIR/privacy-struct-ctor.rs:28:9
2+
--> $DIR/privacy-struct-ctor.rs:26:9
33
|
4-
28 | Z;
4+
26 | Z;
55
| ^
66
| |
77
| did you mean `Z { /* fields */ }`?
@@ -11,9 +11,9 @@ error[E0423]: expected value, found struct `Z`
1111
`use m::n::Z;`
1212

1313
error[E0423]: expected value, found struct `S`
14-
--> $DIR/privacy-struct-ctor.rs:38:5
14+
--> $DIR/privacy-struct-ctor.rs:36:5
1515
|
16-
38 | S;
16+
36 | S;
1717
| ^
1818
| |
1919
| did you mean `S { /* fields */ }`?
@@ -23,9 +23,9 @@ error[E0423]: expected value, found struct `S`
2323
`use m::S;`
2424

2525
error[E0423]: expected value, found struct `xcrate::S`
26-
--> $DIR/privacy-struct-ctor.rs:44:5
26+
--> $DIR/privacy-struct-ctor.rs:42:5
2727
|
28-
44 | xcrate::S;
28+
42 | xcrate::S;
2929
| ^^^^^^^^^
3030
| |
3131
| did you mean `xcrate::S { /* fields */ }`?
@@ -35,33 +35,33 @@ error[E0423]: expected value, found struct `xcrate::S`
3535
`use m::S;`
3636

3737
error: tuple struct `Z` is private
38-
--> $DIR/privacy-struct-ctor.rs:27:9
38+
--> $DIR/privacy-struct-ctor.rs:25:9
3939
|
40-
27 | n::Z; //~ ERROR tuple struct `Z` is private
40+
25 | n::Z; //~ ERROR tuple struct `Z` is private
4141
| ^^^^
4242

4343
error: tuple struct `S` is private
44-
--> $DIR/privacy-struct-ctor.rs:37:5
44+
--> $DIR/privacy-struct-ctor.rs:35:5
4545
|
46-
37 | m::S; //~ ERROR tuple struct `S` is private
46+
35 | m::S; //~ ERROR tuple struct `S` is private
4747
| ^^^^
4848

4949
error: tuple struct `Z` is private
50-
--> $DIR/privacy-struct-ctor.rs:41:5
50+
--> $DIR/privacy-struct-ctor.rs:39:5
5151
|
52-
41 | m::n::Z; //~ ERROR tuple struct `Z` is private
52+
39 | m::n::Z; //~ ERROR tuple struct `Z` is private
5353
| ^^^^^^^
5454

5555
error: tuple struct `S` is private
56-
--> $DIR/privacy-struct-ctor.rs:43:5
56+
--> $DIR/privacy-struct-ctor.rs:41:5
5757
|
58-
43 | xcrate::m::S; //~ ERROR tuple struct `S` is private
58+
41 | xcrate::m::S; //~ ERROR tuple struct `S` is private
5959
| ^^^^^^^^^^^^
6060

6161
error: tuple struct `Z` is private
62-
--> $DIR/privacy-struct-ctor.rs:47:5
62+
--> $DIR/privacy-struct-ctor.rs:45:5
6363
|
64-
47 | xcrate::m::n::Z; //~ ERROR tuple struct `Z` is private
64+
45 | xcrate::m::n::Z; //~ ERROR tuple struct `Z` is private
6565
| ^^^^^^^^^^^^^^^
6666

6767
error: aborting due to 8 previous errors

‎src/test/ui/span/pub-struct-field.rs

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
// Regression test for issue #26083 and #35435
1212
// Test that span for public struct fields start at `pub`
1313

14-
#![feature(pub_restricted)]
15-
1614
struct Foo {
1715
bar: u8,
1816
pub bar: u8,

‎src/test/ui/span/pub-struct-field.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
error[E0124]: field `bar` is already declared
2-
--> $DIR/pub-struct-field.rs:18:5
2+
--> $DIR/pub-struct-field.rs:16:5
33
|
4-
17 | bar: u8,
4+
15 | bar: u8,
55
| ------- `bar` first declared here
6-
18 | pub bar: u8,
6+
16 | pub bar: u8,
77
| ^^^^^^^^^^^ field already declared
88

99
error[E0124]: field `bar` is already declared
10-
--> $DIR/pub-struct-field.rs:19:5
10+
--> $DIR/pub-struct-field.rs:17:5
1111
|
12-
17 | bar: u8,
12+
15 | bar: u8,
1313
| ------- `bar` first declared here
14-
18 | pub bar: u8,
15-
19 | pub(crate) bar: u8,
14+
16 | pub bar: u8,
15+
17 | pub(crate) bar: u8,
1616
| ^^^^^^^^^^^^^^^^^^ field already declared
1717

1818
error: aborting due to 2 previous errors

0 commit comments

Comments
 (0)
Please sign in to comment.