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

Emit a warning if a match is too complex #122685

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

GuillaumeGomez
Copy link
Member

@GuillaumeGomez GuillaumeGomez commented Mar 18, 2024

Based on the results of crater run from #121979, we can safely set this limit without introducing regressions in the current rust ecosystem.

Follow-up of #121917.

r? @Nadrieril

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 18, 2024
@rustbot
Copy link
Collaborator

rustbot commented Mar 18, 2024

Some changes occurred in exhaustiveness checking

cc @Nadrieril

@rustbot
Copy link
Collaborator

rustbot commented Mar 18, 2024

Some changes occurred in match checking

cc @Nadrieril

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

Comment on lines 409 to 411
// Default value to emit the warning for "too complex" match. We picked it based on
// this crater run: <https://github.com/rust-lang/rust/pull/121979>.
.or(Some(10_000_000));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// Default value to emit the warning for "too complex" match. We picked it based on
// this crater run: <https://github.com/rust-lang/rust/pull/121979>.
.or(Some(10_000_000));
// Default value to emit the warning for "too complex" match. We picked it to warn after a second or two.
.or(Some(10_000_000));

I don't think the crater matters much to future readers of this code, but the timing intent does.

Copy link
Member

Choose a reason for hiding this comment

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

Actually you said that limit took more like 20s on your machine? I'd be curious to crater a lower limit then, because I'm afraid people won't wait 20s

Copy link
Member Author

Choose a reason for hiding this comment

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

Fine by me!

@Nadrieril
Copy link
Member

Beautiful :D One last remark

@Nadrieril
Copy link
Member

Dear T-lang, this PR adds a warning that cannot be silenced, triggered when a match takes a really long time to analyze (in the order of seconds). This is to help users figure out what's taking so long and fix it.

We could make the limit configurable or the warning allowable. I argue that's not necessary because crater showed zero regressions with the current limit, and it's be pretty easy in general to split up a match into smaller matches to avoid blowup.

We're still figuring out the exact limit, but does the team approve in principle?

@Nadrieril Nadrieril added the I-lang-nominated Nominated for discussion during a lang team meeting. label Mar 18, 2024
@GuillaumeGomez
Copy link
Member Author

Updated!

@GuillaumeGomez
Copy link
Member Author

Fixed typo.

@Nadrieril Nadrieril added the S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). label Mar 25, 2024
@traviscross
Copy link
Contributor

traviscross commented Apr 8, 2024

Those interested in pushing the limits of exhaustiveness checking may be interested in:

@Dylan-DPC Dylan-DPC removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 3, 2024
@bors
Copy link
Contributor

bors commented Jul 26, 2024

☔ The latest upstream changes (presumably #128034) made this pull request unmergeable. Please resolve the merge conflicts.

@traviscross traviscross added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Sep 25, 2024
@GuillaumeGomez
Copy link
Member Author

Any update here?

@traviscross
Copy link
Contributor

No, we're still working to get to this one on our nominations list.

Looking at this myself, though, one thought. I think I'd prefer it to be possible to allow this, and that such a change would make this an easier call for us when we pick it up in a meeting. Crater doesn't necessarily represent the full ecosystem, and if someone does hit this and is willing to pay the compilation cost, I don't really see a reason to force that person to rewrite the code or live with an unsilenceable warning.

@Nadrieril
Copy link
Member

This is not just a lint, it's more accurately compared to overflow in the trait system (IIUC): it's a failure condition where the compiler says "this is too hard for me" to avoid looping for unbounded time

@traviscross
Copy link
Contributor

traviscross commented Oct 24, 2024

I don't know. It still feels lint-like to me. It's just an arbitrary limit1. When it fires, it's not saying that "this is too hard for me"2 it's saying "this might take longer to compute than an arbitrarily-decided number of cycles".

So it's "linting" against code that would require more than that number of cycles.

Footnotes

  1. I.e., we're not coming up against a "real" resource limit here, like some fixed-size buffer.

  2. We know from prior work that rustc can handle rather complex matches if given enough time.

@Nadrieril
Copy link
Member

it's not saying that "this is too hard for me" it's saying "this might take longer to compute than an arbitrarily-decided number of cycles"

Except that if we reached the limit it's quite likely that we're hitting an exponential case, in which case the computation may need days to complete.

If we can't allow the warning there should be a way to increase the pattern complexity limit to silence the warning, I do agree there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-team Status: Awaiting decision from the relevant subteam (see the T-<team> label). T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants