Skip to content
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

Remove #[cfg(not(test))] gates in core #138082

Merged
merged 1 commit into from
Mar 16, 2025

Conversation

thaliaarchi
Copy link
Contributor

@thaliaarchi thaliaarchi commented Mar 6, 2025

These gates are unnecessary now that unit tests for core are in a separate package, coretests, instead of in the same files as the source code. They previously prevented the two core versions from conflicting with each other.

@rustbot
Copy link
Collaborator

rustbot commented Mar 6, 2025

r? @thomcc

rustbot has assigned @thomcc.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Mar 6, 2025
@thaliaarchi
Copy link
Contributor Author

I only found this comment explaining it after opening this. It seems like doc tests and macros require their own gates, and this commented gate is for everything else.

// Since core defines many fundamental lang items, all tests live in a
// separate crate, coretests (library/coretests), to avoid bizarre issues.
//
// Here we explicitly #[cfg]-out this whole crate when testing. If we don't do
// this, both the generated test artifact and the linked libtest (which
// transitively includes core) will both define the same set of lang items,
// and this will cause the E0152 "found duplicate lang item" error. See
// discussion in #50466 for details.
//
// This cfg won't affect doc tests.
#![cfg(not(test))]

I still want to see whether CI passes, as this PR essentially tests the doc test case.

@thaliaarchi thaliaarchi changed the title Remove #[cfg(not(test))] from core::slice impl blocks Remove #[cfg(not(test))] from core impl blocks Mar 6, 2025
@thaliaarchi thaliaarchi marked this pull request as draft March 6, 2025 02:21
@thaliaarchi
Copy link
Contributor Author

Interesting. CI passed with #[cfg(not(test))] gates removed on impl blocks in core::slice. I've now pushed a change to try removing those gates throughout all of core.

@thaliaarchi
Copy link
Contributor Author

thaliaarchi commented Mar 6, 2025

It seems these gates were added in #50466 (rustbuild: Allow quick testing of libstd and libcore at stage0, 2018-05-05), including the comment which I quoted above.

@tgross35
Copy link
Contributor

tgross35 commented Mar 6, 2025

Cc @bjorn3 who knows a lot about the test infra

@bjorn3
Copy link
Member

bjorn3 commented Mar 6, 2025

These were necessary way back when libcore unit tests were in the same files as the libcore source code to prevent the two libcore versions from conflicting with each other. They are no longer necessary anymore as the libcore unit tests are now in a separate coretests package (and even before that got moved into a separate integration test crate years ago).

@thaliaarchi thaliaarchi force-pushed the slice-cfg-not-test branch 2 times, most recently from 9ad9f77 to 0b41057 Compare March 6, 2025 20:24
@thaliaarchi
Copy link
Contributor Author

So it sounds like we can get rid of them! I've identified all the other occurrences and removed them now. Since the PR description will be in the merge, I've updated it with some context.

Are these mentioned issues relevant?

#[cfg(not(test))] // See #65860
#[macro_use]
mod macros;
// We don't export this through #[macro_export] for now, to avoid breakage.
// See https://github.com/rust-lang/rust/issues/82913
#[cfg(not(test))]
#[unstable(feature = "assert_matches", issue = "82775")]
/// Unstable module containing the unstable `assert_matches` macro.
pub mod assert_matches {
#[unstable(feature = "assert_matches", issue = "82775")]
pub use crate::macros::{assert_matches, debug_assert_matches};
}

@thaliaarchi thaliaarchi marked this pull request as ready for review March 6, 2025 20:27
These gates are unnecessary now that unit tests for `core` are in a
separate package, `coretests`, instead of in the same files as the
source code. They previously prevented the two `core` versions from
conflicting with each other.
@rustbot
Copy link
Collaborator

rustbot commented Mar 6, 2025

Some changes occurred in compiler/rustc_codegen_cranelift

cc @bjorn3

@thaliaarchi
Copy link
Contributor Author

A Cranelift patch needed to be regenerated

@tgross35
Copy link
Contributor

tgross35 commented Mar 8, 2025

There are some compiler tie-ins also gated behind not(test), e.g. #[cfg_attr(not(test), lang = "String")] and #[cfg_attr(not(test), rustc_diagnostic_item = "Vec")]. Are those removable for the same reason?

@thaliaarchi
Copy link
Contributor Author

Interestingly, some of those are in std like #[cfg_attr(not(test), rustc_diagnostic_item = "HashMap")]. The rustc_diagnostic_items in core and std are inconsistent whether they are gated and occur both ways.

@thaliaarchi thaliaarchi changed the title Remove #[cfg(not(test))] from core impl blocks Remove #[cfg(not(test))] gates in core Mar 8, 2025
@bjorn3
Copy link
Member

bjorn3 commented Mar 8, 2025

The ones in libstd do still need to be gated as libstd has a fair amount of unit tests that aren't split into a separate package like is the case for libcore. I'm trying to move unit tests to integration tests for libstd too.

@thomcc
Copy link
Member

thomcc commented Mar 16, 2025

Very nice. @bors r+

@bors
Copy link
Contributor

bors commented Mar 16, 2025

📌 Commit 638b226 has been approved by thomcc

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 16, 2025
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 16, 2025
Rollup of 16 pull requests

Successful merges:

 - rust-lang#133055 (Expand `CloneToUninit` documentation.)
 - rust-lang#137147 (Add exclude to config.toml)
 - rust-lang#137864 (Don't drop `Rvalue::WrapUnsafeBinder` during GVN)
 - rust-lang#137890 (doc: clarify that consume can be called after BufReader::peek)
 - rust-lang#137956 (Add RTN support to rustdoc)
 - rust-lang#137968 (Properly escape regexes in Python scripts)
 - rust-lang#138082 (Remove `#[cfg(not(test))]` gates in `core`)
 - rust-lang#138275 (expose `is_s390x_feature_detected!` from `std::arch`)
 - rust-lang#138303 (Fix Ptr inconsistency in {Rc,Arc})
 - rust-lang#138309 (Add missing doc for intrinsic (Fix PR135334))
 - rust-lang#138323 (Expand and organize `offset_of!` documentation.)
 - rust-lang#138329 (debug-assert that the size_hint is well-formed in `collect`)
 - rust-lang#138465 (linkchecker: bump html5ever)
 - rust-lang#138471 (Clean up some tests in tests/ui)
 - rust-lang#138472 (Add codegen test for rust-lang#129795)
 - rust-lang#138484 (Use lit span when suggesting suffix lit cast)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e084680 into rust-lang:master Mar 16, 2025
6 checks passed
@rustbot rustbot added this to the 1.87.0 milestone Mar 16, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 16, 2025
Rollup merge of rust-lang#138082 - thaliaarchi:slice-cfg-not-test, r=thomcc

Remove `#[cfg(not(test))]` gates in `core`

These gates are unnecessary now that unit tests for `core` are in a separate package, `coretests`, instead of in the same files as the source code. They previously prevented the two `core` versions from conflicting with each other.
@thaliaarchi thaliaarchi deleted the slice-cfg-not-test branch March 16, 2025 05:20
@thaliaarchi
Copy link
Contributor Author

I've now checked alloc to see whether it could benefit from this same cleanup. Looks like bjorn3 already handled it in #136642. The only modules with #[cfg(not(test))] gates and related are those which are #[path = …]-included and still require them.

github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request Mar 19, 2025
…thomcc

Remove `#[cfg(not(test))]` gates in `core`

These gates are unnecessary now that unit tests for `core` are in a separate package, `coretests`, instead of in the same files as the source code. They previously prevented the two `core` versions from conflicting with each other.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants