Requirement to bind a trait to a related type of inherited trait

I have a Foo trait inheriting from another attribute of Bar . Bar has a related type of Baz . Foo restricts Baz in such a way that Baz must implement Hoge .

 trait Hoge {} trait Bar { type Baz; } trait Foo: Bar where Self::Baz: Hoge {} 

However, when I define a generic function that requires a generic type T to implement Foo ,

 // [DESIRED CODE] fn fizz<T: Foo>(buzz: T) { // ... } 

rustc complains about EO277 if I don't restrict T explicitly:

 fn fizz<T: Foo>(buzz: T) where T::Baz: Hoge { // ... } 

I don’t understand why I need this. I would like to write [DESIRED CODE] . What is the recommended way to do this?

+7
rust
source share
1 answer

Sadly (or not), you have to repeat the boundaries.

Last year, I opened the issue , believing that type checking was inconsistent. The code is similar to yours.

@ arielb1 closed the problem and said that this was the intended behavior and gave the following explanation:

The fact is that we don’t want too many restrictions to be implicitly available for functions, since this can lead to changes that stop compilation of functions. There are basically 3 types of restrictions available for a function:

  • boundaries from explicit where-eg clauses. T: B when you have this offer. This includes a “semi-explicit” Sized evaluation.
  • the boundaries of superapplications of explicit where-clauses - the where clause adds ratings for its add-ons (as trait B: A T: B binding adds T: A ).
  • limits the life properties of arguments (outlives / implicator / implied boundaries). These are only the limits of the lifetime, and it does not matter for the current problem. rust-lang / rfcs # 1214 there are many.

If your binding is not in the list, you will need to add it explicitly if you want to use it. I assume this should be an entry in the FAQ.

Today I opened issue to request that this information be added to the documents.

+4
source share

All Articles