{Android} Optimization bug with constant variables

Monkey Forums/Monkey Bug Reports/{Android} Optimization bug with constant variables

ImmutableOctet(SKNG)(Posted 2014) [#1]
This bug happens when trying to compile using either of the current Android targets, and it seems to be an annoying trait of the JDK's Java compiler. If an expression used within an 'If' statement uses a constant, and the compiler evaluates it to true, that statement cannot contain a return statement with this/these targets (Unlike other targets). This behavior works just fine with the GLFW/Desktop target (Which produces nearly identical code), but since most C++ compilers aren't this paranoid, they'll just evaluate the path to the return point.

Needless to say, this is a small (But annoying) bug, which would force users to write inefficient code for the sake of portability. The worst part about this is that I already have a default return value, which means this is over-optimization. That being said, the 'If' statement itself should probably just be removed by 'Trans', that would solve the problem.

All testing was done using version 79(D) of Monkey X.

The compiler error-output:
[javac] Compiling 4 source files to DIRECTORYHERE\NAMEHERE.buildv79d\android_new\bin\classes
[javac] DIRECTORYHERE/NAMEHERE.buildv79d\android_new\src\com\ANDROIDLOCATIONHERE\MonkeyGame.java:6644: error: missing return statement[javac]    }
[javac]    ^


The associated Java output:
public final float p_ZPositionScalar(){
	if(true){
		return bb_math.g_Max2(p_ZPlane()/-100.0f,m_SmallestZPositionScalar)-1.0f;
	}
}


As you can see, the statement which states "if (true)" should not exist, and instead the contents of it should remain.

GLFW/Desktop output:
Float c_GameObject::p_ZPositionScalar(){
	if(true){
		return bb_math_Max2(p_ZPlane()/FLOAT(-100.0),m_SmallestZPositionScalar)-FLOAT(1.0);
	}
}


Original method written in Monkey (The variable named 'Lowest_ZPosition' is a constant variable):

Method ZPositionScalar:Float() Property
	If (Lowest_ZPosition <> 0.0) Then
		Return Max(ZPlane / Lowest_ZPosition, SmallestZPositionScalar) - 1.0
	Endif
		
	' Return the default response.
	Return 1.0
End


P.S. This is also a bug for me as well. (Asynchronous image loading incorrectly using a private method)


marksibly(Posted 2014) [#2]
Weird! This fails...

Function Test()
   If True Return 1
End


...but this works...
Function Test()
   While True Return 1
End


Apparently, Java's dead code analysis ignores 'if(true)' but not 'while(true)'. Dunno why, but I should be able to kludge around it.