Skip to content

Commit 221115c

Browse files
committed
auto merge of #18792 : sfackler/rust/struct-variants, r=alexcrichton
We need a snapshot before the parser can be adjusted.
2 parents 830c82d + 00741a2 commit 221115c

10 files changed

+94
-69
lines changed

src/librustc/middle/dead.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ struct MarkSymbolVisitor<'a, 'tcx: 'a> {
5151
tcx: &'a ty::ctxt<'tcx>,
5252
live_symbols: Box<HashSet<ast::NodeId>>,
5353
struct_has_extern_repr: bool,
54-
ignore_non_const_paths: bool
54+
ignore_non_const_paths: bool,
55+
inherited_pub_visibility: bool,
5556
}
5657

5758
impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
@@ -62,7 +63,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
6263
tcx: tcx,
6364
live_symbols: box HashSet::new(),
6465
struct_has_extern_repr: false,
65-
ignore_non_const_paths: false
66+
ignore_non_const_paths: false,
67+
inherited_pub_visibility: false,
6668
}
6769
}
6870

@@ -206,6 +208,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
206208
fn visit_node(&mut self, node: &ast_map::Node) {
207209
let had_extern_repr = self.struct_has_extern_repr;
208210
self.struct_has_extern_repr = false;
211+
let had_inherited_pub_visibility = self.inherited_pub_visibility;
212+
self.inherited_pub_visibility = false;
209213
match *node {
210214
ast_map::NodeItem(item) => {
211215
match item.node {
@@ -217,8 +221,11 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
217221

218222
visit::walk_item(self, &*item);
219223
}
224+
ast::ItemEnum(..) => {
225+
self.inherited_pub_visibility = item.vis == ast::Public;
226+
visit::walk_item(self, &*item);
227+
}
220228
ast::ItemFn(..)
221-
| ast::ItemEnum(..)
222229
| ast::ItemTy(..)
223230
| ast::ItemStatic(..)
224231
| ast::ItemConst(..) => {
@@ -244,6 +251,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
244251
_ => ()
245252
}
246253
self.struct_has_extern_repr = had_extern_repr;
254+
self.inherited_pub_visibility = had_inherited_pub_visibility;
247255
}
248256
}
249257

@@ -252,8 +260,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
252260
fn visit_struct_def(&mut self, def: &ast::StructDef, _: ast::Ident,
253261
_: &ast::Generics, _: ast::NodeId) {
254262
let has_extern_repr = self.struct_has_extern_repr;
263+
let inherited_pub_visibility = self.inherited_pub_visibility;
255264
let live_fields = def.fields.iter().filter(|f| {
256-
has_extern_repr || match f.node.kind {
265+
has_extern_repr || inherited_pub_visibility || match f.node.kind {
257266
ast::NamedField(_, ast::Public) => true,
258267
_ => false
259268
}

src/librustc/middle/privacy.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,8 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
668668
let struct_desc = match ty::get(struct_type).sty {
669669
ty::ty_struct(_, _) =>
670670
format!("struct `{}`", ty::item_path_str(self.tcx, id)),
671-
ty::ty_enum(enum_id, _) =>
672-
format!("variant `{}` of enum `{}`",
673-
ty::with_path(self.tcx, id, |mut p| p.last().unwrap()),
674-
ty::item_path_str(self.tcx, enum_id)),
671+
// struct variant fields have inherited visibility
672+
ty::ty_enum(..) => return,
675673
_ => self.tcx.sess.span_bug(span, "can't find struct for field")
676674
};
677675
let msg = match name {
@@ -1214,11 +1212,6 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
12141212
ast::ItemEnum(ref def, _) => {
12151213
for v in def.variants.iter() {
12161214
check_inherited(tcx, v.span, v.node.vis);
1217-
1218-
match v.node.kind {
1219-
ast::StructVariantKind(ref s) => check_struct(&**s),
1220-
ast::TupleVariantKind(..) => {}
1221-
}
12221215
}
12231216
}
12241217

src/test/auxiliary/issue-8044.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct BTree<V> {
1515
}
1616

1717
pub enum TreeItem<V> {
18-
TreeLeaf { pub value: V },
18+
TreeLeaf { value: V },
1919
}
2020

2121
pub fn leaf<V>(value: V) -> TreeItem<V> {

src/test/auxiliary/privacy-struct-variant.rs renamed to src/test/auxiliary/struct_variant_privacy.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10-
1110
#![feature(struct_variant)]
1211

13-
pub enum Foo {
14-
Bar {
15-
baz: int
16-
}
12+
enum Bar {
13+
Baz { a: int }
1714
}
15+

src/test/auxiliary/struct_variant_xc_aux.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515

1616
pub enum Enum {
1717
Variant(u8),
18-
StructVariant { pub arg: u8 }
18+
StructVariant { arg: u8 }
1919
}

src/test/compile-fail/privacy-struct-variant.rs

-48
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2014 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:struct_variant_privacy.rs
12+
#![feature(struct_variant)]
13+
14+
extern crate struct_variant_privacy;
15+
16+
fn f(b: struct_variant_privacy::Bar) { //~ ERROR enum `Bar` is private
17+
match b {
18+
struct_variant_privacy::Bar::Baz { a: _a } => {} //~ ERROR variant `Baz` is private
19+
}
20+
}
21+
22+
fn main() {}
23+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2014 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+
#![feature(struct_variant)]
11+
12+
mod foo {
13+
enum Bar {
14+
Baz { a: int }
15+
}
16+
}
17+
18+
fn f(b: foo::Bar) { //~ ERROR enum `Bar` is private
19+
match b {
20+
foo::Bar::Baz { a: _a } => {} //~ ERROR variant `Baz` is inaccessible
21+
// ^~ ERROR enum `Bar` is private
22+
}
23+
}
24+
25+
fn main() {}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#[deny(dead_code)]
1414
pub enum Foo {
1515
Bar {
16-
pub baz: int
16+
baz: int
1717
}
1818
}
1919

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2014 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(struct_variant)]
12+
13+
mod foo {
14+
pub enum Foo {
15+
Bar { a: int }
16+
}
17+
}
18+
19+
fn f(f: foo::Foo) {
20+
match f {
21+
foo::Foo::Bar { a: _a } => {}
22+
}
23+
}
24+
25+
pub fn main() {}

0 commit comments

Comments
 (0)