Skip to content
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
6 changes: 4 additions & 2 deletions compiler/rustc_builtin_macros/src/test_harness.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Code that generates a test runner to run all the tests in a crate

use std::mem;
use std::sync::atomic::Ordering;

use rustc_ast as ast;
use rustc_ast::attr::contains_name;
Expand Down Expand Up @@ -202,7 +203,7 @@ impl<'a> MutVisitor for EntryPointCleaner<'a> {
// clash with the one we're going to add, but mark it as
// #[allow(dead_code)] to avoid printing warnings.
match entry_point_type(&item, self.depth == 0) {
EntryPointType::MainNamed | EntryPointType::RustcMainAttr => {
EntryPointType::RustcMainAttr => {
let allow_dead_code = attr::mk_attr_nested_word(
&self.sess.psess.attr_id_generator,
ast::AttrStyle::Outer,
Expand All @@ -213,8 +214,9 @@ impl<'a> MutVisitor for EntryPointCleaner<'a> {
);
item.attrs.retain(|attr| !attr.has_name(sym::rustc_main));
item.attrs.push(allow_dead_code);
self.sess.removed_rustc_main_attr.store(true, Ordering::Relaxed);
}
EntryPointType::None | EntryPointType::OtherMain => {}
EntryPointType::None | EntryPointType::MainNamed | EntryPointType::OtherMain => {}
};
}
}
Expand Down
16 changes: 16 additions & 0 deletions compiler/rustc_passes/src/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use std::mem;
use std::ops::ControlFlow;
use std::sync::atomic::Ordering;

use hir::def_id::{LocalDefIdMap, LocalDefIdSet};
use rustc_abi::FieldIdx;
Expand Down Expand Up @@ -930,6 +931,21 @@ fn create_and_seed_worklist(tcx: TyCtxt<'_>) -> SeedWorklists {
});
}

// Under `--test`, what `main` resolves to is the would-be entry point of a normal build,
// so keep it live, unless a stripped user `#[rustc_main]` would have been the entry instead.
if tcx.sess.is_test_crate()
Comment thread
mu001999 marked this conversation as resolved.
&& !tcx.sess.removed_rustc_main_attr.load(Ordering::Relaxed)
&& let Some(main_def) = tcx.resolutions(()).main_def
&& let Some(def_id) = main_def.opt_fn_def_id()
&& let Some(local_def_id) = def_id.as_local()
{
worklist.push(WorkItem {
id: local_def_id,
propagated: ComesFromAllowExpect::No,
own: ComesFromAllowExpect::No,
});
}

for (id, effective_vis) in tcx.effective_visibilities(()).iter() {
if effective_vis.is_public_at_level(Level::Reachable) {
deferred_seeds.push(WorkItem {
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_session/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ pub struct Session {
///
/// The value is the `DepNodeIndex` of the node encodes the used feature.
pub used_features: Lock<FxHashMap<Symbol, u32>>,

/// Whether the test harness removed a user-written `#[rustc_main]` attribute
/// while generating the synthetic test entry point.
pub removed_rustc_main_attr: AtomicBool,
}

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -1133,6 +1137,7 @@ pub fn build_session(
thin_lto_supported: true, // filled by `run_compiler`
mir_opt_bisect_eval_count: AtomicUsize::new(0),
used_features: Lock::default(),
removed_rustc_main_attr: AtomicBool::new(false),
};

validate_commandline_args_with_session_available(&sess);
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/lint/dead-code/imported_main_dead_code_under_test.rs
Comment thread
mu001999 marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@ check-pass
//@ compile-flags: --test

// Regression test for https://github.com/rust-lang/rust/issues/157608: a function used
// as `main` via a rename import was wrongly reported as dead code under `--test`.

#![deny(dead_code)]

fn different_main() {
println!("Hello from different_main");
}

use different_main as main;
10 changes: 10 additions & 0 deletions tests/ui/lint/dead-code/imported_main_glob_under_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ check-pass
//@ compile-flags: --test

#![deny(dead_code)]

mod m {
pub fn main() {}
}

use m::*;
10 changes: 10 additions & 0 deletions tests/ui/lint/dead-code/imported_main_path_under_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//@ check-pass
//@ compile-flags: --test

#![deny(dead_code)]

mod m {
pub fn main() {}
}

use m::main;
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//@ check-pass
//@ compile-flags: --test


#![deny(dead_code)]

mod m {
pub fn other() {}
}

use m::other as main;
22 changes: 22 additions & 0 deletions tests/ui/lint/dead-code/lint-dead-code-2-under-test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//@ compile-flags: --test

// A `fn main` demoted by an explicit `#[rustc_main]` on another function is dead code and
// must be flagged under `--test` just like in a normal build.

#![allow(unused_variables)]
#![deny(dead_code)]
#![feature(rustc_attrs)]

fn dead_fn() {} //~ ERROR: function `dead_fn` is never used

fn used_fn() {}

#[rustc_main]
fn actual_main() {
used_fn();
}

// this is not main
fn main() { //~ ERROR: function `main` is never used
dead_fn();
}
20 changes: 20 additions & 0 deletions tests/ui/lint/dead-code/lint-dead-code-2-under-test.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
error: function `dead_fn` is never used
--> $DIR/lint-dead-code-2-under-test.rs:10:4
|
LL | fn dead_fn() {}
| ^^^^^^^
|
note: the lint level is defined here
--> $DIR/lint-dead-code-2-under-test.rs:7:9
|
LL | #![deny(dead_code)]
| ^^^^^^^^^

error: function `main` is never used
--> $DIR/lint-dead-code-2-under-test.rs:20:4
|
LL | fn main() {
| ^^^^

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@ compile-flags: --test

#![deny(dead_code)]

fn main() {}

mod m {
pub fn main() {} //~ ERROR: function `main` is never used
}
14 changes: 14 additions & 0 deletions tests/ui/lint/dead-code/submodule_main_not_entry_under_test.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: function `main` is never used
--> $DIR/submodule_main_not_entry_under_test.rs:8:12
|
LL | pub fn main() {}
| ^^^^
|
note: the lint level is defined here
--> $DIR/submodule_main_not_entry_under_test.rs:3:9
|
LL | #![deny(dead_code)]
| ^^^^^^^^^

error: aborting due to 1 previous error

Loading