Skip to content

Commit 18b96cf

Browse files
committed
Privatize constructors of tuple structs with private fields
1 parent 1491e04 commit 18b96cf

File tree

4 files changed

+83
-10
lines changed

4 files changed

+83
-10
lines changed

src/librustc_metadata/encoder.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,10 +396,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
396396

397397
let struct_id = tcx.hir.as_local_node_id(adt_def_id).unwrap();
398398
let struct_vis = &tcx.hir.expect_item(struct_id).vis;
399+
let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx);
400+
for field in &variant.fields {
401+
if ctor_vis.is_at_least(field.vis, tcx) {
402+
ctor_vis = field.vis;
403+
}
404+
}
399405

400406
Entry {
401407
kind: EntryKind::Struct(self.lazy(&data)),
402-
visibility: self.lazy(&ty::Visibility::from_hir(struct_vis, struct_id, tcx)),
408+
visibility: self.lazy(&ctor_vis),
403409
span: self.lazy(&tcx.def_span(def_id)),
404410
attributes: LazySeq::empty(),
405411
children: LazySeq::empty(),

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -327,21 +327,25 @@ impl<'a> Resolver<'a> {
327327
let def = Def::Struct(self.definitions.local_def_id(item.id));
328328
self.define(parent, ident, TypeNS, (def, vis, sp, expansion));
329329

330-
// If this is a tuple or unit struct, define a name
331-
// in the value namespace as well.
332-
if !struct_def.is_struct() {
333-
let ctor_def = Def::StructCtor(self.definitions.local_def_id(struct_def.id()),
334-
CtorKind::from_ast(struct_def));
335-
self.define(parent, ident, ValueNS, (ctor_def, vis, sp, expansion));
336-
}
337-
338330
// Record field names for error reporting.
331+
let mut ctor_vis = vis;
339332
let field_names = struct_def.fields().iter().filter_map(|field| {
340-
self.resolve_visibility(&field.vis);
333+
let field_vis = self.resolve_visibility(&field.vis);
334+
if ctor_vis.is_at_least(field_vis, &*self) {
335+
ctor_vis = field_vis;
336+
}
341337
field.ident.map(|ident| ident.name)
342338
}).collect();
343339
let item_def_id = self.definitions.local_def_id(item.id);
344340
self.insert_field_names(item_def_id, field_names);
341+
342+
// If this is a tuple or unit struct, define a name
343+
// in the value namespace as well.
344+
if !struct_def.is_struct() {
345+
let ctor_def = Def::StructCtor(self.definitions.local_def_id(struct_def.id()),
346+
CtorKind::from_ast(struct_def));
347+
self.define(parent, ident, ValueNS, (ctor_def, ctor_vis, sp, expansion));
348+
}
345349
}
346350

347351
ItemKind::Union(ref vdata, _) => {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2016 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+
#![feature(pub_restricted)]
12+
13+
pub mod m {
14+
pub struct S(u8);
15+
16+
pub mod n {
17+
pub(m) struct Z(pub(m::n) u8);
18+
}
19+
}
20+
21+
pub use m::S;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2016 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+
// aux-build:privacy-struct-ctor.rs
12+
13+
#![feature(pub_restricted)]
14+
15+
extern crate privacy_struct_ctor as xcrate;
16+
17+
mod m {
18+
pub struct S(u8);
19+
20+
pub mod n {
21+
pub(m) struct Z(pub(m::n) u8);
22+
}
23+
24+
use m::n::Z; // OK, only the type is imported
25+
26+
fn f() {
27+
n::Z; //~ ERROR tuple struct `Z` is private
28+
Z; //~ ERROR expected value, found struct `Z`
29+
}
30+
}
31+
32+
use m::S; // OK, only the type is imported
33+
34+
fn main() {
35+
m::S; //~ ERROR tuple struct `S` is private
36+
S; //~ ERROR expected value, found struct `S`
37+
m::n::Z; //~ ERROR tuple struct `Z` is private
38+
39+
xcrate::m::S; //~ ERROR tuple struct `S` is private
40+
xcrate::S; //~ ERROR expected value, found struct `xcrate::S`
41+
xcrate::m::n::Z; //~ ERROR tuple struct `Z` is private
42+
}

0 commit comments

Comments
 (0)