Description
This is the summary issue for the WHERE_CLAUSES_OBJECT_SAFETY
future-compatibility warning and other related errors. The goal of this page is describe why this change was made and how you can fix code that is affected by it. It also provides a place to ask questions or register a complaint if you feel the change should not be made. For more information on the policy around future-compatibility warnings, see our [breaking change policy guidelines (https://github.com/rust-lang/rfcs/blob/master/text/1122-language-semver.md).
What is the warning for?
As was discovered in #50781 a combination of implementing a trait directly for a dyn
type and where clauses involving Self
can punch a hole in our dyn-capability rules rules. See the minimization:
trait Trait {}
trait X { fn foo(&self) where Self: Trait; }
impl X for () { fn foo(&self) {} }
impl Trait for dyn X {}
// Segfault at opt-level 0, SIGILL otherwise.
pub fn main() { <dyn X as X>::foo(&()); }
The fix applied in #50966 is to tighten the dyn-capability rules to make X
not dyn-capable in this case, in general making any trait that contains Self
in where clauses not dyn-capable, much like we disallow Self
in arguments or return type. Few root regressions appeared in the crater run and those were fixable, though some crates being unmaintained complicates things.
However that fix is heavy handed and disallows things we actually wish to support, still we went ahead with the warning as a stop gap while we look for a better, more targeted fix. The original issue contains some discussion of alternative fixes. With tight chalk integration, we could do some clever things.
Other alternatives discussed for the short-term:
- Checking from an
impl Foo for dyn Bar + ..
whether that impl is causing trouble. This seems to be a dead end because it's hard to tell whichdyn
types are targeted by a blanket impl such asimpl<U: ?Sized + Bounds> Trait for U
. - Checking the cast sites with a rule like "if
dyn Foo: Trait
then to castT
intodyn Foo
we require thatT: Trait
must also hold", probably for everyTrait
such thatSelf: Trait
appears in where clauses in the traits methods. This would alleviate the warning forSelf: Trait
where clauses. This may be practical to implement now, but seems subtle, we'd need to give it more thought.
When will this warning become a hard error?
Hopefully we will develop a finer-grained rule and this warning will never be an error.
How to fix this?
Either drop the where clause or stop using dyn
types with the affected trait. If this is not viable, that's probably ok, it's very unlikely that your code is actually unsound. But please do report your case here so take we may take it into consideration and see how to better support it!
Metadata
Metadata
Assignees
Labels
Type
Projects
Status