Skip to content

Java: Improve virtual dispatch via better unification check and deduplicate code with parameterised module #10097

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

Merged
merged 5 commits into from
Aug 29, 2022

Conversation

aschackmull
Copy link
Contributor

The first commit is a pure refactor that changes all four copies of the unification code to instead use a single parameterised module.
The second commit improves the unification algorithm to account for the upper bounds of type variables and wildcards - until now the code assumed that all type variables and wildcards were merely extends Object and thus unifiable with any RefType.
The third commit also accounts for wildcards with lower bounds.

@aschackmull aschackmull added the no-change-note-required This PR does not need a change note label Aug 18, 2022
@aschackmull aschackmull requested a review from a team as a code owner August 18, 2022 12:52
@github-actions github-actions bot added the Java label Aug 18, 2022
@aschackmull
Copy link
Contributor Author

cc @ginsbach to check parameterised module usage.

@ginsbach
Copy link
Contributor

Looks good to me from a parameterised modules perspective.

@aschackmull
Copy link
Contributor Author

Finally managed to get a decent-looking dca run.

smowton
smowton previously approved these changes Aug 25, 2022
Copy link
Contributor

@smowton smowton left a comment

Choose a reason for hiding this comment

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

Some optional comments and factoring a duplicated line, otherwise LGTM

or
failsUnification(t1.(Array).getComponentType(), t2.(Array).getComponentType())
or
exists(RefType upperbound, RefType other |
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
exists(RefType upperbound, RefType other |
// Can't unify an `extends` bound against a concrete type that doesn't descend from that upper bound:
exists(RefType upperbound, RefType other |

not other.getASourceSupertype*() = upperbound
)
or
exists(RefType upperbound1, RefType upperbound2 |
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
exists(RefType upperbound1, RefType upperbound2 |
// Can't unify two `extends` bounds that don't intersect:
exists(RefType upperbound1, RefType upperbound2 |

notHaveIntersection(upperbound1, upperbound2)
)
or
exists(RefType lowerbound, RefType upperbound |
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
exists(RefType lowerbound, RefType upperbound |
// Can't unify `? super X` against `Y` or `? extends Y` where `Y` isn't an ancestor of `X`:
exists(RefType lowerbound, RefType upperbound |

not lowerbound.getASourceSupertype*() = upperbound
)
or
exists(BoundedType lowerbound, RefType upperbound |
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
exists(BoundedType lowerbound, RefType upperbound |
// Can't unify `? super T`, where `T` is a type variable `T extends S`, with a type that doesn't intersect with `S`:
exists(BoundedType lowerbound, RefType upperbound |

Comment on lines 96 to 102
not lowerbound instanceof BoundedType
or
t2.(Wildcard).getLowerBoundType().(RefType).getSourceDeclaration() = lowerbound and
getUpperBound(t1).getSourceDeclaration() = upperbound and
not lowerbound instanceof BoundedType
|
not lowerbound.getASourceSupertype*() = upperbound
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
not lowerbound instanceof BoundedType
or
t2.(Wildcard).getLowerBoundType().(RefType).getSourceDeclaration() = lowerbound and
getUpperBound(t1).getSourceDeclaration() = upperbound and
not lowerbound instanceof BoundedType
|
not lowerbound.getASourceSupertype*() = upperbound
or
t2.(Wildcard).getLowerBoundType().(RefType).getSourceDeclaration() = lowerbound and
getUpperBound(t1).getSourceDeclaration() = upperbound and
|
not lowerbound instanceof BoundedType
not lowerbound.getASourceSupertype*() = upperbound

@aschackmull
Copy link
Contributor Author

Some optional comments and factoring a duplicated line

Fixed.

@aschackmull aschackmull merged commit 6e7dcfc into github:main Aug 29, 2022
@aschackmull aschackmull deleted the java/unification branch August 29, 2022 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Java no-change-note-required This PR does not need a change note
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants