Skip to content

Convert to named struct uses wrong locations for patterns in nested macros #15630

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

Closed
narpfel opened this issue Sep 18, 2023 · 0 comments · Fixed by #15887
Closed

Convert to named struct uses wrong locations for patterns in nested macros #15630

narpfel opened this issue Sep 18, 2023 · 0 comments · Fixed by #15887
Labels
A-assists C-bug Category: bug

Comments

@narpfel
Copy link

narpfel commented Sep 18, 2023

I have this (somewhat minimised) example:

fn f() {
    println!("42");
    println!("Hello world");
    println!("Hello world");
}

pub enum Error {
    Eof(EofInner),
}

pub struct EofInner;

macro_rules! check {
    ($body:expr) => {
        |result: Result<(), Error>| -> () {
            let () = $body(result);
        }
    };
}

macro_rules! check_err {
    ($pattern:pat) => {
        check!(|result| match result {
            Ok(_) => (),
            Err($pattern) => (),
        })
    };
}

fn main() {
    f();
    check_err!(Error::Eof(_))(Ok(()));
}

When using “Convert to named struct” on the Eof variant of Error, this is the result:

fn f() {
    println!("42");
    println!("Hello world");
    println!("Error::Eof { field1: _ };
}

pub enum Error {
    Eof { field1: EofInner },
}

pub struct EofInner;

macro_rules! check {
    ($body:expr) => {
        |result: Result<(), Error>| -> () {
            let () = $body(result);
        }
    };
}

macro_rules! check_err {
    ($pattern:pat) => {
        check!(|result| match result {
            Ok(_) => (),
            Err($pattern) => (),
        })
    };
}

fn main() {
    f();
    check_err!(Error::Eof(_))(Ok(()));
}

Changing the code before or after the Error enum changes the location of the wrong replacement for the Error::Eof(_) pattern.

rust-analyzer version:

$ rust-analyzer --version
rust-analyzer 1 (05666441ba 2023-09-17)

(installed from the Arch Linux repos)

rustc version:

$ rustc -vV
rustc 1.74.0-nightly (8142a319e 2023-09-13)
binary: rustc
commit-hash: 8142a319ed5c1d1f96e5a1881a6546e463b77c8f
commit-date: 2023-09-13
host: x86_64-unknown-linux-gnu
release: 1.74.0-nightly
LLVM version: 17.0.0

(installed via rustup)

relevant settings: Editor used is nvim version v0.10.0-dev-1145+g46402c16c0 (current git head) using current git head of nvim-lspconfig. The command used to trigger the refactor is :lua vim.lsp.buf.code_action() and then selecting “Convert to named struct” while in normal mode with the cursor above the Eof token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assists C-bug Category: bug
Projects
None yet
2 participants