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

Support unnamed variables #1001

Merged
merged 1 commit into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3563,7 +3563,7 @@ int declareOne(
if (receiverExpression.isPresent()) {
scan(receiverExpression.get(), null);
} else {
visit(name);
variableName(name);
}
builder.op(op);
}
Expand Down Expand Up @@ -3606,6 +3606,10 @@ int declareOne(
return baseDims;
}

protected void variableName(Name name) {
visit(name);
}

private void maybeAddDims(Deque<List<? extends AnnotationTree>> annotations) {
maybeAddDims(new ArrayDeque<>(), annotations);
}
Expand Down Expand Up @@ -3696,7 +3700,7 @@ private void declareMany(List<VariableTree> fragments, Direction annotationDirec
builder.breakOp(" ");
builder.open(ZERO);
maybeAddDims(dims);
visit(fragment.getName());
variableName(fragment.getName());
maybeAddDims(dims);
ExpressionTree initializer = fragment.getInitializer();
if (initializer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.PatternCaseLabelTree;
import com.sun.source.tree.PatternTree;
import javax.lang.model.element.Name;

/**
* Extends {@link Java17InputAstVisitor} with support for AST nodes that were added or modified in
Expand Down Expand Up @@ -76,4 +77,13 @@ public Void visitDeconstructionPattern(DeconstructionPatternTree node, Void unus
token(")");
return null;
}

@Override
protected void variableName(Name name) {
if (name.isEmpty()) {
token("_");
} else {
visit(name);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ public class FormatterIntegrationTest {
.putAll(16, "I588")
.putAll(17, "I683", "I684", "I696")
.putAll(
21, "SwitchGuardClause", "SwitchRecord", "SwitchDouble", "SwitchUnderscore", "I880")
21,
"SwitchGuardClause",
"SwitchRecord",
"SwitchDouble",
"SwitchUnderscore",
"I880",
"Unnamed")
.build();

@Parameters(name = "{index}: {0}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class Unnamed {
{
int acc = 0;
for (Order _ : orders) {
if (acc < LIMIT) {
acc++;
}
}

for (int i = 0, _ = sideEffect(); i < 10; i++) { }

Queue<Integer> q = null;
while (q.size() >= 3) {
var x = q.remove();
var y = q.remove();
var _ = q.remove();
new Point(x, y);
}

while (q.size() >= 3) {
var x = q.remove();
var _ = q.remove();
var _ = q.remove();
new Point(x, 0) ;
}

String s = null;
try {
int i = Integer.parseInt(s);
} catch (NumberFormatException _) {
System.out.println("Bad number: " + s);
}

try { doSomething(); }
catch (Exception _) { doSomething(); }
catch (Throwable _) { doSomething(); }

try (var _ = ScopedContext.acquire()) {
doSomething();
}

stream.collect(Collectors.toMap(String::toUpperCase, _ -> "NODATA"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
class Unnamed {
{
int acc = 0;
for (Order _ : orders) {
if (acc < LIMIT) {
acc++;
}
}

for (int i = 0, _ = sideEffect(); i < 10; i++) {}

Queue<Integer> q = null;
while (q.size() >= 3) {
var x = q.remove();
var y = q.remove();
var _ = q.remove();
new Point(x, y);
}

while (q.size() >= 3) {
var x = q.remove();
var _ = q.remove();
var _ = q.remove();
new Point(x, 0);
}

String s = null;
try {
int i = Integer.parseInt(s);
} catch (NumberFormatException _) {
System.out.println("Bad number: " + s);
}

try {
doSomething();
} catch (Exception _) {
doSomething();
} catch (Throwable _) {
doSomething();
}

try (var _ = ScopedContext.acquire()) {
doSomething();
}

stream.collect(Collectors.toMap(String::toUpperCase, _ -> "NODATA"));
}
}
Loading