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

[operators]feat: add final keyword check on assignment expr #366

Merged
merged 4 commits into from
Oct 6, 2020
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 @@ -15,6 +15,7 @@
package com.google.api.generator.engine.ast;

import com.google.auto.value.AutoValue;
import com.google.common.base.Preconditions;

@AutoValue
public abstract class AssignmentExpr implements Expr {
Expand Down Expand Up @@ -67,6 +68,15 @@ public AssignmentExpr build() {
lhsType.reference().name(), rhsType.reference().name()));
}
}

if (!assignmentExpr.variableExpr().isDecl()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What if the decl is final too? That is, I think we can remove the outer check.

Copy link
Contributor

Choose a reason for hiding this comment

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

Never mind, that would apply only on the general for statement.

Preconditions.checkState(
!assignmentExpr.variableExpr().isFinal(),
String.format(
"Cannot assign a value to final variable '%s'.",
assignmentExpr.variableExpr().variable().name()));
}

return assignmentExpr;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,32 @@ public void writeAssignmentExpr_invalidBoxedPrimitiveType() {
assertInvalidAssignmentExpr(lVariableExpr, rVariableExpr);
}

@Test
public void writeAssignmentExpr_validIsDeclFinalVariableExpr() {
Variable lVariable = Variable.builder().setName("x").setType(TypeNode.INT).build();
VariableExpr lVariableExpr =
VariableExpr.builder().setVariable(lVariable).setIsDecl(true).setIsFinal(true).build();

ValueExpr valueExpr =
ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("0").build());
assertValidAssignmentExpr(lVariableExpr, valueExpr);
}

@Test
public void writeAssignmentExpr_invalidIsNotDeclFinalVariableExpr() {
Variable lVariable = Variable.builder().setName("x").setType(TypeNode.INT).build();
VariableExpr lVariableExpr =
VariableExpr.builder().setVariable(lVariable).setIsDecl(false).setIsFinal(true).build();

ValueExpr valueExpr =
ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("0").build());
assertThrows(
IllegalStateException.class,
() -> {
AssignmentExpr.builder().setVariableExpr(lVariableExpr).setValueExpr(valueExpr).build();
});
}

private static void assertInvalidAssignmentExpr(VariableExpr variableExpr, Expr valueExpr) {
assertThrows(
TypeMismatchException.class,
Expand Down