authorgravatar for stel@comfy.monsterpraschke <stel@comfy.monster> 2021-11-28 11:28:47+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-01-27 19:51:17+02:00
log6d1b1374f722f2b6f6f91e181e831238e3c8ce00
tree89608bd60607ae14893f268bf0cf766391ef19df
parent1adb6440db30e5906f685ca5c188ce87bbe0d475

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