Skip to content

Commit d0fcb1f

Browse files
committed
Rollup merge of rust-lang#22990 - japaric:privATe, r=alexcrichton
Associated types are now treated as part of the public API by the privacy checker. If you were exposing a private type in your public API via an associated type, make that type public: ``` diff pub struct PublicType { .. } - struct Struct { .. } + pub struct Struct { .. } pub trait PublicTrait { type Output; fn foo(&self) -> Self::Output; } impl PublicTrait for PublicType { type Output = Struct; fn foo(&self) -> Struct { // `Struct` is part of the public API, it must be marked as `pub`lic .. } } ``` [breaking-change] --- r? @nikomatsakis closes rust-lang#22912
2 parents ea208a8 + 89776ae commit d0fcb1f

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

src/librustc_privacy/lib.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1376,10 +1376,11 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
13761376
}
13771377
}
13781378
Some(ref tr) => {
1379-
// Any private types in a trait impl fall into two
1379+
// Any private types in a trait impl fall into three
13801380
// categories.
13811381
// 1. mentioned in the trait definition
13821382
// 2. mentioned in the type params/generics
1383+
// 3. mentioned in the associated types of the impl
13831384
//
13841385
// Those in 1. can only occur if the trait is in
13851386
// this crate and will've been warned about on the
@@ -1389,6 +1390,16 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
13891390
// Those in 2. are warned via walk_generics and this
13901391
// call here.
13911392
visit::walk_path(self, &tr.path);
1393+
1394+
// Those in 3. are warned with this call.
1395+
for impl_item in impl_items {
1396+
match *impl_item {
1397+
ast::MethodImplItem(..) => {},
1398+
ast::TypeImplItem(ref typedef) => {
1399+
self.visit_ty(&typedef.typ);
1400+
}
1401+
}
1402+
}
13921403
}
13931404
}
13941405
} else if trait_ref.is_none() && self_is_public_path {

src/test/compile-fail/issue-22912.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2015 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+
pub struct PublicType;
12+
struct PrivateType;
13+
14+
pub trait PublicTrait {
15+
type Item;
16+
}
17+
18+
trait PrivateTrait {
19+
type Item;
20+
}
21+
22+
impl PublicTrait for PublicType {
23+
type Item = PrivateType; //~ ERROR private type in exported type signature
24+
}
25+
26+
// OK
27+
impl PublicTrait for PrivateType {
28+
type Item = PrivateType;
29+
}
30+
31+
// OK
32+
impl PrivateTrait for PublicType {
33+
type Item = PrivateType;
34+
}
35+
36+
// OK
37+
impl PrivateTrait for PrivateType {
38+
type Item = PrivateType;
39+
}
40+
41+
fn main() {}

src/test/run-pass/associated-types-binding-in-where-clause.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub trait Foo {
1616
}
1717

1818
#[derive(PartialEq)]
19-
struct Bar;
19+
pub struct Bar;
2020

2121
impl Foo for int {
2222
type A = uint;

src/test/run-pass/associated-types-eq-obj.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub trait Foo {
1515
fn boo(&self) -> <Self as Foo>::A;
1616
}
1717

18-
struct Bar;
18+
pub struct Bar;
1919

2020
impl Foo for char {
2121
type A = Bar;

src/test/run-pass/associated-types-return.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub trait Foo {
1616
}
1717

1818
#[derive(PartialEq)]
19-
struct Bar;
19+
pub struct Bar;
2020

2121
impl Foo for int {
2222
type A = uint;

0 commit comments

Comments
 (0)