@@ -3573,14 +3573,161 @@ const optional_value: ?i32 = null;
35733573 {#header_close#}
35743574 {#header_close#}
35753575 {#header_open|Casting#}
3576- <p>TODO: explain implicit vs explicit casting</p>
3577- <p>TODO: resolve peer types builtin</p>
3578- <p>TODO: truncate builtin</p>
3579- <p>TODO: bitcast builtin</p>
3580- <p>TODO: int to ptr builtin</p>
3581- <p>TODO: ptr to int builtin</p>
3582- <p>TODO: ptrcast builtin</p>
3583- <p>TODO: explain number literals vs concrete types</p>
3576+ <p>
3577+ A <strong>type cast</strong> converts a value of one type to another.
3578+ Zig has {#link|Implicit Casts#} for conversions that are known to be completely safe and unambiguous,
3579+ and {#link|Explicit Casts#} for conversions that one would not want to happen on accident.
3580+ There is also a third kind of type conversion called {#link|Peer Type Resolution#} for
3581+ the case when a result type must be decided given multiple operand types.
3582+ </p>
3583+ {#header_open|Implicit Casts#}
3584+ <p>
3585+ An implicit cast occurs when one type is expected, but different type is provided:
3586+ </p>
3587+ {#code_begin|test#}
3588+ test "implicit cast - variable declaration" {
3589+ var a: u8 = 1;
3590+ var b: u16 = a;
3591+ }
3592+
3593+ test "implicit cast - function call" {
3594+ var a: u8 = 1;
3595+ foo(a);
3596+ }
3597+
3598+ fn foo(b: u16) void {}
3599+
3600+ test "implicit cast - invoke a type as a function" {
3601+ var a: u8 = 1;
3602+ var b = u16(a);
3603+ }
3604+ {#code_end#}
3605+ {#header_open|Implicit Cast: Stricter Qualification#}
3606+ <p>
3607+ Values which have the same representation at runtime can be cast to increase the strictness
3608+ of the qualifiers, no matter how nested the qualifiers are:
3609+ </p>
3610+ <ul>
3611+ <li><code>const</code> - non-const to const is allowed</li>
3612+ <li><code>volatile</code> - non-volatile to volatile is allowed</li>
3613+ <li><code>align</code> - bigger to smaller alignment is allowed </li>
3614+ <li>{#link|error sets|Error Set Type#} to supersets is allowed</li>
3615+ </ul>
3616+ <p>
3617+ These casts are no-ops at runtime since the value representation does not change.
3618+ </p>
3619+ {#code_begin|test#}
3620+ test "implicit cast - const qualification" {
3621+ var a: i32 = 1;
3622+ var b: *i32 = &a;
3623+ foo(b);
3624+ }
3625+
3626+ fn foo(a: *const i32) void {}
3627+ {#code_end#}
3628+ <p>
3629+ In addition, pointers implicitly cast to const optional pointers:
3630+ </p>
3631+ {#code_begin|test#}
3632+ const std = @import("std");
3633+ const assert = std.debug.assert;
3634+ const mem = std.mem;
3635+
3636+ test "cast *[1][*]const u8 to [*]const ?[*]const u8" {
3637+ const window_name = [1][*]const u8{c"window name"};
3638+ const x: [*]const ?[*]const u8 = &window_name;
3639+ assert(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name"));
3640+ }
3641+ {#code_end#}
3642+ {#header_close#}
3643+ {#header_open|Implicit Cast: Integer and Float Widening#}
3644+ <p>
3645+ {#link|Integers#} implicitly cast to integer types which can represent every value of the old type, and likewise
3646+ {#link|Floats#} implicitly cast to float types which can represent every value of the old type.
3647+ </p>
3648+ {#code_begin|test#}
3649+ const std = @import("std");
3650+ const assert = std.debug.assert;
3651+ const mem = std.mem;
3652+
3653+ test "integer widening" {
3654+ var a: u8 = 250;
3655+ var b: u16 = a;
3656+ var c: u32 = b;
3657+ var d: u64 = c;
3658+ var e: u64 = d;
3659+ var f: u128 = e;
3660+ assert(f == a);
3661+ }
3662+
3663+ test "implicit unsigned integer to signed integer" {
3664+ var a: u8 = 250;
3665+ var b: i16 = a;
3666+ assert(b == 250);
3667+ }
3668+
3669+ test "float widening" {
3670+ var a: f32 = 12.34;
3671+ var b: f64 = a;
3672+ var c: f128 = b;
3673+ assert(c == a);
3674+ }
3675+ {#code_end#}
3676+ {#header_close#}
3677+ {#header_open|Implicit Cast: Arrays#}
3678+ <p>TODO: [N]T to []const T</p>
3679+ <p>TODO: *const [N]T to []const T</p>
3680+ <p>TODO: [N]T to *const []const T</p>
3681+ <p>TODO: [N]T to ?[]const T</p>
3682+ <p>TODO: *[N]T to []T</p>
3683+ <p>TODO: *[N]T to [*]T</p>
3684+ <p>TODO: *T to *[1]T</p>
3685+ <p>TODO: [N]T to E![]const T</p>
3686+ {#header_close#}
3687+ {#header_open|Implicit Cast: Optionals#}
3688+ <p>TODO: T to ?T</p>
3689+ <p>TODO: T to E!?T</p>
3690+ <p>TODO: null to ?T</p>
3691+ {#header_close#}
3692+ {#header_open|Implicit Cast: T to E!T#}
3693+ <p>TODO</p>
3694+ {#header_close#}
3695+ {#header_open|Implicit Cast: E to E!T#}
3696+ <p>TODO</p>
3697+ {#header_close#}
3698+ {#header_open|Implicit Cast: comptime_int to *const integer#}
3699+ <p>TODO</p>
3700+ {#header_close#}
3701+ {#header_open|Implicit Cast: comptime_float to *const float#}
3702+ <p>TODO</p>
3703+ {#header_close#}
3704+ {#header_open|Implicit Cast: compile-time known numbers#}
3705+ <p>TODO</p>
3706+ {#header_close#}
3707+ {#header_open|Implicit Cast: union to enum#}
3708+ <p>TODO</p>
3709+ {#header_close#}
3710+ {#header_open|Implicit Cast: enum to union#}
3711+ <p>TODO</p>
3712+ {#header_close#}
3713+ {#header_open|Implicit Cast: T to *T when @sizeOf(T) == 0#}
3714+ <p>TODO</p>
3715+ {#header_close#}
3716+ {#header_open|Implicit Cast: undefined#}
3717+ <p>TODO</p>
3718+ {#header_close#}
3719+ {#header_open|Implicit Cast: T to *const T#}
3720+ <p>TODO</p>
3721+ {#header_close#}
3722+ {#header_close#}
3723+
3724+ {#header_open|Explicit Casts#}
3725+ <p>TODO</p>
3726+ {#header_close#}
3727+
3728+ {#header_open|Peer Type Resolution#}
3729+ <p>TODO</p>
3730+ {#header_close#}
35843731 {#header_close#}
35853732
35863733 {#header_open|void#}
@@ -5522,12 +5669,6 @@ pub const FloatMode = enum {
55225669 </p>
55235670 {#see_also|Compile Variables#}
55245671 {#header_close#}
5525- {#header_open|@setGlobalSection#}
5526- <pre><code class="zig">@setGlobalSection(global_variable_name, comptime section_name: []const u8) bool</code></pre>
5527- <p>
5528- Puts the global variable in the specified section.
5529- </p>
5530- {#header_close#}
55315672 {#header_open|@shlExact#}
55325673 <pre><code class="zig">@shlExact(value: T, shift_amt: Log2T) T</code></pre>
55335674 <p>
@@ -6928,7 +7069,7 @@ hljs.registerLanguage("zig", function(t) {
69287069 a = t.IR + "\\s*\\(",
69297070 c = {
69307071 keyword: "const align var extern stdcallcc nakedcc volatile export pub noalias inline struct packed enum union break return try catch test continue unreachable comptime and or asm defer errdefer if else switch while for fn use bool f32 f64 void type noreturn error i8 u8 i16 u16 i32 u32 i64 u64 isize usize i8w u8w i16w i32w u32w i64w u64w isizew usizew c_short c_ushort c_int c_uint c_long c_ulong c_longlong c_ulonglong resume cancel await async orelse",
6931- built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage setGlobalSection divTrunc divFloor enumTagName intToPtr ptrToInt panic ptrCast intCast floatCast intToFloat floatToInt boolToInt bytesToSlice sliceToBytes errSetCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field typeInfo typeName newStackCall errorToInt intToError enumToInt intToEnum",
7072+ built_in: "atomicLoad breakpoint returnAddress frameAddress fieldParentPtr setFloatMode IntType OpaqueType compileError compileLog setCold setRuntimeSafety setEvalBranchQuota offsetOf memcpy inlineCall setGlobalLinkage divTrunc divFloor enumTagName intToPtr ptrToInt panic ptrCast intCast floatCast intToFloat floatToInt boolToInt bytesToSlice sliceToBytes errSetCast bitCast rem mod memset sizeOf alignOf alignCast maxValue minValue memberCount memberName memberType typeOf addWithOverflow subWithOverflow mulWithOverflow shlWithOverflow shlExact shrExact cInclude cDefine cUndef ctz clz import cImport errorName embedFile cmpxchgStrong cmpxchgWeak fence divExact truncate atomicRmw sqrt field typeInfo typeName newStackCall errorToInt intToError enumToInt intToEnum",
69327073 literal: "true false null undefined"
69337074 },
69347075 n = [e, t.CLCM, t.CBCM, s, r];
0 commit comments