Skip to content

privacy: no nominal visibility for assoc fns #114099

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
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
9 changes: 7 additions & 2 deletions compiler/rustc_middle/src/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,20 @@ impl EffectiveVisibilities {
// All effective visibilities except `reachable_through_impl_trait` are limited to
// nominal visibility. For some items nominal visibility doesn't make sense so we
// don't check this condition for them.
if !matches!(tcx.def_kind(def_id), DefKind::Impl { .. }) {
let is_impl = matches!(tcx.def_kind(def_id), DefKind::Impl { .. });
let is_associated_item_in_trait_impl = tcx
.impl_of_method(def_id.to_def_id())
.and_then(|impl_id| tcx.trait_id_of_impl(impl_id))
.is_some();
if !is_impl && !is_associated_item_in_trait_impl {
let nominal_vis = tcx.visibility(def_id);
if !nominal_vis.is_at_least(ev.reachable, tcx) {
span_bug!(
span,
"{:?}: reachable {:?} > nominal {:?}",
def_id,
ev.reachable,
nominal_vis
nominal_vis,
);
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/ui/privacy/issue-113860-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(staged_api)]
//~^ ERROR module has missing stability attribute

pub trait Trait {
//~^ ERROR trait has missing stability attribute
fn fun() {}
//~^ ERROR associated function has missing stability attribute
}

impl Trait for u8 {
//~^ ERROR implementation has missing stability attribute
pub(self) fn fun() {}
//~^ ERROR visibility qualifiers are not permitted here [E0449]
}

fn main() {}
49 changes: 49 additions & 0 deletions tests/ui/privacy/issue-113860-1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
error[E0449]: visibility qualifiers are not permitted here
--> $DIR/issue-113860-1.rs:12:5
|
LL | pub(self) fn fun() {}
| ^^^^^^^^^
|
= note: trait items always share the visibility of their trait

error: module has missing stability attribute
--> $DIR/issue-113860-1.rs:1:1
|
LL | / #![feature(staged_api)]
LL | |
LL | |
LL | | pub trait Trait {
... |
LL | |
LL | | fn main() {}
| |____________^

error: trait has missing stability attribute
--> $DIR/issue-113860-1.rs:4:1
|
LL | / pub trait Trait {
LL | |
LL | | fn fun() {}
LL | |
LL | | }
| |_^

error: implementation has missing stability attribute
--> $DIR/issue-113860-1.rs:10:1
|
LL | / impl Trait for u8 {
LL | |
LL | | pub(self) fn fun() {}
LL | |
LL | | }
| |_^

error: associated function has missing stability attribute
--> $DIR/issue-113860-1.rs:6:5
|
LL | fn fun() {}
| ^^^^^^^^^^^

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0449`.
16 changes: 16 additions & 0 deletions tests/ui/privacy/issue-113860-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(staged_api)]
//~^ ERROR module has missing stability attribute

pub trait Trait {
//~^ ERROR trait has missing stability attribute
type X;
//~^ ERROR associated type has missing stability attribute
}

impl Trait for u8 {
//~^ ERROR implementation has missing stability attribute
pub(self) type X = Self;
//~^ ERROR visibility qualifiers are not permitted here [E0449]
}

fn main() {}
49 changes: 49 additions & 0 deletions tests/ui/privacy/issue-113860-2.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
error[E0449]: visibility qualifiers are not permitted here
--> $DIR/issue-113860-2.rs:12:5
|
LL | pub(self) type X = Self;
| ^^^^^^^^^
|
= note: trait items always share the visibility of their trait

error: module has missing stability attribute
--> $DIR/issue-113860-2.rs:1:1
|
LL | / #![feature(staged_api)]
LL | |
LL | |
LL | | pub trait Trait {
... |
LL | |
LL | | fn main() {}
| |____________^

error: trait has missing stability attribute
--> $DIR/issue-113860-2.rs:4:1
|
LL | / pub trait Trait {
LL | |
LL | | type X;
LL | |
LL | | }
| |_^

error: implementation has missing stability attribute
--> $DIR/issue-113860-2.rs:10:1
|
LL | / impl Trait for u8 {
LL | |
LL | | pub(self) type X = Self;
LL | |
LL | | }
| |_^

error: associated type has missing stability attribute
--> $DIR/issue-113860-2.rs:6:5
|
LL | type X;
| ^^^^^^^

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0449`.
16 changes: 16 additions & 0 deletions tests/ui/privacy/issue-113860.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(staged_api)]
//~^ ERROR module has missing stability attribute

pub trait Trait {
//~^ ERROR trait has missing stability attribute
const X: u32;
//~^ ERROR associated constant has missing stability attribute
}

impl Trait for u8 {
//~^ ERROR implementation has missing stability attribute
pub(self) const X: u32 = 3;
//~^ ERROR visibility qualifiers are not permitted here [E0449]
}

fn main() {}
49 changes: 49 additions & 0 deletions tests/ui/privacy/issue-113860.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
error[E0449]: visibility qualifiers are not permitted here
--> $DIR/issue-113860.rs:12:5
|
LL | pub(self) const X: u32 = 3;
| ^^^^^^^^^
|
= note: trait items always share the visibility of their trait

error: module has missing stability attribute
--> $DIR/issue-113860.rs:1:1
|
LL | / #![feature(staged_api)]
LL | |
LL | |
LL | | pub trait Trait {
... |
LL | |
LL | | fn main() {}
| |____________^

error: trait has missing stability attribute
--> $DIR/issue-113860.rs:4:1
|
LL | / pub trait Trait {
LL | |
LL | | const X: u32;
LL | |
LL | | }
| |_^

error: implementation has missing stability attribute
--> $DIR/issue-113860.rs:10:1
|
LL | / impl Trait for u8 {
LL | |
LL | | pub(self) const X: u32 = 3;
LL | |
LL | | }
| |_^

error: associated constant has missing stability attribute
--> $DIR/issue-113860.rs:6:5
|
LL | const X: u32;
| ^^^^^^^^^^^^^

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0449`.