Skip to content

Tracking issue for future-incompatibility lint where_clauses_object_safety #51443

Closed

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 which dyn types are targeted by a blanket impl such as impl<U: ?Sized + Bounds> Trait for U.
  • Checking the cast sites with a rule like "if dyn Foo: Trait then to cast T into dyn Foo we require that T: Trait must also hold", probably for every Trait such that Self: Trait appears in where clauses in the traits methods. This would alleviate the warning for Self: 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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-lintArea: Lints (warnings about flaws in source code) such as unused_mut.Area: Lints (warnings about flaws in source code) such as unused_mut.A-trait-objectsArea: trait objects, vtable layoutArea: trait objects, vtable layoutA-traitsArea: Trait systemArea: Trait systemA-typesystemArea: The type systemArea: The type systemC-future-incompatibilityCategory: Future-incompatibility lintsCategory: Future-incompatibility lintsC-tracking-issueCategory: A tracking issue for an RFC or an unstable feature.Category: A tracking issue for an RFC or an unstable feature.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions