Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upType inequality constraints in `where` clauses #1834
Comments
nrc
added
the
T-lang
label
Jan 4, 2017
This comment has been minimized.
This comment has been minimized.
|
This seems plausible. I can imagine that in the future, if we have full specialization support (allowing overlapping instances with rules for selecting more specific instances over more general instances), the use case of manually excluding conflicting types would disappear. However, in the meantime, this would work. In the interests of not making the perfect the enemy of the good, it'd be nice to know whether we expect to have specialization support quickly enough to make this unnecessary, or if the ecosystem would benefit significantly from an intermediate step. |
This comment has been minimized.
This comment has been minimized.
|
Negative reasoning tends to be a source of issues, this is definitely a non-trivial feature. We have to work through the implications of it to make sure it doesn't break any guarantees we want coherence to uphold. I would say specialization will definitely be stabilized before this feature could be. We should maybe have a tag for negative reasoning proposals so we can keep track of all of them. |
This comment has been minimized.
This comment has been minimized.
In that case, it seems like the most critical question on this proposal is whether it has use cases that specialization would not address. If it does, I'd like to see some examples of them. If it doesn't, and we think specialization will get implemented first before this feature would, then I don't think we'd want to accept it. |
This comment has been minimized.
This comment has been minimized.
|
You could |
This comment has been minimized.
This comment has been minimized.
|
@withoutboats You could do that, but I wondered if any specific use case might motivate that. None come to mind, but I wondered if the proposer might have one, or if anyone else might. |
This comment has been minimized.
This comment has been minimized.
|
There's the blanket From<!> for everything. I think this would be overkill there, as I rather somehow exploit the fact that the overlapping implementations are identical. |
This comment has been minimized.
This comment has been minimized.
|
@Ericson2314 is there a blanket |
This comment has been minimized.
This comment has been minimized.
|
I suspect the point was intended to be that there isn't, because it wouldn't be, and that this could be a way to solve it (but not the best one). |
This comment has been minimized.
This comment has been minimized.
|
Oh I see I misread "There's the blanket From<!> impl" as "There is a blanket From<!> impl" |
This comment has been minimized.
This comment has been minimized.
Do you mean |
This comment has been minimized.
This comment has been minimized.
spiveeworks
commented
Nov 15, 2017
•
|
use case: trait TypeInfo {}
trait Downcast<_T: TypeInfo> {}
union Cons<A, B>
where A: Copy + TypeInfo,
B: Copy,
{
head: A,
tail: B,
}
impl<A, B> Downcast<A> for Cons<A, B>
where A: Copy + TypeInfo,
B: Copy,
{}
impl<E, A, B> Downcast<E> for Cons<A, B>
where A: Copy + TypeInfo,
B: Copy + Downcast<E>,
{}This gives an error for conflicting implementations, which could be fixed by constraining the second impl to Specialization itself doesn't solve this, although if I recall correctly, Niko has mentioned that specialization could be loosened further eventually, but I don't understand well enough to tell if this would be allowed. |
This comment has been minimized.
This comment has been minimized.
mjbshaw
commented
Dec 20, 2018
|
Another use case is relaxing the object safety escape hatch for traits (discussion here). RFC 255 introduces the concept of object safety for traits. For example, consider the following trait trait T {
fn foo();
fn bar<T>(&self);
}
Type inequality constraints could solve this. The escape hatch could be |
spinda commentedDec 29, 2016
The idea of a
!=constraint form inwhereclauses has come up multiple times, in discussions of thewhereclause itself, in rust-lang/rust#20041 as a counterpart to the==constraint form, and in various proposals for negative trait reasoning (!Trait). I'd like to extract this idea into its own RFC.Say we want to write a
Frominstance for!reflecting its ability to implicitly cast to any type.This produces an overlapping error because both
impls cover!: From<!>. Specialization can't help here, as the firstimpldoes not fully contain the second. What's necessary is a way to limit the scope of the secondimplto excludeT == !, avoiding the overlap altogether. I'd like to propose the following syntax:Negative reasoning for traits has been held up in the past due to concerns over implementing a trait for a new type becoming a breaking change. The
==constraint has been held up due to an expectation that it would affect normalization (see rust-lang/rust#22074 (comment)). The!=constraint doesn't suffer from either of these issues and can be very useful on its own, so I think it makes sense to split it off.