Skip to content

Commit 3fe6228

Browse files
committed
Restrict visibility check to specific HIR items
1 parent ec49af7 commit 3fe6228

File tree

4 files changed

+75
-19
lines changed

4 files changed

+75
-19
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,26 +1300,34 @@ impl UnreachablePub {
13001300
{
13011301
// prefer suggesting `pub(super)` instead of `pub(crate)` when possible
13021302
let new_vis = match cx.tcx.opt_parent(def_id.into()) {
1303-
Some(parent_def_id) => match cx.tcx.visibility(parent_def_id) {
1304-
// parent is either `pub(crate)`, `pub(self)` or `pub(in ...)`
1305-
ty::Visibility::Restricted(restricted_id) => {
1306-
if let Some(local_parent_def_id) = parent_def_id.as_local()
1307-
&& restricted_id
1308-
== cx.tcx.parent_module_from_def_id(local_parent_def_id).into()
1309-
{
1310-
// parent is `pub(self)`, current item can only be used by parent
1311-
"pub(super)"
1312-
} else {
1313-
// parent is `pub(crate)` or `pub(in ...)`, current item accessible
1314-
// from more place then parent
1303+
Some(parent_def_id) => match cx.tcx.def_kind(parent_def_id) {
1304+
// parent is a module, or an impl/extern block, check it's visibility
1305+
DefKind::Mod | DefKind::Impl { .. } | DefKind::ForeignMod => match cx
1306+
.tcx
1307+
.visibility(parent_def_id)
1308+
{
1309+
// parent is either `pub(crate)`, `pub(self)` or `pub(in ...)`
1310+
ty::Visibility::Restricted(restricted_id) => {
1311+
if let Some(local_parent_def_id) = parent_def_id.as_local()
1312+
&& restricted_id
1313+
== cx.tcx.parent_module_from_def_id(local_parent_def_id).into()
1314+
{
1315+
// parent is `pub(self)`, current item can only be used by parent
1316+
"pub(super)"
1317+
} else {
1318+
// parent is `pub(crate)` or `pub(in ...)`, current item accessible
1319+
// from more place then parent
1320+
"pub(crate)"
1321+
}
1322+
}
1323+
// parent is `pub`
1324+
ty::Visibility::Public => {
1325+
// current item may be used from anywhere in the crate
13151326
"pub(crate)"
13161327
}
1317-
}
1318-
// parent is `pub`
1319-
ty::Visibility::Public => {
1320-
// current item may be used from anywhere in the crate
1321-
"pub(crate)"
1322-
}
1328+
},
1329+
// current item can't be accessed outside of the current block
1330+
_ => "",
13231331
},
13241332
None => "pub(crate)",
13251333
};

tests/ui/lint/unreachable_pub.fixed

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ mod private_mod {
6262
pub(crate) static NITROGEN: usize = 2; //~ WARNING unreachable_pub
6363
}
6464

65+
fn foo() {
66+
const {
67+
struct Foo; //~ WARNING unreachable_pub
68+
};
69+
}
70+
71+
enum Weird {
72+
Variant = {
73+
struct Foo; //~ WARNING unreachable_pub
74+
0
75+
},
76+
}
77+
6578
// items leaked through signatures (see `get_neon` below) are OK
6679
pub struct Neon {}
6780

@@ -84,4 +97,5 @@ fn main() {
8497
let _ = private_mod::beryllium();
8598
let _ = private_mod::crate_in_private::CARBON;
8699
let _ = private_mod::pub_in_private::NITROGEN;
100+
let _ = unsafe { private_mod::catalyze() };
87101
}

tests/ui/lint/unreachable_pub.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ mod private_mod {
6262
pub static NITROGEN: usize = 2; //~ WARNING unreachable_pub
6363
}
6464

65+
fn foo() {
66+
const {
67+
pub struct Foo; //~ WARNING unreachable_pub
68+
};
69+
}
70+
71+
enum Weird {
72+
Variant = {
73+
pub struct Foo; //~ WARNING unreachable_pub
74+
0
75+
},
76+
}
77+
6578
// items leaked through signatures (see `get_neon` below) are OK
6679
pub struct Neon {}
6780

@@ -84,4 +97,5 @@ fn main() {
8497
let _ = private_mod::beryllium();
8598
let _ = private_mod::crate_in_private::CARBON;
8699
let _ = private_mod::pub_in_private::NITROGEN;
100+
let _ = unsafe { private_mod::catalyze() };
87101
}

tests/ui/lint/unreachable_pub.stderr

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,26 @@ LL | pub mod pub_in_private {
146146
|
147147
= help: or consider exporting it for use by other crates
148148

149+
warning: unreachable `pub` item
150+
--> $DIR/unreachable_pub.rs:67:13
151+
|
152+
LL | pub struct Foo;
153+
| ---^^^^^^^^^^^
154+
| |
155+
| help: consider restricting its visibility
156+
|
157+
= help: or consider exporting it for use by other crates
158+
159+
warning: unreachable `pub` item
160+
--> $DIR/unreachable_pub.rs:73:13
161+
|
162+
LL | pub struct Foo;
163+
| ---^^^^^^^^^^^
164+
| |
165+
| help: consider restricting its visibility
166+
|
167+
= help: or consider exporting it for use by other crates
168+
149169
warning: unreachable `pub` item
150170
--> $DIR/unreachable_pub.rs:53:9
151171
|
@@ -186,5 +206,5 @@ LL | pub static NITROGEN: usize = 2;
186206
|
187207
= help: or consider exporting it for use by other crates
188208

189-
warning: 18 warnings emitted
209+
warning: 20 warnings emitted
190210

0 commit comments

Comments
 (0)