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

IdentifierName: support JDK 22 unnamed variables #4848

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

commonquail
Copy link

@commonquail commonquail commented Feb 26, 2025

This PR nominally fixes #4847 but it is perhaps more illustrative than usable.

@commonquail commonquail force-pushed the identifiername-accept-unnamed branch 2 times, most recently from afc0184 to cc90fe1 Compare February 26, 2025 18:12

@Test
public void unnamedVariables() {
assume().that(Runtime.version().feature()).isAtLeast(22);
Copy link
Author

Choose a reason for hiding this comment

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

Or 21 and --enable-preview --release n

@@ -278,6 +278,9 @@ private static boolean isConformant(Symbol symbol, String name) {
if (isStaticVariable(symbol) && isConformantStaticVariableName(name)) {
return true;
}
if (name.isEmpty()) {
Copy link
Author

Choose a reason for hiding this comment

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

isConformant is also called for methods. That seems ok; javac does not accept an absent method name and rejects the meta-name _ with

/Test.java:5: error: underscore not allowed here
  void _() {

and if, improbably, that ever did change, presumably an unnamed method should be treated no differently from an unnamed variable.

A more difficult question is whether something without a name can "conform" to a naming scheme. It seems more like asking the question in the first place is nonsensical. So perhaps this check should be at a higher level inside the checker, and perhaps it should be more explicitly isUnnamed(name).

Copy link
Member

Choose a reason for hiding this comment

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

A more difficult question is whether something without a name can "conform" to a naming scheme.

I think most definitions you'd give of, say, lowerCamelCase would pass for the empty string, so I think the answer here is just "yes".

Function<String, String> f = _ -> "bar";
String _ = f.apply("foo");
}
catch (Exception _) {}
Copy link
Author

Choose a reason for hiding this comment

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

Possibly all these are excessive compared to just

int _ = 1;

@graememorgan
Copy link
Member

Thanks for the fix!

@graememorgan graememorgan self-assigned this Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IdentifierName: JEP 456 unnamed variable
2 participants