@@ -70,29 +70,52 @@ public static String generate(CompileFramework comp) {
7070 var withConstantsTemplate = Template .make ("expression" , (Expression expression ) -> {
7171 // Create a token: fill the expression with a fixed set of constants.
7272 // We then use the same token with the same constants, once compiled and once not compiled.
73+ //
74+ // Some expressions can throw Exceptions. We have to catch them. In such a case, we return
75+ // the Exception instead of the value from the expression, and compare the Exceptions.
76+ //
77+ // Some Expressions do not have a deterministic result. For example, different NaN or
78+ // precision results from some operators. We only compare the results if we know that the
79+ // result is deterministically the same.
7380 TemplateToken expressionToken = expression .asToken (expression .argumentTypes .stream ().map (t -> t .con ()).toList ());
7481 return scope (
7582 let ("returnType" , expression .returnType ),
7683 """
7784 @Test
7885 public static void $primitiveConTest() {
79- #returnType v0 = ${primitiveConTest}_compiled();
80- #returnType v1 = ${primitiveConTest}_reference();
81- Verify.checkEQ(v0, v1);
86+ Object v0 = ${primitiveConTest}_compiled();
87+ Object v1 = ${primitiveConTest}_reference();
88+ """ ,
89+ expression .info .isResultDeterministic ? "Verify.checkEQ(v0, v1);\n " : "" ,
90+ """
8291 }
8392
8493 @DontInline
85- public static #returnType ${primitiveConTest}_compiled() {
94+ public static Object ${primitiveConTest}_compiled() {
95+ try {
8696 """ ,
87- "return " , expressionToken , ";\n " ,
97+ "return " , expressionToken , ";\n " ,
98+ expression .info .exceptions .stream ().map (exception ->
99+ "} catch (" + exception + " e) { return e;\n "
100+ ).toList (),
88101 """
102+ } finally {
103+ // Just so that javac is happy if there are no exceptions to catch.
104+ }
89105 }
90106
91107 @DontCompile
92- public static #returnType ${primitiveConTest}_reference() {
108+ public static Object ${primitiveConTest}_reference() {
109+ try {
93110 """ ,
94- "return " , expressionToken , ";\n " ,
111+ "return " , expressionToken , ";\n " ,
112+ expression .info .exceptions .stream ().map (exception ->
113+ "} catch (" + exception + " e) { return e;\n "
114+ ).toList (),
95115 """
116+ } finally {
117+ // Just so that javac is happy if there are no exceptions to catch.
118+ }
96119 }
97120 """
98121 );
0 commit comments