Skip to content

Conversation

vicente-romero-oracle
Copy link
Contributor

@vicente-romero-oracle vicente-romero-oracle commented Apr 18, 2025

This PR is defining a new internal annotation, @jdk.internal.RequiresIdentity, with target types PARAMETER and TYPE_PARAMETER. The @RequiresIdentity annotation expresses the expectation that an argument to a given method or constructor parameter will be an object with a unique identity, not an instance of a value-based class; or that the type argument to a given type parameter will not be a value-based class type.

For more details please refer to the complete description in the corresponding JIRA entry [1]

TIA

[1] https://bugs.openjdk.org/browse/JDK-8354556


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
  • Change requires CSR request JDK-8356539 to be approved

Issues

  • JDK-8354556: Expand value-based class warnings to java.lang.ref API (Enhancement - P4)
  • JDK-8356539: Expand value-based class warnings to java.lang.ref API (CSR)

Reviewers

Contributors

  • Archie Cobbs <acobbs@openjdk.org>

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 24746

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Apr 18, 2025

👋 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
Copy link

openjdk bot commented Apr 18, 2025

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

8354556: Expand value-based class warnings to java.lang.ref API

Co-authored-by: Archie Cobbs <acobbs@openjdk.org>
Reviewed-by: jlahoda

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

  • 6a58af3: 8357143: New test AOTCodeCompressedOopsTest.java fails on platforms without AOT Code Cache support
  • 84a98ab: 8357166: Many AOT tests failed with VM crash
  • fbc12be: 8349151: Refactor test/java/security/cert/CertificateFactory/slowstream.sh to java test

Please see this link for an up-to-date comparison between the source branch of this pull request and 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
Copy link

openjdk bot commented Apr 18, 2025

@vicente-romero-oracle The following labels will be automatically applied to this pull request:

  • build
  • compiler
  • core-libs

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 build build-dev@openjdk.org core-libs core-libs-dev@openjdk.org compiler compiler-dev@openjdk.org labels Apr 18, 2025
Copy link
Member

Choose a reason for hiding this comment

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

Need to add handling of RequiresIdentity in createAnnotation

Copy link
Contributor Author

Choose a reason for hiding this comment

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

update on CreateSymbols, the complete implementation with the changes for CreateSymbols should be included in the PR for [1]. Which will be developed in parallel to this proposal

[1] https://bugs.openjdk.org/browse/JDK-8356894

@@ -4249,6 +4249,14 @@ compiler.warn.declared.using.preview=\
compiler.warn.attempt.to.synchronize.on.instance.of.value.based.class=\
attempt to synchronize on an instance of a value-based class

# lint: identity
compiler.warn.attempt.to.synchronize.on.instance.of.value.based.class2=\
Copy link
Member

Choose a reason for hiding this comment

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

How is this different from the message with no 2 suffix? Is there a reason why one localization entry is required for one type of lint?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the requirement is for the identity lint warning to be an alias of the synchronization warning

@openjdk
Copy link

openjdk bot commented Apr 24, 2025

⚠️ @vicente-romero-oracle This pull request contains merges that bring in commits not present in the target repository. Since this is not a "merge style" pull request, these changes will be squashed when this pull request in integrated. If this is your intention, then please ignore this message. If you want to preserve the commit structure, you must change the title of this pull request to Merge <project>:<branch> where <project> is the name of another project in the OpenJDK organization (for example Merge jdk:master).

@vicente-romero-oracle vicente-romero-oracle marked this pull request as ready for review May 1, 2025 15:01
@archiecobbs
Copy link
Contributor

FYI, this issue now effectively fixes JDK-7004476 Lint.LintCategory should support aliases, which happened to be closed as "Rejected" on Monday. Not sure if anything needs to be done.

@vicente-romero-oracle
Copy link
Contributor Author

FYI, this issue now effectively fixes JDK-7004476 Lint.LintCategory should support aliases, which happened to be closed as "Rejected" on Monday. Not sure if anything needs to be done.

I guess we can add a note to that bug once this one goes through

@kimbarrett
Copy link

I'm coming here from "far away" and am not familiar with all the work leading
up to this proposal, so maybe there's some rationale that I don't know about.
(I did look, but didn't find anything other than this draft JEP:
https://openjdk.org/jeps/8340476. And that doesn't really deal with the issue
I discuss below.)

Keeping that in mind, I'm surprised by some parts of this change. Specifically

  1. Apply the annotation to the following:
  • The referent parameter of all constructors in classes PhantomReference, SoftReference, and WeakReference
    ...
  • The type parameter T of Reference, PhantomReference, SoftReference, WeakReference, and ReferenceQueue

I wonder why it is not sufficient to apply the annotation to the type
parameter T of Reference, and leave it at that?

The type parameter of the subclasses is passed as the type parameter to
Reference in the extends clause. I would expect if the type parameter for a
subclass didn't satisfy the annotation that would be noticed there. Indeed, if
that isn't the case then many uses of Reference types won't benefit from this
annotation, since they are using classes derived from one of those subclasses
and won't have this internal annotation. That would significantly limit the
utility of this annotation in this area.

