Skip to content

Inlining function defined in macro produces incorrect results (concatenated tokens). #12860

Closed
@zachs18

Description

@zachs18

Rust-analyzer's "Inline <function>" code action fails1 when the function to be inlined is defined in a macro.

macro_rules! define_function {
    () => {
        fn test_macro(x: bool) {
            if x { println!("x is true!"); }
        }
    };
}
define_function!();
fn test_free(x: bool) {
    if x { println!("x is true!"); }
}
fn main() {
    test_macro(true);
    test_free(true);
}

Result when selecting test_macro(true) and running the "Inline `test_macro`" action: (at least if and x should be separated)

// ...
fn main() {
    {
        let x = true; ifx{println!("x is true!");}};
    test_free(true);
}

Result when selecting test_free(true) and running the "Inline `test_free`" action: (to show that the issue only happens for functions defined in macros)

// ...
fn main() {
    test_macro(true);
    if true { println!("x is true!"); };
}

The problem also occurs in associated functions/methods. I originally ran into this error when trying to inline usize::next_multiple_of, which is defined in a macro.


rust-analyzer version: (eg. output of "Rust Analyzer: Show RA Version" command)
rust-analyzer version: 0.4.1138-standalone (977e12a 2022-07-23)
("Pre-Release" version on VSCode)
(Also happens on normal version on VSCode, rust-analyzer version: 0.3.1131-standalone (897a7ec 2022-07-17))

rustc version: (eg. output of rustc -V)
rustc 1.62.1 (e092d0b6b 2022-07-16)

relevant settings: (eg. client settings, or environment variables like CARGO, RUSTUP_HOME or CARGO_HOME)
None that I am aware are relevant.

Footnotes

  1. It succeeds but does not produce correct code if any sequential tokens need to be separated by spaces.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions