Skip to content

Commit

Permalink
added error for calling static method on object
Browse files Browse the repository at this point in the history
fixes #750
  • Loading branch information
peq committed Nov 8, 2018
1 parent 2bd09f7 commit 0546e7c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,21 @@ public static OptExpr getImplicitParameter(ExprVarArrayAccess e) {


public static OptExpr getImplicitParameter(ExprFunctionCall e) {
return getImplicitParamterCaseNormalFunctionCall(e);
return getImplicitParameterCaseNormalFunctionCall(e);
}

public static OptExpr getImplicitParameter(ExprMemberMethod e) {
Expr result = getImplicitParameterUsingLeft(e);
if (result == null) {
return getImplicitParamterCaseNormalFunctionCall(e);
return getImplicitParameterCaseNormalFunctionCall(e);
} else {
FuncLink calledFunc = e.attrFuncLink();
if (calledFunc != null
&& !calledFunc.getDef().attrIsDynamicClassMember()
&& !(calledFunc.getDef() instanceof ExtensionFuncDef)) {
e.addError("Cannot call static method " + e.getFuncName() + " on an object.");
}

return result;
}
}
Expand All @@ -58,7 +65,7 @@ public static OptExpr getImplicitParameter(ExprMemberMethod e) {
return e.getLeft();
}

private static OptExpr getImplicitParamterCaseNormalFunctionCall(FunctionCall e) {
private static OptExpr getImplicitParameterCaseNormalFunctionCall(FunctionCall e) {
FuncLink calledFunc = e.attrFuncLink();
if (calledFunc == null) {
return Ast.NoExpr();
Expand All @@ -80,6 +87,7 @@ private static OptExpr getImplicitParamterCaseNormalFunctionCall(FunctionCall e)
return Ast.NoExpr();
}
} else {

// static function:
return Ast.NoExpr();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1183,5 +1183,17 @@ public void protectedInOtherPackage2() {
);
}

@Test
public void callStaticFunctionFromInit() {
testAssertErrorsLines(true, "Cannot call static method f on an object.",
"package Test",
"public class A",
" static function f()",
"init",
" new A().f()",
"endpackage"
);
}


}

0 comments on commit 0546e7c

Please sign in to comment.