authorgravatar for scheibo@users.noreply.github.comKirk Scheibelhut <scheibo@users.noreply.github.com> 2022-02-04 11:27:50-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-07 12:22:06-07:00
log3c40cf1693df735c5d06ce98fce48532df2a3f22
tree1ba2186cdbd1e5526fe20bfc5fcddbe09c51c592
parent5cfc22bd36d1b34611a65ea59815bb83bdc248e6

Various documentation fixes

Co-authored-by: Kirk Scheibelhut <kjs@scheibo.com> Co-authored-by: extrasharp <genericpb@gmail.com>

1 files changed, 111 insertions(+), 63 deletions(-)

doc/langref.html.in+111-63
......@@ -1290,13 +1290,39 @@ test "expectError demo" {
12901290 A variable is a unit of {#link|Memory#} storage.
12911291 </p>
12921292 <p>
1293 Variables are never allowed to shadow identifiers from an outer scope.
1294 </p>
1295 <p>
12961293 It is generally preferable to use {#syntax#}const{#endsyntax#} rather than
12971294 {#syntax#}var{#endsyntax#} when declaring a variable. This causes less work for both
12981295 humans and computers to do when reading code, and creates more optimization opportunities.
12991296 </p>
1297
1298 {#header_open|Identifiers#}
1299 <p>
1300 Variable identifiers are never allowed to shadow identifiers from an outer scope.
1301 </p>
1302 <p>
1303 Identifiers must start with an alphabetic character or underscore and may be followed
1304 by any number of alphanumeric characters or underscores.
1305 They must not overlap with any keywords. See {#link|Keyword Reference#}.
1306 </p>
1307 <p>
1308 If a name that does not fit these requirements is needed, such as for linking with external libraries, the {#syntax#}@""{#endsyntax#} syntax may be used.
1309 </p>
1310 {#code_begin|syntax#}
1311const @"identifier with spaces in it" = 0xff;
1312const @"1SmallStep4Man" = 112358;
1313
1314const c = @import("std").c;
1315pub extern "c" fn @"error"() anyopaque;
1316pub extern "c" fn @"fstat$INODE64"(fd: c.fd_t, buf: *c.Stat) c_int;
1317
1318const Color = enum {
1319 red,
1320 @"really red",
1321};
1322const color: Color = .@"really red";
1323 {#code_end#}
1324 {#header_close#}
1325
13001326 {#header_open|Container Level Variables#}
13011327 <p>
13021328 Container level variables have static lifetime and are order-independent and lazily analyzed.
......@@ -1481,7 +1507,7 @@ fn divide(a: i32, b: i32) i32 {
14811507 </p>
14821508 <p>
14831509 Operators such as {#syntax#}+{#endsyntax#} and {#syntax#}-{#endsyntax#} cause undefined behavior on
1484 integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets.
1510 integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets.
14851511 {#syntax#}+%{#endsyntax#} and {#syntax#}-%{#endsyntax#} perform wrapping arithmetic
14861512 while {#syntax#}+|{#endsyntax#} and {#syntax#}-|{#endsyntax#} perform saturating arithmetic.
14871513 </p>
......@@ -2488,32 +2514,32 @@ test "null terminated array" {
24882514 or using the shorthand function {#syntax#}std.meta.Vector{#endsyntax#}.
24892515 </p>
24902516 <p>
2491 Vectors support the same builtin operators as their underlying base types. These operations are performed
2517 Vectors support the same builtin operators as their underlying base types. These operations are performed
24922518 element-wise, and return a vector of the same length as the input vectors. This includes:
24932519 </p>
24942520 <ul>
2495 <li>Arithmetic ({#syntax#}+{#endsyntax#}, {#syntax#}-{#endsyntax#}, {#syntax#}/{#endsyntax#}, {#syntax#}*{#endsyntax#},
2496 {#syntax#}@divFloor{#endsyntax#}, {#syntax#}@sqrt{#endsyntax#}, {#syntax#}@ceil{#endsyntax#},
2521 <li>Arithmetic ({#syntax#}+{#endsyntax#}, {#syntax#}-{#endsyntax#}, {#syntax#}/{#endsyntax#}, {#syntax#}*{#endsyntax#},
2522 {#syntax#}@divFloor{#endsyntax#}, {#syntax#}@sqrt{#endsyntax#}, {#syntax#}@ceil{#endsyntax#},
24972523 {#syntax#}@log{#endsyntax#}, etc.)</li>
2498 <li>Bitwise operators ({#syntax#}>>{#endsyntax#}, {#syntax#}<<{#endsyntax#}, {#syntax#}&{#endsyntax#},
2524 <li>Bitwise operators ({#syntax#}>>{#endsyntax#}, {#syntax#}<<{#endsyntax#}, {#syntax#}&{#endsyntax#},
24992525 {#syntax#}|{#endsyntax#}, {#syntax#}~{#endsyntax#}, etc.)</li>
25002526 <li>Comparison operators ({#syntax#}<{#endsyntax#}, {#syntax#}>{#endsyntax#}, {#syntax#}=={#endsyntax#}, etc.)</li>
25012527 </ul>
25022528 <p>
2503 It is prohibited to use a math operator on a mixture of scalars (individual numbers) and vectors.
2504 Zig provides the {#link|@splat#} builtin to easily convert from scalars to vectors, and it supports {#link|@reduce#}
2505 and array indexing syntax to convert from vectors to scalars. Vectors also support assignment to and from
2529 It is prohibited to use a math operator on a mixture of scalars (individual numbers) and vectors.
2530 Zig provides the {#link|@splat#} builtin to easily convert from scalars to vectors, and it supports {#link|@reduce#}
2531 and array indexing syntax to convert from vectors to scalars. Vectors also support assignment to and from
25062532 fixed-length arrays with comptime known length.
25072533 </p>
25082534 <p>
25092535 For rearranging elements within and between vectors, Zig provides the {#link|@shuffle#} and {#link|@select#} functions.
25102536 </p>
25112537 <p>
2512 Operations on vectors shorter than the target machine's native SIMD size will typically compile to single SIMD
2513 instructions, while vectors longer than the target machine's native SIMD size will compile to multiple SIMD
2514 instructions. If a given operation doesn't have SIMD support on the target architecture, the compiler will default
2515 to operating on each vector element one at a time. Zig supports any comptime-known vector length up to 2^32-1,
2516 although small powers of two (2-64) are most typical. Note that excessively long vector lengths (e.g. 2^20) may
2538 Operations on vectors shorter than the target machine's native SIMD size will typically compile to single SIMD
2539 instructions, while vectors longer than the target machine's native SIMD size will compile to multiple SIMD
2540 instructions. If a given operation doesn't have SIMD support on the target architecture, the compiler will default
2541 to operating on each vector element one at a time. Zig supports any comptime-known vector length up to 2^32-1,
2542 although small powers of two (2-64) are most typical. Note that excessively long vector lengths (e.g. 2^20) may
25172543 result in compiler crashes on current versions of Zig.
25182544 </p>
25192545 {#code_begin|test|vector_example#}
......@@ -2563,7 +2589,7 @@ test "Conversion between vectors, arrays, and slices" {
25632589 TODO consider suggesting std.MultiArrayList
25642590 </p>
25652591 {#see_also|@splat|@shuffle|@select|@reduce#}
2566
2592
25672593 {#header_close#}
25682594
25692595 {#header_open|Pointers#}
......@@ -2981,8 +3007,8 @@ test "null terminated slice" {
29813007}
29823008 {#code_end#}
29833009 <p>
2984 Sentinel-terminated slices can also be created using a variation of the slice syntax
2985 {#syntax#}data[start..end :x]{#endsyntax#}, where {#syntax#}data{#endsyntax#} is a many-item pointer,
3010 Sentinel-terminated slices can also be created using a variation of the slice syntax
3011 {#syntax#}data[start..end :x]{#endsyntax#}, where {#syntax#}data{#endsyntax#} is a many-item pointer,
29863012 array or slice and {#syntax#}x{#endsyntax#} is the sentinel value.
29873013 </p>
29883014 {#code_begin|test|null_terminated_slicing#}
......@@ -2999,7 +3025,7 @@ test "null terminated slicing" {
29993025}
30003026 {#code_end#}
30013027 <p>
3002 Sentinel-terminated slicing asserts that the element in the sentinel position of the backing data is
3028 Sentinel-terminated slicing asserts that the element in the sentinel position of the backing data is
30033029 actually the sentinel value. If this is not the case, safety-protected {#link|Undefined Behavior#} results.
30043030 </p>
30053031 {#code_begin|test_safety|sentinel mismatch#}
......@@ -3008,10 +3034,10 @@ const expect = std.testing.expect;
30083034
30093035test "sentinel mismatch" {
30103036 var array = [_]u8{ 3, 2, 1, 0 };
3011
3012 // Creating a sentinel-terminated slice from the array with a length of 2
3013 // will result in the value `1` occupying the sentinel element position.
3014 // This does not match the indicated sentinel value of `0` and will lead
3037
3038 // Creating a sentinel-terminated slice from the array with a length of 2
3039 // will result in the value `1` occupying the sentinel element position.
3040 // This does not match the indicated sentinel value of `0` and will lead
30153041 // to a runtime panic.
30163042 var runtime_length: usize = 2;
30173043 const slice = array[0..runtime_length :0];
......@@ -3159,7 +3185,7 @@ test "linked list" {
31593185 .last = &node,
31603186 .len = 1,
31613187 };
3162
3188
31633189 // When using a pointer to a struct, fields can be accessed directly,
31643190 // without explicitly dereferencing the pointer.
31653191 // So you can do
......@@ -3491,7 +3517,7 @@ fn dump(args: anytype) !void {
34913517 </p>
34923518 <p>
34933519 The fields are implicitly named using numbers starting from 0. Because their names are integers,
3494 the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as identifiers.
3520 the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as {#link|identifiers|Identifiers#}.
34953521 </p>
34963522 <p>
34973523 Like arrays, tuples have a .len field, can be indexed and work with the ++ and ** operators. They can also be iterated over with {#link|inline for#}.
......@@ -3980,7 +4006,7 @@ test "labeled break from labeled block expression" {
39804006 {#see_also|Labeled while|Labeled for#}
39814007
39824008 {#header_open|Shadowing#}
3983 <p>Identifiers are never allowed to "hide" other identifiers by using the same name:</p>
4009 <p>{#link|Identifiers#} are never allowed to "hide" other identifiers by using the same name:</p>
39844010 {#code_begin|test_err|local shadows declaration#}
39854011const pi = 3.14;
39864012
......@@ -3992,8 +4018,8 @@ test "inside test block" {
39924018}
39934019 {#code_end#}
39944020 <p>
3995 Because of this, when you read Zig code you can always rely on an identifier to consistently mean
3996 the same thing within the scope it is defined. Note that you can, however, use the same name if
4021 Because of this, when you read Zig code you can always rely on an identifier to consistently mean
4022 the same thing within the scope it is defined. Note that you can, however, use the same name if
39974023 the scopes are separate:
39984024 </p>
39994025 {#code_begin|test|test_scopes#}
......@@ -4031,7 +4057,7 @@ test "switch simple" {
40314057 1, 2, 3 => 0,
40324058
40334059 // Ranges can be specified using the ... syntax. These are inclusive
4034 // both ends.
4060 // of both ends.
40354061 5...100 => 1,
40364062
40374063 // Branches can be arbitrarily complex.
......@@ -4803,7 +4829,7 @@ test "errdefer unwinding" {
48034829 </p>
48044830 {#header_open|Basics#}
48054831 {#code_begin|test|test_unreachable#}
4806// unreachable is used to assert that control flow will never happen upon a
4832// unreachable is used to assert that control flow will never reach a
48074833// particular location:
48084834test "basic math" {
48094835 const x = 1;
......@@ -6771,8 +6797,7 @@ test "variable values" {
67716797 generic data structure.
67726798 </p>
67736799 <p>
6774 Here is an example of a generic {#syntax#}List{#endsyntax#} data structure, that we will instantiate with
6775 the type {#syntax#}i32{#endsyntax#}. In Zig we refer to the type as {#syntax#}List(i32){#endsyntax#}.
6800 Here is an example of a generic {#syntax#}List{#endsyntax#} data structure.
67766801 </p>
67776802 {#code_begin|syntax#}
67786803fn List(comptime T: type) type {
......@@ -6781,27 +6806,46 @@ fn List(comptime T: type) type {
67816806 len: usize,
67826807 };
67836808}
6809
6810// The generic List data structure can be instantiated by passing in a type:
6811var buffer: [10]i32 = undefined;
6812var list = List(i32){
6813 .items = &buffer,
6814 .len = 0,
6815};
67846816 {#code_end#}
67856817 <p>
6786 That's it. It's a function that returns an anonymous {#syntax#}struct{#endsyntax#}. For the purposes of error messages
6787 and debugging, Zig infers the name {#syntax#}"List(i32)"{#endsyntax#} from the function name and parameters invoked when creating
6818 That's it. It's a function that returns an anonymous {#syntax#}struct{#endsyntax#}.
6819 To keep the language small and uniform, all aggregate types in Zig are anonymous.
6820 For the purposes of error messages and debugging, Zig infers the name
6821 {#syntax#}"List(i32)"{#endsyntax#} from the function name and parameters invoked when creating
67886822 the anonymous struct.
67896823 </p>
67906824 <p>
6791 To keep the language small and uniform, all aggregate types in Zig are anonymous. To give a type
6792 a name, we assign it to a constant:
6825 To explicitly give a type a name, we assign it to a constant.
67936826 </p>
67946827 {#code_begin|syntax#}
67956828const Node = struct {
6796 next: *Node,
6797 name: []u8,
6829 next: ?*Node,
6830 name: []const u8,
6831};
6832
6833var node_a = Node{
6834 .next = null,
6835 .name = &"Node A",
6836};
6837
6838var node_b = Node{
6839 .next = &node_a,
6840 .name = &"Node B",
67986841};
67996842 {#code_end#}
68006843 <p>
6801 This works because all top level declarations are order-independent, and as long as there isn't
6802 an actual infinite regression, values can refer to themselves, directly or indirectly. In this case,
6803 {#syntax#}Node{#endsyntax#} refers to itself as a pointer, which is not actually an infinite regression, so
6804 it works fine.
6844 In this example, the {#syntax#}Node{#endsyntax#} struct refers to itself.
6845 This works because all top level declarations are order-independent.
6846 As long as the compiler can determine the size of the struct, it is free to refer to itself.
6847 In this case, {#syntax#}Node{#endsyntax#} refers to itself as a pointer, which has a
6848 well-defined size at compile time, so it works fine.
68056849 </p>
68066850 {#header_close#}
68076851 {#header_open|Case Study: print in Zig#}
......@@ -7214,10 +7258,10 @@ test "global assembly" {
72147258 provided explicitly by the caller, and it can be suspended and resumed any number of times.
72157259 </p>
72167260 <p>
7217 The code following the {#syntax#}async{#endsyntax#} callsite runs immediately after the async
7218 function first suspends. When the return value of the async function is needed,
7219 the calling code can {#syntax#}await{#endsyntax#} on the async function frame.
7220 This will suspend the calling code until the async function completes, at which point
7261 The code following the {#syntax#}async{#endsyntax#} callsite runs immediately after the async
7262 function first suspends. When the return value of the async function is needed,
7263 the calling code can {#syntax#}await{#endsyntax#} on the async function frame.
7264 This will suspend the calling code until the async function completes, at which point
72217265 execution resumes just after the {#syntax#}await{#endsyntax#} callsite.
72227266 </p>
72237267 <p>
......@@ -7327,8 +7371,8 @@ fn testResumeFromSuspend(my_result: *i32) void {
73277371 in standard code.
73287372 </p>
73297373 <p>
7330 However, it is possible to have an {#syntax#}async{#endsyntax#} call
7331 without a matching {#syntax#}await{#endsyntax#}. Upon completion of the async function,
7374 However, it is possible to have an {#syntax#}async{#endsyntax#} call
7375 without a matching {#syntax#}await{#endsyntax#}. Upon completion of the async function,
73327376 execution would continue at the most recent {#syntax#}async{#endsyntax#} callsite or {#syntax#}resume{#endsyntax#} callsite,
73337377 and the return value of the async function would be lost.
73347378 </p>
......@@ -7365,8 +7409,8 @@ fn func() void {
73657409 </p>
73667410 <p>
73677411 {#syntax#}await{#endsyntax#} is a suspend point, and takes as an operand anything that
7368 coerces to {#syntax#}anyframe->T{#endsyntax#}. Calling {#syntax#}await{#endsyntax#} on
7369 the frame of an async function will cause execution to continue at the
7412 coerces to {#syntax#}anyframe->T{#endsyntax#}. Calling {#syntax#}await{#endsyntax#} on
7413 the frame of an async function will cause execution to continue at the
73707414 {#syntax#}await{#endsyntax#} callsite once the target function completes.
73717415 </p>
73727416 <p>
......@@ -8291,8 +8335,8 @@ fn internalName() callconv(.C) void {}
82918335 {#code_begin|obj#}
82928336export fn foo() void {}
82938337 {#code_end#}
8294 <p>Note that even when using {#syntax#}export{#endsyntax#}, {#syntax#}@"foo"{#endsyntax#} syntax can
8295 be used to choose any string for the symbol name:</p>
8338 <p>Note that even when using {#syntax#}export{#endsyntax#}, the {#syntax#}@"foo"{#endsyntax#} syntax for
8339 {#link|identifiers|Identifiers#} can be used to choose any string for the symbol name:</p>
82968340 {#code_begin|obj#}
82978341export fn @"A function name that is a complete sentence."() void {}
82988342 {#code_end#}
......@@ -8591,7 +8635,9 @@ test "integer cast panic" {
85918635 {#header_open|@intToPtr#}
85928636 <pre>{#syntax#}@intToPtr(comptime DestType: type, address: usize) DestType{#endsyntax#}</pre>
85938637 <p>
8594 Converts an integer to a {#link|pointer|Pointers#}. To convert the other way, use {#link|@ptrToInt#}.
8638 Converts an integer to a {#link|pointer|Pointers#}. To convert the other way, use {#link|@ptrToInt#}. Casting an address of 0 to a destination type
8639 which in not {#link|optional|Optional Pointers#} and does not have the {#syntax#}allowzero{#endsyntax#} attribute will result in a
8640 {#link|Pointer Cast Invalid Null#} panic when runtime safety checks are enabled.
85958641 </p>
85968642 <p>
85978643 If the destination pointer type does not allow address zero and {#syntax#}address{#endsyntax#}
......@@ -8705,7 +8751,8 @@ test "@wasmMemoryGrow" {
87058751 <pre>{#syntax#}@mod(numerator: T, denominator: T) T{#endsyntax#}</pre>
87068752 <p>
87078753 Modulus division. For unsigned integers this is the same as
8708 {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}.
8754 {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}, otherwise the
8755 operation will result in a {#link|Remainder Division by Zero#} when runtime safety checks are enabled.
87098756 </p>
87108757 <ul>
87118758 <li>{#syntax#}@mod(-5, 3) == 1{#endsyntax#}</li>
......@@ -8723,7 +8770,7 @@ test "@wasmMemoryGrow" {
87238770 If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}.
87248771 </p>
87258772 {#header_close#}
8726
8773
87278774 {#header_open|@panic#}
87288775 <pre>{#syntax#}@panic(message: []const u8) noreturn{#endsyntax#}</pre>
87298776 <p>
......@@ -8830,7 +8877,8 @@ pub const PrefetchOptions = struct {
88308877 <pre>{#syntax#}@rem(numerator: T, denominator: T) T{#endsyntax#}</pre>
88318878 <p>
88328879 Remainder division. For unsigned integers this is the same as
8833 {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}.
8880 {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}, otherwise the
8881 operation will result in a {#link|Remainder Division by Zero#} when runtime safety checks are enabled.
88348882 </p>
88358883 <ul>
88368884 <li>{#syntax#}@rem(-5, 3) == -2{#endsyntax#}</li>
......@@ -8872,14 +8920,14 @@ pub const PrefetchOptions = struct {
88728920 {#header_close#}
88738921
88748922 {#header_open|@setCold#}
8875 <pre>{#syntax#}@setCold(is_cold: bool){#endsyntax#}</pre>
8923 <pre>{#syntax#}@setCold(comptime is_cold: bool){#endsyntax#}</pre>
88768924 <p>
88778925 Tells the optimizer that a function is rarely called.
88788926 </p>
88798927 {#header_close#}
88808928
88818929 {#header_open|@setEvalBranchQuota#}
8882 <pre>{#syntax#}@setEvalBranchQuota(new_quota: u32){#endsyntax#}</pre>
8930 <pre>{#syntax#}@setEvalBranchQuota(comptime new_quota: u32){#endsyntax#}</pre>
88838931 <p>
88848932 Changes the maximum number of backwards branches that compile-time code
88858933 execution can use before giving up and making a compile error.
......@@ -8914,7 +8962,7 @@ test "foo" {
89148962 {#header_close#}
89158963
89168964 {#header_open|@setFloatMode#}
8917 <pre>{#syntax#}@setFloatMode(mode: @import("std").builtin.FloatMode){#endsyntax#}</pre>
8965 <pre>{#syntax#}@setFloatMode(comptime mode: @import("std").builtin.FloatMode){#endsyntax#}</pre>
89188966 <p>
89198967 Sets the floating point mode of the current scope. Possible values are:
89208968 </p>
......@@ -8949,7 +8997,7 @@ pub const FloatMode = enum {
89498997 {#header_close#}
89508998
89518999 {#header_open|@setRuntimeSafety#}
8952 <pre>{#syntax#}@setRuntimeSafety(safety_on: bool) void{#endsyntax#}</pre>
9000 <pre>{#syntax#}@setRuntimeSafety(comptime safety_on: bool) void{#endsyntax#}</pre>
89539001 <p>
89549002 Sets whether runtime safety checks are enabled for the scope that contains the function call.
89559003 </p>
......@@ -9010,7 +9058,7 @@ test "@setRuntimeSafety" {
90109058 </p>
90119059 {#see_also|@shlExact|@shrExact#}
90129060 {#header_close#}
9013
9061
90149062 {#header_open|@shrExact#}
90159063 <pre>{#syntax#}@shrExact(value: T, shift_amt: Log2T) T{#endsyntax#}</pre>
90169064 <p>
......@@ -9341,7 +9389,7 @@ fn doTheTest() !void {
93419389 If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}.
93429390 </p>
93439391 {#header_close#}
9344
9392
93459393 {#header_open|@tagName#}
93469394 <pre>{#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}</pre>
93479395 <p>