Skip to content
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

8261205: AssertionError: Cannot add metadata to an intersection type #4095

Closed

Conversation

vicente-romero-oracle
Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle commented May 18, 2021

Please review this PR which is adding a field to JCVariableDecl, the need I see for this is that when we declare a local variable using var as in:

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

@Target({ElementType.TYPE_USE, ElementType.LOCAL_VARIABLE})
@interface A {}

class Test {
    void t() {
        @A var c = g(1, 1L);
    }

    <X> X g(X a, X b) {
        return a;
    }
}

the only clue the compiler has to know that the local variable was declared using var is that field JCVariableDecl::vartype is null, but this field is set to an inferred type at some point during the compilation and from that point on there is no way to know for sure that the original AST had a var or not. Unfortunately there is code that keeps asking if the local variable was implicitly typed or not, after field JCVariableDecl::vartype has been written, and thus making uninformed decisions as in: TypeAnnotations.TypeAnnotationPositions::visitVarDef, where the compiler is implementing a portion of section: 9.7.4 Where Annotations May Appear of the spec (JLS16). In particular the portion that defines how to deal with annotations applied to local variables declared with var.

The test case above is interesting because the same annotation works as a declaration annotation and as a type annotation. If it were only a type annotation then the compiler would have issued an error before getting to TypeAnnotations.TypeAnnotationPositions::visitVarDef, precisely at Check::validateAnnotation, see that Check::getApplicableTargets has a special case for local variables declared using var. But again given that the annotation in the example above is applicable as a type and as a declaration annotation, it will seep down to the metadata info associated to the local variable symbol, and that is fine. What is not kosher is that as the compiler has no clue that the AST was originally declared using var that it then tries to type-annotate the type of the local variable. This change is thus making sure that that information is preserved.

This patch is also renaming a method in JCVariableDecl, its previous name was isImplicitlyTyped which was basically checking if the vartype field was null and was renamed to nullVarType, hasNullVarType could be an option too, and a new method was added: declaredUsingVar which won't change its returned value during the lifetime of a JCVariableDecl AST.

TIA


Progress

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

Issue

  • JDK-8261205: AssertionError: Cannot add metadata to an intersection type

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 4095

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

Using diff file

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

@bridgekeeper
Copy link

bridgekeeper bot commented May 18, 2021

👋 Welcome back vromero! 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 added the rfr Pull request is ready for review label May 18, 2021
@openjdk
Copy link

openjdk bot commented May 18, 2021

@vicente-romero-oracle 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.

@openjdk openjdk bot added the compiler compiler-dev@openjdk.org label May 18, 2021
@mlbridge
Copy link

mlbridge bot commented May 18, 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.

What happens if we just use declaredUsingVar everywhere? Shouldn't that be the "right" way to do it?

@vicente-romero-oracle
Copy link
Contributor Author

vicente-romero-oracle commented May 18, 2021

What happens if we just use declaredUsingVar everywhere? Shouldn't that be the "right" way to do it?

yep I also considered that but then realized that parameters of implicit lambdas also have its vartype set to null until the compiler infers a type for them, so the implicitly-typed set and the declared using var set have a non-empty intersection but they are not identical, so I preferred to keep the previous API, just with a tweak in the method name, and add a new method to reflect the fact that there is a difference between being explicitly typed and being declared using var.

So thinking again about the naming I think that I can let the method JCVarDeclaration::isImplicitlyTyped untouched and just add the new method declaredUsingVar or with name isDeclaredUsingVar, what do you think?

@vicente-romero-oracle
Copy link
Contributor Author

I have uploaded a new commit, after an offline comment from Maurizio, he is correct that: given that lambda parameters also have a null type, they will be classified as "declared with var". This commit gets back to the original method name JCVarDeclaration::isImplicitlyTyped which will be true for local variables declared with var and for parameters of implicit lambdas.

@@ -44,7 +44,6 @@
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Kinds.Kind.*;
import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
import static com.sun.tools.javac.tree.JCTree.Tag.VARDEF;
Copy link
Contributor Author

@vicente-romero-oracle vicente-romero-oracle May 18, 2021

Choose a reason for hiding this comment

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

the import removal changes have been introduced by intelliJ

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 better - one question: what about var with annotations and lambda parameters? E.g. you can probably construct a similar example involving a lambda parameter - is this fix enough, or would the code path be slightly different?

@vicente-romero-oracle
Copy link
Contributor Author

Looks better - one question: what about var with annotations and lambda parameters? E.g. you can probably construct a similar example involving a lambda parameter - is this fix enough, or would the code path be slightly different?

good catch! I have modified the patch to include the case for lambda parameters declared with var, I also removed the initial test and added a new one that not only makes sure that the code compiles, it also checks that no type annotations are represented in the class file

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 - but I think there's an issue in the test?


class Test {
void kaa() {
//@A var c = g(1, 1L);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this meant to be commented out?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ok, I see this is fixed now

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yep, thanks for the review

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.

Thumbs up!

@openjdk
Copy link

openjdk bot commented May 20, 2021

@vicente-romero-oracle 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:

8261205: AssertionError: Cannot add metadata to an intersection type

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 no new commits pushed to the master branch. If another commit should be pushed before you perform the /integrate command, your PR will be automatically rebased. If you prefer to avoid any potential 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 ready Pull request is ready to be integrated label May 20, 2021
@vicente-romero-oracle
Copy link
Contributor Author

/integrate

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

openjdk bot commented May 20, 2021

@vicente-romero-oracle Since your change was applied there have been 10 commits pushed to the master branch:

  • 7b98400: 8267348: Rewrite gc/epsilon/TestClasses.java to use Metaspace with less classes
  • ca93399: 8267235: [macos_aarch64] InterpreterRuntime::throw_pending_exception messing up LR results in crash
  • b7b6acd: 8267481: Make sure table row has correct number of cells
  • f67847f: 8267396: Avoid recording "pc" in unhandled oops detector for better performance
  • 878d1b3: 8267434: Remove LinkOutput[Impl]
  • fc7f0a3: 8267480: Explicitly problemlist all runtime/os/TestTracePageSizes.java tests on linux-aarch64 to reduce noise
  • 9425d3d: 8261880: Change nested classes in java.base to static nested classes where possible
  • 459abd5: 8267219: Javadoc method summary breaks when {@inheritdoc} from an empty parent
  • aba2265: 8260267: ZGC: Reduce mark stack usage
  • f979523: 8267463: Problemlist runtime/os/TestTracePageSizes.java on linux-aarch64 to reduce noise

Your commit was automatically rebased without conflicts.

Pushed as commit 81f39ed.

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

@vicente-romero-oracle vicente-romero-oracle deleted the JDK-8261205 branch May 20, 2021 17:55
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