Skip to content

Bad generic inference on very recent nightly #105037

Closed
@dlubarov

Description

@dlubarov

We noticed a build error in Plonky2 on the nightly build 2022-11-27. The same code was building last week, on a slightly older nightly.

Here's a minimal reproducible example (Playground):

#![feature(generic_const_exprs)]

trait Table<const D: usize>: Sync {
    const COLUMNS: usize;
}

struct Table1<const D: usize>;
impl<const D: usize> Table<D> for Table1<D> {
    const COLUMNS: usize = 123;
}

struct Table2<const D: usize>;
impl<const D: usize> Table<D> for Table2<D> {
    const COLUMNS: usize = 456;
}

fn process_table<T: Table<D>, const D: usize>(table: T)
where
    [(); T::COLUMNS]:,
{
}

fn process_all_tables<const D: usize>()
where
    [(); Table1::<D>::COLUMNS]:,
    [(); Table2::<D>::COLUMNS]:,
{
    process_table(Table1::<D>);
    process_table(Table2::<D>);
}

Nightly 2022-11-27 gives this error:

error[[E0308]](https://doc.rust-lang.org/nightly/error-index.html#E0308): mismatched types
  --> src/lib.rs:28:19
   |
28 |     process_table(Table1::<D>);
   |     ------------- ^^^^^^^^^^^ expected struct `Table2`, found struct `Table1`
   |     |
   |     arguments to this function are incorrect
   |
   = note: expected struct `Table2<D>`
              found struct `Table1<D>`

It seems to be sensitive to the order of the where clauses, [(); Table1::<D>::COLUMNS]: and [(); Table2::<D>::COLUMNS]:. I.e. for a process_table call, it seems to infer that T is whichever Table appeared last in the where list.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-const-genericsArea: const generics (parameters and arguments)C-bugCategory: This is a bug.F-generic_const_exprs`#![feature(generic_const_exprs)]`

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions