| ... | ... | @@ -2609,16 +2609,17 @@ test "Basic vector usage" { |
| 2609 | 2609 | |
| 2610 | 2610 | test "Conversion between vectors, arrays, and slices" { |
| 2611 | 2611 | // Vectors and fixed-length arrays can be automatically assigned back and forth |
| 2612 | | var arr1: [4]f32 = [_]f32{ 1.1, 3.2, 4.5, 5.6 }; |
| 2613 | | var vec: @Vector(4, f32) = arr1; |
| 2614 | | var arr2: [4]f32 = vec; |
| 2612 | const arr1: [4]f32 = [_]f32{ 1.1, 3.2, 4.5, 5.6 }; |
| 2613 | const vec: @Vector(4, f32) = arr1; |
| 2614 | const arr2: [4]f32 = vec; |
| 2615 | 2615 | try expectEqual(arr1, arr2); |
| 2616 | 2616 | |
| 2617 | 2617 | // You can also assign from a slice with comptime-known length to a vector using .* |
| 2618 | 2618 | const vec2: @Vector(2, f32) = arr1[1..3].*; |
| 2619 | 2619 | |
| 2620 | | var slice: []const f32 = &arr1; |
| 2621 | | var offset: u32 = 1; |
| 2620 | const slice: []const f32 = &arr1; |
| 2621 | var offset: u32 = 1; // var to make it runtime-known |
| 2622 | _ = &offset; // suppress 'var is never mutated' error |
| 2622 | 2623 | // To extract a comptime-known length from a runtime-known offset, |
| 2623 | 2624 | // first extract a new slice from the starting offset, then an array of |
| 2624 | 2625 | // comptime-known length |
| ... | ... | @@ -2732,7 +2733,8 @@ test "pointer arithmetic with many-item pointer" { |
| 2732 | 2733 | |
| 2733 | 2734 | test "pointer arithmetic with slices" { |
| 2734 | 2735 | var array = [_]i32{ 1, 2, 3, 4 }; |
| 2735 | | var length: usize = 0; |
| 2736 | var length: usize = 0; // var to make it runtime-known |
| 2737 | _ = &length; // suppress 'var is never mutated' error |
| 2736 | 2738 | var slice = array[length..array.len]; |
| 2737 | 2739 | |
| 2738 | 2740 | try expect(slice[0] == 1); |
| ... | ... | @@ -2759,7 +2761,8 @@ const expect = @import("std").testing.expect; |
| 2759 | 2761 | |
| 2760 | 2762 | test "pointer slicing" { |
| 2761 | 2763 | var array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; |
| 2762 | | var start: usize = 2; |
| 2764 | var start: usize = 2; // var to make it runtime-known |
| 2765 | _ = &start; // suppress 'var is never mutated' error |
| 2763 | 2766 | const slice = array[start..4]; |
| 2764 | 2767 | try expect(slice.len == 2); |
| 2765 | 2768 | |
| ... | ... | @@ -2961,8 +2964,9 @@ const std = @import("std"); |
| 2961 | 2964 | const expect = std.testing.expect; |
| 2962 | 2965 | |
| 2963 | 2966 | test "allowzero" { |
| 2964 | | var zero: usize = 0; |
| 2965 | | var ptr: *allowzero i32 = @ptrFromInt(zero); |
| 2967 | var zero: usize = 0; // var to make to runtime-known |
| 2968 | _ = &zero; // suppress 'var is never mutated' error |
| 2969 | const ptr: *allowzero i32 = @ptrFromInt(zero); |
| 2966 | 2970 | try expect(@intFromPtr(ptr) == 0); |
| 2967 | 2971 | } |
| 2968 | 2972 | {#code_end#} |
| ... | ... | @@ -3006,6 +3010,7 @@ const expect = @import("std").testing.expect; |
| 3006 | 3010 | test "basic slices" { |
| 3007 | 3011 | var array = [_]i32{ 1, 2, 3, 4 }; |
| 3008 | 3012 | var known_at_runtime_zero: usize = 0; |
| 3013 | _ = &known_at_runtime_zero; |
| 3009 | 3014 | const slice = array[known_at_runtime_zero..array.len]; |
| 3010 | 3015 | try expect(@TypeOf(slice) == []i32); |
| 3011 | 3016 | try expect(&slice[0] == &array[0]); |
| ... | ... | @@ -3020,6 +3025,7 @@ test "basic slices" { |
| 3020 | 3025 | // to perform some optimisations like recognising a comptime-known length when |
| 3021 | 3026 | // the start position is only known at runtime. |
| 3022 | 3027 | var runtime_start: usize = 1; |
| 3028 | _ = &runtime_start; |
| 3023 | 3029 | const length = 2; |
| 3024 | 3030 | const array_ptr_len = array[runtime_start..][0..length]; |
| 3025 | 3031 | try expect(@TypeOf(array_ptr_len) == *[length]i32); |
| ... | ... | @@ -3056,7 +3062,8 @@ test "using slices for strings" { |
| 3056 | 3062 | var all_together: [100]u8 = undefined; |
| 3057 | 3063 | // You can use slice syntax with at least one runtime-known index on an |
| 3058 | 3064 | // array to convert an array into a slice. |
| 3059 | | var start : usize = 0; |
| 3065 | var start: usize = 0; |
| 3066 | _ = &start; |
| 3060 | 3067 | const all_together_slice = all_together[start..]; |
| 3061 | 3068 | // String concatenation example. |
| 3062 | 3069 | const hello_world = try fmt.bufPrint(all_together_slice, "{s} {s}", .{ hello, world }); |
| ... | ... | @@ -3075,6 +3082,7 @@ test "slice pointer" { |
| 3075 | 3082 | // A pointer to an array can be sliced just like an array: |
| 3076 | 3083 | var start: usize = 0; |
| 3077 | 3084 | var end: usize = 5; |
| 3085 | _ = .{ &start, &end }; |
| 3078 | 3086 | const slice = ptr[start..end]; |
| 3079 | 3087 | // The slice is mutable because we sliced a mutable pointer. |
| 3080 | 3088 | try expect(@TypeOf(slice) == []u8); |
| ... | ... | @@ -3121,6 +3129,7 @@ const expect = std.testing.expect; |
| 3121 | 3129 | test "0-terminated slicing" { |
| 3122 | 3130 | var array = [_]u8{ 3, 2, 1, 0, 3, 2, 1, 0 }; |
| 3123 | 3131 | var runtime_length: usize = 3; |
| 3132 | _ = &runtime_length; |
| 3124 | 3133 | const slice = array[0..runtime_length :0]; |
| 3125 | 3134 | |
| 3126 | 3135 | try expect(@TypeOf(slice) == [:0]u8); |
| ... | ... | @@ -3143,6 +3152,7 @@ test "sentinel mismatch" { |
| 3143 | 3152 | // This does not match the indicated sentinel value of `0` and will lead |
| 3144 | 3153 | // to a runtime panic. |
| 3145 | 3154 | var runtime_length: usize = 2; |
| 3155 | _ = &runtime_length; |
| 3146 | 3156 | const slice = array[0..runtime_length :0]; |
| 3147 | 3157 | |
| 3148 | 3158 | _ = slice; |
| ... | ... | @@ -3266,7 +3276,7 @@ test "linked list" { |
| 3266 | 3276 | // do this: |
| 3267 | 3277 | try expect(LinkedList(i32) == LinkedList(i32)); |
| 3268 | 3278 | |
| 3269 | | var list = LinkedList(i32) { |
| 3279 | const list = LinkedList(i32){ |
| 3270 | 3280 | .first = null, |
| 3271 | 3281 | .last = null, |
| 3272 | 3282 | .len = 0, |
| ... | ... | @@ -3278,12 +3288,12 @@ test "linked list" { |
| 3278 | 3288 | const ListOfInts = LinkedList(i32); |
| 3279 | 3289 | try expect(ListOfInts == LinkedList(i32)); |
| 3280 | 3290 | |
| 3281 | | var node = ListOfInts.Node { |
| 3291 | var node = ListOfInts.Node{ |
| 3282 | 3292 | .prev = null, |
| 3283 | 3293 | .next = null, |
| 3284 | 3294 | .data = 1234, |
| 3285 | 3295 | }; |
| 3286 | | var list2 = LinkedList(i32) { |
| 3296 | const list2 = LinkedList(i32){ |
| 3287 | 3297 | .first = &node, |
| 3288 | 3298 | .last = &node, |
| 3289 | 3299 | .len = 1, |
| ... | ... | @@ -3372,13 +3382,13 @@ test "@bitCast between packed structs" { |
| 3372 | 3382 | fn doTheTest() !void { |
| 3373 | 3383 | try expect(@sizeOf(Full) == 2); |
| 3374 | 3384 | try expect(@sizeOf(Divided) == 2); |
| 3375 | | var full = Full{ .number = 0x1234 }; |
| 3376 | | var divided: Divided = @bitCast(full); |
| 3385 | const full = Full{ .number = 0x1234 }; |
| 3386 | const divided: Divided = @bitCast(full); |
| 3377 | 3387 | try expect(divided.half1 == 0x34); |
| 3378 | 3388 | try expect(divided.quarter3 == 0x2); |
| 3379 | 3389 | try expect(divided.quarter4 == 0x1); |
| 3380 | 3390 | |
| 3381 | | var ordered: [2]u8 = @bitCast(full); |
| 3391 | const ordered: [2]u8 = @bitCast(full); |
| 3382 | 3392 | switch (native_endian) { |
| 3383 | 3393 | .big => { |
| 3384 | 3394 | try expect(ordered[0] == 0x12); |
| ... | ... | @@ -3586,7 +3596,7 @@ const expect = std.testing.expect; |
| 3586 | 3596 | const Point = struct {x: i32, y: i32}; |
| 3587 | 3597 | |
| 3588 | 3598 | test "anonymous struct literal" { |
| 3589 | | var pt: Point = .{ |
| 3599 | const pt: Point = .{ |
| 3590 | 3600 | .x = 13, |
| 3591 | 3601 | .y = 67, |
| 3592 | 3602 | }; |
| ... | ... | @@ -4051,14 +4061,14 @@ const Number = union { |
| 4051 | 4061 | }; |
| 4052 | 4062 | |
| 4053 | 4063 | test "anonymous union literal syntax" { |
| 4054 | | var i: Number = .{.int = 42}; |
| 4055 | | var f = makeNumber(); |
| 4064 | const i: Number = .{ .int = 42 }; |
| 4065 | const f = makeNumber(); |
| 4056 | 4066 | try expect(i.int == 42); |
| 4057 | 4067 | try expect(f.float == 12.34); |
| 4058 | 4068 | } |
| 4059 | 4069 | |
| 4060 | 4070 | fn makeNumber() Number { |
| 4061 | | return .{.float = 12.34}; |
| 4071 | return .{ .float = 12.34 }; |
| 4062 | 4072 | } |
| 4063 | 4073 | {#code_end#} |
| 4064 | 4074 | {#header_close#} |
| ... | ... | @@ -4098,7 +4108,7 @@ test "call foo" { |
| 4098 | 4108 | test "access variable after block scope" { |
| 4099 | 4109 | { |
| 4100 | 4110 | var x: i32 = 1; |
| 4101 | | _ = x; |
| 4111 | _ = &x; |
| 4102 | 4112 | } |
| 4103 | 4113 | x += 1; |
| 4104 | 4114 | } |
| ... | ... | @@ -4149,7 +4159,7 @@ test "separate scopes" { |
| 4149 | 4159 | } |
| 4150 | 4160 | { |
| 4151 | 4161 | var pi: bool = true; |
| 4152 | | _ = pi; |
| 4162 | _ = π |
| 4153 | 4163 | } |
| 4154 | 4164 | } |
| 4155 | 4165 | {#code_end#} |
| ... | ... | @@ -4423,7 +4433,7 @@ fn withSwitch(any: AnySlice) usize { |
| 4423 | 4433 | } |
| 4424 | 4434 | |
| 4425 | 4435 | test "inline for and inline else similarity" { |
| 4426 | | var any = AnySlice{ .c = "hello" }; |
| 4436 | const any = AnySlice{ .c = "hello" }; |
| 4427 | 4437 | try expect(withFor(any) == 5); |
| 4428 | 4438 | try expect(withSwitch(any) == 5); |
| 4429 | 4439 | } |
| ... | ... | @@ -4455,7 +4465,7 @@ fn getNum(u: U) u32 { |
| 4455 | 4465 | } |
| 4456 | 4466 | |
| 4457 | 4467 | test "test" { |
| 4458 | | var u = U{ .b = 42 }; |
| 4468 | const u = U{ .b = 42 }; |
| 4459 | 4469 | try expect(getNum(u) == 42); |
| 4460 | 4470 | } |
| 4461 | 4471 | {#code_end#} |
| ... | ... | @@ -4762,7 +4772,7 @@ test "multi object for" { |
| 4762 | 4772 | } |
| 4763 | 4773 | |
| 4764 | 4774 | test "for reference" { |
| 4765 | | var items = [_]i32 { 3, 4, 2 }; |
| 4775 | var items = [_]i32{ 3, 4, 2 }; |
| 4766 | 4776 | |
| 4767 | 4777 | // Iterate over the slice by reference by |
| 4768 | 4778 | // specifying that the capture value is a pointer. |
| ... | ... | @@ -4777,7 +4787,7 @@ test "for reference" { |
| 4777 | 4787 | |
| 4778 | 4788 | test "for else" { |
| 4779 | 4789 | // For allows an else attached to it, the same as a while loop. |
| 4780 | | var items = [_]?i32 { 3, 4, null, 5 }; |
| 4790 | const items = [_]?i32{ 3, 4, null, 5 }; |
| 4781 | 4791 | |
| 4782 | 4792 | // For loops can also be used as expressions. |
| 4783 | 4793 | // Similar to while loops, when you break from a for loop, the else branch is not evaluated. |
| ... | ... | @@ -5347,7 +5357,7 @@ fn addFortyTwo(x: anytype) @TypeOf(x) { |
| 5347 | 5357 | test "fn type inference" { |
| 5348 | 5358 | try expect(addFortyTwo(1) == 43); |
| 5349 | 5359 | try expect(@TypeOf(addFortyTwo(1)) == comptime_int); |
| 5350 | | var y: i64 = 2; |
| 5360 | const y: i64 = 2; |
| 5351 | 5361 | try expect(addFortyTwo(y) == 44); |
| 5352 | 5362 | try expect(@TypeOf(addFortyTwo(y)) == i64); |
| 5353 | 5363 | } |
| ... | ... | @@ -5795,7 +5805,7 @@ fn getData() !u32 { |
| 5795 | 5805 | } |
| 5796 | 5806 | |
| 5797 | 5807 | fn genFoos(allocator: Allocator, num: usize) ![]Foo { |
| 5798 | | var foos = try allocator.alloc(Foo, num); |
| 5808 | const foos = try allocator.alloc(Foo, num); |
| 5799 | 5809 | errdefer allocator.free(foos); |
| 5800 | 5810 | |
| 5801 | 5811 | for (foos, 0..) |*foo, i| { |
| ... | ... | @@ -5833,7 +5843,7 @@ fn getData() !u32 { |
| 5833 | 5843 | } |
| 5834 | 5844 | |
| 5835 | 5845 | fn genFoos(allocator: Allocator, num: usize) ![]Foo { |
| 5836 | | var foos = try allocator.alloc(Foo, num); |
| 5846 | const foos = try allocator.alloc(Foo, num); |
| 5837 | 5847 | errdefer allocator.free(foos); |
| 5838 | 5848 | |
| 5839 | 5849 | // Used to track how many foos have been initialized |
| ... | ... | @@ -6325,13 +6335,13 @@ test "optional pointers" { |
| 6325 | 6335 | </p> |
| 6326 | 6336 | {#code_begin|test|test_type_coercion#} |
| 6327 | 6337 | test "type coercion - variable declaration" { |
| 6328 | | var a: u8 = 1; |
| 6329 | | var b: u16 = a; |
| 6338 | const a: u8 = 1; |
| 6339 | const b: u16 = a; |
| 6330 | 6340 | _ = b; |
| 6331 | 6341 | } |
| 6332 | 6342 | |
| 6333 | 6343 | test "type coercion - function call" { |
| 6334 | | var a: u8 = 1; |
| 6344 | const a: u8 = 1; |
| 6335 | 6345 | foo(a); |
| 6336 | 6346 | } |
| 6337 | 6347 | |
| ... | ... | @@ -6340,8 +6350,8 @@ fn foo(b: u16) void { |
| 6340 | 6350 | } |
| 6341 | 6351 | |
| 6342 | 6352 | test "type coercion - @as builtin" { |
| 6343 | | var a: u8 = 1; |
| 6344 | | var b = @as(u16, a); |
| 6353 | const a: u8 = 1; |
| 6354 | const b = @as(u16, a); |
| 6345 | 6355 | _ = b; |
| 6346 | 6356 | } |
| 6347 | 6357 | {#code_end#} |
| ... | ... | @@ -6366,7 +6376,7 @@ test "type coercion - @as builtin" { |
| 6366 | 6376 | {#code_begin|test|test_no_op_casts#} |
| 6367 | 6377 | test "type coercion - const qualification" { |
| 6368 | 6378 | var a: i32 = 1; |
| 6369 | | var b: *i32 = &a; |
| 6379 | const b: *i32 = &a; |
| 6370 | 6380 | foo(b); |
| 6371 | 6381 | } |
| 6372 | 6382 | |
| ... | ... | @@ -6399,26 +6409,26 @@ const expect = std.testing.expect; |
| 6399 | 6409 | const mem = std.mem; |
| 6400 | 6410 | |
| 6401 | 6411 | test "integer widening" { |
| 6402 | | var a: u8 = 250; |
| 6403 | | var b: u16 = a; |
| 6404 | | var c: u32 = b; |
| 6405 | | var d: u64 = c; |
| 6406 | | var e: u64 = d; |
| 6407 | | var f: u128 = e; |
| 6412 | const a: u8 = 250; |
| 6413 | const b: u16 = a; |
| 6414 | const c: u32 = b; |
| 6415 | const d: u64 = c; |
| 6416 | const e: u64 = d; |
| 6417 | const f: u128 = e; |
| 6408 | 6418 | try expect(f == a); |
| 6409 | 6419 | } |
| 6410 | 6420 | |
| 6411 | 6421 | test "implicit unsigned integer to signed integer" { |
| 6412 | | var a: u8 = 250; |
| 6413 | | var b: i16 = a; |
| 6422 | const a: u8 = 250; |
| 6423 | const b: i16 = a; |
| 6414 | 6424 | try expect(b == 250); |
| 6415 | 6425 | } |
| 6416 | 6426 | |
| 6417 | 6427 | test "float widening" { |
| 6418 | | var a: f16 = 12.34; |
| 6419 | | var b: f32 = a; |
| 6420 | | var c: f64 = b; |
| 6421 | | var d: f128 = c; |
| 6428 | const a: f16 = 12.34; |
| 6429 | const b: f32 = a; |
| 6430 | const c: f64 = b; |
| 6431 | const d: f128 = c; |
| 6422 | 6432 | try expect(d == a); |
| 6423 | 6433 | } |
| 6424 | 6434 | {#code_end#} |
| ... | ... | @@ -6435,7 +6445,7 @@ test "float widening" { |
| 6435 | 6445 | {#code_begin|test_err|test_ambiguous_coercion#} |
| 6436 | 6446 | // Compile time coercion of float to int |
| 6437 | 6447 | test "implicit cast to comptime_int" { |
| 6438 | | var f: f32 = 54.0 / 5; |
| 6448 | const f: f32 = 54.0 / 5; |
| 6439 | 6449 | _ = f; |
| 6440 | 6450 | } |
| 6441 | 6451 | {#code_end#} |
| ... | ... | @@ -6449,31 +6459,31 @@ const expect = std.testing.expect; |
| 6449 | 6459 | // const modifier on the element type. Useful in particular for |
| 6450 | 6460 | // String literals. |
| 6451 | 6461 | test "*const [N]T to []const T" { |
| 6452 | | var x1: []const u8 = "hello"; |
| 6453 | | var x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 6462 | const x1: []const u8 = "hello"; |
| 6463 | const x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 6454 | 6464 | try expect(std.mem.eql(u8, x1, x2)); |
| 6455 | 6465 | |
| 6456 | | var y: []const f32 = &[2]f32{ 1.2, 3.4 }; |
| 6466 | const y: []const f32 = &[2]f32{ 1.2, 3.4 }; |
| 6457 | 6467 | try expect(y[0] == 1.2); |
| 6458 | 6468 | } |
| 6459 | 6469 | |
| 6460 | 6470 | // Likewise, it works when the destination type is an error union. |
| 6461 | 6471 | test "*const [N]T to E![]const T" { |
| 6462 | | var x1: anyerror![]const u8 = "hello"; |
| 6463 | | var x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 6472 | const x1: anyerror![]const u8 = "hello"; |
| 6473 | const x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 6464 | 6474 | try expect(std.mem.eql(u8, try x1, try x2)); |
| 6465 | 6475 | |
| 6466 | | var y: anyerror![]const f32 = &[2]f32{ 1.2, 3.4 }; |
| 6476 | const y: anyerror![]const f32 = &[2]f32{ 1.2, 3.4 }; |
| 6467 | 6477 | try expect((try y)[0] == 1.2); |
| 6468 | 6478 | } |
| 6469 | 6479 | |
| 6470 | 6480 | // Likewise, it works when the destination type is an optional. |
| 6471 | 6481 | test "*const [N]T to ?[]const T" { |
| 6472 | | var x1: ?[]const u8 = "hello"; |
| 6473 | | var x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 6482 | const x1: ?[]const u8 = "hello"; |
| 6483 | const x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 }; |
| 6474 | 6484 | try expect(std.mem.eql(u8, x1.?, x2.?)); |
| 6475 | 6485 | |
| 6476 | | var y: ?[]const f32 = &[2]f32{ 1.2, 3.4 }; |
| 6486 | const y: ?[]const f32 = &[2]f32{ 1.2, 3.4 }; |
| 6477 | 6487 | try expect(y.?[0] == 1.2); |
| 6478 | 6488 | } |
| 6479 | 6489 | |
| ... | ... | @@ -6609,18 +6619,18 @@ const U2 = union(enum) { |
| 6609 | 6619 | }; |
| 6610 | 6620 | |
| 6611 | 6621 | test "coercion between unions and enums" { |
| 6612 | | var u = U{ .two = 12.34 }; |
| 6613 | | var e: E = u; // coerce union to enum |
| 6622 | const u = U{ .two = 12.34 }; |
| 6623 | const e: E = u; // coerce union to enum |
| 6614 | 6624 | try expect(e == E.two); |
| 6615 | 6625 | |
| 6616 | 6626 | const three = E.three; |
| 6617 | | var u_2: U = three; // coerce enum to union |
| 6627 | const u_2: U = three; // coerce enum to union |
| 6618 | 6628 | try expect(u_2 == E.three); |
| 6619 | 6629 | |
| 6620 | | var u_3: U = .three; // coerce enum literal to union |
| 6630 | const u_3: U = .three; // coerce enum literal to union |
| 6621 | 6631 | try expect(u_3 == E.three); |
| 6622 | 6632 | |
| 6623 | | var u_4: U2 = .a; // coerce enum literal to union with inferred enum tag type. |
| 6633 | const u_4: U2 = .a; // coerce enum literal to union with inferred enum tag type. |
| 6624 | 6634 | try expect(u_4.tag() == 1); |
| 6625 | 6635 | |
| 6626 | 6636 | // The following example is invalid. |
| ... | ... | @@ -6698,9 +6708,9 @@ const expect = std.testing.expect; |
| 6698 | 6708 | const mem = std.mem; |
| 6699 | 6709 | |
| 6700 | 6710 | test "peer resolve int widening" { |
| 6701 | | var a: i8 = 12; |
| 6702 | | var b: i16 = 34; |
| 6703 | | var c = a + b; |
| 6711 | const a: i8 = 12; |
| 6712 | const b: i16 = 34; |
| 6713 | const c = a + b; |
| 6704 | 6714 | try expect(c == 46); |
| 6705 | 6715 | try expect(@TypeOf(c) == i16); |
| 6706 | 6716 | } |
| ... | ... | @@ -6809,6 +6819,7 @@ export fn entry() void { |
| 6809 | 6819 | var x: void = {}; |
| 6810 | 6820 | var y: void = {}; |
| 6811 | 6821 | x = y; |
| 6822 | y = x; |
| 6812 | 6823 | } |
| 6813 | 6824 | {#code_end#} |
| 6814 | 6825 | <p>When this turns into machine code, there is no code generated in the |
| ... | ... | @@ -7121,6 +7132,7 @@ fn performFn(start_value: i32) i32 { |
| 7121 | 7132 | // expect(performFn('w', 99) == 99); |
| 7122 | 7133 | fn performFn(start_value: i32) i32 { |
| 7123 | 7134 | var result: i32 = start_value; |
| 7135 | _ = &result; |
| 7124 | 7136 | return result; |
| 7125 | 7137 | } |
| 7126 | 7138 | {#end_syntax_block#} |
| ... | ... | @@ -8664,8 +8676,9 @@ test "@hasDecl" { |
| 8664 | 8676 | </p> |
| 8665 | 8677 | {#code_begin|test_err|test_intCast_builtin|cast truncated bits#} |
| 8666 | 8678 | test "integer cast panic" { |
| 8667 | | var a: u16 = 0xabcd; |
| 8668 | | var b: u8 = @intCast(a); |
| 8679 | var a: u16 = 0xabcd; // runtime-known |
| 8680 | _ = &a; |
| 8681 | const b: u8 = @intCast(a); |
| 8669 | 8682 | _ = b; |
| 8670 | 8683 | } |
| 8671 | 8684 | {#code_end#} |
| ... | ... | @@ -8825,7 +8838,7 @@ const expect = std.testing.expect; |
| 8825 | 8838 | test "@wasmMemoryGrow" { |
| 8826 | 8839 | if (native_arch != .wasm32) return error.SkipZigTest; |
| 8827 | 8840 | |
| 8828 | | var prev = @wasmMemorySize(0); |
| 8841 | const prev = @wasmMemorySize(0); |
| 8829 | 8842 | try expect(prev == @wasmMemoryGrow(0, 1)); |
| 8830 | 8843 | try expect(prev + 1 == @wasmMemorySize(0)); |
| 8831 | 8844 | } |
| ... | ... | @@ -9560,8 +9573,8 @@ const std = @import("std"); |
| 9560 | 9573 | const expect = std.testing.expect; |
| 9561 | 9574 | |
| 9562 | 9575 | test "integer truncation" { |
| 9563 | | var a: u16 = 0xabcd; |
| 9564 | | var b: u8 = @truncate(a); |
| 9576 | const a: u16 = 0xabcd; |
| 9577 | const b: u8 = @truncate(a); |
| 9565 | 9578 | try expect(b == 0xcd); |
| 9566 | 9579 | } |
| 9567 | 9580 | {#code_end#} |
| ... | ... | @@ -9845,7 +9858,7 @@ comptime { |
| 9845 | 9858 | <p>At runtime:</p> |
| 9846 | 9859 | {#code_begin|exe_err|runtime_index_out_of_bounds#} |
| 9847 | 9860 | pub fn main() void { |
| 9848 | | var x = foo("hello"); |
| 9861 | const x = foo("hello"); |
| 9849 | 9862 | _ = x; |
| 9850 | 9863 | } |
| 9851 | 9864 | |
| ... | ... | @@ -9858,7 +9871,7 @@ fn foo(x: []const u8) u8 { |
| 9858 | 9871 | <p>At compile-time:</p> |
| 9859 | 9872 | {#code_begin|test_err|test_comptime_invalid_cast|type 'u32' cannot represent integer value '-1'#} |
| 9860 | 9873 | comptime { |
| 9861 | | var value: i32 = -1; |
| 9874 | const value: i32 = -1; |
| 9862 | 9875 | const unsigned: u32 = @intCast(value); |
| 9863 | 9876 | _ = unsigned; |
| 9864 | 9877 | } |
| ... | ... | @@ -9868,8 +9881,9 @@ comptime { |
| 9868 | 9881 | const std = @import("std"); |
| 9869 | 9882 | |
| 9870 | 9883 | pub fn main() void { |
| 9871 | | var value: i32 = -1; |
| 9872 | | var unsigned: u32 = @intCast(value); |
| 9884 | var value: i32 = -1; // runtime-known |
| 9885 | _ = &value; |
| 9886 | const unsigned: u32 = @intCast(value); |
| 9873 | 9887 | std.debug.print("value: {}\n", .{unsigned}); |
| 9874 | 9888 | } |
| 9875 | 9889 | {#code_end#} |
| ... | ... | @@ -9891,7 +9905,8 @@ comptime { |
| 9891 | 9905 | const std = @import("std"); |
| 9892 | 9906 | |
| 9893 | 9907 | pub fn main() void { |
| 9894 | | var spartan_count: u16 = 300; |
| 9908 | var spartan_count: u16 = 300; // runtime-known |
| 9909 | _ = &spartan_count; |
| 9895 | 9910 | const byte: u8 = @intCast(spartan_count); |
| 9896 | 9911 | std.debug.print("value: {}\n", .{byte}); |
| 9897 | 9912 | } |
| ... | ... | @@ -9975,7 +9990,7 @@ pub fn main() !void { |
| 9975 | 9990 | {#code_begin|exe|addWithOverflow_builtin#} |
| 9976 | 9991 | const print = @import("std").debug.print; |
| 9977 | 9992 | pub fn main() void { |
| 9978 | | var byte: u8 = 255; |
| 9993 | const byte: u8 = 255; |
| 9979 | 9994 | |
| 9980 | 9995 | const ov = @addWithOverflow(byte, 10); |
| 9981 | 9996 | if (ov[1] != 0) { |
| ... | ... | @@ -10025,8 +10040,9 @@ comptime { |
| 10025 | 10040 | const std = @import("std"); |
| 10026 | 10041 | |
| 10027 | 10042 | pub fn main() void { |
| 10028 | | var x: u8 = 0b01010101; |
| 10029 | | var y = @shlExact(x, 2); |
| 10043 | var x: u8 = 0b01010101; // runtime-known |
| 10044 | _ = &x; |
| 10045 | const y = @shlExact(x, 2); |
| 10030 | 10046 | std.debug.print("value: {}\n", .{y}); |
| 10031 | 10047 | } |
| 10032 | 10048 | {#code_end#} |
| ... | ... | @@ -10044,8 +10060,9 @@ comptime { |
| 10044 | 10060 | const std = @import("std"); |
| 10045 | 10061 | |
| 10046 | 10062 | pub fn main() void { |
| 10047 | | var x: u8 = 0b10101010; |
| 10048 | | var y = @shrExact(x, 2); |
| 10063 | var x: u8 = 0b10101010; // runtime-known |
| 10064 | _ = &x; |
| 10065 | const y = @shrExact(x, 2); |
| 10049 | 10066 | std.debug.print("value: {}\n", .{y}); |
| 10050 | 10067 | } |
| 10051 | 10068 | {#code_end#} |
| ... | ... | @@ -10067,7 +10084,8 @@ const std = @import("std"); |
| 10067 | 10084 | pub fn main() void { |
| 10068 | 10085 | var a: u32 = 1; |
| 10069 | 10086 | var b: u32 = 0; |
| 10070 | | var c = a / b; |
| 10087 | _ = .{ &a, &b }; |
| 10088 | const c = a / b; |
| 10071 | 10089 | std.debug.print("value: {}\n", .{c}); |
| 10072 | 10090 | } |
| 10073 | 10091 | {#code_end#} |
| ... | ... | @@ -10089,7 +10107,8 @@ const std = @import("std"); |
| 10089 | 10107 | pub fn main() void { |
| 10090 | 10108 | var a: u32 = 10; |
| 10091 | 10109 | var b: u32 = 0; |
| 10092 | | var c = a % b; |
| 10110 | _ = .{ &a, &b }; |
| 10111 | const c = a % b; |
| 10093 | 10112 | std.debug.print("value: {}\n", .{c}); |
| 10094 | 10113 | } |
| 10095 | 10114 | {#code_end#} |
| ... | ... | @@ -10111,7 +10130,8 @@ const std = @import("std"); |
| 10111 | 10130 | pub fn main() void { |
| 10112 | 10131 | var a: u32 = 10; |
| 10113 | 10132 | var b: u32 = 3; |
| 10114 | | var c = @divExact(a, b); |
| 10133 | _ = .{ &a, &b }; |
| 10134 | const c = @divExact(a, b); |
| 10115 | 10135 | std.debug.print("value: {}\n", .{c}); |
| 10116 | 10136 | } |
| 10117 | 10137 | {#code_end#} |
| ... | ... | @@ -10131,7 +10151,8 @@ const std = @import("std"); |
| 10131 | 10151 | |
| 10132 | 10152 | pub fn main() void { |
| 10133 | 10153 | var optional_number: ?i32 = null; |
| 10134 | | var number = optional_number.?; |
| 10154 | _ = &optional_number; |
| 10155 | const number = optional_number.?; |
| 10135 | 10156 | std.debug.print("value: {}\n", .{number}); |
| 10136 | 10157 | } |
| 10137 | 10158 | {#code_end#} |
| ... | ... | @@ -10212,9 +10233,10 @@ comptime { |
| 10212 | 10233 | const std = @import("std"); |
| 10213 | 10234 | |
| 10214 | 10235 | pub fn main() void { |
| 10215 | | var err = error.AnError; |
| 10236 | const err = error.AnError; |
| 10216 | 10237 | var number = @intFromError(err) + 500; |
| 10217 | | var invalid_err = @errorFromInt(number); |
| 10238 | _ = &number; |
| 10239 | const invalid_err = @errorFromInt(number); |
| 10218 | 10240 | std.debug.print("value: {}\n", .{invalid_err}); |
| 10219 | 10241 | } |
| 10220 | 10242 | {#code_end#} |
| ... | ... | @@ -10245,7 +10267,8 @@ const Foo = enum { |
| 10245 | 10267 | |
| 10246 | 10268 | pub fn main() void { |
| 10247 | 10269 | var a: u2 = 3; |
| 10248 | | var b: Foo = @enumFromInt(a); |
| 10270 | _ = &a; |
| 10271 | const b: Foo = @enumFromInt(a); |
| 10249 | 10272 | std.debug.print("value: {s}\n", .{@tagName(b)}); |
| 10250 | 10273 | } |
| 10251 | 10274 | {#code_end#} |
| ... | ... | @@ -10402,17 +10425,18 @@ fn bar(f: *Foo) void { |
| 10402 | 10425 | <p>At compile-time:</p> |
| 10403 | 10426 | {#code_begin|test_err|test_comptime_out_of_bounds_float_to_integer_cast|float value '4294967296' cannot be stored in integer type 'i32'#} |
| 10404 | 10427 | comptime { |
| 10405 | | 	const float: f32 = 4294967296; |
| 10406 | | 	const int: i32 = @intFromFloat(float); |
| 10407 | | 	_ = int; |
| 10428 | const float: f32 = 4294967296; |
| 10429 | const int: i32 = @intFromFloat(float); |
| 10430 | _ = int; |
| 10408 | 10431 | } |
| 10409 | 10432 | {#code_end#} |
| 10410 | 10433 | <p>At runtime:</p> |
| 10411 | 10434 | {#code_begin|exe_err|runtime_out_of_bounds_float_to_integer_cast#} |
| 10412 | 10435 | pub fn main() void { |
| 10413 | | 	var float: f32 = 4294967296; |
| 10414 | | 	var int: i32 = @intFromFloat(float); |
| 10415 | | 	_ = int; |
| 10436 | var float: f32 = 4294967296; // runtime-known |
| 10437 | _ = &float; |
| 10438 | const int: i32 = @intFromFloat(float); |
| 10439 | _ = int; |
| 10416 | 10440 | } |
| 10417 | 10441 | {#code_end#} |
| 10418 | 10442 | {#header_close#} |
| ... | ... | @@ -10435,7 +10459,8 @@ comptime { |
| 10435 | 10459 | {#code_begin|exe_err|runtime_invalid_null_pointer_cast#} |
| 10436 | 10460 | pub fn main() void { |
| 10437 | 10461 | var opt_ptr: ?*i32 = null; |
| 10438 | | var ptr: *i32 = @ptrCast(opt_ptr); |
| 10462 | _ = &opt_ptr; |
| 10463 | const ptr: *i32 = @ptrCast(opt_ptr); |
| 10439 | 10464 | _ = ptr; |
| 10440 | 10465 | } |
| 10441 | 10466 | {#code_end#} |
| ... | ... | @@ -11120,7 +11145,9 @@ int foo(void) { |
| 11120 | 11145 | {#code_begin|syntax|macro#} |
| 11121 | 11146 | pub export fn foo() c_int { |
| 11122 | 11147 | var a: c_int = 1; |
| 11148 | _ = &a; |
| 11123 | 11149 | var b: c_int = 2; |
| 11150 | _ = &b; |
| 11124 | 11151 | return a + b; |
| 11125 | 11152 | } |
| 11126 | 11153 | pub const MAKELOCAL = @compileError("unable to translate C expr: unexpected token .Equal"); // macro.c:1:9 |