Skip to content

8339296: Record deconstruction pattern in switch fails to compile - #20990

Closed
lahodaj wants to merge 2 commits into
openjdk:masterfrom
lahodaj:JDK-8339296
Closed

lahodaj wants to merge 2 commits into
openjdk:masterfrom
lahodaj:JDK-8339296

Conversation

@lahodaj

@lahodaj lahodaj commented Sep 13, 2024 •

Copy link
Copy Markdown
Contributor

Consider code like this:

    int nestedSwitchesInArgumentPosition(Object o1) {
        return id(switch (o1) {
            case R(var o2) -> switch (o2) {
                case R(String s) -> s;
                default -> "n";
            };
            default -> "";
        });
    }

    int id(String s) {
        return s.length();
    }

    int id(int i) {
        return i;
    }

Compiling this fails with a StackOverflowError, because:

  • there are speculative attribution happening for the switch expressions,
  • "completes normally" is computed during these speculative attribution, which have parts of the AST unfilled - specifically the nested case R(String s)
  • so, Attr.PostAttrAnalyzer fills in the missing types. In particular, the String s binding will get the Symtab.unknownType.
  • Flow.makePatternDescription will eventually ask Types.isSubtype(..., unknownType). This is guaranteed to fail with the StackOverflowError, as:
  • unknownType.isPartial() returns true, so Types.isSubtype(t, s) (s is the unknownType) calls Types.isSuperType(s, t), and Types.isSuperType(s, t) does not contain any special handling for the unknownType, so it will delegate to Types.isSubtype(t, s), leading to an infinite recursion.

It may be possible to side-step the issue by not computing the completes normally property during speculative attribution, or move that computation outside of Attr. It may be good to do, for performance reasons.

But, that does not seem to solve the underlying issue with unknownType. Many methods in Types misbehave in weird ways when the unknownType is passed to them.

The proposal herein is to attempt to resolve that. In particular, the UnknownType is proposed to extend the ErrorType, dropping the (internal) UNKNOWN type tag. The uses of the UNKNOWN type tag appear to be equivalent to handling of the ERROR type tag anyway. And the special handling of the unknownType appear to usually use == syms.unknownType:

