From a09246ad34ee83b5c8be9af836d7b0aa06d4aabe Mon Sep 17 00:00:00 2001 From: Vadim Petrochenkov Date: Mon, 23 Nov 2015 13:36:49 +0300 Subject: [PATCH] Approximate type aliases as public when determining impl publicity --- src/librustc_privacy/lib.rs | 10 ++++++++++ .../lint-visible-private-types.rs | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/librustc_privacy/lib.rs b/src/librustc_privacy/lib.rs index 46074423917..a4c9874e74d 100644 --- a/src/librustc_privacy/lib.rs +++ b/src/librustc_privacy/lib.rs @@ -1128,11 +1128,21 @@ impl<'a, 'tcx: 'a, 'v> Visitor<'v> for SearchInterfaceForPrivateItemsVisitor<'a, def::DefPrimTy(..) | def::DefSelfTy(..) | def::DefTyParam(..) => { // Public } + def::DefAssociatedTy(..) if self.is_quiet => { + // Conservatively approximate the whole type alias as public without + // recursing into its components when determining impl publicity. + return + } def::DefStruct(def_id) | def::DefTy(def_id, _) | def::DefTrait(def_id) | def::DefAssociatedTy(def_id, _) => { // Non-local means public, local needs to be checked if let Some(node_id) = self.tcx.map.as_local_node_id(def_id) { if let Some(ast_map::NodeItem(ref item)) = self.tcx.map.find(node_id) { + if let (&hir::ItemTy(..), true) = (&item.node, self.is_quiet) { + // Conservatively approximate the whole type alias as public without + // recursing into its components when determining impl publicity. + return + } if item.vis != hir::Public { if !self.is_quiet { span_err!(self.tcx.sess, ty.span, E0446, diff --git a/src/test/compile-fail/lint-visible-private-types.rs b/src/test/compile-fail/lint-visible-private-types.rs index f43c17c3854..89be4d9789d 100644 --- a/src/test/compile-fail/lint-visible-private-types.rs +++ b/src/test/compile-fail/lint-visible-private-types.rs @@ -121,3 +121,22 @@ impl>> //~ ERROR private type in public interface ParamTrait for Public { fn foo() -> T { panic!() } } + +type PrivAlias = Public; + +trait PrivTrait2 { + type Alias; +} +impl PrivTrait2 for Private { + type Alias = Public; +} + +impl PubTrait for PrivAlias { + fn bar(&self) -> Private { panic!() } //~ ERROR private type in public interface + fn baz() -> Private { panic!() } //~ ERROR private type in public interface +} + +impl PubTrait for as PrivTrait2>::Alias { + fn bar(&self) -> Private { panic!() } //~ ERROR private type in public interface + fn baz() -> Private { panic!() } //~ ERROR private type in public interface +}