Default impls are not restricted to same module (or submodule) as the trait

a0879bf
Opened by Nick Hamann at 2023-04-05 17:36:44

In the OIBIT RFC there are two limitations for default impls:

  1. The default impl must appear in the same module as Trait (or a submodule).
  2. Trait must not define any methods.

Limitation (1) does not appear to match the current implementation. The only error I can find pertaining to this rule is E0318, whose message is:

cannot create default implementations for traits outside the crate they're defined in; define a new trait instead

Also, the following example currently compiles:

#![feature(optin_builtin_traits)]

mod foo {
    pub trait Foo { }
}

mod bar {
    impl super::foo::Foo for .. { }
}

fn main() {}

I'm not sure if this is a bug in the implementation, if it's merely unimplemented, or if the RFC should be updated to reflect that you can define a default impl in the same crate, with no restriction on which module it's defined in.

  1. Note that the tracking issue for OIBIT is #13231. One of the checklist items is " Coherence interaction: a trait can only be defaulted in the crate where it is defined", so it looks to me like the RFC might need to be updated.

    Nick Hamann at 2015-05-20 22:42:36

  2. Triage: no changes I'm aware of

    Steve Klabnik at 2016-11-29 21:07:33

  3. Triage: same, no changes that I know of.

    Steve Klabnik at 2018-10-31 13:59:23