Skip to content

8265899: Use pattern matching for instanceof at module jdk.compiler(part 1) #3673

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
wants to merge 2 commits into from

Conversation

lgxbslgx
Copy link
Member

@lgxbslgx lgxbslgx commented Apr 25, 2021

Hi all,

This series of patches update the code of the module jdk.compiler by using the pattern matching for instanceof.

This patch would revise the following packages:

com.sun.tools.javac.api
com.sun.tools.javac.file
com.sun.tools.javac.main
com.sun.tools.javac.model
com.sun.tools.javac.platform
com.sun.tools.javac.tree
com.sun.tools.javac.util

Thank you for taking the time to review.

--
Best Regards,
Guoxiong.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed

Issue

  • JDK-8265899: Use pattern matching for instanceof at module jdk.compiler(part 1)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.java.net/jdk pull/3673/head:pull/3673
$ git checkout pull/3673

Update a local copy of the PR:
$ git checkout pull/3673
$ git pull https://git.openjdk.java.net/jdk pull/3673/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 3673

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.java.net/jdk/pull/3673.diff

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 25, 2021

👋 Welcome back gli! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Apr 25, 2021

@lgxbslgx The following labels will be automatically applied to this pull request:

  • compiler
  • javadoc

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing lists. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added javadoc javadoc-dev@openjdk.org compiler compiler-dev@openjdk.org labels Apr 25, 2021
@lgxbslgx
Copy link
Member Author

/label remove javadoc

@openjdk openjdk bot removed the javadoc javadoc-dev@openjdk.org label Apr 25, 2021
@openjdk
Copy link

openjdk bot commented Apr 25, 2021

@lgxbslgx
The javadoc label was successfully removed.

@lgxbslgx lgxbslgx marked this pull request as ready for review April 25, 2021 09:30
@openjdk openjdk bot added the rfr Pull request is ready for review label Apr 25, 2021
@mlbridge
Copy link

mlbridge bot commented Apr 25, 2021

Webrevs

Copy link
Contributor

@mcimadamore mcimadamore left a comment

Choose a reason for hiding this comment

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

Very nice cleanup - left few comments

JavacScope s = (JavacScope) other;
return (env.equals(s.env)
&& isStarImportScope() == s.isStarImportScope());
if (other instanceof JavacScope javacScope) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can this be:

return other instanceof JavacScope javacScope &&
            env.equals(javacScope.env) &&
            isStarImportScope() == javacScope.isStarImportScope();

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

result = new ArrayList<>(((Collection<?>)paths).size());
else
if (paths != null) {
result = new ArrayList<>(paths.size());
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems odd in the original code - I'd rather have somebody versed in the API double check this - @jonathan-gibbons ? Anyway - this doesn't seem to be related with using pattern matching, so perhaps it's preferrable to just move it out of this PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think that removing the unnecessary instanceof expressions is also the goal of this cleanup. So I want to keep it.

In this case, my logical analysis is shown below.

  1. Please see the code below. The parameter type of the method getJavaFileObjectsFromPaths is Collection<? extends Path>, so we don't need to use paths instanceof Collection<?>. We only need to judge whether paths is null. So I use paths != null instead.
    public Iterable<? extends JavaFileObject> getJavaFileObjectsFromPaths(
        Collection<? extends Path> paths)
  1. Because the paths may be null, I move the following code from top scope to if (paths != null) branch scope to avoid the NPE.
        for (Path p: paths)
            result.add(PathFileObject.forSimplePath(this,
                    fsInfo.getCanonicalFile(p), p));

In my opinion, these two steps are reasonable for removing the instanceof expression.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK

@@ -329,18 +329,17 @@ private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
|| elem.getModifiers().contains(Modifier.PRIVATE))
return Collections.emptySet();

if (!(elem instanceof MethodSymbol))
if (!(elem instanceof MethodSymbol methodSymbol))
Copy link
Contributor

Choose a reason for hiding this comment

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

These two look odd - your cleanup is fine, but it seems in the first cast, we want to wrap with IllegalArgumentException instead of failing with ClassCastException. In the second cast (for class symbol) it seems we're ok with just CCE. I'm fine with leaving this as per your cleanup - just noting the inconsistency.

