From 85d0f0d45bf1529db8965b8176f8021d1ca27534 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Wed, 13 Mar 2019 14:46:53 -0400 Subject: [PATCH] fix @setRuntimeSafety not able to override release modes --- doc/docgen.zig | 23 +++++++++++++++++++---- doc/langref.html.in | 26 +++++++++++++++++++++++++- src/codegen.cpp | 7 +++---- 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/doc/docgen.zig b/doc/docgen.zig index 3242220af4..75e8d2d28d 100644 --- a/doc/docgen.zig +++ b/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("
$ zig test {}.zig\n{}
\n", code.name, colored_stderr); + try out.print( + "
$ zig test {}.zig{}\n{}
\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); diff --git a/doc/langref.html.in b/doc/langref.html.in index f9e053ee17..b8ee292462 100644 --- a/doc/langref.html.in +++ b/doc/langref.html.in @@ -6780,8 +6780,32 @@ pub const FloatMode = enum { {#header_open|@setRuntimeSafety#}
{#syntax#}@setRuntimeSafety(safety_on: bool){#endsyntax#}

- 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.

+ {#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#} diff --git a/src/codegen.cpp b/src/codegen.cpp index 22f3b5be09..9f7bef52cb 100644 --- a/src/codegen.cpp +++ b/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) {