Skip to content

Comments

fix: generate method assist uses enclosing impl block instead of first found#21684

Merged
A4-Tacks merged 3 commits intorust-lang:masterfrom
akashchakrabortymsc-cmd:fix/generate-method-wrong-impl-block
Feb 21, 2026
Merged

fix: generate method assist uses enclosing impl block instead of first found#21684
A4-Tacks merged 3 commits intorust-lang:masterfrom
akashchakrabortymsc-cmd:fix/generate-method-wrong-impl-block

Conversation

@akashchakrabortymsc-cmd
Copy link
Contributor

@akashchakrabortymsc-cmd akashchakrabortymsc-cmd commented Feb 20, 2026

Fixes #21550

Problem

When using the "Generate method" assist from inside an impl block,
the generated method was always appended to the first impl block
found for that type, instead of the impl block where the cursor is.

For example, given:

struct Foo;

impl Foo {
    fn new() -> Self { Foo }
}

impl Foo {
    fn method1(&self) {
        self.method2(42) // cursor here
    }
}

The generated method2 was incorrectly inserted into the first
impl Foo block instead of the one containing the cursor.

Fix

In gen_method, before falling back to get_adt_source, we now
check if the cursor is already inside an impl block for the target
type. If so, we use that impl block as the insertion target.

Test

Added a test case generate_method_uses_current_impl_block that
reproduces the issue and verifies the fix.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 20, 2026
@A4-Tacks A4-Tacks added this pull request to the merge queue Feb 21, 2026
Merged via the queue into rust-lang:master with commit af68fc6 Feb 21, 2026
16 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Feb 21, 2026
@akashchakrabortymsc-cmd
Copy link
Contributor Author

Thank you for merging .Sorry about that in convenience .

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.

Assist "Generate method" is appended to first found impl block

3 participants