Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename refactoring produce syntactically incorrect code #14137

Closed
bazhenov opened this issue Feb 13, 2023 · 5 comments
Closed

Rename refactoring produce syntactically incorrect code #14137

bazhenov opened this issue Feb 13, 2023 · 5 comments
Labels
A-proc-macro proc macro C-bug Category: bug

Comments

@bazhenov
Copy link

rust-analyzer version: rust-analyzer version: 0.3.1394-standalone (0b32b65 2023-02-05)

rustc version: rustc 1.67.1 (d5a82bbd2 2023-02-07)

relevant settings: N/A

Consider following code:

struct Value {
    inner: u8,
}

#[tokio::main]
async fn main() {
    let value = Value { inner: 1 };
    let Value { inner } = value;
    println!("{}", inner);
}

If I'm trying to rename inner local variable on last line (println!("{}", inner);) I get broken code:

struct Value {
    inner: u8,
}

#[tokio::main]
async fninner: value() {
    let value = Value { inner: 1 };
    let Value { inner } = value;
    println!("{}", inner);
}

This happens wherever I use struct unpack syntax or match-statement. In both cases rust-analyzer reports incorrect TextEdit offsets back to IDE (vscode).

The problem seems to be related to #9403 and #13388 because manifest itself only in presence of #[tokio::main] or #[tokio::test] proc macro.

@bazhenov bazhenov added the C-bug Category: bug label Feb 13, 2023
bazhenov added a commit to bazhenov/crab that referenced this issue Feb 13, 2023
@Veykril Veykril added the A-proc-macro proc macro label Feb 13, 2023
@bazhenov
Copy link
Author

bazhenov commented Feb 18, 2023

If surrounding function is not annotated with [tokio::main] everything work correctly and ide_db::rename::rename_reference() creates two TextEdits – one from source_edit_from_references() another one from source_edit_from_def(). It makes sense. When renaming such a trivial case we need to change two places in sources files.

But in presence of [tokio::main] some basic mechanism is broken. Like for example rust-analyzer can't rename even local variable in following code:

#[tokio::main]
async fn main() {
    let f = 2;
    println!("{}", f);
}

In this example when trying rename variable f, rust-analyzer correctly rename definition (source_edit_from_def()), but unable to find reference to a variables (source_edit_from_references()). This line produce empty usages

let usages = def.usages(sema).all();

Later in FindUsages::search() I see that search_scope is pointing not to a function body (as in normal case) but to the macro mention before function definition (eg. #[tokio::main]). I suspect this has something to do with how tokio::main implemented.

@Veykril
Copy link
Member

Veykril commented Feb 18, 2023

This is a known bug with proc-macros for r-a. r-a currently fails to properly handle macros re-using spans.

@bazhenov
Copy link
Author

I wonder if there is a practical way it can be solved in general case? Proc macro can rewrite a TokenStream in any way. But r-a should somehow map tokens in the source file to tokens after proc-macro expansion. Is this even possible?

@Veykril
Copy link
Member

Veykril commented Feb 18, 2023

This will be fixed once #9403 is implemented.
For now you can set

    "rust-analyzer.procMacro.ignored": {
        "tokio-macros": [
            "main"
        ]
    },

in your settings to get around this mostly

@Veykril
Copy link
Member

Veykril commented Jan 31, 2024

This works now

@Veykril Veykril closed this as completed Jan 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-proc-macro proc macro C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

2 participants