if (target == null || target == syms.unknownType) {

After this change, the unknownType should behave as an erroneous type, unless specifically requested otherwise.

The intent is that the public API behavior for the unknownType should remain the same.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8339296: Record deconstruction pattern in switch fails to compile (Bug - P3)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 20990

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

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/20990.diff

Webrev

Link to Webrev Comment

@bridgekeeper

bridgekeeper Bot commented Sep 13, 2024

Copy link
Copy Markdown

👋 Welcome back jlahoda! 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

openjdk Bot commented Sep 13, 2024 •

Copy link
Copy Markdown

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

8339296: Record deconstruction pattern in switch fails to compile

Reviewed-by: vromero, 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 608 new commits pushed to the master branch:

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 master branch, type /integrate in a new comment.

@openjdk openjdk Bot added the rfr Pull request is ready for review label Sep 13, 2024
@openjdk

openjdk Bot commented Sep 13, 2024

Copy link
Copy Markdown

@lahodaj The following label will be automatically applied to this pull request:

  • compiler

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

@mlbridge

mlbridge Bot commented Sep 13, 2024 •

Copy link
Copy Markdown

Webrevs

import com.sun.tools.javac.util.List;
import java.util.Objects;

public class UnknownTypeTest extends TypeHarness {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice test!

@Override
public boolean isPartial() {
return true;
@Override @DefinedBy(Api.LANGUAGE_MODEL)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Modelling-wise, I'm not sure. It almost seems as if UnknownType should be subclassed by ErrorType and not the other way around. Is there a specific reason you went for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would see the UnknownType as a specific kind of an erroneous type (and hence UnknownType being a subtype or erroneous, as it is here).

But, even from a practical point of view - ErrorType basically must extend ClassType, I think, so implanting UnknownType in there seems difficult.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I guess I'm a bit surprised by the typeKind - which is OTHER (you added the override to keep compatibility with what was there before). In that sense, it doesn't seem like UnknownType "is a" ErrorType, because its tag seems "less general" than the one in ErrorType.

@mcimadamore mcimadamore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The approach seems good. I left some modelling questions. As you notice, I'm skeptical that just handling unknown types better is going to be the last time we see issues like this. E.g. the switch code in Attr seems to be doing way too much when we're in speculative mode.

@lahodaj

lahodaj commented Sep 27, 2024

Copy link
Copy Markdown
Contributor Author

The approach seems good. I left some modelling questions. As you notice, I'm skeptical that just handling unknown types better is going to be the last time we see issues like this. E.g. the switch code in Attr seems to be doing way too much when we're in speculative mode.

I don't disagree. I think we need to see if we can move the completeness computation to a later point, and maybe even skip the exhaustiveness check during speculative attribution. But, having a type that crashes the compiler when sent to an innocuous method seemed relatively important to fix.

unknownSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.fromString("<any?>"), null, rootPackage);
unknownSymbol.members_field = new Scope.ErrorScope(unknownSymbol);
unknownSymbol.type = unknownType;
// Create the unknown type

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

More general question: is the distinction between unknown and erroneous even relevant? It seems that an unknown type is just an erroneous type whose "class" happens to be the unknown symbol class.

Can we just say:

        unknownSymbol = new ClassSymbol(PUBLIC|STATIC|ACYCLIC, names.fromString("<any?>"), null, rootPackage);
        unknownType = unknownSymbol.type = new ErrorType(unknownSymbol);

Is the only difference what the public getTypeKind will report? Do we care if there's a difference there?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Internally, after this patch, the distinction is minimal, and usually checked using == unknownType. Whether we can change the external behavior not sure. We can try.

@openjdk openjdk Bot added the ready Pull request is ready to be integrated label Sep 27, 2024
@vicente-romero-oracle

Copy link
Copy Markdown
Contributor

The approach seems good. I left some modelling questions. As you notice, I'm skeptical that just handling unknown types better is going to be the last time we see issues like this. E.g. the switch code in Attr seems to be doing way too much when we're in speculative mode.

I agree with Maurizio that we are probably doing too much in speculative mode in the switch. I wonder if this patch should be part of the code that will refactor what we are doing there. I would expect any refactoring of what it is done in the switch code to affect this current PR

@lahodaj

lahodaj commented Sep 30, 2024

Copy link
Copy Markdown
Contributor Author

The approach seems good. I left some modelling questions. As you notice, I'm skeptical that just handling unknown types better is going to be the last time we see issues like this. E.g. the switch code in Attr seems to be doing way too much when we're in speculative mode.

I agree with Maurizio that we are probably doing too much in speculative mode in the switch. I wonder if this patch should be part of the code that will refactor what we are doing there. I would expect any refactoring of what it is done in the switch code to affect this current PR

That depends on what the merit of this PR is. There's a type that is inherently unsafe in javac (Symtab.unknownType) and leads to problems like StackOverflowError when sent to various methods on Types. This PR considers the specific problem with pattern matching switch just as a symptom of the problem with the type, and fixes the problem with the type. The intent in this PR is that any change to what is done during speculative attribution of pattern matching switches shouldn't affect it much.

Note that even though we don't know about any usecase right now where the problem would happen outside of speculative attribution, as long as Attr.PostAttrAnalyzer will set the unknownType to AST nodes, there's always a potential it will break unless the unknownType is fixed.

@vicente-romero-oracle

Copy link
Copy Markdown
Contributor

The approach seems good. I left some modelling questions. As you notice, I'm skeptical that just handling unknown types better is going to be the last time we see issues like this. E.g. the switch code in Attr seems to be doing way too much when we're in speculative mode.

I agree with Maurizio that we are probably doing too much in speculative mode in the switch. I wonder if this patch should be part of the code that will refactor what we are doing there. I would expect any refactoring of what it is done in the switch code to affect this current PR

That depends on what the merit of this PR is. There's a type that is inherently unsafe in javac (Symtab.unknownType) and leads to problems like StackOverflowError when sent to various methods on Types. This PR considers the specific problem with pattern matching switch just as a symptom of the problem with the type, and fixes the problem with the type. The intent in this PR is that any change to what is done during speculative attribution of pattern matching switches shouldn't affect it much.

Note that even though we don't know about any usecase right now where the problem would happen outside of speculative attribution, as long as Attr.PostAttrAnalyzer will set the unknownType to AST nodes, there's always a potential it will break unless the unknownType is fixed.

yep that makes sense to me we can revisit this issue later on if needed

@vicente-romero-oracle vicente-romero-oracle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm

@openjdk openjdk Bot removed the ready Pull request is ready to be integrated label Oct 2, 2024

@vicente-romero-oracle vicente-romero-oracle left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

looks good

@openjdk openjdk Bot added the ready Pull request is ready to be integrated label Oct 21, 2024
@lahodaj

lahodaj commented Oct 24, 2024

Copy link
Copy Markdown
Contributor Author

/integrate

@openjdk

openjdk Bot commented Oct 24, 2024

Copy link
Copy Markdown

Going to push as commit f0b130e.
Since your change was applied there have been 651 commits pushed to the master branch:

  • e96b4cf: 8342387: C2 SuperWord: refactor and improve compiler/loopopts/superword/TestDependencyOffsets.java
  • f7a61fc: 8342931: ProblemList failing tests from JDK-8335912
  • 25c2f48: 8338544: Dedicated Array class descriptor implementation
  • 158b93d: 8335912: Add an operation mode to the jar command when extracting to not overwriting existing files
  • 28d23ad: 8340177: Malformed system classes loaded by bootloader crash the JVM in product builds
  • 98403b7: 8342854: [JVMCI] Block secondary thread reporting a JVMCI fatal error
  • 9a7a850: 8341939: SigningOptionsTest fails without Xcode with command line developer tools after JDK-8341443
  • de92fe3: 8233451: (fs) Files.newInputStream() cannot be used with character special files
  • 002de86: 8342673: Test serviceability/jvmti/events/NotifyFramePopStressTest/NotifyFramePopStressTest.java failed: waited too long for notify
  • a21c558: 8342863: Use pattern matching for instanceof in equals methods of wrapper classes
  • ... and 641 more: https://git.openjdk.org/jdk/compare/a9bb04331df6788561921202cac73e35afbfe314...master

Your commit was automatically rebased without conflicts.

@openjdk openjdk Bot added the integrated Pull request has been integrated label Oct 24, 2024
@openjdk openjdk Bot closed this Oct 24, 2024
@openjdk openjdk Bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 24, 2024
@openjdk

openjdk Bot commented Oct 24, 2024

Copy link
Copy Markdown

@lahodaj Pushed as commit f0b130e.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compiler [email protected] integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

3 participants