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

[integrate]feat: integrate return into method definition #369

Merged
merged 3 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -66,7 +66,7 @@ public abstract class MethodDefinition implements AstNode {
abstract ImmutableList<String> returnTemplateNames();

@Nullable
public abstract Expr returnExpr();
public abstract ReturnExpr returnExpr();

abstract boolean isOverride();

Expand Down Expand Up @@ -147,7 +147,11 @@ public Builder setArguments(VariableExpr... arguments) {

public abstract Builder setBody(List<Statement> body);

public abstract Builder setReturnExpr(Expr returnExpr);
public Builder setReturnExpr(Expr expr) {
return setReturnExpr(ReturnExpr.withExpr(expr));
}

public abstract Builder setReturnExpr(ReturnExpr returnExpr);

public abstract Builder setIsOverride(boolean isOverride);

Expand Down Expand Up @@ -292,18 +296,14 @@ public MethodDefinition build() {
}

if (method.returnExpr() != null && !isLastStatementThrowExpr) {
// TODO(miraleung): Refactor this to use ReturnExpr under the covers instead.
Preconditions.checkState(
!(method.returnExpr() instanceof ReturnExpr),
"A method's return expression can only consist of non-ReturnExpr expressions");
if (method.returnType().isPrimitiveType()
|| method.returnExpr().type().isPrimitiveType()) {
|| method.returnExpr().expr().type().isPrimitiveType()) {
Preconditions.checkState(
method.returnType().equals((method.returnExpr().type())),
method.returnType().equals((method.returnExpr().expr().type())),
"Method return type does not match the return expression type");
} else {
Preconditions.checkState(
method.returnType().isSupertypeOrEquals(method.returnExpr().type()),
method.returnType().isSupertypeOrEquals(method.returnExpr().expr().type()),
"Method reference return type is not a supertype of the return expression type");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,11 +767,7 @@ public void visit(MethodDefinition methodDefinition) {
newline();
statements(methodDefinition.body());
if (methodDefinition.returnExpr() != null) {
buffer.append(RETURN);
space();
methodDefinition.returnExpr().accept(this);
semicolon();
newline();
ExprStatement.withExpr(methodDefinition.returnExpr()).accept(this);
}

rightBrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,21 @@ public void validMethodDefinition_boxedReturnType() {
.build();
}

@Test
public void validMethodDefinition_setReturnExprUsingReturnExpr() {
ReturnExpr returnExpr =
ReturnExpr.withExpr(
ValueExpr.withValue(
PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build()));
MethodDefinition.builder()
.setName("foobar")
.setScope(ScopeNode.PUBLIC)
.setReturnType(TypeNode.INT_OBJECT)
.setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr())))
.setReturnExpr(returnExpr)
.build();
}

@Test
public void invalidMethodDefinition_badTemplateName() {
assertThrows(
Expand Down