Rustc bug, or current rustc limitation?

⚓ rust    📅 2025-05-15    👤 surdeus    👁️ 5      

surdeus

Warning

This post was published 46 days ago. The information described in this article may have changed.

When one write trait A: B where "some bound"{} logically one could say that the compiler should apply the rule T:A => "some bound". But it does not. It just check "some bound" when A is implemented, and that is all.

The consequence is that this code, which use a where clause, does not compile:

trait A {
  type B;
  fn f(&self, v: u32);
}

trait C: A 
  where Self::B: Into<u32>, 
{}

fn f<T: C>(v: T, arg: T::B) {
   v.f(arg.into()) //Error B does not implement Into<u32>
}

On the other hand this code, and the following one do compile:

trait A2 {
  type B;
  fn f(&self, v: u32);
}

trait C2: A2<B: Into<U32>> {}

fn f2<T: C2>(v: T, arg: T::B) {
  v.f(arg.into())
}  

or:

trait A2 {
  type B: Into<u32>;
  fn f(&self, v: u32);
}

trait C2: A2 {}

fn f2<T: C2>(v: T, arg: T::B) {
  v.f(arg.into())
}

The fact the first piece of code does not compile, is normal? Is this a bug? Or a limitation of rustc?

3 posts - 3 participants

Read full topic

🏷️ rust_feed