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

feat: add non-exhaustive-let diagnostic #16303

Merged
merged 2 commits into from
Feb 19, 2024

Conversation

rosefromthedead
Copy link
Contributor

I want this to have a quickfix to add an else branch but I couldn't figure out how to do that, so here's the diagnostic on its own. It pretends a let is a match with one arm, and asks the match checking whether that match would be exhaustive.

Previously the pattern was checked based on its own type, but that was causing a panic in match_check (while processing e.g. crates/hir/src/lib.rs) so I changed it to use the initialiser's type instead, to align with the checking of actual match expressions. I think the panic can still happen, but I hear that match_check is going to be updated to a new version from rustc, so I'm posting this now in the hopes that the panic will magically go away when that happens.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 7, 2024
@rosefromthedead rosefromthedead marked this pull request as draft January 7, 2024 13:44
Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

Nice and simple diagnostic, thanks! Just some small things to fix. Regarding quickfix we should just wait until we switch to rustc's exhaustiveness checker which should help in that regard.

crates/hir-ty/src/diagnostics/expr.rs Outdated Show resolved Hide resolved
fn validate_block(&mut self, db: &dyn HirDatabase, expr: &Expr) {
let Expr::Block { statements, .. } = expr else { return };
let body = db.body(self.owner);
let pattern_arena = Arena::new();
Copy link
Member

Choose a reason for hiding this comment

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

Likewise, lets make this a field so we can re-use the arena from previous iterations (together with the one for general match checking)

Copy link
Contributor Author

@rosefromthedead rosefromthedead Jan 14, 2024

Choose a reason for hiding this comment

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

Is it beneficial to reuse an arena when there are no references to the old items? I thought it might be similar logic to reusing a Vec after .clear()ing it, but there is no equivalent method on arenas.

(I'm also having lifetime issues trying to move the arena inside ExprValidator :P)

Copy link
Member

@Veykril Veykril Jan 16, 2024

Choose a reason for hiding this comment

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

Clearing isn't too beneficial for an arena, as that will just drop the allocated chunks which includes capacity, unlike a vec where capacity remains. The lifetime issues I can see, given how the arena is used. So without some lifetime transmuting, not much we can do about so let's keep it this way.

Copy link
Member

Choose a reason for hiding this comment

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

I think it drops all but the last chunk, which means there is still some good amortizing of capacity compared to making a new one. Also lifetimes should be cleaner with the new code, it might work now.

Copy link
Member

Choose a reason for hiding this comment

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

Actually it would make sense to intern the lowered patterns if many parts of r-a start to use them

Copy link
Member

@Nadrieril Nadrieril Jan 25, 2024

Choose a reason for hiding this comment

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

I'm actually trying to remove the arena entirely, if this turns out ok perf-wise (allocating slices on an arena is actually not great perf-wise because TypedArena allocates into a SmallVec first anyway)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, don't worry about reusing the arena, it's going away soon

@Veykril Veykril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 10, 2024
@bors
Copy link
Contributor

bors commented Jan 24, 2024

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

@rosefromthedead rosefromthedead marked this pull request as ready for review February 11, 2024 15:55
@rosefromthedead
Copy link
Contributor Author

Re: the panic I had when I posted this, I never had a solid reproducer, so I can only hope that the pattern analysis update solved that. I'd be happy to mark this diagnostic as experimental if we want to avoid shipping that panic.

@rosefromthedead rosefromthedead force-pushed the non-exhaustive-let branch 2 times, most recently from a7f16e6 to f6cf381 Compare February 11, 2024 16:17
@Nadrieril
Copy link
Member

Yeah don't worry about panics in match analysis, they should all be replaced with an Err path soon

@Nadrieril
Copy link
Member

Nadrieril commented Feb 11, 2024

(and by soon I mean #16533)

(sorry for breaking your PR again)

@bors
Copy link
Contributor

bors commented Feb 12, 2024

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

@Veykril
Copy link
Member

Veykril commented Feb 19, 2024

Rebased it for you, thanks!
@bors r+

@bors
Copy link
Contributor

bors commented Feb 19, 2024

📌 Commit 5390e4c has been approved by Veykril

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Feb 19, 2024

⌛ Testing commit 5390e4c with merge ff8fe52...

@bors
Copy link
Contributor

bors commented Feb 19, 2024

☀️ Test successful - checks-actions
Approved by: Veykril
Pushing ff8fe52 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants