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

Broken snippets being applied in assert!, debug_assert! and format! #19069

Open
airblast-dev opened this issue Jan 29, 2025 · 0 comments · May be fixed by #19129
Open

Broken snippets being applied in assert!, debug_assert! and format! #19069

airblast-dev opened this issue Jan 29, 2025 · 0 comments · May be fixed by #19129
Assignees
Labels
A-macro macro expansion C-bug Category: bug

Comments

@airblast-dev
Copy link

airblast-dev commented Jan 29, 2025

rust-analyzer version: rust-analyzer 1 (248bd51 2025-01-18)

rustc version: rustc 1.83.0 (90b35a623 2024-11-26)

editor or extension: Zed, Neovim (via rustaceanvim)

relevant settings: No environment variables, default settings for both editors and rustaceanvim.

repository link (if public, optional): No need, see the snippet.

When applying a snippet in assert!, debug_assert! or format! any whitespace (excluding ones inside string literals) get removed, and possibly broken code is inserted.
The problematic snippets I tested are box and unsafe but there might be more.
The exact behavior depends on the snippet, but using another snippet breaks things further and instead of just removing whitespaces. See a few examples in the reproducer.

code snippet to reproduce:

This is the code before applying the snippet.

fn main() {
    let a = true;
    assert!(if a == false { true } else { false });
}

After applying the snippet the whitespace's are removed from whatever was inside of it.
The example is with the unsafe snippet but it works with any of the snippets mentioned above.

fn main() {
    let a = true;
    assert!(unsafe { ifa==false{true}else{false} })
}

Not only does this break any code that needs whitespace's, it tampers with undo's in Zed as well (not in Neovim so Im unsure if this part is an editor bug or configuration bug of some kind).
Issuing an undo after applying the snippet yields us with:

fn main() {
    let a = true;
    assert!(if a == false { true } else { false }.unsafe { ifa==false{true}else{false} })
}

There are many snippet combinations that could be broken so just leaving a few examples instead of all possible combinations.

Applying a box snippet followed by an unsafe snippet.
Reversiving the order of how the snippet is applied yields us the correct result except of the missing whitespace's.

fn main() {
    let a = true;
    assert!(Box::new(ifa==false{true}else{false}).unsafe {
        
    })
}

Applying a box snippet twice.

fn main() {
    let a = true;
    assert!(Box::new(ifa==false{true}else{false}).Box)
}
@airblast-dev airblast-dev added the C-bug Category: bug label Jan 29, 2025
@airblast-dev airblast-dev changed the title Broken snippets being applied in assert! and debug_assert! Broken snippets being applied in assert!, debug_assert! and format! Jan 29, 2025
@ChayimFriedman2 ChayimFriedman2 added the A-macro macro expansion label Jan 29, 2025
@ChayimFriedman2 ChayimFriedman2 self-assigned this Feb 10, 2025
@ChayimFriedman2 ChayimFriedman2 linked a pull request Feb 10, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-macro macro expansion C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants