-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Rust: Support non-universal impl
blocks
#19372
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
Open
paldepind
wants to merge
6
commits into
github:main
Choose a base branch
from
paldepind:rust-ti-implementing-type-method
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+2,052
−1,100
Conversation
This file contains hidden or 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
c66a71a
to
a9afbb3
Compare
a9afbb3
to
76baa34
Compare
76baa34
to
02115f6
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
no-change-note-required
This PR does not need a change note
Rust
Pull requests that update Rust code
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.
Adds type inference support for non-universal
impl
blocks. By "non-universal" we meanimpl
blocks that target generic types but which are not valid for all instantiations of the generic type.For instance
where the
flatten
method is only valid for someOption
instantiations.Non-universal
impl
block affect both method resolution and trait implementations as the method/trait implementation can be valid only for some instantiations of a type. AFoo<i64>
might have a different set of methods/traits than aFoo<String>
. Finding the right method/trait implementation is the crux of the matter. The tests have examples of this.I've tried to document the new additions in this PR with QLdoc, but here are some additional high-level comments on the changes:
As mentioned above, with non-universal
impl
blocks it is no longer enough to know the root of a type to determine which traits it implements and which methods it supports. This affects a bunch of things.getMethod
andgetABaseTypeMention
member predicate onType
is removed, as aType
is now not enough to determine these things.conditionSatisfiesConstraint
takes aTypeMention
as its second parameter as aType
is not enough.In this PR I've split subtype handling (inferring type parameters through supertypes) from constraint handling (inferring type parameters from type parameter interface constraints (in C#)/trait bounds (in Rust). The former is now the sole job of the
AccessBaseType
sub-module and the later is done in the newAccessConstraint
sub-module. There's also a predicate for each in the module signature:getABaseTypeMention
andconditionSatisfiesConstraint
.This has both pros and cons:
getABaseTypeMention
asnone()
and avoid any computation related to subtyping.All in all I'm not sure which approach is best, but when I made this change performance improved quite a bit (but that was before making some other optimizations) so that's why I ended up with this. Another approach (in follow up work) could be to 1/ merge the two things back again, 2/ add a
getVariance
predicate for access positions than can becovariant
orinvariant
, 3/ only do subtyping for covariant positions, 4/ make all positions invariant for Rust. That should give equal performance for Rust, cut down on the duplicated code and hopefully the optimization withcountConstraintImplementations
would mean that subtyping for languages like C# wouldn't regress in performance (but we'd have no way to measure that).