rust-lang/rust

Compiler refuses code, demanding bound that's provided

Closed this issue · 2 comments

This is a more complex, but different, case as #54972.

When I try to compile:

use std::marker::PhantomData;

struct Pin;
struct Adc<MODE>(PhantomData<MODE>) where MODE: RunMode;

trait RunMode {}

trait Channel<ADC> {
    type ID;
    const CHANNEL: Self::ID;
}

impl<MODE> Channel<Adc<MODE>> for Pin where MODE: RunMode {
    type ID = u8;
    const CHANNEL: u8 = 0;
}

the compiler refuses it with the error:

error[E0277]: the size for values of type `MODE` cannot be known at compilation time
  --> src/lib.rs:17:5
   |
17 |     const CHANNEL: u8 = 0;
   |     ^^^^^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
   |
   = help: the trait `std::marker::Sized` is not implemented for `MODE`
   = note: to learn more, visit <https://doc.rust-lang.org/book/second-edition/ch19-04-advanced-types.html#dynamically-sized-types-and-sized>
   = help: consider adding a `where MODE: std::marker::Sized` bound
   = note: required because of the requirements on the impl of `Channel<Adc<MODE>>` for `Pin`

error[E0277]: the trait bound `MODE: RunMode` is not satisfied
  --> src/lib.rs:17:5
   |
17 |     const CHANNEL: u8 = 0;
   |     ^^^^^^^^^^^^^^^^^^^^^^ the trait `RunMode` is not implemented for `MODE`
   |
   = help: consider adding a `where MODE: RunMode` bound
   = note: required because of the requirements on the impl of `Channel<Adc<MODE>>` for `Pin`

error: aborting due to 2 previous errors

Similarly to #54972, if I add MODE: ?Sized bounds to lines 4 and 15, the Sized (first) error goes away, but the RunMode (second) one does not.

This seems to compile correctly now?

Accepted since 1.35.