authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-10 22:58:42-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-04-10 23:00:53-04:00
loge309ad884a5d2fc8b78325199cd6e81d2efa220d
tree3a421862bdef192c0f823feafbea0315bf5d9bf3
parenta4c7e4c4eb7b1000252fab3f98f6204edb48a4bd
signaturelock-open Commit is signed but in an unrecognized format.

fix outdated/incorrect docs for `@truncate`

closes #2234

1 files changed, 18 insertions(+), 8 deletions(-)

doc/langref.html.in+18-8
......@@ -7360,20 +7360,30 @@ fn List(comptime T: type) type {
73607360 <pre>{#syntax#}@truncate(comptime T: type, integer: var) T{#endsyntax#}</pre>
73617361 <p>
73627362 This function truncates bits from an integer type, resulting in a smaller
7363 integer type.
7363 or same-sized integer type.
73647364 </p>
73657365 <p>
7366 The following produces a crash in {#link|Debug#} mode and {#link|Undefined Behavior#} in
7367 {#link|ReleaseFast#} mode:
7366 The following produces safety-checked {#link|Undefined Behavior#}:
73687367 </p>
7369 <pre>{#syntax#}const a: u16 = 0xabcd;
7370const b: u8 = u8(a);{#endsyntax#}</pre>
7368 {#code_begin|test_err|cast truncated bits#}
7369test "integer cast panic" {
7370 var a: u16 = 0xabcd;
7371 var b: u8 = @intCast(u8, a);
7372}
7373 {#code_end#}
73717374 <p>
73727375 However this is well defined and working code:
73737376 </p>
7374 <pre>{#syntax#}const a: u16 = 0xabcd;
7375const b: u8 = @truncate(u8, a);
7376// b is now 0xcd{#endsyntax#}</pre>
7377 {#code_begin|test|truncate#}
7378const std = @import("std");
7379const assert = std.debug.assert;
7380
7381test "integer truncation" {
7382 var a: u16 = 0xabcd;
7383 var b: u8 = @truncate(u8, a);
7384 assert(b == 0xcd);
7385}
7386 {#code_end#}
73777387 <p>
73787388 This function always truncates the significant bits of the integer, regardless
73797389 of endianness on the target platform.