Confusing error from const generic hackery

So I have the following very convoluted code:

pub struct Predicate<const B: bool> {}
pub trait True {}
impl True for Predicate<true> {}
pub const fn check(N: usize) -> bool {
    N % 2 > 0
impl<const N: usize> Montgomery<N> {
    const R: usize = N.next_power_of_two();
    pub const fn new(original: usize) -> Montgomery<N>
        Predicate<{ Self::R.is_power_of_two() }>: True,
        Predicate<{ check(N) }>: True,

And rustc (the 11-May-21 nightly) flags this,

error[E0283]: type annotations needed
  --> src\
4  | pub trait True {}
   | -------------- required by this bound in `True`
82 |         Predicate<{ check(N) }>: True,
   |                                  ^^^^ cannot infer type for struct `Predicate<{ check(N) }>`
   = note: cannot satisfy `Predicate<{ check(N) }>: True`

And I have absolutely no idea what this refers to, because there is only one True, and the manual entry for E0283 doesn't seem to help because it's talking about ambiguity in implementation where no ambiguity appears to exist.

What's going on? I know const generics are incomplete, have I just ran into an edge case or misunderstood the message?

I couldn't reproduce in the Playground (2021-05-25 nightly). Maybe now fixed, or maybe your example lacks context?

Sorry, turns out this was actually a mistake in my setup. It does work now that I've got my hands on the 25-May-21 compiler. Rustup was initially refusing to download it because apparently the last version with RLS was on the 11th.

Thanks for pointing out that Playground accepted it

1 Like