Skip to content

Commit

Permalink
Rollup merge of rust-lang#32674 - jseyfried:1422_groundwork, r=nikoma…
Browse files Browse the repository at this point in the history
…tsakis

Lay groundwork for RFC 1422  and improve `PrivateItemsInPublicInterfacesVisitor`

This PR lays groundwork for RFC 1422 (cc rust-lang#32409) and improves `PrivateItemsInPublicInterfacesVisitor`. More specifically, it
 - Refactors away `hir::Visibility::inherit_from`, the semantics of which are obsolete.
 - Makes `hir::Visibility` non-`Copy` so that we will be able to add new variants to represent `pub(restricted)` (for example, `Visibility::Restricted(Path)`).
 - Adds a new `Copy` type `ty::Visibility` that represents a visibility value, i.e. a characterization of where an item is accessible. This is able to represent `pub(restricted)` visibilities.
 - Improves `PrivateItemsInPublicInterfacesVisitor` so that it checks for items in an interface that are less visible than the interface. This fixes rust-lang#30079 but doesn't change any other behavior.

r? @nikomatsakis
  • Loading branch information
Manishearth committed Apr 7, 2016
2 parents 444a118 + dcd4621 commit 76e2349
Show file tree
Hide file tree
Showing 22 changed files with 385 additions and 298 deletions.
8 changes: 4 additions & 4 deletions src/librustc/hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ use std::u32;
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum FnKind<'a> {
/// fn foo() or extern "Abi" fn foo()
ItemFn(Name, &'a Generics, Unsafety, Constness, Abi, Visibility, &'a [Attribute]),
ItemFn(Name, &'a Generics, Unsafety, Constness, Abi, &'a Visibility, &'a [Attribute]),

/// fn foo(&self)
Method(Name, &'a MethodSig, Option<Visibility>, &'a [Attribute]),
Method(Name, &'a MethodSig, Option<&'a Visibility>, &'a [Attribute]),

/// |x, y| {}
Closure(&'a [Attribute]),
Expand Down Expand Up @@ -324,7 +324,7 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
unsafety,
constness,
abi,
item.vis,
&item.vis,
&item.attrs),
declaration,
body,
Expand Down Expand Up @@ -672,7 +672,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplIt
ImplItemKind::Method(ref sig, ref body) => {
visitor.visit_fn(FnKind::Method(impl_item.name,
sig,
Some(impl_item.vis),
Some(&impl_item.vis),
&impl_item.attrs),
&sig.decl,
body,
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/hir/map/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ struct ItemFnParts<'a> {
unsafety: ast::Unsafety,
constness: ast::Constness,
abi: abi::Abi,
vis: ast::Visibility,
vis: &'a ast::Visibility,
generics: &'a ast::Generics,
body: &'a Block,
id: NodeId,
Expand Down Expand Up @@ -208,7 +208,7 @@ impl<'a> FnLikeNode<'a> {
M: FnOnce(NodeId,
Name,
&'a ast::MethodSig,
Option<ast::Visibility>,
Option<&'a ast::Visibility>,
&'a ast::Block,
Span,
&'a [Attribute])
Expand All @@ -226,7 +226,7 @@ impl<'a> FnLikeNode<'a> {
body: &block,
generics: generics,
abi: abi,
vis: i.vis,
vis: &i.vis,
constness: constness,
span: i.span,
attrs: &i.attrs,
Expand All @@ -242,7 +242,7 @@ impl<'a> FnLikeNode<'a> {
map::NodeImplItem(ii) => {
match ii.node {
ast::ImplItemKind::Method(ref sig, ref body) => {
method(ii.id, ii.name, sig, Some(ii.vis), body, ii.span, &ii.attrs)
method(ii.id, ii.name, sig, Some(&ii.vis), body, ii.span, &ii.attrs)
}
_ => {
bug!("impl method FnLikeNode that is not fn-like")
Expand Down
14 changes: 1 addition & 13 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ impl<'ast> Map<'ast> {

/// Returns the NodeId of `id`'s nearest module parent, or `id` itself if no
/// module parent is in this map.
fn get_module_parent(&self, id: NodeId) -> NodeId {
pub fn get_module_parent(&self, id: NodeId) -> NodeId {
match self.walk_parent_nodes(id, |node| match *node {
NodeItem(&Item { node: Item_::ItemMod(_), .. }) => true,
_ => false,
Expand All @@ -440,18 +440,6 @@ impl<'ast> Map<'ast> {
}
}

pub fn private_item_is_visible_from(&self, item: NodeId, block: NodeId) -> bool {
// A private item is visible from everything in its nearest module parent.
let visibility = self.get_module_parent(item);
let mut block_ancestor = self.get_module_parent(block);
loop {
if block_ancestor == visibility { return true }
let block_ancestor_parent = self.get_module_parent(block_ancestor);
if block_ancestor_parent == block_ancestor { return false }
block_ancestor = block_ancestor_parent;
}
}

/// Returns the nearest enclosing scope. A scope is an item or block.
/// FIXME it is not clear to me that all items qualify as scopes - statics
/// and associated types probably shouldn't, for example. Behaviour in this
Expand Down
11 changes: 1 addition & 10 deletions src/librustc/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,21 +1431,12 @@ pub struct PolyTraitRef {
pub span: Span,
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug, Copy)]
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Visibility {
Public,
Inherited,
}

impl Visibility {
pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility {
match self {
&Inherited => parent_visibility,
&Public => *self,
}
}
}

#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub struct StructField {
pub span: Span,
Expand Down
62 changes: 31 additions & 31 deletions src/librustc/hir/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ pub fn fun_to_string(decl: &hir::FnDecl,
Some(name),
generics,
opt_explicit_self,
hir::Inherited)?;
&hir::Inherited)?;
s.end()?; // Close the head box
s.end() // Close the outer box
})
Expand Down Expand Up @@ -322,8 +322,8 @@ pub fn arg_to_string(arg: &hir::Arg) -> String {
to_string(|s| s.print_arg(arg, false))
}

pub fn visibility_qualified(vis: hir::Visibility, s: &str) -> String {
match vis {
pub fn visibility_qualified(vis: &hir::Visibility, s: &str) -> String {
match *vis {
hir::Public => format!("pub {}", s),
hir::Inherited => s.to_string(),
}
Expand Down Expand Up @@ -573,13 +573,13 @@ impl<'a> State<'a> {
Some(item.name),
generics,
None,
item.vis)?;
&item.vis)?;
self.end()?; // end head-ibox
word(&mut self.s, ";")?;
self.end() // end the outer fn box
}
hir::ForeignItemStatic(ref t, m) => {
self.head(&visibility_qualified(item.vis, "static"))?;
self.head(&visibility_qualified(&item.vis, "static"))?;
if m {
self.word_space("mut")?;
}
Expand All @@ -597,7 +597,7 @@ impl<'a> State<'a> {
name: ast::Name,
ty: &hir::Ty,
default: Option<&hir::Expr>,
vis: hir::Visibility)
vis: &hir::Visibility)
-> io::Result<()> {
word(&mut self.s, &visibility_qualified(vis, ""))?;
self.word_space("const")?;
Expand Down Expand Up @@ -648,7 +648,7 @@ impl<'a> State<'a> {
self.ann.pre(self, NodeItem(item))?;
match item.node {
hir::ItemExternCrate(ref optional_path) => {
self.head(&visibility_qualified(item.vis, "extern crate"))?;
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
if let Some(p) = *optional_path {
let val = p.as_str();
if val.contains("-") {
Expand All @@ -666,14 +666,14 @@ impl<'a> State<'a> {
self.end()?; // end outer head-block
}
hir::ItemUse(ref vp) => {
self.head(&visibility_qualified(item.vis, "use"))?;
self.head(&visibility_qualified(&item.vis, "use"))?;
self.print_view_path(&vp)?;
word(&mut self.s, ";")?;
self.end()?; // end inner head-block
self.end()?; // end outer head-block
}
hir::ItemStatic(ref ty, m, ref expr) => {
self.head(&visibility_qualified(item.vis, "static"))?;
self.head(&visibility_qualified(&item.vis, "static"))?;
if m == hir::MutMutable {
self.word_space("mut")?;
}
Expand All @@ -689,7 +689,7 @@ impl<'a> State<'a> {
self.end()?; // end the outer cbox
}
hir::ItemConst(ref ty, ref expr) => {
self.head(&visibility_qualified(item.vis, "const"))?;
self.head(&visibility_qualified(&item.vis, "const"))?;
self.print_name(item.name)?;
self.word_space(":")?;
self.print_type(&ty)?;
Expand All @@ -710,12 +710,12 @@ impl<'a> State<'a> {
Some(item.name),
typarams,
None,
item.vis)?;
&item.vis)?;
word(&mut self.s, " ")?;
self.print_block_with_attrs(&body, &item.attrs)?;
}
hir::ItemMod(ref _mod) => {
self.head(&visibility_qualified(item.vis, "mod"))?;
self.head(&visibility_qualified(&item.vis, "mod"))?;
self.print_name(item.name)?;
self.nbsp()?;
self.bopen()?;
Expand All @@ -732,7 +732,7 @@ impl<'a> State<'a> {
hir::ItemTy(ref ty, ref params) => {
self.ibox(indent_unit)?;
self.ibox(0)?;
self.word_nbsp(&visibility_qualified(item.vis, "type"))?;
self.word_nbsp(&visibility_qualified(&item.vis, "type"))?;
self.print_name(item.name)?;
self.print_generics(params)?;
self.end()?; // end the inner ibox
Expand All @@ -745,16 +745,16 @@ impl<'a> State<'a> {
self.end()?; // end the outer ibox
}
hir::ItemEnum(ref enum_definition, ref params) => {
self.print_enum_def(enum_definition, params, item.name, item.span, item.vis)?;
self.print_enum_def(enum_definition, params, item.name, item.span, &item.vis)?;
}
hir::ItemStruct(ref struct_def, ref generics) => {
self.head(&visibility_qualified(item.vis, "struct"))?;
self.head(&visibility_qualified(&item.vis, "struct"))?;
self.print_struct(struct_def, generics, item.name, item.span, true)?;
}

hir::ItemDefaultImpl(unsafety, ref trait_ref) => {
self.head("")?;
self.print_visibility(item.vis)?;
self.print_visibility(&item.vis)?;
self.print_unsafety(unsafety)?;
self.word_nbsp("impl")?;
self.print_trait_ref(trait_ref)?;
Expand All @@ -771,7 +771,7 @@ impl<'a> State<'a> {
ref ty,
ref impl_items) => {
self.head("")?;
self.print_visibility(item.vis)?;
self.print_visibility(&item.vis)?;
self.print_unsafety(unsafety)?;
self.word_nbsp("impl")?;

Expand Down Expand Up @@ -809,7 +809,7 @@ impl<'a> State<'a> {
}
hir::ItemTrait(unsafety, ref generics, ref bounds, ref trait_items) => {
self.head("")?;
self.print_visibility(item.vis)?;
self.print_visibility(&item.vis)?;
self.print_unsafety(unsafety)?;
self.word_nbsp("trait")?;
self.print_name(item.name)?;
Expand Down Expand Up @@ -867,7 +867,7 @@ impl<'a> State<'a> {
generics: &hir::Generics,
name: ast::Name,
span: codemap::Span,
visibility: hir::Visibility)
visibility: &hir::Visibility)
-> io::Result<()> {
self.head(&visibility_qualified(visibility, "enum"))?;
self.print_name(name)?;
Expand Down Expand Up @@ -895,8 +895,8 @@ impl<'a> State<'a> {
self.bclose(span)
}

pub fn print_visibility(&mut self, vis: hir::Visibility) -> io::Result<()> {
match vis {
pub fn print_visibility(&mut self, vis: &hir::Visibility) -> io::Result<()> {
match *vis {
hir::Public => self.word_nbsp("pub"),
hir::Inherited => Ok(()),
}
Expand All @@ -915,7 +915,7 @@ impl<'a> State<'a> {
if struct_def.is_tuple() {
self.popen()?;
self.commasep(Inconsistent, struct_def.fields(), |s, field| {
s.print_visibility(field.vis)?;
s.print_visibility(&field.vis)?;
s.maybe_print_comment(field.span.lo)?;
s.print_type(&field.ty)
})?;
Expand All @@ -937,7 +937,7 @@ impl<'a> State<'a> {
self.hardbreak_if_not_bol()?;
self.maybe_print_comment(field.span.lo)?;
self.print_outer_attributes(&field.attrs)?;
self.print_visibility(field.vis)?;
self.print_visibility(&field.vis)?;
self.print_name(field.name)?;
self.word_nbsp(":")?;
self.print_type(&field.ty)?;
Expand All @@ -964,7 +964,7 @@ impl<'a> State<'a> {
pub fn print_method_sig(&mut self,
name: ast::Name,
m: &hir::MethodSig,
vis: hir::Visibility)
vis: &hir::Visibility)
-> io::Result<()> {
self.print_fn(&m.decl,
m.unsafety,
Expand All @@ -986,13 +986,13 @@ impl<'a> State<'a> {
self.print_associated_const(ti.name,
&ty,
default.as_ref().map(|expr| &**expr),
hir::Inherited)?;
&hir::Inherited)?;
}
hir::MethodTraitItem(ref sig, ref body) => {
if body.is_some() {
self.head("")?;
}
self.print_method_sig(ti.name, sig, hir::Inherited)?;
self.print_method_sig(ti.name, sig, &hir::Inherited)?;
if let Some(ref body) = *body {
self.nbsp()?;
self.print_block_with_attrs(body, &ti.attrs)?;
Expand Down Expand Up @@ -1021,11 +1021,11 @@ impl<'a> State<'a> {

match ii.node {
hir::ImplItemKind::Const(ref ty, ref expr) => {
self.print_associated_const(ii.name, &ty, Some(&expr), ii.vis)?;
self.print_associated_const(ii.name, &ty, Some(&expr), &ii.vis)?;
}
hir::ImplItemKind::Method(ref sig, ref body) => {
self.head("")?;
self.print_method_sig(ii.name, sig, ii.vis)?;
self.print_method_sig(ii.name, sig, &ii.vis)?;
self.nbsp()?;
self.print_block_with_attrs(body, &ii.attrs)?;
}
Expand Down Expand Up @@ -1910,7 +1910,7 @@ impl<'a> State<'a> {
name: Option<ast::Name>,
generics: &hir::Generics,
opt_explicit_self: Option<&hir::ExplicitSelf_>,
vis: hir::Visibility)
vis: &hir::Visibility)
-> io::Result<()> {
self.print_fn_header_info(unsafety, constness, abi, vis)?;

Expand Down Expand Up @@ -2267,7 +2267,7 @@ impl<'a> State<'a> {
name,
&generics,
opt_explicit_self,
hir::Inherited)?;
&hir::Inherited)?;
self.end()
}

Expand Down Expand Up @@ -2347,7 +2347,7 @@ impl<'a> State<'a> {
unsafety: hir::Unsafety,
constness: hir::Constness,
abi: Abi,
vis: hir::Visibility)
vis: &hir::Visibility)
-> io::Result<()> {
word(&mut self.s, &visibility_qualified(vis, ""))?;
self.print_unsafety(unsafety)?;
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub const LOCAL_CRATE: ast::CrateNum = 0;
pub struct ChildItem {
pub def: DefLike,
pub name: ast::Name,
pub vis: hir::Visibility
pub vis: ty::Visibility,
}

pub enum FoundAst<'ast> {
Expand Down Expand Up @@ -157,7 +157,7 @@ pub trait CrateStore<'tcx> : Any {
// item info
fn stability(&self, def: DefId) -> Option<attr::Stability>;
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation>;
fn visibility(&self, def: DefId) -> hir::Visibility;
fn visibility(&self, def: DefId) -> ty::Visibility;
fn closure_kind(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
-> ty::ClosureKind;
fn closure_ty(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
Expand Down Expand Up @@ -334,7 +334,7 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
// item info
fn stability(&self, def: DefId) -> Option<attr::Stability> { bug!("stability") }
fn deprecation(&self, def: DefId) -> Option<attr::Deprecation> { bug!("deprecation") }
fn visibility(&self, def: DefId) -> hir::Visibility { bug!("visibility") }
fn visibility(&self, def: DefId) -> ty::Visibility { bug!("visibility") }
fn closure_kind(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
-> ty::ClosureKind { bug!("closure_kind") }
fn closure_ty(&self, tcx: &TyCtxt<'tcx>, def_id: DefId)
Expand Down
Loading

0 comments on commit 76e2349

Please sign in to comment.