Skip to content

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

Open
@camelid

Description

@camelid

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`

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.F-generic_const_exprs`#![feature(generic_const_exprs)]`T-typesRelevant to the types team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions