Skip to content

Add little is_test_crate function #109985

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
merged 2 commits into from
Apr 10, 2023
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
2 changes: 1 addition & 1 deletion compiler/rustc_builtin_macros/src/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pub fn inject(krate: &mut ast::Crate, sess: &Session, resolver: &mut dyn Resolve
// even in non-test builds
let test_runner = get_test_runner(span_diagnostic, &krate);

if sess.opts.test {
if sess.is_test_crate() {
let panic_strategy = match (panic_strategy, sess.opts.unstable_opts.panic_abort_tests) {
(PanicStrategy::Abort, true) => PanicStrategy::Abort,
(PanicStrategy::Abort, false) => {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/back/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2301,7 +2301,7 @@ fn add_native_libs_from_crate(
|| (whole_archive == None
&& bundle
&& cnum == LOCAL_CRATE
&& sess.opts.test);
&& sess.is_test_crate());

if bundle && cnum != LOCAL_CRATE {
if let Some(filename) = lib.filename {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ fn configure_and_expand(
features: Some(features),
recursion_limit,
trace_mac: sess.opts.unstable_opts.trace_macros,
should_test: sess.opts.test,
should_test: sess.is_test_crate(),
span_debug: sess.opts.unstable_opts.span_debug,
proc_macro_backtrace: sess.opts.unstable_opts.proc_macro_backtrace,
..rustc_expand::expand::ExpansionConfig::default(crate_name.to_string())
Expand Down Expand Up @@ -292,7 +292,7 @@ fn configure_and_expand(
}

sess.time("maybe_create_a_macro_crate", || {
let is_test_crate = sess.opts.test;
let is_test_crate = sess.is_test_crate();
rustc_builtin_macros::proc_macro_harness::inject(
&mut krate,
sess,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ struct MissingStabilityAnnotations<'tcx> {
impl<'tcx> MissingStabilityAnnotations<'tcx> {
fn check_missing_stability(&self, def_id: LocalDefId, span: Span) {
let stab = self.tcx.stability().local_stability(def_id);
if !self.tcx.sess.opts.test
if !self.tcx.sess.is_test_crate()
&& stab.is_none()
&& self.effective_visibilities.is_reachable(def_id)
{
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_resolve/src/check_unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ impl Resolver<'_, '_> {
// If we are in the `--test` mode, suppress a help that adds the `#[cfg(test)]`
// attribute; however, if not, suggest adding the attribute. There is no way to
// retrieve attributes here because we do not have a `TyCtxt` yet.
let test_module_span = if tcx.sess.opts.test {
let test_module_span = if tcx.sess.is_test_crate() {
None
} else {
let parent_module = visitor.r.get_nearest_non_block_module(
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_session/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1258,7 +1258,7 @@ pub fn build_configuration(sess: &Session, mut user_cfg: CrateConfig) -> CrateCo
// some default and generated configuration items.
let default_cfg = default_configuration(sess);
// If the user wants a test runner, then add the test cfg.
if sess.opts.test {
if sess.is_test_crate() {
user_cfg.insert((sym::test, None));
}
user_cfg.extend(default_cfg.iter().cloned());
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 @@ -294,6 +294,11 @@ impl Session {
self.crate_types.get().unwrap().as_slice()
}

/// Returns true if the crate is a testing one.
pub fn is_test_crate(&self) -> bool {
self.opts.test
}

pub fn needs_crate_hash(&self) -> bool {
// Why is the crate hash needed for these configurations?
// - debug_assertions: for the "fingerprint the result" check in
Expand Down