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

Tidy: Allow inline tests in the compiler #110619

Closed
wants to merge 1 commit into from

Conversation

WaffleLapkin
Copy link
Member

It seems like the original reason for this check is irrelevant now, so let's just remove it.

TL;DR: this allows using #[cfg(test)] mod tests { ... } (inline module instead of mod tests;)

@rustbot
Copy link
Collaborator

rustbot commented Apr 20, 2023

r? @albertlarsan68

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) labels Apr 20, 2023
@Mark-Simulacrum
Copy link
Member

Do you have a pointer to what fixed the reason this was done? I believe cargo without this will recompile the base library and then compile the tests even if only the tests are changed. I'm not aware of what has changed to make that not true (though would love to be pleasantly surprised!)

@WaffleLapkin
Copy link
Member Author

@Mark-Simulacrum hm, I'm not sure, but I do not see how

#[cfg(test)]
mod tests { /* ... */ }

and

#[cfg(test)]
mod tests;

might be different, iirc rustc expands the latter into the former.


#61097 looked like it was caused by the migration from unit tests to UI tests, which I thought was complete, but now re-reading it, maybe there is a problem with incremental re-builds 🤔

@ehuss
Copy link
Contributor

ehuss commented Apr 20, 2023

#[cfg(test)]
mod tests;

is different in that during a normal build, Cargo will ignore any changes to the tests.rs file. But when built as a test, then it will monitor for changes to the file. For the compiler this is important because for the inline version, if you modify a test, that would require rebuilding the entirety of the compiler and then build the crate being unittested (which can take a long time). When it is a separate file, only the crate being unittested would need to be recompiled.

@WaffleLapkin
Copy link
Member Author

I see...

This is weird though, mod tests { ... } is such a common pattern everywhere in rust, shouldn't we support it better?...

@ehuss
Copy link
Contributor

ehuss commented Apr 20, 2023

That seems like an extremely difficult thing to accomplish. Cargo needs some way to very quickly determine if a file has changed and should trigger a rebuild. I can only imagine that to do intra-file change detection would require parsing the entire crate and doing name resolution and expansion to determine if only content within some cfg removed section has changed (or having some kind of line-to-used-cfg mapping). I think that would have a very high complexity and performance hit.

@WaffleLapkin
Copy link
Member Author

@ehuss I'd imagine that the compiler output after a change under #[cfg(false)] is not changed, so you don't need to rebuild the reverse dependencies.

From your words I assume that cargo doesn't try to hash rustc output to determine if anything actually changed and always issues reverse-dep-rebuilds... Is this a deliberate thing, or just implementation detail?

@ehuss
Copy link
Contributor

ehuss commented Apr 20, 2023

It might be feasible to hash and compare the outputs, but that might require a significant rewrite of how Cargo does change tracking. It also assumes a few factors, such as reproducible builds, that the source changes don't materially change span locations, and that Cargo knows all the outputs from rustc (which it currently doesn't, it just guesses in most cases).

@Mark-Simulacrum
Copy link
Member

Note that even if we solved the reverse dependency problem, we would still do ~2x the work since we'd compile both the cfg=test crate and the non-cfg=test crate (just to detect it hasn't changed).

@jyn514
Copy link
Member

jyn514 commented Apr 21, 2023

I'm going to close this since it would regress compile times today. Feel free to reopen if you want to add a comment documenting why this change exists. If you want to pursue changing cargo's cache invalidation algorithm, it sounds like that would need fairly close consultation with the cargo team and I think Zulip is a better place for that than this PR.

@jyn514 jyn514 closed this Apr 21, 2023
@WaffleLapkin WaffleLapkin deleted the yes_inline_tests branch April 21, 2023 17:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-testsuite Area: The testsuite used to check the correctness of rustc S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants