Skip to content

Commit a1fb861

Browse files
authored
Rollup merge of #114099 - davidtwco:issue-113860-staged-api-effective-vis-gt-nominal-vis-when-trait-method-vis, r=petrochenkov
privacy: no nominal visibility for assoc fns Fixes #113860. When `staged_api` is enabled, effective visibilities are computed earlier and this can trigger an ICE in some cases. In particular, if a impl of a trait method has a visibility then an error will be reported for that, but when privacy invariants are being checked, the effective visibility will still be greater than the nominal visbility and that will trigger a `span_bug!`. However, this invariant - that effective visibilites are limited to nominal visibility - doesn't make sense for associated functions.
2 parents 317ec04 + e051a32 commit a1fb861

File tree

7 files changed

+202
-2
lines changed

7 files changed

+202
-2
lines changed

Diff for: compiler/rustc_middle/src/middle/privacy.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,20 @@ impl EffectiveVisibilities {
178178
// All effective visibilities except `reachable_through_impl_trait` are limited to
179179
// nominal visibility. For some items nominal visibility doesn't make sense so we
180180
// don't check this condition for them.
181-
if !matches!(tcx.def_kind(def_id), DefKind::Impl { .. }) {
181+
let is_impl = matches!(tcx.def_kind(def_id), DefKind::Impl { .. });
182+
let is_associated_item_in_trait_impl = tcx
183+
.impl_of_method(def_id.to_def_id())
184+
.and_then(|impl_id| tcx.trait_id_of_impl(impl_id))
185+
.is_some();
186+
if !is_impl && !is_associated_item_in_trait_impl {
182187
let nominal_vis = tcx.visibility(def_id);
183188
if !nominal_vis.is_at_least(ev.reachable, tcx) {
184189
span_bug!(
185190
span,
186191
"{:?}: reachable {:?} > nominal {:?}",
187192
def_id,
188193
ev.reachable,
189-
nominal_vis
194+
nominal_vis,
190195
);
191196
}
192197
}

Diff for: tests/ui/privacy/issue-113860-1.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(staged_api)]
2+
//~^ ERROR module has missing stability attribute
3+
4+
pub trait Trait {
5+
//~^ ERROR trait has missing stability attribute
6+
fn fun() {}
7+
//~^ ERROR associated function has missing stability attribute
8+
}
9+
10+
impl Trait for u8 {
11+
//~^ ERROR implementation has missing stability attribute
12+
pub(self) fn fun() {}
13+
//~^ ERROR visibility qualifiers are not permitted here [E0449]
14+
}
15+
16+
fn main() {}

Diff for: tests/ui/privacy/issue-113860-1.stderr

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0449]: visibility qualifiers are not permitted here
2+
--> $DIR/issue-113860-1.rs:12:5
3+
|
4+
LL | pub(self) fn fun() {}
5+
| ^^^^^^^^^
6+
|
7+
= note: trait items always share the visibility of their trait
8+
9+
error: module has missing stability attribute
10+
--> $DIR/issue-113860-1.rs:1:1
11+
|
12+
LL | / #![feature(staged_api)]
13+
LL | |
14+
LL | |
15+
LL | | pub trait Trait {
16+
... |
17+
LL | |
18+
LL | | fn main() {}
19+
| |____________^
20+
21+
error: trait has missing stability attribute
22+
--> $DIR/issue-113860-1.rs:4:1
23+
|
24+
LL | / pub trait Trait {
25+
LL | |
26+
LL | | fn fun() {}
27+
LL | |
28+
LL | | }
29+
| |_^
30+
31+
error: implementation has missing stability attribute
32+
--> $DIR/issue-113860-1.rs:10:1
33+
|
34+
LL | / impl Trait for u8 {
35+
LL | |
36+
LL | | pub(self) fn fun() {}
37+
LL | |
38+
LL | | }
39+
| |_^
40+
41+
error: associated function has missing stability attribute
42+
--> $DIR/issue-113860-1.rs:6:5
43+
|
44+
LL | fn fun() {}
45+
| ^^^^^^^^^^^
46+
47+
error: aborting due to 5 previous errors
48+
49+
For more information about this error, try `rustc --explain E0449`.

Diff for: tests/ui/privacy/issue-113860-2.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(staged_api)]
2+
//~^ ERROR module has missing stability attribute
3+
4+
pub trait Trait {
5+
//~^ ERROR trait has missing stability attribute
6+
type X;
7+
//~^ ERROR associated type has missing stability attribute
8+
}
9+
10+
impl Trait for u8 {
11+
//~^ ERROR implementation has missing stability attribute
12+
pub(self) type X = Self;
13+
//~^ ERROR visibility qualifiers are not permitted here [E0449]
14+
}
15+
16+
fn main() {}

