Skip to content

fix: Use quote! inside ast::make::expr_call() - #22782

Merged
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
Wilfred:fix-generate-delegate-trait-call-panic
Jul 14, 2026
Merged

fix: Use quote! inside ast::make::expr_call()#22782
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
Wilfred:fix-generate-delegate-trait-call-panic

Conversation

@Wilfred

@Wilfred Wilfred commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

It's possible to make rust-analyzer panic because of expr_from_text not roundtripping. Prefer quote!, which is better anyway (less work, more robust).

This crash only seems to occur in rare circumstances, so I haven't written a regression test. For reference, you can replicate it with the following code:

mod serde {
    pub struct Serializer;

    pub trait Serialize {
        fn serialize(&self, s: Serializer);
    }
}

struct Duration;

impl serde::Serialize for Duration {
    fn serialize(&self, s: serde::Serializer) {}
}

struct S {
    map: <Duration
}

When the cursor was on map, the generate_delegate_trait would try to build the call <<Duration as Serialize>::serialize(&self.map, s). Due to the extra leading <, we'd parse it as <<, which isn't valid in an expression (left shift is an infix operator).

Since we were passing invalid Rust through the syntax builder, which only allows valid Rust code, we then panicked.

18:        0x104007fdc - <core[eea836d19939cdd9]::option::Option<syntax[8d95e7771a925714]::ast::generated::nodes::Expr>>::unwrap
                             at /rustc/2d8144b7880597b6e6d3dfd63a9a9efae3f533d3/library/core/src/option.rs:1013:21
19:        0x104007fdc - <syntax[8d95e7771a925714]::Parse<syntax[8d95e7771a925714]::ast::generated::nodes::Expr>>::tree
                             at /Users/wilfred/src/rust-analyzer/crates/syntax/src/lib.rs:120:37
20:        0x104006014 - syntax[8d95e7771a925714]::ast::make::expr_from_text_with_edition::<syntax[8d95e7771a925714]::ast::generated::nodes::CallExpr>
                             at /Users/wilfred/src/rust-analyzer/crates/syntax/src/ast/make.rs:1360:28
21:        0x103ff9d80 - syntax[8d95e7771a925714]::ast::make::expr_from_text::<syntax[8d95e7771a925714]::ast::generated::nodes::CallExpr>
                             at /Users/wilfred/src/rust-analyzer/crates/syntax/src/ast/make.rs:1354:5
22:        0x103f8e298 - syntax[8d95e7771a925714]::ast::make::expr_call
                             at /Users/wilfred/src/rust-analyzer/crates/syntax/src/ast/make.rs:689:5
23:        0x103fed920 - <syntax[8d95e7771a925714]::ast::syntax_factory::SyntaxFactory>::expr_call
                             at /Users/wilfred/src/rust-analyzer/crates/syntax/src/ast/syntax_factory/constructors.rs:1157:19
24:        0x1027feb9c - ide_assists[84949edff2a7fc81]::handlers::generate_delegate_trait::func_assoc_item
                             at /Users/wilfred/src/rust-analyzer/crates/ide-assists/src/handlers/generate_delegate_trait.rs:796:22
25:        0x102800120 - ide_assists[84949edff2a7fc81]::handlers::generate_delegate_trait::process_assoc_item

AI disclosure: Written with 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 13, 2026
@ChayimFriedman2

Copy link
Copy Markdown
Contributor

I'm not a fan of switching makers to quote!() currently; until we autogenerate them, they need to be checked closely to match the AST otherwise trouble can happen. So unless the panic is really bothering you in the real world, I'd prefer not to merge this.

@Wilfred

Wilfred commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

I've seen this crash hit three users recently: so it's not a huge deal, but it does occur in real usage sometimes. Happy to close this PR if you don't think it's worthwhile.

Alternatively, are there any ways I can check the quote! usage to ensure it's correct? Are you concerned about drift going forwards, or the correctness of this PR?

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Alternatively, are there any ways I can check the quote! usage to ensure it's correct?

Only manually via rust.ungram; that's the problem.

Are you concerned about drift going forwards, or the correctness of this PR?

Both, although of course future drift is more problematic.

@ChayimFriedman2

Copy link
Copy Markdown
Contributor

Does it happen without invoking an assist manually? If yes, it's a bigger annoyance and there is more reason to merge this PR.

@Wilfred

Wilfred commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Yep, they're passive: they're happening on textDocument/codeAction, not codeAction/resolve FWIW.

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Okay, I'll merge this then.

View changes since this review