@@ -2178,7 +2178,7 @@ protected JCInstanceOf(JCExpression expr, JCTree pattern) {

@Override @DefinedBy(Api.COMPILER_TREE)
public JCPattern getPattern() {
return pattern instanceof JCPattern ? (JCPattern) pattern : null;
return pattern instanceof JCPattern jcPattern ? jcPattern : null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Using patterns to implement patterns support :-)

Copy link
Contributor

@mcimadamore mcimadamore left a comment

Choose a reason for hiding this comment

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

Looks good

result = new ArrayList<>(((Collection<?>)paths).size());
else
if (paths != null) {
result = new ArrayList<>(paths.size());
Copy link
Contributor

Choose a reason for hiding this comment

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

OK

@openjdk
Copy link

openjdk bot commented Apr 26, 2021

@lgxbslgx 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:

8265899: Use pattern matching for instanceof at module jdk.compiler(part 1)

Reviewed-by: mcimadamore

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 25 new commits pushed to the master branch:

  • efe6b93: 8265961: Fix comments in logging.properties
  • 65c19c4: 4926314: Optimize Reader.read(CharBuffer)
  • 082abbd: 8265900: Use pattern matching for instanceof at module jdk.compiler(part 2)
  • 851b219: 8265901: Use pattern matching for instanceof at module jdk.compiler(part 3)
  • fb8f0c5: 8261168: Convert javadoc tool to use Stream.toList()
  • 8559a53: 8265394: G1: Improve assert in HeapRegion::reset_not_compacted_after_full_gc
  • 68011c6: 8265928: G1: Update copyright in several files
  • 222f9f0: 8265682: G1: Mutex::_name dangling in HeapRegionRemSet references after JDK-8264146
  • 2b09ff2: 8232765: NullPointerException at Types.eraseNotNeeded() when compiling a class
  • b9f66d9: 8264188: Improve handling of assembly files in the JDK
  • ... and 15 more: https://git.openjdk.java.net/jdk/compare/f6e22d14e3ee529f21ab8747cfbafe72b906ec68...master

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.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@mcimadamore) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

@lgxbslgx
Copy link
Member Author

/integrate

@openjdk openjdk bot added ready Pull request is ready to be integrated sponsor Pull request is ready to be sponsored labels Apr 26, 2021
@openjdk
Copy link

openjdk bot commented Apr 26, 2021

@lgxbslgx
Your change (at version dc6c3dd) is now ready to be sponsored by a Committer.

@lgxbslgx
Copy link
Member Author

@mcimadamore Thank you for your review. May I ask your help to sponsor this patch?

@mcimadamore
Copy link
Contributor

/sponsor

@openjdk openjdk bot closed this Apr 26, 2021
@openjdk openjdk bot added integrated Pull request has been integrated and removed sponsor Pull request is ready to be sponsored ready Pull request is ready to be integrated rfr Pull request is ready for review labels Apr 26, 2021
@openjdk
Copy link

openjdk bot commented Apr 26, 2021

@mcimadamore @lgxbslgx Since your change was applied there have been 29 commits pushed to the master branch:

  • a6f2863: 8266003: ProblemList sanity/client/SwingSet/src/ButtonDemoScreenshotTest.java on macosx-all
  • 852a41d: 8258625: [JVMCI] refactor and unify JVMCI readFieldValue path
  • b5c6351: 8264663: Update test SuspendWithCurrentThread.java to verify that suspend doesn't exit until resumed
  • b524a81: 8265982: JDK-8264188 breaks build on macOS-aarch64
  • efe6b93: 8265961: Fix comments in logging.properties
  • 65c19c4: 4926314: Optimize Reader.read(CharBuffer)
  • 082abbd: 8265900: Use pattern matching for instanceof at module jdk.compiler(part 2)
  • 851b219: 8265901: Use pattern matching for instanceof at module jdk.compiler(part 3)
  • fb8f0c5: 8261168: Convert javadoc tool to use Stream.toList()
  • 8559a53: 8265394: G1: Improve assert in HeapRegion::reset_not_compacted_after_full_gc
  • ... and 19 more: https://git.openjdk.java.net/jdk/compare/f6e22d14e3ee529f21ab8747cfbafe72b906ec68...master

Your commit was automatically rebased without conflicts.

Pushed as commit 41daa88.

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

@lgxbslgx lgxbslgx deleted the JDK-8265899 branch April 26, 2021 23:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler compiler-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

2 participants