authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-11-17 13:16:07+00:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2023-11-19 11:11:49+00:00
loged4bab66d8a0c518245a6ca1ff401f0b30befe64
treeb288939bc5dea5c2345246ccaa56447267ca80b4
parent026a8278f829670b434fddce34030290b6910898
signature Commit is signed but in an unrecognized format.

langref: correct unnecessary uses of 'var'


1 files changed, 121 insertions(+), 94 deletions(-)

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