Skip to content

rustc lexer/macro_rules diagnostics feature request: Color highlight acceptable characters #156458

Closed

Description

@peter-lyons-kehl

This applies probably to macro_rules/macro v.2 only (correct this otherwise). When the lexer/tokenizer reports errors like

note: allowed there are: `{`, `[`, `=>`, `,`, `>`, `=`, `:`, `;`, `|`, `as` or `where`

then highlight each allowed characters/tokens (in terminals that use colors). This should not be a rocket science, since the error reporting already makes "error" red and "warning" yellow (though that is surely higher up the control chain).

macro_rules! m {
    ($e:expr ~ $id:ident) => {};
}

generates error:

    Checking macro_ru v0.1.0 (/home/pkehl/macro_ru)
error: `$e:expr` is followed by `~`, which is not allowed for `expr` fragments
 --> src/lib.rs:2:14
  |
2 |     ($e:expr ~ $id:ident) => {};
  |              ^ not allowed after `expr` fragments
  |
  = note: allowed there are: `=>`, `,` or `;`

and

macro_rules! m {
    ($t:ty ~ $id:ident) => {};
}

generates even less readable:

...
2 |     ($t:ty ~ $id:ident) => {};
  |            ^ not allowed after `ty` fragments
  |
  = note: allowed there are: `{`, `[`, `=>`, `,`, `>`, `=`, `:`, `;`, `|`, `as` or `where`

As per https://internals.rust-lang.org/t/rustc-lexer-diagnostics-feature-request-color-highlight-acceptable-characters/24227.
@rustbot label +A-diagnostics +D-diagnostic-infra

Activity

  1. added
    needs-triage
    A-diagnostics
    D-diagnostic-infra
    T-compiler
    on May 11, 2026
  2. peter-lyons-kehl commented on Jun 16, 2026

    @peter-lyons-kehl
    ContributorAuthor

    tl;dr Any basic color choice may be fine, maybe green as if a "green light."

    Accessibility/color blindness assumption: (Based on a quick online/AI query, simplified, no knowledge of the subject; please correct this if needed): It seems that there is no single "basic/mainstream" color that, when used as "pure/intense" for text foreground, would be invisible/difficult for the same person on both white background and black background (with no other colors nearby).

    Also

    • major terminals/window managers/IDE's offer choice of background colors/themes, so there should be a way to workaround, as far as we don't use various non-black and non-white colors next to each other/contrasting with each other.
    • in Mac OS/Linux/Unix terminals/shells (and maybe in Windows Powershell, too), one can pipe the outputs (including error output) to a file, or to another command (like cat), which removes any color/formatting control escapes.
    • cargo build (and friends) take --color never parameter, like cargo build --color never; or, you can set the environment variable CARGO_TERM_COLOR=never. That does not work completely; for now you need to use cargo test --color=never -- --color=never (and friends), give thumbs up to rust-lang/cargo#1983.

    The above generalization is not to discard/minimize needs of people with visual difficulties, but to be realistic about what we can provide, and what workarounds exist already.

    See also

  3. teor2345 commented on Jun 17, 2026

    @teor2345
    Member
    • in Mac OS/Linux/Unix terminals/shells (and maybe in Windows Powershell, too), one can pipe the outputs (including error output) to a file, or to another command (like cat), which removes any color/formatting control escapes.

    Not sure if you meant it how it sounds, but the colours are controlled by the original tool that generates the text, not removed by the terminal, file, or command the output is piped to.

    Tools can auto-detect whether they are writing to a terminal and disable coloured output. This requires the cooperation of the colour/terminal library, terminal app, and/or the OS. That's what happens when redirecting output, the command or file is detected as "not a terminal", so the tool doesn't output colour escapes.

    Sometimes the detection doesn't work, and users will see raw output with colour codes in it (in tools that escape control codes). This can be rendered as colours using less --raw-control-chars or similar.

  4. estebank commented on Oct 6, 2026

    @estebank
    Contributor

    We already have a highlight mechanism (which is used most prominently by "expected/found" type errors, but also a few other places). The highlight color is currently magenta (as it was the only somewhat readable color that wasn't already used for other semantic meaning, it used to be bold white, but people thought it wasn't highlighted enough). Doing this should be relatively straightforward by modifying

    let msg = "allowed there are: ";
    match possible {
    &[] => {}
    &[t] => {
    err.note(format!(
    "only {t} is allowed after `{kind}` fragments",
    ));
    }
    ts => {
    err.note(format!(
    "{}{} or {}",
    msg,
    ts[..ts.len() - 1].to_vec().join(", "),
    ts[ts.len() - 1],
    ));
    }
    }

    to use

    pub fn highlighted_note(&mut self, msg: Vec<StringPart>) -> &mut Self {

  5. added
    E-easy
    E-mentor
    and removed
    needs-triage
    on Oct 6, 2026
  6. mtan37 commented on Oct 6, 2026

    @mtan37
    Contributor

    Hi @estebank , I'm new to the project and saw you left some useful pointers in your previous comment. I would love to work on this as my first issue!

  7. estebank commented on Oct 7, 2026

    @estebank
    Contributor

    Feel free to do so! Make sure to read https://rustc-dev-guide.rust-lang.org/index.html

    The change should be straightforward to make.

  8. mtan37 commented on Oct 7, 2026

    @mtan37
    Contributor

    Feel free to do so! Make sure to read https://rustc-dev-guide.rust-lang.org/index.html

    The change should be straightforward to make.

    Thank you for the link! I will read through it

    @rustbot claim

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

Metadata

Metadata

Assignees

Labels

A-diagnosticsD-diagnostic-infraE-easyE-mentorT-compiler

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions