rust-lang / rust-lang/rust

`const`s that depend on other constants which fail to evaluate create difficult to read error messages due to `notes:` output

Open
#135,259 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code
macro_rules! blarg {
    () => {
        const A: u32 = const {
            panic!()
        };
        
        const B: u32 = A + 1;
        const C: u32 = B + 1;
        const D: u32 = C + 1;
    }
}

blarg!();
Current output
Compiling playground v0.0.1 (/playground)
error[E0080]: evaluation of `A::{constant#0}` failed
  --> src/lib.rs:13:1
   |
13 | blarg!();
   | ^^^^^^^^ the evaluated program panicked at 'explicit panic', src/lib.rs:13:1
   |
   = note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `blarg` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
  --> src/lib.rs:3:24
   |
3  |           const A: u32 = const {
   |  ________________________^
4  | |             panic!()
5  | |         };
   | |_________^
...
13 |   blarg!();
   |   -------- in this macro invocation
   |
   = note: this note originates in the macro `blarg` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
  --> src/lib.rs:7:24
   |
7  |         const B: u32 = A + 1;
   |                        ^
...
13 | blarg!();
   | -------- in this macro invocation
   |
   = note: this note originates in the macro `blarg` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
  --> src/lib.rs:8:24
   |
8  |         const C: u32 = B + 1;
   |                        ^
...
13 | blarg!();
   | -------- in this macro invocation
   |
   = note: this note originates in the macro `blarg` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
  --> src/lib.rs:9:24
   |
9  |         const D: u32 = C + 1;
   |                        ^
...
13 | blarg!();
   | -------- in this macro invocation
   |
   = note: this note originates in the macro `blarg` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error
Desired output
Compiling playground v0.0.1 (/playground)
error[E0080]: evaluation of `A::{constant#0}` failed
  --> src/lib.rs:13:1
   |
13 | blarg!();
   | ^^^^^^^^ the evaluated program panicked at 'explicit panic', src/lib.rs:13:1
   |
   = note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `blarg` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0080`.
error: could not compile `playground` (lib) due to 1 previous error
Rationale and extra context

playground link to repro: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f8bd0c35877ea97f896174c4543618cc

seems like a regression of this previous issue & fix: https://github.com/rust-lang/rust/issues/110891

while working on a macro for rasn, I tested the panic conditions for failure to parse the input literal, which I noticed produces a wall of note:s that are utterly useless (especially given the fact that the macro is not expanded in the doctest so the error spans are just the same macro invocation over and over + a couple others, which makes finding the error very difficult:

error[E0080]: evaluation of `main::_doctest_main_src_macros_rs_193_0::SYS_DESCR::OID::{constant#0}::COMPONENTS::{constant#0}` failed
 --> src/macros.rs:195:46
  |
5 | const SYS_DESCR: &'static rasn::types::Oid = rasn::oid!(".3.3.6.1.2.1.2.2.1.3");
  |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'the first OID arc must be <= 2', src/macros.rs:5:46
  |
  = note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `rasn::oid` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
 --> src/macros.rs:195:46
  |
5 | const SYS_DESCR: &'static rasn::types::Oid = rasn::oid!(".3.3.6.1.2.1.2.2.1.3");
  |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this note originates in the macro `rasn::oid` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
 --> src/macros.rs:195:46
  |
5 | const SYS_DESCR: &'static rasn::types::Oid = rasn::oid!(".3.3.6.1.2.1.2.2.1.3");
  |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this note originates in the macro `rasn::oid` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
 --> src/macros.rs:195:46
  |
5 | const SYS_DESCR: &'static rasn::types::Oid = rasn::oid!(".3.3.6.1.2.1.2.2.1.3");
  |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this note originates in the macro `rasn::oid` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
 --> src/macros.rs:195:46
  |
5 | const SYS_DESCR: &'static rasn::types::Oid = rasn::oid!(".3.3.6.1.2.1.2.2.1.3");
  |                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this note originates in the macro `rasn::oid` (in Nightly builds, run with -Z macro-backtrace for more info)

note: erroneous constant encountered
 --> src/macros.rs:196:12
  |
6 | assert_eq!(SYS_DESCR, rasn::types::Oid::new(&[1, 3, 6, 1, 2, 1, 2, 2, 1, 3]).unwrap());
  |            ^^^^^^^^^

note: erroneous constant encountered
 --> src/macros.rs:196:1
  |
6 | assert_eq!(SYS_DESCR, rasn::types::Oid::new(&[1, 3, 6, 1, 2, 1, 2, 2, 1, 3]).unwrap());
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: this note originates in the macro `assert_eq` (in Nightly builds, run with -Z macro-backtrace for more info)
Other cases

Rust Version
rustc 1.85.0-beta.1 (e30eefff4 2025-01-08)
binary: rustc
commit-hash: e30eefff41038ceea427009023627d6d66b36715
commit-date: 2025-01-08
host: x86_64-unknown-linux-gnu
release: 1.85.0-beta.1
LLVM version: 19.1.6
Anything else?

No response

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.

Research direction

Start by compiling the provided macro_rules! blarg reproducer with the Rust version described in the issue and compare the diagnostic with the desired output. Trace the compiler diagnostic path responsible for repeated note: erroneous constant encountered messages, then add or update a regression test showing that dependent failed constants no longer produce those notes.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.