nitg: implements implicit casts for unsafe nexpr
[nit.git] / src / global_compiler.nit
index 66969f7..2f63d20 100644 (file)
@@ -47,6 +47,9 @@ redef class ToolContext
        # --no-check-assert
        var opt_no_check_assert: OptionBool = new OptionBool("Disable the evaluation of explicit 'assert' and 'as' (dangerous)", "--no-check-assert")
 
+       # --no-check-autocast
+       var opt_no_check_autocast: OptionBool = new OptionBool("Disable implicit casts on unsafe expression usage (dangerous)", "--no-check-autocast")
+
        # --no-check-other
        var opt_no_check_other: OptionBool = new OptionBool("Disable implicit tests: unset attribute, null receiver (dangerous)", "--no-check-other")
 
@@ -54,7 +57,7 @@ redef class ToolContext
        do
                super
                self.option_context.add_option(self.opt_output, self.opt_no_cc, self.opt_hardening)
-               self.option_context.add_option(self.opt_no_check_covariance, self.opt_no_check_initialization, self.opt_no_check_assert, self.opt_no_check_other)
+               self.option_context.add_option(self.opt_no_check_covariance, self.opt_no_check_initialization, self.opt_no_check_assert, self.opt_no_check_autocast, self.opt_no_check_other)
        end
 end
 
@@ -951,6 +954,13 @@ class GlobalCompilerVisitor
                        mtype = self.anchor(mtype)
                        res = self.autobox(res, mtype)
                end
+               var implicit_cast_to = nexpr.implicit_cast_to
+               if implicit_cast_to != null and not self.compiler.modelbuilder.toolcontext.opt_no_check_autocast.value then
+                       var castres = self.type_test(res, implicit_cast_to)
+                       self.add("if (!{castres}) \{")
+                       self.add_abort("Cast failed")
+                       self.add("\}")
+               end
                self.current_node = old
                return res
        end