authorgravatar for stel@comfy.monsterpraschke <stel@comfy.monster> 2021-11-28 11:28:47+00:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-02 22:00:00-07:00
log2b29424efd05fb059ba35f16eb804613676c869d
tree1c9e1f3119ed6a92862891629b6995549c3c20ea
parent8f89056dc27a77e0e35739db5b4310e68a49d9d3

docs: reorganize `@truncate` and `@intCast` for clarity


1 files changed, 14 insertions(+), 11 deletions(-)

doc/langref.html.in+14-11
......@@ -8537,6 +8537,16 @@ test "@hasDecl" {
85378537 Attempting to convert a number which is out of range of the destination type results in
85388538 safety-protected {#link|Undefined Behavior#}.
85398539 </p>
8540 {#code_begin|test_err|cast truncated bits#}
8541test "integer cast panic" {
8542 var a: u16 = 0xabcd;
8543 var b: u8 = @intCast(u8, a);
8544 _ = b;
8545}
8546 {#code_end#}
8547 <p>
8548 To truncate the significant bits of a number out of range of the destination type, use {#link|@truncate#}.
8549 </p>
85408550 <p>
85418551 If {#syntax#}T{#endsyntax#} is {#syntax#}comptime_int{#endsyntax#},
85428552 then this is semantically equivalent to {#link|Type Coercion#}.
......@@ -9380,17 +9390,11 @@ fn List(comptime T: type) type {
93809390 or same-sized integer type.
93819391 </p>
93829392 <p>
9383 The following produces safety-checked {#link|Undefined Behavior#}:
9393 This function always truncates the significant bits of the integer, regardless
9394 of endianness on the target platform.
93849395 </p>
9385 {#code_begin|test_err|cast truncated bits#}
9386test "integer cast panic" {
9387 var a: u16 = 0xabcd;
9388 var b: u8 = @intCast(u8, a);
9389 _ = b;
9390}
9391 {#code_end#}
93929396 <p>
9393 However this is well defined and working code:
9397 Calling {#syntax#}@truncate{#endsyntax#} on a number out of range of the destination type is well defined and working code:
93949398 </p>
93959399 {#code_begin|test|truncate#}
93969400const std = @import("std");
......@@ -9403,8 +9407,7 @@ test "integer truncation" {
94039407}
94049408 {#code_end#}
94059409 <p>
9406 This function always truncates the significant bits of the integer, regardless
9407 of endianness on the target platform.
9410 Use {#link|@intCast#} to convert numbers guaranteed to fit the destination type.
94089411 </p>
94099412 {#header_close#}
94109413