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

Highlighting of format_args! does not work with tracing macros #12405

Closed
ian-h-chamberlain opened this issue May 28, 2022 · 4 comments
Closed
Labels
A-highlighting (semantic) token highlighting A-macro macro expansion C-bug Category: bug

Comments

@ian-h-chamberlain
Copy link
Contributor

Syntax highlighting in macros like println! and format_args! is awesome! Unfortunately, it doesn't seem to work with tracing macros as I would expect it to, since the expansion appears to use format_args! similarly to other cases. Example:

Screen Shot 2022-05-28 at 9 00 54 AM

So far, I have not been able to reproduce this using my own macro definition, and the upstream tracing::event has a fairly big + complicated definition, but one match arm does use format_args! and the expansion contains this:

Some(
    &(unsafe {
        std::fmt::Arguments::new_v1(
            &[],
            &[std::fmt::ArgumentV1::new(
                &("not highlighted"),
                std::fmt::Display::fmt,
            )],
        )
    }) as &Value,
),

Please let me know if there's anything I can do to help track down why this expansion doesn't match the criteria for highlighting. Thanks!


rust-analyzer version: rust-analyzer version: 84be2ea 2022-05-23 stable

rustc version: rustc 1.61.0 (fe5b13d68 2022-05-18)

relevant settings: None that I know of

@Veykril Veykril added A-highlighting (semantic) token highlighting C-bug Category: bug labels May 28, 2022
@jonas-schievink jonas-schievink added the A-macro macro expansion label May 29, 2022
@ian-h-chamberlain
Copy link
Contributor Author

ian-h-chamberlain commented Sep 14, 2023

I know this issue hasn't seen much movement since I first filed it, but I had some renewed interest after seeing #15559 in the recent weekly changelog. Good news: I was able to find a minimum viable reproduction! Using 0.3.1657-standalone (326f37e 2023-09-10) this time.

macro_rules! nothing {
    ($tt:tt) => {};
}

macro_rules! expand_multiple {
    ($fmt:literal) => {{
        format_args!($fmt);
        nothing!($fmt);
    }};
}

fn main() {
    let some_val = "okay";

    expand_multiple!("reproduces: {some_val}");
}

When format_args! is last in the expansion, highlighting works as expected. When nothing! is last, highlighting is disabled. It seems that the highlight logic is based on whichever expansion is encountered most recently while traversing.

I'm not sure if this is really supported, since of course macros can do whatever they want with the literal, but I'd suggest that usually, if any part of the expansion is format_args!($lit) it is probably safe to assume the macro is intended to be used this way. Maybe the logic could be updated to turn on highlighting in this scenario? It seems this might affect other highlight use cases though so might be a bigger change than just for format_args!.

@Veykril
Copy link
Member

Veykril commented Sep 14, 2023

Yes, syntax highlighting currently only looks at the first (as in iteration order) mapping it encounters for performance reasons. That might change in the future as we are rewriting some of the mapping stuff soon.

@Veykril Veykril closed this as completed Dec 6, 2023
@SE2Dev
Copy link

SE2Dev commented Dec 7, 2023

Just out of curiosity, which PR / commit fixed this issue? Was it #16027?

I did confirm that the current pre-release version of rust-lang.rust-analyzer (v0.4.1760) does seem to fix this issue.

Edit: The stable version of rust-lang.rust-analyzer (v0.3.1766) now includes the fix!

@Veykril
Copy link
Member

Veykril commented Dec 7, 2023

Yes, 9b7ec5e specifically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-highlighting (semantic) token highlighting A-macro macro expansion C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

4 participants