Skip to content
Open
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 @@ -43,6 +43,12 @@
* you can use the method "<code>TupleExpression getTupleExpression()</code>" method.
* Calling either of these expression getters when the "isMultipleAssignment" condition
* is not appropriate is unsafe and will result in a <code>ClassCastException</code>.
* <p>
* JEP&nbsp;394 type patterns reuse this node as the right-hand side of
* {@code instanceof}: {@code e instanceof String s} is modelled as an
* {@code instanceof} {@link BinaryExpression} whose RHS is a
* {@code DeclarationExpression} with an {@link EmptyExpression} initializer.
* The pattern variable's scope is flow-sensitive (see GROOVY-12242).
*/
public class DeclarationExpression extends BinaryExpression {

Expand Down Expand Up @@ -130,6 +136,11 @@ public TupleExpression getTupleExpression() {
: null;
}

/**
* Returns the type of the declared variable (or of the tuple for a multiple
* assignment). For a JEP&nbsp;394 pattern used as the RHS of {@code instanceof},
* this is the pattern type {@code T} in {@code e instanceof T t}.
*/
@Override
public ClassNode getType() {
return (isMultipleAssignmentDeclaration() ? getTupleExpression() : getVariableExpression()).getType();
Expand Down
22 changes: 18 additions & 4 deletions src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.codehaus.groovy.ast.ClassHelper;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.ConstructorNode;
import org.codehaus.groovy.ast.DynamicVariable;
import org.codehaus.groovy.ast.FieldNode;
import org.codehaus.groovy.ast.GenericsType;
import org.codehaus.groovy.ast.InnerClassNode;
Expand Down Expand Up @@ -1648,7 +1649,7 @@
* {@inheritDoc}
*/
@Override
public void visitVariableExpression(final VariableExpression expression) {

Check failure on line 1652 in src/main/java/org/codehaus/groovy/classgen/AsmClassGenerator.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_groovy&issues=AZ_nziroIdg5QZyCFXHd&open=AZ_nziroIdg5QZyCFXHd&pullRequest=2773
CompileStack compileStack = controller.getCompileStack();

if (expression.isThisExpression()) {
Expand All @@ -1673,14 +1674,27 @@
return;
}

BytecodeVariable variable = compileStack.getVariable(expression.getName(), /*throwIfMissing*/false);
if (variable != null) {
controller.getOperandStack().loadOrStoreVariable(variable, expression.isUseReferenceDirectly());
} else {
// GROOVY-12242: VariableScopeVisitor marks out-of-scope pattern references
// as DynamicVariable. Name-based CompileStack lookup must not bypass that:
// pattern slots are allocated during condition evaluation and can still be
// present on the stack for short-circuit RHS of || (where the pattern is
// not definitely bound) or briefly on a non-live arm before hide. Treat
// DynamicVariable as property access even when a same-named slot exists.
if (expression.getAccessedVariable() instanceof DynamicVariable) {
PropertyExpression pexp = thisPropX(/*implicit-this*/true, expression.getName());
pexp.getProperty().setSourcePosition(expression);
pexp.copyNodeMetaData(expression);
pexp.visit(this);
} else {
BytecodeVariable variable = compileStack.getVariable(expression.getName(), /*throwIfMissing*/false);
if (variable != null) {
controller.getOperandStack().loadOrStoreVariable(variable, expression.isUseReferenceDirectly());
} else {
PropertyExpression pexp = thisPropX(/*implicit-this*/true, expression.getName());
pexp.getProperty().setSourcePosition(expression);
pexp.copyNodeMetaData(expression);
pexp.visit(this);
}
}

if (!compileStack.isLHS()) {
Expand Down
Loading
Loading