@ChayimFriedman2
ChayimFriedman2 added this pull request to the merge queue Jul 14, 2026
@Wilfred
Wilfred force-pushed the fix-generate-delegate-trait-call-panic branch from 0c67bfc to 3a45c77 Compare July 14, 2026 12:31
@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 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

I just added a link to the ungram code FWIW.

@Wilfred

Wilfred commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Regarding usage of quote!, I did also notice today that we explicitly encourage it in this file at least:

//! `parse(format!())` we use internally is an implementation detail -- long
//! term, it will be replaced with `quote!`. Do not add more usages to `from_text` -
//! use `quote!` instead.

@ChayimFriedman2
ChayimFriedman2 removed this pull request from the merge queue due to a manual request Jul 14, 2026
@ChayimFriedman2

Copy link
Copy Markdown
Contributor

You should not add a warning specifically in this function, rather in the module, and we already have it (in the docs for the quote!() macro).

Regarding usage of quote!, I did also notice today that we explicitly encourage it in this file at least:

Yes, we should probably remove this.

It's possible to make rust-analyzer panic because of `expr_from_text`
not roundtripping. Prefer `quote!`, which is better anyway (less work,
more robust).

This crash only seems to occur in rare circumstances, so I haven't
written a regression test. For reference, you can replicate it with
the following code:

    mod serde {
        pub struct Serializer;

        pub trait Serialize {
            fn serialize(&self, s: Serializer);
        }
    }

    struct Duration;

    impl serde::Serialize for Duration {
        fn serialize(&self, s: serde::Serializer) {}
    }

    struct S {
        map: <Duration
    }

When the cursor was on `map`, the `generate_delegate_trait` would try
to build the call `<<Duration as Serialize>::serialize(&self.map, s)`.
Due to the extra leading `<`, we'd parse it as `<<`, which isn't valid
in an expression (left shift is an infix operator).

Since we were passing invalid Rust through the syntax builder, which
only allows valid Rust code, we then panicked.

    18:        0x104007fdc - <core[eea836d19939cdd9]::option::Option<syntax[8d95e7771a925714]::ast::generated::nodes::Expr>>::unwrap
                                 at /rustc/2d8144b7880597b6e6d3dfd63a9a9efae3f533d3/library/core/src/option.rs:1013:21
    19:        0x104007fdc - <syntax[8d95e7771a925714]::Parse<syntax[8d95e7771a925714]::ast::generated::nodes::Expr>>::tree
                                 at /Users/wilfred/src/rust-analyzer/crates/syntax/src/lib.rs:120:37
    20:        0x104006014 - syntax[8d95e7771a925714]::ast::make::expr_from_text_with_edition::<syntax[8d95e7771a925714]::ast::generated::nodes::CallExpr>
                                 at /Users/wilfred/src/rust-analyzer/crates/syntax/src/ast/make.rs:1360:28
    21:        0x103ff9d80 - syntax[8d95e7771a925714]::ast::make::expr_from_text::<syntax[8d95e7771a925714]::ast::generated::nodes::CallExpr>
                                 at /Users/wilfred/src/rust-analyzer/crates/syntax/src/ast/make.rs:1354:5
    22:        0x103f8e298 - syntax[8d95e7771a925714]::ast::make::expr_call
                                 at /Users/wilfred/src/rust-analyzer/crates/syntax/src/ast/make.rs:689:5
    23:        0x103fed920 - <syntax[8d95e7771a925714]::ast::syntax_factory::SyntaxFactory>::expr_call
                                 at /Users/wilfred/src/rust-analyzer/crates/syntax/src/ast/syntax_factory/constructors.rs:1157:19
    24:        0x1027feb9c - ide_assists[84949edff2a7fc81]::handlers::generate_delegate_trait::func_assoc_item
                                 at /Users/wilfred/src/rust-analyzer/crates/ide-assists/src/handlers/generate_delegate_trait.rs:796:22
    25:        0x102800120 - ide_assists[84949edff2a7fc81]::handlers::generate_delegate_trait::process_assoc_item

AI disclosure: Written with help by GPT-5.5.
@Wilfred
Wilfred force-pushed the fix-generate-delegate-trait-call-panic branch from 3a45c77 to 763669f Compare July 14, 2026 13:08
@Wilfred

Wilfred commented Jul 14, 2026

Copy link
Copy Markdown
Contributor Author

Reverted the comment change.

@ChayimFriedman2
ChayimFriedman2 added this pull request to the merge queue Jul 14, 2026
Merged via the queue into rust-lang:master with commit 229e3bf Jul 14, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 14, 2026
@Wilfred
Wilfred deleted the fix-generate-delegate-trait-call-panic branch July 14, 2026 13:59
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.

3 participants