Skip to content
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 @@ -1315,6 +1315,11 @@ private boolean isInlineCandidate(ImFunction f) {
// can wrap them in xpcall. Inlining removes the call site and loses the guard.
return false;
}
if (f.hasFlag(FunctionFlagEnum.RETURNS_INT_AS_REAL)) {
// Only Jass for a legacy target gets here (elsewhere it is a type error). Inlined, the integer it
// returns as a real would land in an operand, where `f(n) / m` divides integers.
return false;
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public enum FunctionFlagEnum implements FunctionFlag {
IS_COMPILETIME_NATIVE,
IS_EXTERN,
IS_VARARG,
PRESERVE_NAME
PRESERVE_NAME,
/** A Jass function which returns an integer as a real (legacy targets only); inlined, the integer would land in an operand. */
RETURNS_INT_AS_REAL

}
Original file line number Diff line number Diff line change
Expand Up @@ -1559,6 +1559,9 @@ public ImFunction getFuncFor(TranslatedToImFunction funcDef) {
if (funcDef2.attrHasAnnotation("test")) {
flags.add(IS_TEST);
}
if (returnsIntAsJassReal(funcDef2)) {
flags.add(RETURNS_INT_AS_REAL);
}
}

// Check if last parameter is vararg
Expand Down Expand Up @@ -1596,6 +1599,26 @@ public ImFunction getFuncFor(TranslatedToImFunction funcDef) {
return requested(f);
}

/** Whether a Jass function returns an integer as its real, which only a legacy target accepts. */
private static boolean returnsIntAsJassReal(FuncDef funcDef) {
if (!Utils.isJassCode(funcDef)) {
return false;
}
WurstType returnType = funcDef.attrReturnTyp();
boolean[] found = {false};
funcDef.getBody().accept(new de.peeeq.wurstscript.ast.Element.DefaultVisitor() {
@Override
public void visit(StmtReturn ret) {
super.visit(ret);
if (ret.getReturnedObj() instanceof Expr returned
&& WurstValidator.isIntReturnedAsJassReal(returned.attrTyp(), returnType, ret)) {
found[0] = true;
}
}
});
return found[0];
}

private ImClass getClassForFunc(TranslatedToImFunction funcDef) {
if (funcDef == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ public static WurstNativeType instance(NativeType definition, WurstType superTyp
return t;
}

/** The type at the end of the supertype chain: integer for {@code type myint extends integer}. */
public WurstType rootSupertype() {
WurstType type = this;
while (type instanceof WurstNativeType nativeType) {
type = nativeType.superType;
}
return type;
}

@Override
public ImType imTranslateType(ImTranslator tr) {
// Native types keep their source identity, including their supertypes: a selected subtype
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2592,6 +2592,24 @@ private boolean closureReturnsVoid(ExprStatementsBlock block) {
return abstractMethod != null && abstractMethod.getReturnType().isVoid();
}

/**
* Jass takes an integer where a real is stored or passed, but pjass rejects one returned from a function
* declared to return a real. Inlining would hide that return from pjass and leave the integer in an
* operand, where {@code f(n) / m} is the integer division {@code n / m}. A legacy target accepts it, so the
* translation keeps such a function a call ({@code FunctionFlagEnum.RETURNS_INT_AS_REAL}).
*/
public static boolean isIntReturnedAsJassReal(WurstType returnedType, WurstType returnType, StmtReturn s) {
WurstType returned = rootOfNativeType(returnedType);
return rootOfNativeType(returnType) instanceof WurstTypeReal
&& (returned instanceof WurstTypeInt || returned instanceof WurstTypeIntLiteral)
&& Utils.isJassCode(s);
}

/** A Jass {@code type myint extends integer} is an integer here, as it is to pjass. */
private static WurstType rootOfNativeType(WurstType type) {
return type instanceof WurstNativeType nativeType ? nativeType.rootSupertype() : type;
}

private void checkReturnInFunc(StmtReturn s, FunctionImplementation func) {
WurstType returnType = func.attrReturnTyp();
if (s.getReturnedObj() instanceof Expr) {
Expand All @@ -2600,6 +2618,16 @@ private void checkReturnInFunc(StmtReturn s, FunctionImplementation func) {
s.addError("Cannot return a value from a function which returns nothing");
} else {
WurstType returnedType = returned.attrTyp();
if (isIntReturnedAsJassReal(returnedType, returnType, s)) {
String message = "Cannot convert returned value from integer to real: "
+ "a function which returns real must return a real, for example I2R(...).";
if (legacyJassTypeChecks) {
s.addWarning(message);
} else {
s.addError(message);
}
return;
}
if (!returnedType.isSubtypeOf(returnType, s)) {
// NEW: Allow returning null for generic type parameters
if (returnedType instanceof WurstTypeNull &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,113 @@ public void jassReturnTypeMismatchIsErrorByDefault() {
);
}

/**
* pjass rejects an integer returned from a function declared to return a real, so it is an error in Jass as
* in Wurst. Accepted, it would compile only where the call is inlined, and there `f(n) / m` becomes the
* integer division `n / m`.
*/
@Test
public void jassIntReturnedFromRealFunctionIsError() {
test()
.setStopOnFirstError(false)
.executeProg(false)
.expectError("Cannot convert returned value from integer to real")
.compilationUnits(
compilationUnit("input.j",
"function jassToReal takes integer n returns real",
" return n",
"endfunction",
"function jassDivReturn takes integer n, integer m returns real",
" return jassToReal(n) / m",
"endfunction"
)
);
}

/** pjass rejects an integer literal there too. */
@Test
public void jassIntLiteralReturnedFromRealFunctionIsError() {
test()
.setStopOnFirstError(false)
.executeProg(false)
.expectError("Cannot convert returned value from integer to real")
.compilationUnits(
compilationUnit("input.j",
"function jassZero takes nothing returns real",
" return 0",
"endfunction"
)
);
}

/** A type derived from integer is an integer to pjass, returned as a real. */
@Test
public void jassDerivedIntReturnedFromRealFunctionIsError() {
test()
.setStopOnFirstError(false)
.executeProg(false)
.expectError("Cannot convert returned value from integer to real")
.compilationUnits(
compilationUnit("input.j",
"type myint extends integer",
"function jassMyToReal takes myint n returns real",
" return n",
"endfunction"
)
);
}

/** And a type derived from real is a real to pjass, returned from. */
@Test
public void jassIntReturnedFromDerivedRealFunctionIsError() {
test()
.setStopOnFirstError(false)
.executeProg(false)
.expectError("Cannot convert returned value from integer to real")
.compilationUnits(
compilationUnit("input.j",
"type myreal extends real",
"function jassToMyReal takes integer n returns myreal",
" return n",
"endfunction"
)
);
}

/** For a legacy target the integer return is a warning, like the other Jass return mismatches. */
@Test
public void jassIntReturnedFromRealFunctionIsWarningForLegacyTarget() {
test()
.setStopOnFirstError(false)
.executeProg(false)
.legacyJassTypeChecks()
.expectWarning("Cannot convert returned value from integer to real")
.compilationUnits(
compilationUnit("input.j",
"function jassToReal takes integer n returns real",
" return n",
"endfunction"
)
);
}

/** An integer stored into or passed as a real stays valid Jass, as pjass has it. */
@Test
public void jassIntStoredOrPassedAsRealIsOk() {
testAssertOk(false, false,
compilationUnit("input.j",
"function jassHalf takes real x returns real",
" return x / 2",
"endfunction",
"function jassToReal takes integer n returns real",
" local real r = n",
" set r = jassHalf(n) + r",
" return r",
"endfunction"
)
);
}

/** The same return-type mismatch in user Wurst code must always be an error. */
@Test
public void wurstReturnTypeMismatchIsError() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1923,6 +1923,69 @@ public void constantFolding() {
);
}

/**
* A legacy target accepts an integer returned from a real Jass function (with a warning). The call is where
* the integer becomes the declared real, so it stays a call: inlined, `f(n) / m` would divide integers.
*/
@Test
public void inlinerKeepsALegacyIntReturnedAsRealACall() throws IOException {
test().setStopOnFirstError(false).executeProg(false).legacyJassTypeChecks()
.expectWarning("Cannot convert returned value from integer to real")
.compilationUnits(
compilationUnit("input.j",
"function jassToReal takes integer n returns real",
"\treturn n",
"endfunction",
"function jassDivReturn takes integer n, integer m returns real",
"\treturn jassToReal(n) / m",
"endfunction"),
compilationUnit("test.wurst",
"package test",
"native testSuccess()",
"int array arr",
"init",
" arr[0] = 7",
" arr[1] = 2",
" if jassDivReturn(arr[0], arr[1]) > 3.4",
" testSuccess()",
"endpackage"));
for (String variant : new String[]{"_inl.j", "_inlopt.j"}) {
String out = Files.toString(new File("test-output/OptimizerTests_inlinerKeepsALegacyIntReturnedAsRealACall"
+ variant), Charsets.UTF_8);
assertTrue(out.contains("jassToReal("), variant + ":\n" + out);
}
}

/** The same with a type derived from integer, which the IM names only by its own name. */
@Test
public void inlinerKeepsALegacyDerivedIntReturnedAsRealACall() throws IOException {
test().setStopOnFirstError(false).executeProg(false).legacyJassTypeChecks()
.expectWarning("Cannot convert returned value from integer to real")
.compilationUnits(
compilationUnit("input.j",
"type myint extends integer",
"function jassMyToReal takes myint n returns real",
"\treturn n",
"endfunction",
"function jassDivMy takes myint n, integer m returns real",
"\treturn jassMyToReal(n) / m",
"endfunction"),
compilationUnit("test.wurst",
"package test",
"native testSuccess()",
"int array arr",
"init",
" arr[1] = 2",
" if jassDivMy(null, arr[1]) > -1.",
" testSuccess()",
"endpackage"));
for (String variant : new String[]{"_inl.j", "_inlopt.j"}) {
String out = Files.toString(new File("test-output/OptimizerTests_inlinerKeepsALegacyDerivedIntReturnedAsRealACall"
+ variant), Charsets.UTF_8);
assertTrue(out.contains("jassMyToReal("), variant + ":\n" + out);
}
}

@Test
public void inlinerIntRealsConstantFolding() {
testAssertOkLines(true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,12 @@ private CompilationResult testScript() {

testWithInliningAndOptimizationsAndStacktraces(name, executeProg, executeTests, gui, compiler, model, executeProgOnlyAfterTransforms, runArgs);

runPendingPjass();
if (legacyJassTypeChecks) {
// a legacy target is not checked with pjass, as in a build
pjassPending.clear();
} else {
runPendingPjass();
}
}

if (testLua) {
Expand Down
Loading