Skip to content

Commit 5885285

Browse files
committed
Add new unstable flag -Ztreat-pub-as-pub-crate
1 parent 03ce87d commit 5885285

File tree

6 files changed

+67
-15
lines changed

6 files changed

+67
-15
lines changed

compiler/rustc_passes/src/dead.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_middle::middle::privacy::Level;
1919
use rustc_middle::query::Providers;
2020
use rustc_middle::ty::{self, AssocTag, TyCtxt};
2121
use rustc_middle::{bug, span_bug};
22+
use rustc_session::config::CrateType;
2223
use rustc_session::lint::builtin::DEAD_CODE;
2324
use rustc_session::lint::{self, LintExpectationId};
2425
use rustc_span::{Symbol, kw, sym};
@@ -813,22 +814,24 @@ fn maybe_record_as_seed<'tcx>(
813814
fn create_and_seed_worklist(
814815
tcx: TyCtxt<'_>,
815816
) -> (Vec<(LocalDefId, ComesFromAllowExpect)>, Vec<LocalDefId>) {
816-
let effective_visibilities = &tcx.effective_visibilities(());
817817
let mut unsolved_impl_item = Vec::new();
818-
let mut worklist = effective_visibilities
819-
.iter()
820-
.filter_map(|(&id, effective_vis)| {
821-
effective_vis
822-
.is_public_at_level(Level::Reachable)
823-
.then_some(id)
824-
.map(|id| (id, ComesFromAllowExpect::No))
825-
})
826-
// Seed entry point
827-
.chain(
828-
tcx.entry_fn(())
829-
.and_then(|(def_id, _)| def_id.as_local().map(|id| (id, ComesFromAllowExpect::No))),
830-
)
831-
.collect::<Vec<_>>();
818+
let mut worklist = Vec::new();
819+
820+
if let Some((def_id, _)) = tcx.entry_fn(())
821+
&& let Some(local_def_id) = def_id.as_local()
822+
{
823+
worklist.push((local_def_id, ComesFromAllowExpect::No));
824+
}
825+
826+
if !tcx.sess.opts.unstable_opts.treat_pub_as_pub_crate
827+
|| !tcx.crate_types().contains(&CrateType::Executable)
828+
{
829+
for (id, effective_vis) in tcx.effective_visibilities(()).iter() {
830+
if effective_vis.is_public_at_level(Level::Reachable) {
831+
worklist.push((*id, ComesFromAllowExpect::No));
832+
}
833+
}
834+
}
832835

833836
let crate_items = tcx.hir_crate_items(());
834837
for id in crate_items.owners() {

compiler/rustc_session/src/options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,8 @@ written to standard error output)"),
27272727
treat_err_as_bug: Option<NonZero<usize>> = (None, parse_treat_err_as_bug, [TRACKED],
27282728
"treat the `val`th error that occurs as bug (default if not specified: 0 - don't treat errors as bugs. \
27292729
default if specified without a value: 1 - treat the first error as bug)"),
2730+
treat_pub_as_pub_crate: bool = (false, parse_bool, [UNTRACKED],
2731+
"treat `pub` items as `pub(crate)` items for binary crates (default: no)"),
27302732
trim_diagnostic_paths: bool = (true, parse_bool, [UNTRACKED],
27312733
"in diagnostics, use heuristics to shorten paths referring to items"),
27322734
tune_cpu: Option<String> = (None, parse_opt_string, [TRACKED],
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//@ compile-flags: -Ztreat-pub-as-pub-crate
2+
#![feature(rustc_attrs)]
3+
#![deny(dead_code)]
4+
#![crate_type = "lib"]
5+
6+
pub fn unused_pub_fn() {} // Should NOT error because it's a library crate
7+
8+
fn unused_priv_fn() {} //~ ERROR function `unused_priv_fn` is never used
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: function `unused_priv_fn` is never used
2+
--> $DIR/treat-pub-as-pub-crate-lib.rs:8:4
3+
|
4+
LL | fn unused_priv_fn() {}
5+
| ^^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/treat-pub-as-pub-crate-lib.rs:3:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ compile-flags: -Ztreat-pub-as-pub-crate
2+
#![feature(rustc_attrs)]
3+
#![deny(dead_code)]
4+
5+
pub fn unused_pub_fn() {} //~ ERROR function `unused_pub_fn` is never used
6+
7+
pub fn used_pub_fn() {}
8+
9+
fn main() {
10+
used_pub_fn();
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: function `unused_pub_fn` is never used
2+
--> $DIR/treat-pub-as-pub-crate.rs:5:8
3+
|
4+
LL | pub fn unused_pub_fn() {}
5+
| ^^^^^^^^^^^^^
6+
|
7+
note: the lint level is defined here
8+
--> $DIR/treat-pub-as-pub-crate.rs:3:9
9+
|
10+
LL | #![deny(dead_code)]
11+
| ^^^^^^^^^
12+
13+
error: aborting due to 1 previous error
14+

0 commit comments

Comments
 (0)