babylon icon indicating copy to clipboard operation
babylon copied to clipboard

Refactor code for computing denotable projection of types

Open mcimadamore opened this issue 1 week ago • 3 comments

The code reflection support needs a way to compute the closest fully denotable supertype of the type of an expression. To do this we build on type projections, but this doesn't work fully, because type projectios still allow intersection types in, and the JavaType API (or the j.l.r.Type API) doesn't have a way to model these.

For this reason, we had to tweak the compiler code in Types to accept an extra parameter to tell the projection to also discard intersection/union types. While this works, this creates a lot of noise in the compiler codebase, so I've been looking for a lower-maintenance alternative.

The basic idea behind this PR is to apply a normalization pass before we compute a vanilla type projection. This normalization pass essentially replaces any intersection type with a fresh type variable whose bound is the first bound of the intersection.

As an example, consider the type: Foo<A & B & C>:

  1. we first transform this type into Foo<#1> where #1 is a fresh type-variable with upper bound A
  2. we then add #1 to the list of type variables to be "projected"
  3. we then compute the upward projectin of Foo<#1>, and obtain Foo<? extends A>.

This process is sound because (a) replacing A & B & C with a fresh type variable with bound A & B & C is sound, and because (b) upward projection will always map such type variables into some kind of wildcard (so dropping some bounds from the intersection doesn't make any difference) -- unless the type is a toplevel type, in which case, again dropping a bound doesn't make any difference.


Progress

  • [x] Change must not contain extraneous whitespace

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/babylon.git pull/707/head:pull/707
$ git checkout pull/707

Update a local copy of the PR:
$ git checkout pull/707
$ git pull https://git.openjdk.org/babylon.git pull/707/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 707

View PR using the GUI difftool:
$ git pr show -t 707

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/babylon/pull/707.diff

Using Webrev

Link to Webrev Comment

mcimadamore avatar Nov 25 '25 19:11 mcimadamore

:wave: Welcome back mcimadamore! A progress list of the required criteria for merging this PR into code-reflection will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

bridgekeeper[bot] avatar Nov 25 '25 19:11 bridgekeeper[bot]

@mcimadamore This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

Refactor code for computing denotable projection of types

Reviewed-by: psandoz

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 6 new commits pushed to the code-reflection branch:

  • d7107ab9d5903e5062ca327319b2d2a44d5f29e7: Make java @hat/bld less noisy by default
  • 364743678e6382eefab0ac36976640bf76a34ee8: [hat] Compilation phases formatted with sub-phase
  • 5b7b3b661b079e2cb04fba9199c2a19d60e87ef5: [docs] Babylon and HAT builds and with HAT testing updated
  • ... and 3 more: https://git.openjdk.org/babylon/compare/e644ddea433ebb754e1cf9b8e66bf6a577ecd319...code-reflection

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the code-reflection branch, type /integrate in a new comment.

openjdk[bot] avatar Nov 25 '25 19:11 openjdk[bot]

Webrevs

mlbridge[bot] avatar Nov 25 '25 19:11 mlbridge[bot]

/integrate

mcimadamore avatar Dec 01 '25 10:12 mcimadamore

Going to push as commit e458d3fe74afcb15c65ce70119603b1dbe8c43e7. Since your change was applied there have been 8 commits pushed to the code-reflection branch:

  • b07ea22c6cac90defb932c39425461f9922eccb7: Remove minor unnecessary changes from main javac codebase
  • fa097e590fc5bcbf74b75a123939cadad59b3ed3: Fix javadoc issues
  • d7107ab9d5903e5062ca327319b2d2a44d5f29e7: Make java @hat/bld less noisy by default
  • ... and 5 more: https://git.openjdk.org/babylon/compare/e644ddea433ebb754e1cf9b8e66bf6a577ecd319...code-reflection

Your commit was automatically rebased without conflicts.

openjdk[bot] avatar Dec 01 '25 10:12 openjdk[bot]

@mcimadamore Pushed as commit e458d3fe74afcb15c65ce70119603b1dbe8c43e7.

:bulb: You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

openjdk[bot] avatar Dec 01 '25 10:12 openjdk[bot]