Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explicit OIBIT impls hide the default impls #27554

Closed
wthrowe opened this issue Aug 6, 2015 · 7 comments
Closed

Explicit OIBIT impls hide the default impls #27554

wthrowe opened this issue Aug 6, 2015 · 7 comments
Labels
A-trait-system Area: Trait system A-type-system Area: Type system P-medium Medium priority T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@wthrowe
Copy link
Contributor

wthrowe commented Aug 6, 2015

#![allow(dead_code)]

trait Foo {}
struct A<T>(T);
unsafe impl<T: Foo> Sync for A<T> {} // Comment this line and the code compiles

trait IsSync: Sync {}
struct X;
impl IsSync for A<X> {} // error: the trait `Foo` is not implemented for the type `X` [E0277]

fn main() {}

Nothing in this example opts out of Sync or contains a type that opts out, so everything should be Sync.

I suspect this is the same underlying issue as #23072, but unlike in that case this seems like something that should be allowed.

@Aatch Aatch added the A-trait-system Area: Trait system label Aug 6, 2015
@wthrowe
Copy link
Contributor Author

wthrowe commented Aug 10, 2015

I've been experimenting with ways to fix this, and, oddly, there's a unit test checking that this doesn't work: https://github.com/rust-lang/rust/blob/master/src/test/compile-fail/typeck-default-trait-impl-precedence.rs

The OIBIT RFC seems pretty clear that this is supposed to be accepted, but I know that the rules specified there for negative impls are thought to be too lenient, so maybe this part is also frowned upon?

@huonw huonw added I-nominated T-lang Relevant to the language team, which will review and decide on the PR/issue. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 7, 2015
@nikomatsakis
Copy link
Contributor

As explained in #23072, this was the semantics we implemented, which diverged somewhat from the RFC. It's interesting that this can come about without feature gates (though obvious in retrospect).

@nikomatsakis
Copy link
Contributor

triage: P-medium

We discussed this last week in the language subteam meeting. Our conclusion was that there is (potentially) an issue here where the desired semantics are not entirely clear. It is backwards incompatible to fix it but deemed low risk, because the bad scenario is when one is trying to "add to" the default set that obey a particular trait (or remove further from the negative set, I suppose).

Some notes from our discussion:

The intent of an impl like this is somewhat unclear. Why did one write the impl in the first place? Was the goal to cover a case that the default rules would have excluded? Or was it perhaps to narrow down the default rules to a smaller set of acceptable cases (which kind of "opts out" by "opting in")? The latter is the current semantics; it does seem plausible that a naive read of the code might think that the impl was listing out the cases that are Sync, versus adding to an implicit set. A short-term fix we might use is to try and report errors if the impl is "unnecessary". Overall though our conclusion was the current semantics ought to be revisited with specialization in mind, since there is a lot of overlap (no pun intended!) between the situation here and that of specialization.

@brson
Copy link
Contributor

brson commented Jul 14, 2016

Nominating for retriage and updates. Old type system issue.

@arielb1 arielb1 removed the I-wrong label Jul 17, 2016
@arielb1
Copy link
Contributor

arielb1 commented Jul 17, 2016

Not I-wrong.

@nikomatsakis
Copy link
Contributor

I believe that there is no bug here: the compiler is doing the right thing, essentially. I've been meaning for some time to write-up an amendment to the OIBIT (now: auto trait) RFC to talk about it for some time. My current thoughts (more-or-less) are embedded in this gist, which also covers the connection to negative reasoning.

@nikomatsakis nikomatsakis removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jul 21, 2016
@nikomatsakis
Copy link
Contributor

Going to close this issue -- this is the desired semantics -- but we do need to write an amendment to the RFC, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-system Area: Trait system A-type-system Area: Type system P-medium Medium priority T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants