rust-lang / rust-lang/rust-analyzer

Incorrect constant value resolution on hover if proc-macro emits negative literal

Open
#22,413 0 comments 1 reaction 1 assignee View on GitHub

@ChayimFriedman2 is already working on this.

Since May 21, 2026.

A-macro A-mir C-bug
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

rust-analyzer version: 0.3.2896
rustc version: 1.94.1
editor or extension: VSCode 1.120.0

code snippet to reproduce:
Given a proc-macro like this:

fn generate(input: TokenStream) -> TokenStream
{
    let literal = Literal::i32_unsuffixed(-1); // any negative value will do
    quote!(const #input: i32 = #literal);
}

And used like generate!(VALUE);, when hovering over the name of the constant there's a popup showing the value. For negative values (as above), this popup shows a value of zero. While further const-eval like const OTHER: i32 = VALUE will inherit the problem, expansions are correct (and if the original macro is expanded, correct values will be shown when hovering over OTHER as well). If this constant is then used in a const-eval match arm, it is treated as zero (again, only when hovering over the result in the editor) but there is no unreachable pattern warning as would be the case with an actual zero value.

The problem disappears when changing the macro to

fn generate(input: TokenStream) -> TokenStream
{
    let literal = Literal::i32_unsuffixed(1);
    quote!(const #input: i32 = -#literal);
}

This happens despite the fact that, according to debug printout, the two macros produce the same token stream when invoked by rustc.

Note that I did use proc_macro2 for this but it also happens with only proc_macro (the code is just harder to read without quote!). Further, the exact same issue happens for enums, and again there is no warning for the supposed duplicate value. I don't think this is a regression, as I've tried multiple other versions (including pre-release) which had the same issue.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.