Compound bounds


#1

Would it be weird to allow e.g:

trait Foo where Self+Sized: Clone {
}

let x: &dyn Foo = ...;

?


#2

What would that syntax mean?


#3

For Sized self, Clone must be implemented, whereas for non-Sized self it need not be.

This allows trait objects.


#4

Ah, in that case the real issue is that today there’s no way to write trait bounds that include all non-Sized types and all Clone types without also including Sized-but-not-Clone types. Making that possible would probably require “negative trait bounds”, which is a huge feature of its own with weird and massive ramifications for the trait system that we might never want to add at all. So that would have to get sorted out before we can even begin to talk about sugar for combinations of negative and positive bounds like this.

My vague impression is that the negative trait bounds discussion has probably gone as far as it can go until specialization stabilizes.


#5

I did not suggest negative bounds. Look at the OP again: where Self + Sized : Clone (i.e. where a Self with Sized is Clone).

This is different from saying Self is Sized.


#6

Just as you can where A: B+C you should be able to where A+B: C


#7

This isn’t actually negative reasoning at all, in fact its a form of implication bounds: Self: Sized => Self: Clone (Self: Sized implies Self: Clone). Normally we think of implication bounds as something like T: Foo => Arc<T>: Foo, but this is the same concept.

And though it isn’t implemented in the surface syntax, chalk is powerful enough that it could solve a bound like this already! So this feature is not impossible, but probably won’t be implemented in rustc very soon because it isn’t high priority.

(A negative bound would be the opposite: “self is not clone if it isn’t sized;” that’s actually an implication bound joining two negative bounds!)