-
Notifications
You must be signed in to change notification settings - Fork 13.3k
fix negative impls inference #74525
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
Closed
Closed
fix negative impls inference #74525
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// check-pass | ||
#![feature(negative_impls)] | ||
|
||
struct Foo<T>(T); | ||
|
||
impl !Send for Foo<()> {} | ||
|
||
fn test<T>() -> T where Foo<T>: Send { todo!() } | ||
|
||
fn main() { | ||
let _: u8 = test(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// check-pass | ||
use std::rc::Rc; | ||
|
||
trait A { | ||
fn foo(&self) {} | ||
} | ||
|
||
impl<T: Send> A for T {} | ||
|
||
fn test<T: A>(rc: &Rc<T>) { | ||
rc.foo() | ||
// `Rc: Send` must not be evaluated as ambiguous | ||
// for this to compile, as we are otherwise | ||
// not allowed to use auto deref here to use | ||
// the `T: A` implementation. | ||
} | ||
|
||
fn main() {} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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, asFoo
can still implementSend
, 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 🤔 )
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, interesting.
There was a problem hiding this comment.
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
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 automaticSend 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".
There was a problem hiding this comment.
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 🤔
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:
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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.