Skip to content

Commit f9a7bc5

Browse files
committed
Auto merge of #23290 - nrc:pub_priv_mod, r=nikomatsakis
Closes #22261 r? @nikomatsakis (+ a new test coming soon...)
2 parents c109189 + 46aa621 commit f9a7bc5

File tree

13 files changed

+76
-65
lines changed

13 files changed

+76
-65
lines changed

src/librustc/session/config.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,8 @@ mod opt {
736736
use getopts;
737737
use super::RustcOptGroup;
738738

739-
type R = RustcOptGroup;
740-
type S<'a> = &'a str;
739+
pub type R = RustcOptGroup;
740+
pub type S<'a> = &'a str;
741741

742742
fn stable(g: getopts::OptGroup) -> R { RustcOptGroup::stable(g) }
743743
fn unstable(g: getopts::OptGroup) -> R { RustcOptGroup::unstable(g) }

src/librustc_privacy/lib.rs

+32-18
Original file line numberDiff line numberDiff line change
@@ -1186,6 +1186,7 @@ impl<'a, 'tcx> VisiblePrivateTypesVisitor<'a, 'tcx> {
11861186
if !is_local(did) {
11871187
return false
11881188
}
1189+
11891190
// .. and it corresponds to a private type in the AST (this returns
11901191
// None for type parameters)
11911192
match self.tcx.map.find(did.node) {
@@ -1206,12 +1207,15 @@ impl<'a, 'tcx> VisiblePrivateTypesVisitor<'a, 'tcx> {
12061207
if !self.tcx.sess.features.borrow().visible_private_types &&
12071208
self.path_is_private_type(trait_ref.trait_ref.ref_id) {
12081209
let span = trait_ref.trait_ref.path.span;
1209-
self.tcx.sess.span_err(span,
1210-
"private trait in exported type \
1211-
parameter bound");
1210+
self.tcx.sess.span_err(span, "private trait in exported type \
1211+
parameter bound");
12121212
}
12131213
}
12141214
}
1215+
1216+
fn item_is_public(&self, id: &ast::NodeId, vis: ast::Visibility) -> bool {
1217+
self.exported_items.contains(id) || vis == ast::Public
1218+
}
12151219
}
12161220

12171221
impl<'a, 'b, 'tcx, 'v> Visitor<'v> for CheckTypeForPrivatenessVisitor<'a, 'b, 'tcx> {
@@ -1259,7 +1263,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
12591263
// error messages without (too many) false positives
12601264
// (i.e. we could just return here to not check them at
12611265
// all, or some worse estimation of whether an impl is
1262-
// publicly visible.
1266+
// publicly visible).
12631267
ast::ItemImpl(_, _, ref g, ref trait_ref, ref self_, ref impl_items) => {
12641268
// `impl [... for] Private` is never visible.
12651269
let self_contains_private;
@@ -1321,7 +1325,22 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
13211325
match *trait_ref {
13221326
None => {
13231327
for impl_item in impl_items {
1324-
visit::walk_impl_item(self, impl_item);
1328+
// This is where we choose whether to walk down
1329+
// further into the impl to check its items. We
1330+
// should only walk into public items so that we
1331+
// don't erroneously report errors for private
1332+
// types in private items.
1333+
match impl_item.node {
1334+
ast::MethodImplItem(..)
1335+
if self.item_is_public(&impl_item.id, impl_item.vis) =>
1336+
{
1337+
visit::walk_impl_item(self, impl_item)
1338+
}
1339+
ast::TypeImplItem(..) => {
1340+
visit::walk_impl_item(self, impl_item)
1341+
}
1342+
_ => {}
1343+
}
13251344
}
13261345
}
13271346
Some(ref tr) => {
@@ -1360,7 +1379,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
13601379
match impl_item.node {
13611380
ast::MethodImplItem(ref sig, _) => {
13621381
if sig.explicit_self.node == ast::SelfStatic &&
1363-
self.exported_items.contains(&impl_item.id) {
1382+
self.item_is_public(&impl_item.id, impl_item.vis) {
13641383
found_pub_static = true;
13651384
visit::walk_impl_item(self, impl_item);
13661385
}
@@ -1381,15 +1400,18 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
13811400
ast::ItemTy(..) => return,
13821401

13831402
// not at all public, so we don't care
1384-
_ if !self.exported_items.contains(&item.id) => return,
1403+
_ if !self.item_is_public(&item.id, item.vis) => {
1404+
return;
1405+
}
13851406

13861407
_ => {}
13871408
}
13881409

1389-
// we've carefully constructed it so that if we're here, then
1410+
// We've carefully constructed it so that if we're here, then
13901411
// any `visit_ty`'s will be called on things that are in
13911412
// public signatures, i.e. things that we're interested in for
13921413
// this visitor.
1414+
debug!("VisiblePrivateTypesVisitor entering item {:?}", item);
13931415
visit::walk_item(self, item);
13941416
}
13951417

@@ -1420,20 +1442,12 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
14201442
}
14211443
}
14221444

1423-
fn visit_fn(&mut self, fk: visit::FnKind<'v>, fd: &'v ast::FnDecl,
1424-
b: &'v ast::Block, s: Span, id: ast::NodeId) {
1425-
// needs special handling for methods.
1426-
if self.exported_items.contains(&id) {
1427-
visit::walk_fn(self, fk, fd, b, s);
1428-
}
1429-
}
1430-
14311445
fn visit_ty(&mut self, t: &ast::Ty) {
1446+
debug!("VisiblePrivateTypesVisitor checking ty {:?}", t);
14321447
if let ast::TyPath(_, ref p) = t.node {
14331448
if !self.tcx.sess.features.borrow().visible_private_types &&
14341449
self.path_is_private_type(t.id) {
1435-
self.tcx.sess.span_err(p.span,
1436-
"private type in exported type signature");
1450+
self.tcx.sess.span_err(p.span, "private type in exported type signature");
14371451
}
14381452
}
14391453
visit::walk_ty(self, t)

src/librustc_resolve/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ enum ModuleKind {
382382
}
383383

384384
/// One node in the tree of modules.
385-
struct Module {
385+
pub struct Module {
386386
parent_link: ParentLink,
387387
def_id: Cell<Option<DefId>>,
388388
kind: Cell<ModuleKind>,
@@ -491,7 +491,7 @@ struct ValueNsDef {
491491
// Records the definitions (at most one for each namespace) that a name is
492492
// bound to.
493493
#[derive(Debug)]
494-
struct NameBindings {
494+
pub struct NameBindings {
495495
type_def: RefCell<Option<TypeNsDef>>, //< Meaning in type namespace.
496496
value_def: RefCell<Option<ValueNsDef>>, //< Meaning in value namespace.
497497
}
@@ -767,7 +767,7 @@ impl PrimitiveTypeTable {
767767
}
768768

769769
/// The main resolver class.
770-
struct Resolver<'a, 'tcx:'a> {
770+
pub struct Resolver<'a, 'tcx:'a> {
771771
session: &'a Session,
772772

773773
ast_map: &'a ast_map::Map<'tcx>,

src/librustc_trans/trans/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ pub fn arg_kind<'a, 'tcx>(cx: &FunctionContext<'a, 'tcx>, t: Ty<'tcx>)
14641464
}
14651465

14661466
// work around bizarre resolve errors
1467-
type RvalueDatum<'tcx> = datum::Datum<'tcx, datum::Rvalue>;
1467+
pub type RvalueDatum<'tcx> = datum::Datum<'tcx, datum::Rvalue>;
14681468

14691469
// create_datums_for_fn_args: creates rvalue datums for each of the
14701470
// incoming function arguments. These will later be stored into

src/librustc_trans/trans/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ pub fn validate_substs(substs: &Substs) {
379379

380380
// work around bizarre resolve errors
381381
type RvalueDatum<'tcx> = datum::Datum<'tcx, datum::Rvalue>;
382-
type LvalueDatum<'tcx> = datum::Datum<'tcx, datum::Lvalue>;
382+
pub type LvalueDatum<'tcx> = datum::Datum<'tcx, datum::Lvalue>;
383383

384384
// Function context. Every LLVM function we create will have one of
385385
// these.

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ type DeferredCallResolutionHandler<'tcx> = Box<DeferredCallResolution<'tcx>+'tcx
195195
/// When type-checking an expression, we propagate downward
196196
/// whatever type hint we are able in the form of an `Expectation`.
197197
#[derive(Copy)]
198-
enum Expectation<'tcx> {
198+
pub enum Expectation<'tcx> {
199199
/// We know nothing about what type this expression should have.
200200
NoExpectation,
201201

src/librustc_typeck/check/regionck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ pub struct Rcx<'a, 'tcx: 'a> {
178178

179179
}
180180

181-
struct RepeatingScope(ast::NodeId);
181+
pub struct RepeatingScope(ast::NodeId);
182182
pub enum SubjectNode { Subject(ast::NodeId), None }
183183

184184
impl<'a, 'tcx> Rcx<'a, 'tcx> {

src/librustc_typeck/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ mod constrained_type_params;
128128
mod coherence;
129129
mod variance;
130130

131-
struct TypeAndSubsts<'tcx> {
131+
pub struct TypeAndSubsts<'tcx> {
132132
pub substs: subst::Substs<'tcx>,
133133
pub ty: Ty<'tcx>,
134134
}
135135

136-
struct CrateCtxt<'a, 'tcx: 'a> {
136+
pub struct CrateCtxt<'a, 'tcx: 'a> {
137137
// A mapping from method call sites to traits that have that method.
138138
trait_map: ty::TraitMap,
139139
/// A vector of every trait accessible in the whole crate

src/libstd/sys/unix/stack_overflow.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ mod imp {
144144
munmap(handler._data, SIGSTKSZ);
145145
}
146146

147-
type sighandler_t = *mut libc::c_void;
147+
pub type sighandler_t = *mut libc::c_void;
148148

149149
#[cfg(any(all(target_os = "linux", target_arch = "x86"), // may not match
150150
all(target_os = "linux", target_arch = "x86_64"),
@@ -156,7 +156,7 @@ mod imp {
156156
target_os = "android"))] // may not match
157157
mod signal {
158158
use libc;
159-
use super::sighandler_t;
159+
pub use super::sighandler_t;
160160

161161
pub static SA_ONSTACK: libc::c_int = 0x08000000;
162162
pub static SA_SIGINFO: libc::c_int = 0x00000004;
@@ -210,7 +210,7 @@ mod imp {
210210
target_os = "openbsd"))]
211211
mod signal {
212212
use libc;
213-
use super::sighandler_t;
213+
pub use super::sighandler_t;
214214

215215
pub const SA_ONSTACK: libc::c_int = 0x0001;
216216
pub const SA_SIGINFO: libc::c_int = 0x0040;

src/libstd/sys/windows/backtrace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ struct ADDRESS64 {
104104
Mode: ADDRESS_MODE,
105105
}
106106

107-
struct STACKFRAME64 {
107+
pub struct STACKFRAME64 {
108108
AddrPC: ADDRESS64,
109109
AddrReturn: ADDRESS64,
110110
AddrFrame: ADDRESS64,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2015 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+
// Test that we properly check for private types in public signatures, even
12+
// inside a private module (#22261).
13+
14+
mod a {
15+
struct Priv;
16+
17+
pub fn expose_a() -> Priv { //~Error: private type in exported type signature
18+
panic!();
19+
}
20+
21+
mod b {
22+
pub fn expose_b() -> super::Priv { //~Error: private type in exported type signature
23+
panic!();
24+
}
25+
}
26+
}
27+
28+
pub fn main() {}

src/test/run-pass/export-unexported-dep.rs

-31
This file was deleted.

src/test/run-pass/issue-15774.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#![deny(warnings)]
1212
#![allow(unused_imports)]
1313

14-
enum Foo { A }
14+
pub enum Foo { A }
1515
mod bar {
1616
pub fn normal(x: ::Foo) {
1717
use Foo::A;

0 commit comments

Comments
 (0)