Commit 85d0f0d45b

Andrew Kelley <andrew@ziglang.org>
2019-03-13 19:46:53
fix @setRuntimeSafety not able to override release modes
1 parent d213708
Changed files (3)
doc/docgen.zig
@@ -1183,11 +1183,21 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
                             "--output-dir",
                             tmp_dir_name,
                         });
+                        var mode_arg: []const u8 = "";
                         switch (code.mode) {
                             builtin.Mode.Debug => {},
-                            builtin.Mode.ReleaseSafe => try test_args.append("--release-safe"),
-                            builtin.Mode.ReleaseFast => try test_args.append("--release-fast"),
-                            builtin.Mode.ReleaseSmall => try test_args.append("--release-small"),
+                            builtin.Mode.ReleaseSafe => {
+                                try test_args.append("--release-safe");
+                                mode_arg = " --release-safe";
+                            },
+                            builtin.Mode.ReleaseFast => {
+                                try test_args.append("--release-fast");
+                                mode_arg = " --release-fast";
+                            },
+                            builtin.Mode.ReleaseSmall => {
+                                try test_args.append("--release-small");
+                                mode_arg = " --release-small";
+                            },
                         }
 
                         const result = try os.ChildProcess.exec(allocator, test_args.toSliceConst(), null, &env_map, max_doc_file_size);
@@ -1217,7 +1227,12 @@ fn genHtml(allocator: *mem.Allocator, tokenizer: *Tokenizer, toc: *Toc, out: var
                         }
                         const escaped_stderr = try escapeHtml(allocator, result.stderr);
                         const colored_stderr = try termColor(allocator, escaped_stderr);
-                        try out.print("<pre><code class=\"shell\">$ zig test {}.zig\n{}</code></pre>\n", code.name, colored_stderr);
+                        try out.print(
+                            "<pre><code class=\"shell\">$ zig test {}.zig{}\n{}</code></pre>\n",
+                            code.name,
+                            mode_arg,
+                            colored_stderr,
+                        );
                     },
                     Code.Id.Obj => |maybe_error_match| {
                         const name_plus_obj_ext = try std.fmt.allocPrint(allocator, "{}{}", code.name, obj_ext);
doc/langref.html.in
@@ -6780,8 +6780,32 @@ pub const FloatMode = enum {
       {#header_open|@setRuntimeSafety#}
       <pre>{#syntax#}@setRuntimeSafety(safety_on: bool){#endsyntax#}</pre>
       <p>
-      Sets whether runtime safety checks are on for the scope that contains the function call.
+      Sets whether runtime safety checks are enabled for the scope that contains the function call.
       </p>
+      {#code_begin|test_safety|integer overflow#}
+      {#code_release_fast#}
+test "@setRuntimeSafety" {
+    // The builtin applies to the scope that it is called in. So here, integer overflow
+    // will not be caught in ReleaseFast and ReleaseSmall modes:
+    // var x: u8 = 255;
+    // x += 1; // undefined behavior in ReleaseFast/ReleaseSmall modes.
+    {
+        // However this block has safety enabled, so safety checks happen here,
+        // even in ReleaseFast and ReleaseSmall modes.
+        @setRuntimeSafety(true);
+        var x: u8 = 255;
+        x += 1;
+
+        {
+            // The value can be overridden at any scope. So here integer overflow
+            // would not be caught in any build mode.
+            @setRuntimeSafety(false);
+            // var x: u8 = 255;
+            // x += 1; // undefined behavior in all build modes.
+        }
+    }
+}
+      {#code_end#}
 
       {#header_close#}
 
src/codegen.cpp
@@ -878,9 +878,6 @@ static bool ir_want_fast_math(CodeGen *g, IrInstruction *instruction) {
 }
 
 static bool ir_want_runtime_safety(CodeGen *g, IrInstruction *instruction) {
-    if (g->build_mode == BuildModeFastRelease || g->build_mode == BuildModeSmallRelease)
-        return false;
-
     // TODO memoize
     Scope *scope = instruction->scope;
     while (scope) {
@@ -895,7 +892,9 @@ static bool ir_want_runtime_safety(CodeGen *g, IrInstruction *instruction) {
         }
         scope = scope->parent;
     }
-    return true;
+
+    return (g->build_mode != BuildModeFastRelease &&
+            g->build_mode != BuildModeSmallRelease);
 }
 
 static Buf *panic_msg_buf(PanicMsgId msg_id) {