nitj: avoid dead code execution
[nit.git] / src / compiler / java_compiler.nit
index 50c9bc5..8f8faa3 100644 (file)
@@ -634,6 +634,14 @@ class JavaCompilerVisitor
        # Compile a statement (if any)
        fun stmt(nexpr: nullable AExpr) do
                if nexpr == null then return
+               if nexpr.mtype == null and not nexpr.is_typed then
+                       # Untyped expression.
+                       # Might mean dead code or invalid code
+                       # so aborts
+                       add_abort("FATAL: bad statement executed.")
+                       return
+               end
+
                var old = self.current_node
                current_node = nexpr
                nexpr.stmt(self)
@@ -651,6 +659,19 @@ class JavaCompilerVisitor
                        res = nexpr.expr(self)
                end
 
+               if res == null then
+                       # Untyped expression.
+                       # Might mean dead code or invalid code.
+                       # so aborts
+                       add_abort("FATAL: bad expression executed.")
+                       # and return a placebo result to please the C compiler
+                       if mtype == null then mtype = compiler.mainmodule.object_type
+                       res = null_instance
+
+                       self.current_node = old
+                       return res
+               end
+
                if mtype != null then
                        mtype = anchor(mtype)
                        res = autobox(res, mtype)