Skip to content

Commit e9ad12c

Browse files
committed
librustc: Forbid private types in public APIs.
This breaks code like: struct Foo { ... } pub fn make_foo() -> Foo { ... } Change this code to: pub struct Foo { // note `pub` ... } pub fn make_foo() -> Foo { ... } The `visible_private_types` lint has been removed, since it is now an error to attempt to expose a private type in a public API. In its place a `#[feature(visible_private_types)]` gate has been added. Closes #16463. RFC #48. [breaking-change]
1 parent 43fd619 commit e9ad12c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+169
-80
lines changed

src/libcore/any.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ pub enum Void { }
9494
pub trait Any: AnyPrivate {}
9595

9696
/// An inner trait to ensure that only this module can call `get_type_id()`.
97-
trait AnyPrivate {
97+
pub trait AnyPrivate {
9898
/// Get the `TypeId` of `self`
9999
fn get_type_id(&self) -> TypeId;
100100
}

src/libcore/iter.rs

-1
Original file line numberDiff line numberDiff line change
@@ -2178,7 +2178,6 @@ pub type Iterate<'a, T> = Unfold<'a, T, IterateState<'a, T>>;
21782178

21792179
/// Creates a new iterator that produces an infinite sequence of
21802180
/// repeated applications of the given function `f`.
2181-
#[allow(visible_private_types)]
21822181
pub fn iterate<'a, T: Clone>(seed: T, f: |T|: 'a -> T) -> Iterate<'a, T> {
21832182
Unfold::new((f, Some(seed), true), |st| {
21842183
let &(ref mut f, ref mut val, ref mut first) = st;

src/libdebug/repr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ macro_rules! try( ($me:expr, $e:expr) => (
3232

3333
/// Representations
3434
35-
trait Repr {
35+
pub trait Repr {
3636
fn write_repr(&self, writer: &mut io::Writer) -> io::IoResult<()>;
3737
}
3838

src/libgreen/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@
218218

219219
// NB this does *not* include globs, please keep it that way.
220220
#![feature(macro_rules, phase, default_type_params)]
221-
#![allow(visible_private_types, deprecated)]
221+
#![allow(deprecated)]
222222

223223
#[cfg(test)] #[phase(plugin, link)] extern crate log;
224224
#[cfg(test)] extern crate rustuv;
@@ -385,7 +385,7 @@ pub struct SchedPool {
385385
/// keep track of how many tasks are currently running in the pool and then
386386
/// sending on a channel once the entire pool has been drained of all tasks.
387387
#[deriving(Clone)]
388-
struct TaskState {
388+
pub struct TaskState {
389389
cnt: Arc<AtomicUint>,
390390
done: Sender<()>,
391391
}

src/libnative/io/timer_unix.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,14 @@ pub struct Timer {
6666
inner: Option<Box<Inner>>,
6767
}
6868

69-
struct Inner {
69+
pub struct Inner {
7070
cb: Option<Box<rtio::Callback + Send>>,
7171
interval: u64,
7272
repeat: bool,
7373
target: u64,
7474
id: uint,
7575
}
7676

77-
#[allow(visible_private_types)]
7877
pub enum Req {
7978
// Add a new timer to the helper thread.
8079
NewTimer(Box<Inner>),

src/libregex/compile.rs

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

1111
// Enable this to squash warnings due to exporting pieces of the representation
1212
// for use with the regex! macro. See lib.rs for explanation.
13-
#![allow(visible_private_types)]
1413

1514
use std::cmp;
1615
use parse;

src/libregex/re.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@ pub fn is_match(regex: &str, text: &str) -> Result<bool, parse::Error> {
102102
/// More details about the `regex!` macro can be found in the `regex` crate
103103
/// documentation.
104104
#[deriving(Clone)]
105-
#[allow(visible_private_types)]
106105
pub enum Regex {
107106
// The representation of `Regex` is exported to support the `regex!`
108107
// syntax extension. Do not rely on it.
@@ -516,7 +515,6 @@ impl Regex {
516515
}
517516

518517
#[doc(hidden)]
519-
#[allow(visible_private_types)]
520518
#[experimental]
521519
pub fn names_iter<'a>(&'a self) -> NamesIter<'a> {
522520
match *self {
@@ -534,7 +532,7 @@ impl Regex {
534532

535533
}
536534

537-
enum NamesIter<'a> {
535+
pub enum NamesIter<'a> {
538536
NamesIterNative(::std::slice::Items<'a, Option<&'static str>>),
539537
NamesIterDynamic(::std::slice::Items<'a, Option<String>>)
540538
}

src/librustc/lint/builtin.rs

-4
Original file line numberDiff line numberDiff line change
@@ -1601,9 +1601,6 @@ declare_lint!(pub DEAD_ASSIGNMENT, Warn,
16011601
declare_lint!(pub DEAD_CODE, Warn,
16021602
"detect piece of code that will never be used")
16031603

1604-
declare_lint!(pub VISIBLE_PRIVATE_TYPES, Warn,
1605-
"detect use of private types in exported type signatures")
1606-
16071604
declare_lint!(pub UNREACHABLE_CODE, Warn,
16081605
"detects unreachable code")
16091606

@@ -1636,7 +1633,6 @@ impl LintPass for HardwiredLints {
16361633
UNUSED_VARIABLE,
16371634
DEAD_ASSIGNMENT,
16381635
DEAD_CODE,
1639-
VISIBLE_PRIVATE_TYPES,
16401636
UNREACHABLE_CODE,
16411637
WARNINGS,
16421638
UNKNOWN_FEATURES,

src/librustc/middle/mem_categorization.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ pub fn deref_kind(tcx: &ty::ctxt, t: ty::t) -> deref_kind {
224224
}
225225
}
226226

227-
trait ast_node {
227+
pub trait ast_node {
228228
fn id(&self) -> ast::NodeId;
229229
fn span(&self) -> Span;
230230
}

src/librustc/middle/privacy.rs

+52-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use std::mem::replace;
1616

1717
use metadata::csearch;
1818
use middle::def;
19-
use lint;
2019
use middle::resolve;
2120
use middle::ty;
2221
use middle::typeck::{MethodCall, MethodMap, MethodOrigin, MethodParam, MethodTypeParam};
@@ -1289,19 +1288,38 @@ impl<'a, 'tcx> VisiblePrivateTypesVisitor<'a, 'tcx> {
12891288
};
12901289
// A path can only be private if:
12911290
// it's in this crate...
1292-
is_local(did) &&
1293-
// ... it's not exported (obviously) ...
1294-
!self.exported_items.contains(&did.node) &&
1295-
// .. and it corresponds to a type in the AST (this returns None for
1296-
// type parameters)
1297-
self.tcx.map.find(did.node).is_some()
1291+
if !is_local(did) {
1292+
return false
1293+
}
1294+
// .. and it corresponds to a private type in the AST (this returns
1295+
// None for type parameters)
1296+
match self.tcx.map.find(did.node) {
1297+
Some(ast_map::NodeItem(ref item)) => item.vis != ast::Public,
1298+
Some(_) | None => false,
1299+
}
12981300
}
12991301

13001302
fn trait_is_public(&self, trait_id: ast::NodeId) -> bool {
13011303
// FIXME: this would preferably be using `exported_items`, but all
13021304
// traits are exported currently (see `EmbargoVisitor.exported_trait`)
13031305
self.public_items.contains(&trait_id)
13041306
}
1307+
1308+
fn check_ty_param_bound(&self,
1309+
span: Span,
1310+
ty_param_bound: &ast::TyParamBound) {
1311+
match *ty_param_bound {
1312+
ast::TraitTyParamBound(ref trait_ref) => {
1313+
if !self.tcx.sess.features.borrow().visible_private_types &&
1314+
self.path_is_private_type(trait_ref.ref_id) {
1315+
self.tcx.sess.span_err(span,
1316+
"private type in exported type \
1317+
parameter bound");
1318+
}
1319+
}
1320+
_ => {}
1321+
}
1322+
}
13051323
}
13061324

13071325
impl<'a, 'b, 'tcx, 'v> Visitor<'v> for CheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> {
@@ -1338,7 +1356,15 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
13381356
// namespace (the contents have their own privacies).
13391357
ast::ItemForeignMod(_) => {}
13401358

1341-
ast::ItemTrait(..) if !self.trait_is_public(item.id) => return,
1359+
ast::ItemTrait(_, _, ref bounds, _) => {
1360+
if !self.trait_is_public(item.id) {
1361+
return
1362+
}
1363+
1364+
for bound in bounds.iter() {
1365+
self.check_ty_param_bound(item.span, bound)
1366+
}
1367+
}
13421368

13431369
// impls need some special handling to try to offer useful
13441370
// error messages without (too many) false positives
@@ -1471,6 +1497,19 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
14711497
visit::walk_item(self, item);
14721498
}
14731499

1500+
fn visit_generics(&mut self, generics: &ast::Generics) {
1501+
for ty_param in generics.ty_params.iter() {
1502+
for bound in ty_param.bounds.iter() {
1503+
self.check_ty_param_bound(ty_param.span, bound)
1504+
}
1505+
}
1506+
for predicate in generics.where_clause.predicates.iter() {
1507+
for bound in predicate.bounds.iter() {
1508+
self.check_ty_param_bound(predicate.span, bound)
1509+
}
1510+
}
1511+
}
1512+
14741513
fn visit_foreign_item(&mut self, item: &ast::ForeignItem) {
14751514
if self.exported_items.contains(&item.id) {
14761515
visit::walk_foreign_item(self, item)
@@ -1488,12 +1527,11 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
14881527
fn visit_ty(&mut self, t: &ast::Ty) {
14891528
match t.node {
14901529
ast::TyPath(ref p, _, path_id) => {
1491-
if self.path_is_private_type(path_id) {
1492-
self.tcx.sess.add_lint(
1493-
lint::builtin::VISIBLE_PRIVATE_TYPES,
1494-
path_id, p.span,
1495-
"private type in exported type \
1496-
signature".to_string());
1530+
if !self.tcx.sess.features.borrow().visible_private_types &&
1531+
self.path_is_private_type(path_id) {
1532+
self.tcx.sess.span_err(p.span,
1533+
"private type in exported type \
1534+
signature");
14971535
}
14981536
}
14991537
_ => {}

src/librustc/middle/typeck/infer/error_reporting.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,7 @@ impl<'a, 'tcx> ErrorReportingHelpers for InferCtxt<'a, 'tcx> {
16451645
}
16461646
}
16471647

1648-
trait Resolvable {
1648+
pub trait Resolvable {
16491649
fn resolve(&self, infcx: &InferCtxt) -> Self;
16501650
fn contains_error(&self) -> bool;
16511651
}

src/librustc_llvm/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ pub enum AttributeSet {
153153
FunctionIndex = !0
154154
}
155155

156-
trait AttrHelper {
156+
pub trait AttrHelper {
157157
fn apply_llfn(&self, idx: c_uint, llfn: ValueRef);
158158
fn apply_callsite(&self, idx: c_uint, callsite: ValueRef);
159159
}

src/librustrt/local.rs

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ pub trait Local<Borrowed> {
2626
unsafe fn try_unsafe_borrow() -> Option<*mut Self>;
2727
}
2828

29-
#[allow(visible_private_types)]
3029
impl Local<local_ptr::Borrowed<Task>> for Task {
3130
#[inline]
3231
fn put(value: Box<Task>) { unsafe { local_ptr::put(value) } }

src/librustrt/local_ptr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ pub mod native {
374374

375375
#[inline]
376376
#[cfg(not(test))]
377-
#[allow(visible_private_types)]
378377
pub fn maybe_tls_key() -> Option<tls::Key> {
379378
unsafe {
380379
// NB: This is a little racy because, while the key is

src/librustrt/unwind.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ fn rust_exception_class() -> uw::_Unwind_Exception_Class {
237237

238238
#[cfg(not(target_arch = "arm"), not(windows, target_arch = "x86_64"), not(test))]
239239
#[doc(hidden)]
240-
#[allow(visible_private_types)]
241240
pub mod eabi {
242241
use libunwind as uw;
243242
use libc::c_int;
@@ -291,7 +290,6 @@ pub mod eabi {
291290

292291
#[cfg(target_os = "ios", target_arch = "arm", not(test))]
293292
#[doc(hidden)]
294-
#[allow(visible_private_types)]
295293
pub mod eabi {
296294
use libunwind as uw;
297295
use libc::c_int;
@@ -347,7 +345,6 @@ pub mod eabi {
347345
// but otherwise works the same.
348346
#[cfg(target_arch = "arm", not(target_os = "ios"), not(test))]
349347
#[doc(hidden)]
350-
#[allow(visible_private_types)]
351348
pub mod eabi {
352349
use libunwind as uw;
353350
use libc::c_int;
@@ -397,21 +394,20 @@ pub mod eabi {
397394

398395
#[cfg(windows, target_arch = "x86_64", not(test))]
399396
#[doc(hidden)]
400-
#[allow(visible_private_types)]
401397
#[allow(non_camel_case_types, non_snake_case)]
402398
pub mod eabi {
403399
use libunwind as uw;
404400
use libc::{c_void, c_int};
405401

406402
#[repr(C)]
407-
struct EXCEPTION_RECORD;
403+
pub struct EXCEPTION_RECORD;
408404
#[repr(C)]
409-
struct CONTEXT;
405+
pub struct CONTEXT;
410406
#[repr(C)]
411-
struct DISPATCHER_CONTEXT;
407+
pub struct DISPATCHER_CONTEXT;
412408

413409
#[repr(C)]
414-
enum EXCEPTION_DISPOSITION {
410+
pub enum EXCEPTION_DISPOSITION {
415411
ExceptionContinueExecution,
416412
ExceptionContinueSearch,
417413
ExceptionNestedException,

src/librustuv/addrinfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use net;
1919
use super::{Loop, UvError, Request, wait_until_woken_after, wakeup};
2020
use uvll;
2121

22-
struct Addrinfo {
22+
pub struct Addrinfo {
2323
handle: *const libc::addrinfo,
2424
}
2525

src/librustuv/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ via `close` and `delete` methods.
4646

4747
#![feature(macro_rules, unsafe_destructor)]
4848
#![deny(unused_result, unused_must_use)]
49-
#![allow(visible_private_types)]
5049

5150
#![reexport_test_harness_main = "test_main"]
5251

src/libsyntax/feature_gate.rs

+4
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[
6969
("advanced_slice_patterns", Active),
7070
("tuple_indexing", Active),
7171
("associated_types", Active),
72+
("visible_private_types", Active),
7273

7374
// if you change this list without updating src/doc/rust.md, cmr will be sad
7475

@@ -100,6 +101,7 @@ pub struct Features {
100101
pub overloaded_calls: bool,
101102
pub rustc_diagnostic_macros: bool,
102103
pub import_shadowing: bool,
104+
pub visible_private_types: bool,
103105
}
104106

105107
impl Features {
@@ -109,6 +111,7 @@ impl Features {
109111
overloaded_calls: false,
110112
rustc_diagnostic_macros: false,
111113
import_shadowing: false,
114+
visible_private_types: false,
112115
}
113116
}
114117
}
@@ -479,6 +482,7 @@ pub fn check_crate(span_handler: &SpanHandler, krate: &ast::Crate) -> (Features,
479482
overloaded_calls: cx.has_feature("overloaded_calls"),
480483
rustc_diagnostic_macros: cx.has_feature("rustc_diagnostic_macros"),
481484
import_shadowing: cx.has_feature("import_shadowing"),
485+
visible_private_types: cx.has_feature("visible_private_types"),
482486
},
483487
unknown_features)
484488
}

src/test/auxiliary/iss.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
// part of issue-6919.rs
1414

15-
struct C<'a> {
15+
pub struct C<'a> {
1616
pub k: ||: 'a,
1717
}
1818

src/test/auxiliary/noexporttypelib.rs

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

11-
type oint = Option<int>;
11+
pub type oint = Option<int>;
1212
pub fn foo() -> oint { Some(3) }

src/test/auxiliary/priv-impl-prim-ty.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-
trait A {
11+
pub trait A {
1212
fn frob(&self);
1313
}
1414

src/test/auxiliary/regions-bounded-method-type-parameters-cross-crate-lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub enum MaybeOwned<'a> {
1616
Borrowed(&'a int)
1717
}
1818

19-
struct Inv<'a> { // invariant w/r/t 'a
19+
pub struct Inv<'a> { // invariant w/r/t 'a
2020
x: &'a mut &'a int
2121
}
2222

0 commit comments

Comments
 (0)