Skip to content

Remove cfg(test) from library/core #129592

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 1 commit into from
Aug 26, 2024
Merged

Conversation

saethlin
Copy link
Member

@saethlin saethlin commented Aug 25, 2024

The diff here is very small with the ignore whitespace option.

core doesn't/can't have unit tests. All of its tests are just modules under tests/, so it has no use for cfg(test), because the entire contents of library/core/src are only ever compiled with that cfg off, and the entire contents of library/core/tests are only ever compiled with that cfg on.

You can tell this is what's happening because we had #[cfg(test)] on a module declaration that has no source file.

I also deleted the extra mod tests { layer of nesting; there's no need to mention again in the module path that this is a module of tests. This exposes a name collision between the u128 module of tests and core::u128. Fixed that by using <u128>::MAX like is done in the check! macro, which is what avoids this name ambiguity for the other types.

@rustbot
Copy link
Collaborator

rustbot commented Aug 25, 2024

r? @tgross35

rustbot has assigned @tgross35.
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 Aug 25, 2024
@saethlin saethlin marked this pull request as ready for review August 25, 2024 23:13
@saethlin
Copy link
Member Author

CI passed. Phew. I was afraid these were somehow used for some complicated setup CI uses.

@@ -178,7 +178,7 @@ fn test_can_not_overflow() {

// Check u128 separately:
for base in 2..=36 {
let num = u128::MAX as u128;
let num = core::u128::MAX as u128;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this change needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last paragraph of the PR description was my attempt at a short summary. Here's a bigger one.

Without this part of the PR, removing the mod tests { wrapper module generates this error:

error[E0603]: constant import `MAX` is private
   --> core/tests/num/mod.rs:181:25
    |
181 |         let num = u128::MAX as u128;
    |                         ^^^ private constant import
    |
note: the constant import `MAX` is defined here...
   --> core/tests/num/uint_macros.rs:4:13
    |
1   | / macro_rules! uint_module {
2   | |     ($T:ident) => {
3   | |         use core::ops::{BitAnd, BitOr, BitXor, Not, Shl, Shr};
4   | |         use core::$T::*;
    | |             ^^^^^^^^^^^
...   |
316 | |     };
317 | | }
    | |_- in this expansion of `uint_module!`
    |
   ::: core/tests/num/u128.rs:1:1
    |
1   |   uint_module!(u128);
    |   ------------------ in this macro invocation
note: ...and refers to the constant `MAX` which is defined here
   --> /home/ben/rust/library/core/src/num/shells/int_macros.rs:44:9
    |
3   | / macro_rules! int_module {
4   | |     ($T:ident) => (int_module!($T, #[stable(feature = "rust1", since = "1.0.0")]););
5   | |     ($T:ident, #[$attr:meta]) => (
6   | |         #[doc = concat!(
...   |
44  | |         pub const MAX: $T = $T::MAX;
    | |         ^^^^^^^^^^^^^^^^^ you could import this directly
45  | |     )
46  | | }
    | |_- in this expansion of `int_module!`
    |
   ::: /home/ben/rust/library/core/src/num/shells/u128.rs:11:1
    |
11  |   int_module! { u128, #[stable(feature = "i128", since="1.26.0")] }
    |   ----------------------------------------------------------------- in this macro invocation
help: consider importing this constant instead
    |
181 |         let num = std::u128::MAX as u128;
    |                   ~~~~~~~~~~~~~~

Before this PR, name resolution must be concluding that u128::MAX cannot refer to the local u128 module because it doesn't contain an item called MAX. But with the other changes in this PR the local u128 module does contain an item MAX (which was previously at the path u128::tests::MAX)... except that the item is private. And name resolution doesn't recover from that state and try the other u128::MAX possibility?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh duh yeah I should read better

@tgross35
Copy link
Contributor

checks "hide whitespace" in the GH diff view lgtm. r=me with one comment

You can tell this is what's happening because we had #[cfg(test)] on a module declaration that has no source file.

I thought I noticed that yesterday when trying to figure out https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Unit.20tests.20in.20core. I wonder if we could have check-cfg explicitly forbid test

@saethlin
Copy link
Member Author

Ah! Yes your Zulip thread is exactly why I was thinking about this.

@tgross35
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Aug 26, 2024

📌 Commit 69acb67 has been approved by tgross35

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 Aug 26, 2024
@saethlin
Copy link
Member Author

Little race condition. While re-reading the code I realized why the macro above doesn't need special treatment: It does <$ty>. So I just imitated that in the u128 code. I prefer that better for the symmetry.

@saethlin
Copy link
Member Author

@bors r=tgross35

@bors
Copy link
Collaborator

bors commented Aug 26, 2024

📌 Commit 4f3ef2a has been approved by tgross35

It is now in the queue for this repository.

bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 26, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#129190 (Add f16 and f128 to tests/ui/consts/const-float-bits-conv.rs)
 - rust-lang#129377 (Add implementations for `unbounded_shl`/`unbounded_shr`)
 - rust-lang#129539 (link to Future::poll from the Poll docs)
 - rust-lang#129588 (pal/hermit: correctly round up microseconds in `Thread::sleep`)
 - rust-lang#129592 (Remove cfg(test) from library/core)
 - rust-lang#129597 (mv `build_reduced_graph_for_external_crate_res` into Resolver)
 - rust-lang#129600 (Tie `impl_trait_overcaptures` lint to Rust 2024)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Aug 26, 2024
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#129190 (Add f16 and f128 to tests/ui/consts/const-float-bits-conv.rs)
 - rust-lang#129377 (Add implementations for `unbounded_shl`/`unbounded_shr`)
 - rust-lang#129539 (link to Future::poll from the Poll docs)
 - rust-lang#129588 (pal/hermit: correctly round up microseconds in `Thread::sleep`)
 - rust-lang#129592 (Remove cfg(test) from library/core)
 - rust-lang#129597 (mv `build_reduced_graph_for_external_crate_res` into Resolver)
 - rust-lang#129600 (Tie `impl_trait_overcaptures` lint to Rust 2024)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit b9dfb4d into rust-lang:master Aug 26, 2024
6 checks passed
@rustbot rustbot added this to the 1.82.0 milestone Aug 26, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Aug 26, 2024
Rollup merge of rust-lang#129592 - saethlin:core-cfg-test, r=tgross35

Remove cfg(test) from library/core

The diff here is very small with the ignore whitespace option.

`core` doesn't/can't have unit tests. All of its tests are just modules under `tests/`, so it has no use for `cfg(test)`, because the entire contents of `library/core/src` are only ever compiled with that cfg off, and the entire contents of `library/core/tests` are only ever compiled with that cfg on.

You can tell this is what's happening because we had `#[cfg(test)]` on a module declaration that has no source file.

I also deleted the extra `mod tests {` layer of nesting; there's no need to mention again in the module path that this is a module of tests. This exposes a name collision between the `u128` module of tests and `core::u128`. Fixed that by using `<u128>::MAX` like is done in the `check!` macro, which is what avoids this name ambiguity for the other types.
@saethlin saethlin deleted the core-cfg-test branch August 26, 2024 23:02
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.

4 participants