Skip to content

Commit ad82e0a

Browse files
committed
Auto merge of #28504 - Eljay:fix-trait-privacy, r=nrc
Fixes #16264 / #18241. As far as I can tell, it should be impossible for a trait to be inaccessible if it's in scope, so this check is unnecessary. Are there any cases where this check is actually needed?
2 parents ecbd8c3 + b3e1aca commit ad82e0a

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

Diff for: src/librustc_privacy/lib.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -847,12 +847,8 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
847847
ty::ImplContainer(_) => {
848848
self.check_static_method(span, method_def_id, name)
849849
}
850-
// Trait methods are always all public. The only controlling factor
851-
// is whether the trait itself is accessible or not.
852-
ty::TraitContainer(trait_def_id) => {
853-
self.report_error(self.ensure_public(span, trait_def_id,
854-
None, "source trait"));
855-
}
850+
// Trait methods are always accessible if the trait is in scope.
851+
ty::TraitContainer(_) => {}
856852
}
857853
}
858854
}

Diff for: src/test/compile-fail/privacy-ufcs.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
// Test to ensure private traits are inaccessible with UFCS angle-bracket syntax.
12+
13+
mod foo {
14+
trait Bar {
15+
fn baz() {}
16+
}
17+
18+
impl Bar for i32 {}
19+
}
20+
21+
fn main() {
22+
<i32 as ::foo::Bar>::baz(); //~ERROR method `baz` is inaccessible
23+
//~^NOTE: trait `Bar` is private
24+
}

Diff for: src/test/run-pass/issue-16264.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
use outer::Foo;
12+
13+
mod outer {
14+
pub use self::inner::Foo;
15+
16+
mod inner {
17+
pub trait Foo {
18+
fn bar(&self) {}
19+
}
20+
impl Foo for i32 {}
21+
}
22+
}
23+
24+
fn main() {
25+
let x: i32 = 0;
26+
x.bar();
27+
}

0 commit comments

Comments
 (0)