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

fix negative impls inference #74525

Closed
wants to merge 4 commits into from

Conversation

lcnr
Copy link
Contributor

@lcnr lcnr commented Jul 19, 2020

@lcnr lcnr marked this pull request as draft July 19, 2020 16:58
fn filter_negative_and_reservation_impls(
&mut self,
pred: ty::PolyTraitPredicate<'tcx>,
candidate: SelectionCandidate<'tcx>,
) -> SelectionResult<'tcx, SelectionCandidate<'tcx>> {
if let ImplCandidate(def_id) = candidate {
let tcx = self.tcx();
match tcx.impl_polarity(def_id) {
ty::ImplPolarity::Negative if !self.allow_negative_impls => {
Copy link
Contributor Author

@lcnr lcnr Jul 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we end up here if we have exactly one impl candidate which is negative.

We only have a negative impl for Foo<()> here, while the self type is actually more generic (Foo<_>).
So if Send is an auto trait we should return ambiguous in this case, as Foo can still implement Send, e.g. Foo<u8>.

I don't know how we can detect that the impl is at least as general as the current self type rn though, need some help for this. (I don't think I have to write a new type relation for this 🤔 )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, interesting.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So actually I'm not sure there is a bug here. There has been a long-standing debate about the correct semantics of auto traits in the face of negative impls, but the current semantics (not, perhaps 100% settled) is that

  • If the user writes ANY explicit impl, positive or negative, then the compiler generates NO automatic impl

This was perhaps to be paired with disallowing negative impls that were overly narrow, such as !Foo for Bar<()>.

So in this case, writing impl !Send for Bar<()> (for example) would mean that the compiler does not add an automatic Send for Bar impl at all.

It's debatable what's more intuitive. I personally prefer the current rules -- I would prefer that if you write anything explicit, you must write everything explicit. But also this avoids having a setup (which specialization/coherence currently do not permit) where you have a "base impl" that accepts lots of things and then "negative impls" that cut out "exceptions" where that base impl no longer applies.

In other words, given specialization (and auto traits as currently implemented and specified), if you know that a "base impl" applies, you know that the trait is implemented, and no specialization can make that "untrue".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I think this makes sense 🤔

disallowing negative impls that were overly narrow, such as !Foo for Bar<()>.

While negative impls are still unstable, this seems like a restriction we should probably enforce automatically.

Quite interestingly (or much rather, a logical consequence of how this is implemented rn), we can currently "fix" this inference issue by adding another nonsensical impl:

#![feature(negative_impls)]

struct Foo<T>(T);

struct IDontCare;

impl !Send for Foo<()> {}
unsafe impl Send for Foo<IDontCare> {}

fn test<T>() -> T where Foo<T>: Send { todo!() }

fn main() {
    let _: u8 = test();
}

I would love to use this to forbid calling array_chunks::<0>() in #74373 until we implement const where bounds,
but that may stop us from fixing negative impls (as we now need the broken version in std, even if only for an unstable method).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While negative impls are still unstable, this seems like a restriction we should probably enforce automatically.

these has been an open bug on this forever -- well, no, it's the first checkbox on #13231. It'd be easy enough to do, we can use the same code we use for Drop impls.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, might look into this in the following weeks 🤔 let's see how much time I have for this.

@lcnr lcnr force-pushed the negative-impls-inference branch from 72af60e to 85511d2 Compare July 20, 2020 06:39
@lcnr lcnr closed this Jul 21, 2020
@lcnr lcnr deleted the negative-impls-inference branch July 21, 2020 21:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

negative impls cause problems with type inference
2 participants