Description
There is a curious case with where clauses where sometimes we can show that a method in an impl could not possibly be called. This is because the impl has more precise information than the trait. Here is an example:
trait MyTrait<T> {
fn method(&self, t: &T) where T : Eq;
}
struct Foo;
struct Bar; // note that `Bar` does not derive `Eq`
impl MyTrait<Bar> for Foo {
fn method(&self, t: &T) where Bar : Eq { // <-- `Bar : Eq` cannot be satisfied!
}
}
We should permit the method body to be omitted in such a case. As a workaround, once #20020 is fixed, I imagine it would be possible to write an impl like this:
impl MyTrait<Bar> for Foo {
fn method(&self, t: &T) { // no where clause at all
panic!("Bar : Eq could not be satisfied");
}
}
However, it is unfortunate to require that of the user. For one thing, perhaps it happens later that an impl of Eq
is added for Bar
-- now we have this method hanging around that will panic. It'd be nice to detect that statically.
The plan then would be to permit:
impl MyTrait<Bar> for Foo {
fn method(&self, t: &T); // <-- no body or where clauses needed
}
This serves as a declaration that you believe this method could never be called. At trans time, we will generate a body that simply does the equivalent of panic!("unsatisfiable method
methodinvoked")
.
I plan to open an amendment to the where clause RFC describing this particular case.