Skip to content

feat: add non-exhaustive-let diagnostic#16303

Merged
bors merged 2 commits into
rust-lang:masterfrom
rosefromthedead:non-exhaustive-let
Feb 19, 2024
Merged

feat: add non-exhaustive-let diagnostic#16303
bors merged 2 commits into
rust-lang:masterfrom
rosefromthedead:non-exhaustive-let

Conversation

@rosefromthedead

Copy link
Copy Markdown
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

@Veykril Veykril left a comment

Copy link
Copy Markdown
Member

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.

Comment thread crates/ide-diagnostics/src/handlers/non_exhaustive_let.rs
Comment thread crates/hir-ty/src/diagnostics/expr.rs Outdated
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
Copy Markdown
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)

@rosefromthedead rosefromthedead Jan 14, 2024

Copy link
Copy Markdown
Contributor Author

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)

@Veykril Veykril Jan 16, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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

@Nadrieril Nadrieril Jan 25, 2024

Copy link
Copy Markdown
Member

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
Copy Markdown
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

bors commented Jan 24, 2024

Copy link
Copy Markdown
Contributor

☔ 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
Copy Markdown
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
Copy Markdown
Member

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

@Nadrieril

Nadrieril commented Feb 11, 2024

Copy link
Copy Markdown
Member

(and by soon I mean #16533)

(sorry for breaking your PR again)

@bors

bors commented Feb 12, 2024

Copy link
Copy Markdown
Contributor

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

@Veykril

Veykril commented Feb 19, 2024

Copy link
Copy Markdown
Member

Rebased it for you, thanks!
@bors r+

@bors

bors commented Feb 19, 2024

Copy link
Copy Markdown
Contributor

📌 Commit 5390e4c has been approved by Veykril

It is now in the queue for this repository.

@bors

bors commented Feb 19, 2024

Copy link
Copy Markdown
Contributor

⌛ Testing commit 5390e4c with merge ff8fe52...

@bors

bors commented Feb 19, 2024

Copy link
Copy Markdown
Contributor

☀️ 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