authorgravatar for scheibo@users.noreply.github.comKirk Scheibelhut <scheibo@users.noreply.github.com> 2022-02-04 11:27:50-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-02-04 21:27:50+02:00
log71321b694195a87ab7394a25badf5295eb01875e
treeb00ce9077624e73d673a8f357da85fe383ce302d
parent95fbce2b958395a367a82ce33170edd93e686173
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

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
......@@ -1295,13 +1295,39 @@ test "expectError demo" {
12951295 A variable is a unit of {#link|Memory#} storage.
12961296 </p>
12971297 <p>
1298 Variables are never allowed to shadow identifiers from an outer scope.
1299 </p>
1300 <p>
13011298 It is generally preferable to use {#syntax#}const{#endsyntax#} rather than
13021299 {#syntax#}var{#endsyntax#} when declaring a variable. This causes less work for both
13031300 humans and computers to do when reading code, and creates more optimization opportunities.
13041301 </p>
1302
1303 {#header_open|Identifiers#}
1304 <p>
1305 Variable identifiers are never allowed to shadow identifiers from an outer scope.
1306 </p>
1307 <p>
1308 Identifiers must start with an alphabetic character or underscore and may be followed
1309 by any number of alphanumeric characters or underscores.
1310 They must not overlap with any keywords. See {#link|Keyword Reference#}.
1311 </p>
1312 <p>
1313 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.
1314 </p>
1315 {#code_begin|syntax#}
1316const @"identifier with spaces in it" = 0xff;
1317const @"1SmallStep4Man" = 112358;
1318
1319const c = @import("std").c;
1320pub extern "c" fn @"error"() anyopaque;
1321pub extern "c" fn @"fstat$INODE64"(fd: c.fd_t, buf: *c.Stat) c_int;
1322
1323const Color = enum {
1324 red,
1325 @"really red",
1326};
1327const color: Color = .@"really red";
1328 {#code_end#}
1329 {#header_close#}
1330
13051331 {#header_open|Container Level Variables#}
13061332 <p>
13071333 Container level variables have static lifetime and are order-independent and lazily analyzed.
......@@ -1486,7 +1512,7 @@ fn divide(a: i32, b: i32) i32 {
14861512 </p>
14871513 <p>
14881514 Operators such as {#syntax#}+{#endsyntax#} and {#syntax#}-{#endsyntax#} cause undefined behavior on
1489 integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets.
1515 integer overflow. Alternative operators are provided for wrapping and saturating arithmetic on all targets.
14901516 {#syntax#}+%{#endsyntax#} and {#syntax#}-%{#endsyntax#} perform wrapping arithmetic
14911517 while {#syntax#}+|{#endsyntax#} and {#syntax#}-|{#endsyntax#} perform saturating arithmetic.
14921518 </p>
......@@ -2494,32 +2520,32 @@ test "null terminated array" {
24942520 or using the shorthand function {#syntax#}std.meta.Vector{#endsyntax#}.
24952521 </p>
24962522 <p>
2497 Vectors support the same builtin operators as their underlying base types. These operations are performed
2523 Vectors support the same builtin operators as their underlying base types. These operations are performed
24982524 element-wise, and return a vector of the same length as the input vectors. This includes:
24992525 </p>
25002526 <ul>
2501 <li>Arithmetic ({#syntax#}+{#endsyntax#}, {#syntax#}-{#endsyntax#}, {#syntax#}/{#endsyntax#}, {#syntax#}*{#endsyntax#},
2502 {#syntax#}@divFloor{#endsyntax#}, {#syntax#}@sqrt{#endsyntax#}, {#syntax#}@ceil{#endsyntax#},
2527 <li>Arithmetic ({#syntax#}+{#endsyntax#}, {#syntax#}-{#endsyntax#}, {#syntax#}/{#endsyntax#}, {#syntax#}*{#endsyntax#},
2528 {#syntax#}@divFloor{#endsyntax#}, {#syntax#}@sqrt{#endsyntax#}, {#syntax#}@ceil{#endsyntax#},
25032529 {#syntax#}@log{#endsyntax#}, etc.)</li>
2504 <li>Bitwise operators ({#syntax#}>>{#endsyntax#}, {#syntax#}<<{#endsyntax#}, {#syntax#}&{#endsyntax#},
2530 <li>Bitwise operators ({#syntax#}>>{#endsyntax#}, {#syntax#}<<{#endsyntax#}, {#syntax#}&{#endsyntax#},
25052531 {#syntax#}|{#endsyntax#}, {#syntax#}~{#endsyntax#}, etc.)</li>
25062532 <li>Comparison operators ({#syntax#}<{#endsyntax#}, {#syntax#}>{#endsyntax#}, {#syntax#}=={#endsyntax#}, etc.)</li>
25072533 </ul>
25082534 <p>
2509 It is prohibited to use a math operator on a mixture of scalars (individual numbers) and vectors.
2510 Zig provides the {#link|@splat#} builtin to easily convert from scalars to vectors, and it supports {#link|@reduce#}
2511 and array indexing syntax to convert from vectors to scalars. Vectors also support assignment to and from
2535 It is prohibited to use a math operator on a mixture of scalars (individual numbers) and vectors.
2536 Zig provides the {#link|@splat#} builtin to easily convert from scalars to vectors, and it supports {#link|@reduce#}
2537 and array indexing syntax to convert from vectors to scalars. Vectors also support assignment to and from
25122538 fixed-length arrays with comptime known length.
25132539 </p>
25142540 <p>
25152541 For rearranging elements within and between vectors, Zig provides the {#link|@shuffle#} and {#link|@select#} functions.
25162542 </p>
25172543 <p>
2518 Operations on vectors shorter than the target machine's native SIMD size will typically compile to single SIMD
2519 instructions, while vectors longer than the target machine's native SIMD size will compile to multiple SIMD
2520 instructions. If a given operation doesn't have SIMD support on the target architecture, the compiler will default
2521 to operating on each vector element one at a time. Zig supports any comptime-known vector length up to 2^32-1,
2522 although small powers of two (2-64) are most typical. Note that excessively long vector lengths (e.g. 2^20) may
2544 Operations on vectors shorter than the target machine's native SIMD size will typically compile to single SIMD
2545 instructions, while vectors longer than the target machine's native SIMD size will compile to multiple SIMD
2546 instructions. If a given operation doesn't have SIMD support on the target architecture, the compiler will default
2547 to operating on each vector element one at a time. Zig supports any comptime-known vector length up to 2^32-1,
2548 although small powers of two (2-64) are most typical. Note that excessively long vector lengths (e.g. 2^20) may
25232549 result in compiler crashes on current versions of Zig.
25242550 </p>
25252551 {#code_begin|test|vector_example#}
......@@ -2569,7 +2595,7 @@ test "Conversion between vectors, arrays, and slices" {
25692595 TODO consider suggesting std.MultiArrayList
25702596 </p>
25712597 {#see_also|@splat|@shuffle|@select|@reduce#}
2572
2598
25732599 {#header_close#}
25742600
25752601 {#header_open|Pointers#}
......@@ -2987,8 +3013,8 @@ test "null terminated slice" {
29873013}
29883014 {#code_end#}
29893015 <p>
2990 Sentinel-terminated slices can also be created using a variation of the slice syntax
2991 {#syntax#}data[start..end :x]{#endsyntax#}, where {#syntax#}data{#endsyntax#} is a many-item pointer,
3016 Sentinel-terminated slices can also be created using a variation of the slice syntax
3017 {#syntax#}data[start..end :x]{#endsyntax#}, where {#syntax#}data{#endsyntax#} is a many-item pointer,
29923018 array or slice and {#syntax#}x{#endsyntax#} is the sentinel value.
29933019 </p>
29943020 {#code_begin|test|null_terminated_slicing#}
......@@ -3005,7 +3031,7 @@ test "null terminated slicing" {
30053031}
30063032 {#code_end#}
30073033 <p>
3008 Sentinel-terminated slicing asserts that the element in the sentinel position of the backing data is
3034 Sentinel-terminated slicing asserts that the element in the sentinel position of the backing data is
30093035 actually the sentinel value. If this is not the case, safety-protected {#link|Undefined Behavior#} results.
30103036 </p>
30113037 {#code_begin|test_safety|sentinel mismatch#}
......@@ -3014,10 +3040,10 @@ const expect = std.testing.expect;
30143040
30153041test "sentinel mismatch" {
30163042 var array = [_]u8{ 3, 2, 1, 0 };
3017
3018 // Creating a sentinel-terminated slice from the array with a length of 2
3019 // will result in the value `1` occupying the sentinel element position.
3020 // This does not match the indicated sentinel value of `0` and will lead
3043
3044 // Creating a sentinel-terminated slice from the array with a length of 2
3045 // will result in the value `1` occupying the sentinel element position.
3046 // This does not match the indicated sentinel value of `0` and will lead
30213047 // to a runtime panic.
30223048 var runtime_length: usize = 2;
30233049 const slice = array[0..runtime_length :0];
......@@ -3165,7 +3191,7 @@ test "linked list" {
31653191 .last = &node,
31663192 .len = 1,
31673193 };
3168
3194
31693195 // When using a pointer to a struct, fields can be accessed directly,
31703196 // without explicitly dereferencing the pointer.
31713197 // So you can do
......@@ -3497,7 +3523,7 @@ fn dump(args: anytype) !void {
34973523 </p>
34983524 <p>
34993525 The fields are implicitly named using numbers starting from 0. Because their names are integers,
3500 the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as identifiers.
3526 the {#syntax#}@"0"{#endsyntax#} syntax must be used to access them. Names inside {#syntax#}@""{#endsyntax#} are always recognised as {#link|identifiers|Identifiers#}.
35013527 </p>
35023528 <p>
35033529 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#}.
......@@ -3986,7 +4012,7 @@ test "labeled break from labeled block expression" {
39864012 {#see_also|Labeled while|Labeled for#}
39874013
39884014 {#header_open|Shadowing#}
3989 <p>Identifiers are never allowed to "hide" other identifiers by using the same name:</p>
4015 <p>{#link|Identifiers#} are never allowed to "hide" other identifiers by using the same name:</p>
39904016 {#code_begin|test_err|local shadows declaration#}
39914017const pi = 3.14;
39924018
......@@ -3998,8 +4024,8 @@ test "inside test block" {
39984024}
39994025 {#code_end#}
40004026 <p>
4001 Because of this, when you read Zig code you can always rely on an identifier to consistently mean
4002 the same thing within the scope it is defined. Note that you can, however, use the same name if
4027 Because of this, when you read Zig code you can always rely on an identifier to consistently mean
4028 the same thing within the scope it is defined. Note that you can, however, use the same name if
40034029 the scopes are separate:
40044030 </p>
40054031 {#code_begin|test|test_scopes#}
......@@ -4037,7 +4063,7 @@ test "switch simple" {
40374063 1, 2, 3 => 0,
40384064
40394065 // Ranges can be specified using the ... syntax. These are inclusive
4040 // both ends.
4066 // of both ends.
40414067 5...100 => 1,
40424068
40434069 // Branches can be arbitrarily complex.
......@@ -4809,7 +4835,7 @@ test "errdefer unwinding" {
48094835 </p>
48104836 {#header_open|Basics#}
48114837 {#code_begin|test|test_unreachable#}
4812// unreachable is used to assert that control flow will never happen upon a
4838// unreachable is used to assert that control flow will never reach a
48134839// particular location:
48144840test "basic math" {
48154841 const x = 1;
......@@ -6777,8 +6803,7 @@ test "variable values" {
67776803 generic data structure.
67786804 </p>
67796805 <p>
6780 Here is an example of a generic {#syntax#}List{#endsyntax#} data structure, that we will instantiate with
6781 the type {#syntax#}i32{#endsyntax#}. In Zig we refer to the type as {#syntax#}List(i32){#endsyntax#}.
6806 Here is an example of a generic {#syntax#}List{#endsyntax#} data structure.
67826807 </p>
67836808 {#code_begin|syntax#}
67846809fn List(comptime T: type) type {
......@@ -6787,27 +6812,46 @@ fn List(comptime T: type) type {
67876812 len: usize,
67886813 };
67896814}
6815
6816// The generic List data structure can be instantiated by passing in a type:
6817var buffer: [10]i32 = undefined;
6818var list = List(i32){
6819 .items = &buffer,
6820 .len = 0,
6821};
67906822 {#code_end#}
67916823 <p>
6792 That's it. It's a function that returns an anonymous {#syntax#}struct{#endsyntax#}. For the purposes of error messages
6793 and debugging, Zig infers the name {#syntax#}"List(i32)"{#endsyntax#} from the function name and parameters invoked when creating
6824 That's it. It's a function that returns an anonymous {#syntax#}struct{#endsyntax#}.
6825 To keep the language small and uniform, all aggregate types in Zig are anonymous.
6826 For the purposes of error messages and debugging, Zig infers the name
6827 {#syntax#}"List(i32)"{#endsyntax#} from the function name and parameters invoked when creating
67946828 the anonymous struct.
67956829 </p>
67966830 <p>
6797 To keep the language small and uniform, all aggregate types in Zig are anonymous. To give a type
6798 a name, we assign it to a constant:
6831 To explicitly give a type a name, we assign it to a constant.
67996832 </p>
68006833 {#code_begin|syntax#}
68016834const Node = struct {
6802 next: *Node,
6803 name: []u8,
6835 next: ?*Node,
6836 name: []const u8,
6837};
6838
6839var node_a = Node{
6840 .next = null,
6841 .name = &"Node A",
6842};
6843
6844var node_b = Node{
6845 .next = &node_a,
6846 .name = &"Node B",
68046847};
68056848 {#code_end#}
68066849 <p>
6807 This works because all top level declarations are order-independent, and as long as there isn't
6808 an actual infinite regression, values can refer to themselves, directly or indirectly. In this case,
6809 {#syntax#}Node{#endsyntax#} refers to itself as a pointer, which is not actually an infinite regression, so
6810 it works fine.
6850 In this example, the {#syntax#}Node{#endsyntax#} struct refers to itself.
6851 This works because all top level declarations are order-independent.
6852 As long as the compiler can determine the size of the struct, it is free to refer to itself.
6853 In this case, {#syntax#}Node{#endsyntax#} refers to itself as a pointer, which has a
6854 well-defined size at compile time, so it works fine.
68116855 </p>
68126856 {#header_close#}
68136857 {#header_open|Case Study: print in Zig#}
......@@ -7220,10 +7264,10 @@ test "global assembly" {
72207264 provided explicitly by the caller, and it can be suspended and resumed any number of times.
72217265 </p>
72227266 <p>
7223 The code following the {#syntax#}async{#endsyntax#} callsite runs immediately after the async
7224 function first suspends. When the return value of the async function is needed,
7225 the calling code can {#syntax#}await{#endsyntax#} on the async function frame.
7226 This will suspend the calling code until the async function completes, at which point
7267 The code following the {#syntax#}async{#endsyntax#} callsite runs immediately after the async
7268 function first suspends. When the return value of the async function is needed,
7269 the calling code can {#syntax#}await{#endsyntax#} on the async function frame.
7270 This will suspend the calling code until the async function completes, at which point
72277271 execution resumes just after the {#syntax#}await{#endsyntax#} callsite.
72287272 </p>
72297273 <p>
......@@ -7333,8 +7377,8 @@ fn testResumeFromSuspend(my_result: *i32) void {
73337377 in standard code.
73347378 </p>
73357379 <p>
7336 However, it is possible to have an {#syntax#}async{#endsyntax#} call
7337 without a matching {#syntax#}await{#endsyntax#}. Upon completion of the async function,
7380 However, it is possible to have an {#syntax#}async{#endsyntax#} call
7381 without a matching {#syntax#}await{#endsyntax#}. Upon completion of the async function,
73387382 execution would continue at the most recent {#syntax#}async{#endsyntax#} callsite or {#syntax#}resume{#endsyntax#} callsite,
73397383 and the return value of the async function would be lost.
73407384 </p>
......@@ -7371,8 +7415,8 @@ fn func() void {
73717415 </p>
73727416 <p>
73737417 {#syntax#}await{#endsyntax#} is a suspend point, and takes as an operand anything that
7374 coerces to {#syntax#}anyframe->T{#endsyntax#}. Calling {#syntax#}await{#endsyntax#} on
7375 the frame of an async function will cause execution to continue at the
7418 coerces to {#syntax#}anyframe->T{#endsyntax#}. Calling {#syntax#}await{#endsyntax#} on
7419 the frame of an async function will cause execution to continue at the
73767420 {#syntax#}await{#endsyntax#} callsite once the target function completes.
73777421 </p>
73787422 <p>
......@@ -8297,8 +8341,8 @@ fn internalName() callconv(.C) void {}
82978341 {#code_begin|obj#}
82988342export fn foo() void {}
82998343 {#code_end#}
8300 <p>Note that even when using {#syntax#}export{#endsyntax#}, {#syntax#}@"foo"{#endsyntax#} syntax can
8301 be used to choose any string for the symbol name:</p>
8344 <p>Note that even when using {#syntax#}export{#endsyntax#}, the {#syntax#}@"foo"{#endsyntax#} syntax for
8345 {#link|identifiers|Identifiers#} can be used to choose any string for the symbol name:</p>
83028346 {#code_begin|obj#}
83038347export fn @"A function name that is a complete sentence."() void {}
83048348 {#code_end#}
......@@ -8597,7 +8641,9 @@ test "integer cast panic" {
85978641 {#header_open|@intToPtr#}
85988642 <pre>{#syntax#}@intToPtr(comptime DestType: type, address: usize) DestType{#endsyntax#}</pre>
85998643 <p>
8600 Converts an integer to a {#link|pointer|Pointers#}. To convert the other way, use {#link|@ptrToInt#}.
8644 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
8645 which in not {#link|optional|Optional Pointers#} and does not have the {#syntax#}allowzero{#endsyntax#} attribute will result in a
8646 {#link|Pointer Cast Invalid Null#} panic when runtime safety checks are enabled.
86018647 </p>
86028648 <p>
86038649 If the destination pointer type does not allow address zero and {#syntax#}address{#endsyntax#}
......@@ -8711,7 +8757,8 @@ test "@wasmMemoryGrow" {
87118757 <pre>{#syntax#}@mod(numerator: T, denominator: T) T{#endsyntax#}</pre>
87128758 <p>
87138759 Modulus division. For unsigned integers this is the same as
8714 {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}.
8760 {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}, otherwise the
8761 operation will result in a {#link|Remainder Division by Zero#} when runtime safety checks are enabled.
87158762 </p>
87168763 <ul>
87178764 <li>{#syntax#}@mod(-5, 3) == 1{#endsyntax#}</li>
......@@ -8729,7 +8776,7 @@ test "@wasmMemoryGrow" {
87298776 If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}.
87308777 </p>
87318778 {#header_close#}
8732
8779
87338780 {#header_open|@panic#}
87348781 <pre>{#syntax#}@panic(message: []const u8) noreturn{#endsyntax#}</pre>
87358782 <p>
......@@ -8836,7 +8883,8 @@ pub const PrefetchOptions = struct {
88368883 <pre>{#syntax#}@rem(numerator: T, denominator: T) T{#endsyntax#}</pre>
88378884 <p>
88388885 Remainder division. For unsigned integers this is the same as
8839 {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}.
8886 {#syntax#}numerator % denominator{#endsyntax#}. Caller guarantees {#syntax#}denominator > 0{#endsyntax#}, otherwise the
8887 operation will result in a {#link|Remainder Division by Zero#} when runtime safety checks are enabled.
88408888 </p>
88418889 <ul>
88428890 <li>{#syntax#}@rem(-5, 3) == -2{#endsyntax#}</li>
......@@ -8878,14 +8926,14 @@ pub const PrefetchOptions = struct {
88788926 {#header_close#}
88798927
88808928 {#header_open|@setCold#}
8881 <pre>{#syntax#}@setCold(is_cold: bool){#endsyntax#}</pre>
8929 <pre>{#syntax#}@setCold(comptime is_cold: bool){#endsyntax#}</pre>
88828930 <p>
88838931 Tells the optimizer that a function is rarely called.
88848932 </p>
88858933 {#header_close#}
88868934
88878935 {#header_open|@setEvalBranchQuota#}
8888 <pre>{#syntax#}@setEvalBranchQuota(new_quota: u32){#endsyntax#}</pre>
8936 <pre>{#syntax#}@setEvalBranchQuota(comptime new_quota: u32){#endsyntax#}</pre>
88898937 <p>
88908938 Changes the maximum number of backwards branches that compile-time code
88918939 execution can use before giving up and making a compile error.
......@@ -8920,7 +8968,7 @@ test "foo" {
89208968 {#header_close#}
89218969
89228970 {#header_open|@setFloatMode#}
8923 <pre>{#syntax#}@setFloatMode(mode: @import("std").builtin.FloatMode){#endsyntax#}</pre>
8971 <pre>{#syntax#}@setFloatMode(comptime mode: @import("std").builtin.FloatMode){#endsyntax#}</pre>
89248972 <p>
89258973 Sets the floating point mode of the current scope. Possible values are:
89268974 </p>
......@@ -8955,7 +9003,7 @@ pub const FloatMode = enum {
89559003 {#header_close#}
89569004
89579005 {#header_open|@setRuntimeSafety#}
8958 <pre>{#syntax#}@setRuntimeSafety(safety_on: bool) void{#endsyntax#}</pre>
9006 <pre>{#syntax#}@setRuntimeSafety(comptime safety_on: bool) void{#endsyntax#}</pre>
89599007 <p>
89609008 Sets whether runtime safety checks are enabled for the scope that contains the function call.
89619009 </p>
......@@ -9016,7 +9064,7 @@ test "@setRuntimeSafety" {
90169064 </p>
90179065 {#see_also|@shlExact|@shrExact#}
90189066 {#header_close#}
9019
9067
90209068 {#header_open|@shrExact#}
90219069 <pre>{#syntax#}@shrExact(value: T, shift_amt: Log2T) T{#endsyntax#}</pre>
90229070 <p>
......@@ -9347,7 +9395,7 @@ fn doTheTest() !void {
93479395 If no overflow or underflow occurs, returns {#syntax#}false{#endsyntax#}.
93489396 </p>
93499397 {#header_close#}
9350
9398
93519399 {#header_open|@tagName#}
93529400 <pre>{#syntax#}@tagName(value: anytype) [:0]const u8{#endsyntax#}</pre>
93539401 <p>