Skip to content

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

@ian-h-chamberlain

Description

@ian-h-chamberlain
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

Activity

ian-h-chamberlain

ian-h-chamberlain commented on Sep 14, 2023

@ian-h-chamberlain
ContributorAuthor

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

Veykril commented on Sep 14, 2023

@Veykril
Member

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.

SE2Dev

SE2Dev commented on Dec 7, 2023

@SE2Dev

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

Veykril commented on Dec 7, 2023

@Veykril
Member

Yes, 9b7ec5e specifically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-highlighting(semantic) token highlightingA-macromacro expansionC-bugCategory: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jonas-schievink@Veykril@SE2Dev@ian-h-chamberlain

        Issue actions

          Highlighting of format_args! does not work with tracing macros · Issue #12405 · rust-lang/rust-analyzer