Diff for: tests/ui/privacy/issue-113860-2.stderr

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0449]: visibility qualifiers are not permitted here
2+
--> $DIR/issue-113860-2.rs:12:5
3+
|
4+
LL | pub(self) type X = Self;
5+
| ^^^^^^^^^
6+
|
7+
= note: trait items always share the visibility of their trait
8+
9+
error: module has missing stability attribute
10+
--> $DIR/issue-113860-2.rs:1:1
11+
|
12+
LL | / #![feature(staged_api)]
13+
LL | |
14+
LL | |
15+
LL | | pub trait Trait {
16+
... |
17+
LL | |
18+
LL | | fn main() {}
19+
| |____________^
20+
21+
error: trait has missing stability attribute
22+
--> $DIR/issue-113860-2.rs:4:1
23+
|
24+
LL | / pub trait Trait {
25+
LL | |
26+
LL | | type X;
27+
LL | |
28+
LL | | }
29+
| |_^
30+
31+
error: implementation has missing stability attribute
32+
--> $DIR/issue-113860-2.rs:10:1
33+
|
34+
LL | / impl Trait for u8 {
35+
LL | |
36+
LL | | pub(self) type X = Self;
37+
LL | |
38+
LL | | }
39+
| |_^
40+
41+
error: associated type has missing stability attribute
42+
--> $DIR/issue-113860-2.rs:6:5
43+
|
44+
LL | type X;
45+
| ^^^^^^^
46+
47+
error: aborting due to 5 previous errors
48+
49+
For more information about this error, try `rustc --explain E0449`.

Diff for: tests/ui/privacy/issue-113860.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(staged_api)]
2+
//~^ ERROR module has missing stability attribute
3+
4+
pub trait Trait {
5+
//~^ ERROR trait has missing stability attribute
6+
const X: u32;
7+
//~^ ERROR associated constant has missing stability attribute
8+
}
9+
10+
impl Trait for u8 {
11+
//~^ ERROR implementation has missing stability attribute
12+
pub(self) const X: u32 = 3;
13+
//~^ ERROR visibility qualifiers are not permitted here [E0449]
14+
}
15+
16+
fn main() {}

Diff for: tests/ui/privacy/issue-113860.stderr

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
error[E0449]: visibility qualifiers are not permitted here
2+
--> $DIR/issue-113860.rs:12:5
3+
|
4+
LL | pub(self) const X: u32 = 3;
5+
| ^^^^^^^^^
6+
|
7+
= note: trait items always share the visibility of their trait
8+
9+
error: module has missing stability attribute
10+
--> $DIR/issue-113860.rs:1:1
11+
|
12+
LL | / #![feature(staged_api)]
13+
LL | |
14+
LL | |
15+
LL | | pub trait Trait {
16+
... |
17+
LL | |
18+
LL | | fn main() {}
19+
| |____________^
20+
21+
error: trait has missing stability attribute
22+
--> $DIR/issue-113860.rs:4:1
23+
|
24+
LL | / pub trait Trait {
25+
LL | |
26+
LL | | const X: u32;
27+
LL | |
28+
LL | | }
29+
| |_^
30+
31+
error: implementation has missing stability attribute
32+
--> $DIR/issue-113860.rs:10:1
33+
|
34+
LL | / impl Trait for u8 {
35+
LL | |
36+
LL | | pub(self) const X: u32 = 3;
37+
LL | |
38+
LL | | }
39+
| |_^
40+
41+
error: associated constant has missing stability attribute
42+
--> $DIR/issue-113860.rs:6:5
43+
|
44+
LL | const X: u32;
45+
| ^^^^^^^^^^^^^
46+
47+
error: aborting due to 5 previous errors
48+
49+
For more information about this error, try `rustc --explain E0449`.

0 commit comments

Comments
 (0)