Skip to content

First stage of struct variant field visibility changes #18792

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ struct MarkSymbolVisitor<'a, 'tcx: 'a> {
tcx: &'a ty::ctxt<'tcx>,
live_symbols: Box<HashSet<ast::NodeId>>,
struct_has_extern_repr: bool,
ignore_non_const_paths: bool
ignore_non_const_paths: bool,
inherited_pub_visibility: bool,
}

impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
Expand All @@ -62,7 +63,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
tcx: tcx,
live_symbols: box HashSet::new(),
struct_has_extern_repr: false,
ignore_non_const_paths: false
ignore_non_const_paths: false,
inherited_pub_visibility: false,
}
}

Expand Down Expand Up @@ -206,6 +208,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
fn visit_node(&mut self, node: &ast_map::Node) {
let had_extern_repr = self.struct_has_extern_repr;
self.struct_has_extern_repr = false;
let had_inherited_pub_visibility = self.inherited_pub_visibility;
self.inherited_pub_visibility = false;
match *node {
ast_map::NodeItem(item) => {
match item.node {
Expand All @@ -217,8 +221,11 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {

visit::walk_item(self, &*item);
}
ast::ItemEnum(..) => {
self.inherited_pub_visibility = item.vis == ast::Public;
visit::walk_item(self, &*item);
}
ast::ItemFn(..)
| ast::ItemEnum(..)
| ast::ItemTy(..)
| ast::ItemStatic(..)
| ast::ItemConst(..) => {
Expand All @@ -244,6 +251,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
_ => ()
}
self.struct_has_extern_repr = had_extern_repr;
self.inherited_pub_visibility = had_inherited_pub_visibility;
}
}

Expand All @@ -252,8 +260,9 @@ impl<'a, 'tcx, 'v> Visitor<'v> for MarkSymbolVisitor<'a, 'tcx> {
fn visit_struct_def(&mut self, def: &ast::StructDef, _: ast::Ident,
_: &ast::Generics, _: ast::NodeId) {
let has_extern_repr = self.struct_has_extern_repr;
let inherited_pub_visibility = self.inherited_pub_visibility;
let live_fields = def.fields.iter().filter(|f| {
has_extern_repr || match f.node.kind {
has_extern_repr || inherited_pub_visibility || match f.node.kind {
ast::NamedField(_, ast::Public) => true,
_ => false
}
Expand Down
11 changes: 2 additions & 9 deletions src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,10 +668,8 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
let struct_desc = match ty::get(struct_type).sty {
ty::ty_struct(_, _) =>
format!("struct `{}`", ty::item_path_str(self.tcx, id)),
ty::ty_enum(enum_id, _) =>
format!("variant `{}` of enum `{}`",
ty::with_path(self.tcx, id, |mut p| p.last().unwrap()),
ty::item_path_str(self.tcx, enum_id)),
// struct variant fields have inherited visibility
ty::ty_enum(..) => return,
_ => self.tcx.sess.span_bug(span, "can't find struct for field")
};
let msg = match name {
Expand Down Expand Up @@ -1214,11 +1212,6 @@ impl<'a, 'tcx> SanePrivacyVisitor<'a, 'tcx> {
ast::ItemEnum(ref def, _) => {
for v in def.variants.iter() {
check_inherited(tcx, v.span, v.node.vis);

match v.node.kind {
ast::StructVariantKind(ref s) => check_struct(&**s),
ast::TupleVariantKind(..) => {}
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/auxiliary/issue-8044.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct BTree<V> {
}

pub enum TreeItem<V> {
TreeLeaf { pub value: V },
TreeLeaf { value: V },
}

pub fn leaf<V>(value: V) -> TreeItem<V> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(struct_variant)]

pub enum Foo {
Bar {
baz: int
}
enum Bar {
Baz { a: int }
}

2 changes: 1 addition & 1 deletion src/test/auxiliary/struct_variant_xc_aux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@

pub enum Enum {
Variant(u8),
StructVariant { pub arg: u8 }
StructVariant { arg: u8 }
}
48 changes: 0 additions & 48 deletions src/test/compile-fail/privacy-struct-variant.rs

This file was deleted.

23 changes: 23 additions & 0 deletions src/test/compile-fail/struct-variant-privacy-xc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// aux-build:struct_variant_privacy.rs
#![feature(struct_variant)]

extern crate struct_variant_privacy;

fn f(b: struct_variant_privacy::Bar) { //~ ERROR enum `Bar` is private
match b {
struct_variant_privacy::Bar::Baz { a: _a } => {} //~ ERROR variant `Baz` is private
}
}

fn main() {}

25 changes: 25 additions & 0 deletions src/test/compile-fail/struct-variant-privacy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![feature(struct_variant)]

mod foo {
enum Bar {
Baz { a: int }
}
}

fn f(b: foo::Bar) { //~ ERROR enum `Bar` is private
match b {
foo::Bar::Baz { a: _a } => {} //~ ERROR variant `Baz` is inaccessible
// ^~ ERROR enum `Bar` is private
}
}

fn main() {}
2 changes: 1 addition & 1 deletion src/test/run-pass/issue-14837.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#[deny(dead_code)]
pub enum Foo {
Bar {
pub baz: int
baz: int
}
}

Expand Down
25 changes: 25 additions & 0 deletions src/test/run-pass/struct-variant-field-visibility.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![feature(struct_variant)]

mod foo {
pub enum Foo {
Bar { a: int }
}
}

fn f(f: foo::Foo) {
match f {
foo::Foo::Bar { a: _a } => {}
}
}

pub fn main() {}