Skip to content

fix: offer insert explicit type in macro#22552

Open
A4-Tacks wants to merge 1 commit into
rust-lang:masterfrom
A4-Tacks:insert-explicit-in-macro
Open

fix: offer insert explicit type in macro#22552
A4-Tacks wants to merge 1 commit into
rust-lang:masterfrom
A4-Tacks:insert-explicit-in-macro

Conversation

@A4-Tacks

@A4-Tacks A4-Tacks commented Jun 9, 2026

Copy link
Copy Markdown
Member

Supports attribute macro and macro call

Example

#[tokio::main]
async fn main() {
    let $0x = 3;
}

Before this PR

Assist not applicable

After this PR

#[tokio::main]
async fn main() {
    let x: i32 = 3;
}

Supports attribute macro and macro call

Example
---
```rust
#[tokio::main]
async fn main() {
    let $0x = 3;
}
```

**Before this PR**

Assist not applicable

**After this PR**

```rust
#[tokio::main]
async fn main() {
    let x: i32 = 3;
}
```
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 9, 2026
Comment on lines +27 to +30
let let_range = ctx.sema.original_range_opt(let_stmt.syntax())?.range;
let first = crate::utils::cover_edit_range(ctx.source_file().syntax(), let_range)
.start()
.clone();

@Veykril Veykril Jun 11, 2026

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.

Suggested change
let let_range = ctx.sema.original_range_opt(let_stmt.syntax())?.range;
let first = crate::utils::cover_edit_range(ctx.source_file().syntax(), let_range)
.start()
.clone();
let let_range = ctx.sema.original_range_opt(let_stmt.syntax())?;
if let_range.file_id != ctx.file_id() {
return None;
}
let first = crate::utils::cover_edit_range(ctx.source_file().syntax(), let_range.range)
.start()
.clone();

Or something along those lines (dont recall the apis here), technically upmapping can land us in a different file I think (thanks to include! etc)

View changes since the review

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this is a very niche case, like let x = include!("expr");

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I find it hard to think of a case that would cause this problem. If it's just that, I tend to merge it as it is

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 not sure I understand, that shouldn't be a lot of trouble to guard against? We had issues with mixing wrong text ranges and files in the past so I'd prefer if we'd try to avoid this. Especially if proc-macros end up being able to mix spans from differing files in the future as well (I don't think they can now, but they might in the future) at which point this can very much break again

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Do you think the original_range_in in #22551 is expected?

@A4-Tacks A4-Tacks Jun 12, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this is difficult to solve because macros can mix non adjacent tokens in the same file

In the future, a more consistent solution should be needed to simply run code-action on tokens that are expanded as is in macros

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

same file id concern for the other two original_range calls, we need to check the resulting file id. But otherwise lgtm!

View changes since this review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants