Skip to content

feat: Support extract variable in macro call#21487

Merged
ChayimFriedman2 merged 2 commits intorust-lang:masterfrom
A4-Tacks:extract-var-in-macro
Apr 14, 2026
Merged

feat: Support extract variable in macro call#21487
ChayimFriedman2 merged 2 commits intorust-lang:masterfrom
A4-Tacks:extract-var-in-macro

Conversation

@A4-Tacks
Copy link
Copy Markdown
Member

Implement based first and last token mappings

Example

macro_rules! foo {
    (= $($t:tt)*) => { $($t)* };
}

fn main() {
    let x = foo!(= $02 + 3$0 + 4);
}

Before this PR

Assist not applicable

After this PR

macro_rules! foo {
    (= $($t:tt)*) => { $($t)* };
}

fn main() {
    let $0var_name = 2+3;
    let x = foo!(= var_name + 4);
}

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 18, 2026
@A4-Tacks A4-Tacks changed the title Support extract variable in macro call feat: Support extract variable in macro call Feb 19, 2026
@rustbot

This comment has been minimized.

@A4-Tacks A4-Tacks force-pushed the extract-var-in-macro branch from f5af86e to 4149dd8 Compare March 23, 2026 13:59
@rustbot

This comment has been minimized.

@A4-Tacks A4-Tacks force-pushed the extract-var-in-macro branch from 4149dd8 to d8eb024 Compare March 23, 2026 14:04
Copy link
Copy Markdown
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

Nice, this is useful!

Can you please add a test where the block we extract into is inside the macro call?

View changes since this review

Implement based first and last token mappings

Example
---
```rust
macro_rules! foo {
    (= $($t:tt)*) => { $($t)* };
}

fn main() {
    let x = foo!(= $02 + 3$0 + 4);
}
```

**Before this PR**

Assist not applicable

**After this PR**

```rust
macro_rules! foo {
    (= $($t:tt)*) => { $($t)* };
}

fn main() {
    let $0var_name = 2+3;
    let x = foo!(= var_name + 4);
}
```
@A4-Tacks A4-Tacks force-pushed the extract-var-in-macro branch from d8eb024 to 144cce8 Compare April 14, 2026 10:35
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Apr 14, 2026

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.

@A4-Tacks
Copy link
Copy Markdown
Member Author

At present, it only supports extraction into outside of macros, and there may be improvements in the future

@ChayimFriedman2
Copy link
Copy Markdown
Contributor

Then test that it's not applicable in a macro?

@A4-Tacks
Copy link
Copy Markdown
Member Author

At least for small macro calls, extracting to outside the macro is very practical

Supporting extraction to inside the macros may disrupt mappings
If this is check and not applicable, it is easy to false negative

@ChayimFriedman2
Copy link
Copy Markdown
Contributor

I'm just saying that whatever the current behavior is, it's good to test it, so we won't have surprises now or in the future.

@A4-Tacks
Copy link
Copy Markdown
Member Author

it's good to test it

Oh, I have already pushed the test

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Apr 14, 2026
Merged via the queue into rust-lang:master with commit fed32aa Apr 14, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Apr 14, 2026
@A4-Tacks A4-Tacks deleted the extract-var-in-macro branch April 14, 2026 13:40
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