Skip to content

Fix crashes in assists due to .unwrap() calls in SyntaxFactory - #22759

Merged
A4-Tacks merged 1 commit into
rust-lang:masterfrom
Wilfred:fix/syntax-factory-mapping-unwraps
Jul 18, 2026
Merged

Fix crashes in assists due to .unwrap() calls in SyntaxFactory#22759
A4-Tacks merged 1 commit into
rust-lang:masterfrom
Wilfred:fix/syntax-factory-mapping-unwraps

Conversation

@Wilfred

@Wilfred Wilfred commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

These functions would panic when they try to re-parse code that isn't
valid in a top level function.

For parameters, this is .., which is valid in a closure but not a
function (requires an annotation). For tail block expressions, this is
let var = expr, which is valid in a tail position but not a function
body (requires a semicolon).

I've checked both quote! invocations against the rust.ungram file and
I think the structures are correct.

Include a test of an assist that previously caused a panic.

AI disclosure: Written with a little help by GPT-5.5.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 10, 2026

@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.

I think this is the wrong way to guard agains this. The code rightfully assumes that the make module constructs well formed code if given well formed code. So I guess the issue we are running into now is the way the make module works, by stringifying and then re-parsing, which makes this entire assumption fairly brittle as different parser recovery states can kick in.

I wonder how far away we are from finally changing this now that we have removed the mutable APIs

View changes since this review

@A4-Tacks A4-Tacks 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.

IMO, this should be fixed in the assists

View changes since this review

Comment thread crates/ide-assists/src/handlers/unwrap_branch.rs Outdated
Comment thread crates/ide-assists/src/handlers/convert_closure_to_fn.rs Outdated
Comment thread crates/ide-assists/src/handlers/convert_closure_to_fn.rs Outdated
@ChayimFriedman2

Copy link
Copy Markdown
Contributor

I agree with @Veykril that we should not make make constructors fallible. FWIW a possible future goal for the GSoC project of @Shourya742 (if we'll have time) was to implement auto-generation of make constructors without textual parsing, which will solve this.

@Wilfred
Wilfred force-pushed the fix/syntax-factory-mapping-unwraps branch from d6bec93 to 9822056 Compare July 13, 2026 11:19
@rustbot

This comment has been minimized.

@Wilfred
Wilfred force-pushed the fix/syntax-factory-mapping-unwraps branch from 9822056 to 4d014d3 Compare July 13, 2026 12:17
@bit-aloo

Copy link
Copy Markdown
Member

I agree with @Veykril that we should not make make constructors fallible. FWIW a possible future goal for the GSoC project of @Shourya742 (if we'll have time) was to implement auto-generation of make constructors without textual parsing, which will solve this.

We will, the only pre-requisite (AFAICT), for this gonna be moving trivia inside the tokens, and making our grammar generator smarter. Currently, working on removing mutable rowan API's.

Comment thread crates/ide-assists/src/handlers/convert_closure_to_fn.rs Outdated
Comment thread crates/ide-assists/src/handlers/unwrap_branch.rs Outdated
@Wilfred
Wilfred force-pushed the fix/syntax-factory-mapping-unwraps branch 2 times, most recently from 0f13176 to 918f5a6 Compare July 13, 2026 13:18
@Wilfred

Wilfred commented Jul 13, 2026

Copy link
Copy Markdown
Contributor Author

OK, I've done another pass on this: using quote! fixes the crash cases I've seen, round-tripping properly. I've also tidied up the assist fixes, just focusing on the closure case.

I think this small and sensible now, but let me know if you'd like other changes.

Comment thread crates/ide-assists/src/handlers/convert_closure_to_fn.rs Outdated
@Veykril

Veykril commented Jul 13, 2026

Copy link
Copy Markdown
Member

yea that looks better to me :)

@A4-Tacks

Copy link
Copy Markdown
Member

There seems to be a similar PR: #21578

@Wilfred
Wilfred force-pushed the fix/syntax-factory-mapping-unwraps branch from 918f5a6 to c17becc Compare July 14, 2026 13:40
@rustbot

rustbot commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Wilfred
Wilfred force-pushed the fix/syntax-factory-mapping-unwraps branch from c17becc to 6faf4c7 Compare July 14, 2026 13:42
@Wilfred

Wilfred commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

OK, done another pass on this. I've got one regression test, and just changed the logic to use quote! in two places. I've also rewritten with the commit message with my current understanding.

As with #22782, I am seeing this cause crashes for a few users (8 people recently) even without invoking the assist, so I think it's worth porting to quote! even before we have the proper generated code for quote! usage.

Let me know what you think.

@A4-Tacks

Copy link
Copy Markdown
Member

I've got one regression test, and just changed the logic to use quote! in two places.

What about your second regression test? I mentioned it in #22759 (comment)

These functions would panic when they try to re-parse code that isn't
valid in a top level function.

For parameters, this is `..`, which is valid in a closure but not a
function (requires an annotation). For tail block expressions, this is
`let var = expr`, which is valid in a tail position but not a function
body (requires a semicolon).

I've checked both quote! invocations against the rust.ungram file and
I think the structures are correct.

Include a test of an assist that previously caused a panic.

AI disclosure: Written with a little help by GPT-5.5.
@Wilfred
Wilfred force-pushed the fix/syntax-factory-mapping-unwraps branch from 6faf4c7 to 0da74dd Compare July 17, 2026 10:11
@Wilfred

Wilfred commented Jul 17, 2026

Copy link
Copy Markdown
Contributor Author

Added the second regression test, sorry for the slow response.

@A4-Tacks
A4-Tacks added this pull request to the merge queue Jul 18, 2026
Merged via the queue into rust-lang:master with commit cac0779 Jul 18, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 18, 2026
@Wilfred
Wilfred deleted the fix/syntax-factory-mapping-unwraps branch August 7, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants