rust-lang / rust-lang/rust

Combining `generic_const_exprs` with macros gives incorrect error suggestions (referencing source code)

Open
#125,905 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-generics A-diagnostics C-bug F-generic_const_exprs P-low T-types
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

It seems like the suggestion uses span_to_snippet since using macros gives weird results. The errors themselves are due to #120905. Playground:

#![feature(associated_const_equality)]
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]

macro_rules! p {
    ($x:ident) => {
        <Self::Params as Parameters>::$x
    }
}

trait Container {
    type Params: Parameters;

    fn get(&self) -> Box<dyn Element<Params = Self::Params>>;
}

trait Element {
    type Params: Parameters;

    fn stuff(&self) -> [f32; p!(N)];
}

trait Parameters {
    const N: usize;

    fn info(&self) -> [i32; Self::N];
}

fn process<C, P, const N: usize>(c: C, p: P)
where
    C: Container<Params = P>,
    P: Parameters<N = { N }>,
{
    analyze(c.get().stuff(), p.info());
}

fn analyze<const N: usize>(_stuff: [f32; N], _info: [i32; N]) {
    todo!()
}

Error:

error: unconstrained generic constant
  --> src/lib.rs:34:21
   |
34 |     analyze(c.get().stuff(), p.info());
   |                     ^^^^^
   |
note: required by a bound in `Element::stuff`
  --> src/lib.rs:7:9
   |
7  |         <Self::Params as Parameters>::$x
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Element::stuff`
...
20 |     fn stuff(&self) -> [f32; p!(N)];
   |        -----                 ----- in this macro invocation
   |        |
   |        required by a bound in this associated function
   = note: this error originates in the macro `p` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try adding a `where` bound
   |
32 |     P: Parameters<N = { N }>, [(); <Self::Params as Parameters>::$x]:
   |                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error: unconstrained generic constant
  --> src/lib.rs:34:32
   |
34 |     analyze(c.get().stuff(), p.info());
   |                                ^^^^
   |
note: required by a bound in `Parameters::info`
  --> src/lib.rs:26:29
   |
26 |     fn info(&self) -> [i32; Self::N];
   |                             ^^^^^^^ required by this bound in `Parameters::info`
help: try adding a `where` bound
   |
32 |     P: Parameters<N = { N }>, [(); Self::N]:
   |                             ~~~~~~~~~~~~~~~~

error: unconstrained generic constant
  --> src/lib.rs:34:5
   |
34 |     analyze(c.get().stuff(), p.info());
   |     ^^^^^^^
   |
note: required by a bound in `Element::stuff`
  --> src/lib.rs:7:9
   |
7  |         <Self::Params as Parameters>::$x
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `Element::stuff`
...
20 |     fn stuff(&self) -> [f32; p!(N)];
   |        -----                 ----- in this macro invocation
   |        |
   |        required by a bound in this associated function
   = note: this error originates in the macro `p` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try adding a `where` bound
   |
32 |     P: Parameters<N = { N }>, [(); <Self::Params as Parameters>::$x]:
   |                             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0308]: mismatched types
  --> src/lib.rs:34:30
   |
34 |     analyze(c.get().stuff(), p.info());
   |                              ^^^^^^^^ expected `<Self::Params as Parameters>::$x`, found `Self::N`
   |
   = note: expected constant `<Self::Params as Parameters>::$x`
              found constant `Self::N`

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 with the supplied src/lib.rs reproduction in the Rust Playground and compare the diagnostics produced when generic_const_exprs is combined with the p macro. Trace how span_to_snippet handles the macro-expanded references and inspect the reported suggestions. Done means the diagnostics no longer suggest bounds containing incorrect source references.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.