RFC 0735: allow-inherent-impls-anywhere

lang (typesystem | impls | inherent-impl)

Summary

Allow inherent implementations on types outside of the module they are defined in, effectively reverting RFC PR 155.

Motivation

The main motivation for disallowing such impl bodies was the implementation detail of fake modules being created to allow resolving Type::method, which only worked correctly for impl Type {...} if a struct Type or enum Type were defined in the same module. The old mechanism was obsoleted by UFCS, which desugars Type::method to <Type>::method and performs a type-based method lookup instead, with path resolution having no knowledge of inherent impls - and all of that was implemented by rust-lang/rust#22172.

Aside from invalidating the previous RFC's motivation, there is something to be said about dealing with restricted inherent impls: it leads to non-DRY single use extension traits, the worst offender being AstBuilder in libsyntax, with almost 300 lines of redundant method definitions.

Detailed design

Remove the existing limitation, and only require that the Self type of the impl is defined in the same crate. This allows moving methods to other modules:

struct Player;

mod achievements {
    struct Achievement;
    impl Player {
        fn achieve(&mut self, _: Achievement) {}
    }
}

Drawbacks

Consistency and ease of finding method definitions by looking at the module the type is defined in, has been mentioned as an advantage of this limitation. However, trait impls already have that problem and single use extension traits could arguably be worse.

Alternatives

Unresolved questions

None.