Similarly, I wonder why the constructors need the annotation. And why don't
the return type of get or the argument for Reference::refersTo need it?
(Maybe because those values can be null? But there's nothing that prevents
passing null to one of the constructors. It's not very useful to do so, but
not forbidden. I've even seen a use-case for doing so.)

@@ -5667,4 +5668,193 @@ private <E extends Element> Void runUnderLint(E symbol, JCClassDecl p, BiConsume

}

void checkRequiresIdentity(JCTree tree, Lint lint) {
switch (tree) {
case JCClassDecl classDecl : {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit - unless fall-through is needed (it does not seem to be, on the first sight), I would suggest to use -> instead of :, esp. given the cases use blocks anyway.

Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

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

javac changes look overall good to me. One question and some comments for consideration inline.

@@ -2672,6 +2670,9 @@ public void visitApply(JCMethodInvocation tree) {
Type capturedRes = resultInfo.checkContext.inferenceContext().cachedCapture(tree, restype, true);
result = check(tree, capturedRes, KindSelector.VAL, resultInfo);
}
if (env.info.lint.isEnabled(LintCategory.IDENTITY)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is the only place where there's a check whether the lint is enabled before the call to checkRequiresIdentity. Is there a reason for that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

left over of the initial approach I guess. I will remove it

@@ -5667,4 +5668,193 @@ private <E extends Element> Void runUnderLint(E symbol, JCClassDecl p, BiConsume

}

void checkRequiresIdentity(JCTree tree, Lint lint) {
Copy link
Contributor

Choose a reason for hiding this comment

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

For consideration: as far as I can see, we have a sharp(er) type when we call checkRequiresIdentity, but we give up the type, and re-instante it here using the pattern matching switch. I wonder if it would be more elegant (and hopefully not really too much longer) if the checkRequiresIdentity method would have multiple overloads, with the sharp(er) types, like JCClassDecl/JCVariableDecl, etc.

Or is there a reason to given up the sharp(er) type and re-create it using the switch?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I guess the motivation was to have all this related code in one place, if this code is split into several overloaded methods then I guess an entity, a class, should probably be defined to contain all this code. Dunno I guess the current solution seemed more self-contained to me

if (t != null && t.tsym != null) {
SymbolMetadata sm = t.tsym.getMetadata();
if (sm != null && !t.getTypeArguments().isEmpty()) {
for (Attribute.TypeCompound ta: sm.getTypeAttributes().stream()
Copy link
Contributor

Choose a reason for hiding this comment

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

The code here, and the code in checkIfTypeParamsRequiresIdentity look similar a lot (although they manipulate List<Type> and List<JCExpression>, of course. I wonder if there's a chance to share the code, at least partially.

Copy link
Contributor Author

@vicente-romero-oracle vicente-romero-oracle May 15, 2025

Choose a reason for hiding this comment

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

I have rewritten both using streams, technically we could still reuse some code but not a big amount of it, dunno

@openjdk openjdk bot removed the csr Pull request needs approved CSR before integration label May 15, 2025
Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

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

javac changes look good to me, with one trivial nit in Check.

CompilerProperties.LintWarnings.AttemptToUseValueBasedWhereIdentityExpected));
}
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit - there appears to be an extra space at the beginning of this line.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 19, 2025
@@ -304,13 +304,18 @@ public void createSymbols(String ctDescriptionFileExtra, String ctDescriptionFil
"Ljdk/internal/ValueBased;";
private static final String VALUE_BASED_ANNOTATION_INTERNAL =
"Ljdk/internal/ValueBased+Annotation;";
private static final String REQUIRES_IDENTITY_ANNOTATION =
"Ljdk/internal/RequieresIdentity;";
Copy link

Choose a reason for hiding this comment

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

typo: RequieresIdentity -> RequiresIdentity

@openjdk openjdk bot removed the ready Pull request is ready to be integrated label May 19, 2025
Copy link
Contributor

@lahodaj lahodaj left a comment

Choose a reason for hiding this comment

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

javac changes look good to me.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label May 19, 2025
@vicente-romero-oracle
Copy link
Contributor Author

vicente-romero-oracle commented May 19, 2025

thanks for the reviews, rerunning tests before pushing

@vicente-romero-oracle
Copy link
Contributor Author

/integrate

@vicente-romero-oracle
Copy link
Contributor Author

thanks guys for the reviews and contributions

@openjdk
Copy link

openjdk bot commented May 19, 2025

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

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label May 19, 2025
@openjdk openjdk bot closed this May 19, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels May 19, 2025
@openjdk
Copy link

openjdk bot commented May 19, 2025

@vicente-romero-oracle Pushed as commit 637e9d1.

💡 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 compiler-dev@openjdk.org core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated
Development

Successfully merging this pull request may close these issues.

8 participants