| author | |
| committer | |
| log | 87a7ea4c420ca0db0e13fab82ce08ab4e293d1da |
| tree | fed7d84fc1ce023c47f3b4a8f77b898b4396c111 |
| parent | 1ee59c5c31efca394d7e6d9da3f64c289a996b99 |
| parent | 95fefcd4c91e517a51f4b55924979421c6f7e8d3 |
| signature |
Fix missing const on address of literal9 files changed, 62 insertions(+), 31 deletions(-)
lib/std/json.zig+6-5| ... | ... | @@ -1327,12 +1327,13 @@ test "Value.jsonStringify" { |
| 1327 | 1327 | { |
| 1328 | 1328 | var buffer: [10]u8 = undefined; |
| 1329 | 1329 | var fbs = std.io.fixedBufferStream(&buffer); |
| 1330 | var vals = [_]Value{ | |
| 1331 | .{ .Integer = 1 }, | |
| 1332 | .{ .Integer = 2 }, | |
| 1333 | .{ .Integer = 3 }, | |
| 1334 | }; | |
| 1330 | 1335 | try (Value{ |
| 1331 | .Array = Array.fromOwnedSlice(undefined, &[_]Value{ | |
| 1332 | .{ .Integer = 1 }, | |
| 1333 | .{ .Integer = 2 }, | |
| 1334 | .{ .Integer = 3 }, | |
| 1335 | }), | |
| 1336 | .Array = Array.fromOwnedSlice(undefined, &vals), | |
| 1336 | 1337 | }).jsonStringify(.{}, fbs.outStream()); |
| 1337 | 1338 | testing.expectEqualSlices(u8, fbs.getWritten(), "[1,2,3]"); |
| 1338 | 1339 | } |
lib/std/testing.zig+2-1| ... | ... | @@ -7,7 +7,8 @@ pub const FailingAllocator = @import("testing/failing_allocator.zig").FailingAll |
| 7 | 7 | pub const allocator = &allocator_instance.allocator; |
| 8 | 8 | pub var allocator_instance = LeakCountAllocator.init(&base_allocator_instance.allocator); |
| 9 | 9 | |
| 10 | pub const failing_allocator = &FailingAllocator.init(&base_allocator_instance.allocator, 0).allocator; | |
| 10 | pub const failing_allocator = &failing_allocator_instance.allocator; | |
| 11 | pub var failing_allocator_instance = FailingAllocator.init(&base_allocator_instance.allocator, 0); | |
| 11 | 12 | |
| 12 | 13 | pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]); |
| 13 | 14 | var allocator_mem: [1024 * 1024]u8 = undefined; |
src/all_types.hpp-2| ... | ... | @@ -3519,8 +3519,6 @@ struct IrInstSrcRef { |
| 3519 | 3519 | IrInstSrc base; |
| 3520 | 3520 | |
| 3521 | 3521 | IrInstSrc *value; |
| 3522 | bool is_const; | |
| 3523 | bool is_volatile; | |
| 3524 | 3522 | }; |
| 3525 | 3523 | |
| 3526 | 3524 | struct IrInstGenRef { |
src/ir.cpp+13-8| ... | ... | @@ -3290,13 +3290,9 @@ static IrInstSrc *ir_build_import(IrBuilderSrc *irb, Scope *scope, AstNode *sour |
| 3290 | 3290 | return &instruction->base; |
| 3291 | 3291 | } |
| 3292 | 3292 | |
| 3293 | static IrInstSrc *ir_build_ref_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, IrInstSrc *value, | |
| 3294 | bool is_const, bool is_volatile) | |
| 3295 | { | |
| 3293 | static IrInstSrc *ir_build_ref_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, IrInstSrc *value) { | |
| 3296 | 3294 | IrInstSrcRef *instruction = ir_build_instruction<IrInstSrcRef>(irb, scope, source_node); |
| 3297 | 3295 | instruction->value = value; |
| 3298 | instruction->is_const = is_const; | |
| 3299 | instruction->is_volatile = is_volatile; | |
| 3300 | 3296 | |
| 3301 | 3297 | ir_ref_instruction(value, irb->current_basic_block); |
| 3302 | 3298 | |
| ... | ... | @@ -5938,7 +5934,7 @@ static IrInstSrc *ir_gen_symbol(IrBuilderSrc *irb, Scope *scope, AstNode *node, |
| 5938 | 5934 | } else { |
| 5939 | 5935 | IrInstSrc *value = ir_build_const_type(irb, scope, node, primitive_type); |
| 5940 | 5936 | if (lval == LValPtr) { |
| 5941 | return ir_build_ref_src(irb, scope, node, value, false, false); | |
| 5937 | return ir_build_ref_src(irb, scope, node, value); | |
| 5942 | 5938 | } else { |
| 5943 | 5939 | return ir_expr_wrap(irb, scope, value, result_loc); |
| 5944 | 5940 | } |
| ... | ... | @@ -7486,7 +7482,7 @@ static IrInstSrc *ir_lval_wrap(IrBuilderSrc *irb, Scope *scope, IrInstSrc *value |
| 7486 | 7482 | if (lval == LValPtr) { |
| 7487 | 7483 | // We needed a pointer to a value, but we got a value. So we create |
| 7488 | 7484 | // an instruction which just makes a pointer of it. |
| 7489 | return ir_build_ref_src(irb, scope, value->base.source_node, value, false, false); | |
| 7485 | return ir_build_ref_src(irb, scope, value->base.source_node, value); | |
| 7490 | 7486 | } else if (result_loc != nullptr) { |
| 7491 | 7487 | return ir_expr_wrap(irb, scope, value, result_loc); |
| 7492 | 7488 | } else { |
| ... | ... | @@ -23348,7 +23344,16 @@ static IrInstGen *ir_analyze_instruction_ref(IrAnalyze *ira, IrInstSrcRef *ref_i |
| 23348 | 23344 | IrInstGen *value = ref_instruction->value->child; |
| 23349 | 23345 | if (type_is_invalid(value->value->type)) |
| 23350 | 23346 | return ira->codegen->invalid_inst_gen; |
| 23351 | return ir_get_ref(ira, &ref_instruction->base.base, value, ref_instruction->is_const, ref_instruction->is_volatile); | |
| 23347 | ||
| 23348 | bool is_const = false; | |
| 23349 | bool is_volatile = false; | |
| 23350 | ||
| 23351 | ZigValue *child_value = value->value; | |
| 23352 | if (child_value->special == ConstValSpecialStatic) { | |
| 23353 | is_const = true; | |
| 23354 | } | |
| 23355 | ||
| 23356 | return ir_get_ref(ira, &ref_instruction->base.base, value, is_const, is_volatile); | |
| 23352 | 23357 | } |
| 23353 | 23358 | |
| 23354 | 23359 | static IrInstGen *ir_analyze_union_init(IrAnalyze *ira, IrInst* source_instruction, |
src/ir_print.cpp+1-3| ... | ... | @@ -1476,9 +1476,7 @@ static void ir_print_import(IrPrintSrc *irp, IrInstSrcImport *instruction) { |
| 1476 | 1476 | } |
| 1477 | 1477 | |
| 1478 | 1478 | static void ir_print_ref(IrPrintSrc *irp, IrInstSrcRef *instruction) { |
| 1479 | const char *const_str = instruction->is_const ? "const " : ""; | |
| 1480 | const char *volatile_str = instruction->is_volatile ? "volatile " : ""; | |
| 1481 | fprintf(irp->f, "%s%sref ", const_str, volatile_str); | |
| 1479 | fprintf(irp->f, "ref "); | |
| 1482 | 1480 | ir_print_other_inst_src(irp, instruction->value); |
| 1483 | 1481 | } |
| 1484 | 1482 |
test/compile_errors.zig+33-5| ... | ... | @@ -2,6 +2,34 @@ const tests = @import("tests.zig"); |
| 2 | 2 | const std = @import("std"); |
| 3 | 3 | |
| 4 | 4 | pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 5 | cases.addTest("reference to const data", | |
| 6 | \\export fn foo() void { | |
| 7 | \\ var ptr = &[_]u8{0,0,0,0}; | |
| 8 | \\ ptr[1] = 2; | |
| 9 | \\} | |
| 10 | \\export fn bar() void { | |
| 11 | \\ var ptr = &@as(u32, 2); | |
| 12 | \\ ptr.* = 2; | |
| 13 | \\} | |
| 14 | \\export fn baz() void { | |
| 15 | \\ var ptr = &true; | |
| 16 | \\ ptr.* = false; | |
| 17 | \\} | |
| 18 | \\export fn qux() void { | |
| 19 | \\ const S = struct{ | |
| 20 | \\ x: usize, | |
| 21 | \\ y: usize, | |
| 22 | \\ }; | |
| 23 | \\ var ptr = &S{.x=1,.y=2}; | |
| 24 | \\ ptr.x = 2; | |
| 25 | \\} | |
| 26 | , &[_][]const u8{ | |
| 27 | "tmp.zig:3:14: error: cannot assign to constant", | |
| 28 | "tmp.zig:7:13: error: cannot assign to constant", | |
| 29 | "tmp.zig:11:13: error: cannot assign to constant", | |
| 30 | "tmp.zig:19:13: error: cannot assign to constant", | |
| 31 | }); | |
| 32 | ||
| 5 | 33 | cases.addTest("cast between ?T where T is not a pointer", |
| 6 | 34 | \\pub const fnty1 = ?fn (i8) void; |
| 7 | 35 | \\pub const fnty2 = ?fn (u64) void; |
| ... | ... | @@ -969,7 +997,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 969 | 997 | \\ const x = 1 << &@as(u8, 10); |
| 970 | 998 | \\} |
| 971 | 999 | , &[_][]const u8{ |
| 972 | "tmp.zig:2:21: error: shift amount has to be an integer type, but found '*u8'", | |
| 1000 | "tmp.zig:2:21: error: shift amount has to be an integer type, but found '*const u8'", | |
| 973 | 1001 | "tmp.zig:2:17: note: referenced here", |
| 974 | 1002 | }); |
| 975 | 1003 | |
| ... | ... | @@ -978,7 +1006,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 978 | 1006 | \\ const x = &@as(u8, 1) << 10; |
| 979 | 1007 | \\} |
| 980 | 1008 | , &[_][]const u8{ |
| 981 | "tmp.zig:2:16: error: bit shifting operation expected integer type, found '*u8'", | |
| 1009 | "tmp.zig:2:16: error: bit shifting operation expected integer type, found '*const u8'", | |
| 982 | 1010 | "tmp.zig:2:27: note: referenced here", |
| 983 | 1011 | }); |
| 984 | 1012 | |
| ... | ... | @@ -6005,7 +6033,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 6005 | 6033 | \\ fn bar(self: *const Foo) void {} |
| 6006 | 6034 | \\}; |
| 6007 | 6035 | , &[_][]const u8{ |
| 6008 | "tmp.zig:2:4: error: variable of type '*comptime_int' must be const or comptime", | |
| 6036 | "tmp.zig:2:4: error: variable of type '*const comptime_int' must be const or comptime", | |
| 6009 | 6037 | "tmp.zig:5:4: error: variable of type '(undefined)' must be const or comptime", |
| 6010 | 6038 | "tmp.zig:8:4: error: variable of type 'comptime_int' must be const or comptime", |
| 6011 | 6039 | "tmp.zig:11:4: error: variable of type 'comptime_float' must be const or comptime", |
| ... | ... | @@ -6849,12 +6877,12 @@ pub fn addCases(cases: *tests.CompileErrorContext) void { |
| 6849 | 6877 | \\ const word: u16 = @bitCast(u16, bytes[0..]); |
| 6850 | 6878 | \\} |
| 6851 | 6879 | \\export fn foo2() void { |
| 6852 | \\ var bytes: []u8 = &[_]u8{1, 2}; | |
| 6880 | \\ var bytes: []const u8 = &[_]u8{1, 2}; | |
| 6853 | 6881 | \\ const word: u16 = @bitCast(u16, bytes); |
| 6854 | 6882 | \\} |
| 6855 | 6883 | , &[_][]const u8{ |
| 6856 | 6884 | "tmp.zig:3:42: error: unable to @bitCast from pointer type '*[2]u8'", |
| 6857 | "tmp.zig:7:32: error: destination type 'u16' has size 2 but source type '[]u8' has size 16", | |
| 6885 | "tmp.zig:7:32: error: destination type 'u16' has size 2 but source type '[]const u8' has size 16", | |
| 6858 | 6886 | "tmp.zig:7:37: note: referenced here", |
| 6859 | 6887 | }); |
| 6860 | 6888 |
test/stage1/behavior/for.zig+2-2| ... | ... | @@ -161,8 +161,8 @@ test "for copies its payload" { |
| 161 | 161 | |
| 162 | 162 | test "for on slice with allowzero ptr" { |
| 163 | 163 | const S = struct { |
| 164 | fn doTheTest(slice: []u8) void { | |
| 165 | var ptr = @ptrCast([*]allowzero u8, slice.ptr)[0..slice.len]; | |
| 164 | fn doTheTest(slice: []const u8) void { | |
| 165 | var ptr = @ptrCast([*]const allowzero u8, slice.ptr)[0..slice.len]; | |
| 166 | 166 | for (ptr) |x, i| expect(x == i + 1); |
| 167 | 167 | for (ptr) |*x, i| expect(x.* == i + 1); |
| 168 | 168 | } |
test/stage1/behavior/pointers.zig+3-3| ... | ... | @@ -253,7 +253,7 @@ test "pointer sentinel with enums" { |
| 253 | 253 | }; |
| 254 | 254 | |
| 255 | 255 | fn doTheTest() void { |
| 256 | var ptr: [*:.sentinel]Number = &[_:.sentinel]Number{ .one, .two, .two, .one }; | |
| 256 | var ptr: [*:.sentinel]const Number = &[_:.sentinel]Number{ .one, .two, .two, .one }; | |
| 257 | 257 | expect(ptr[4] == .sentinel); // TODO this should be comptime expect, see #3731 |
| 258 | 258 | } |
| 259 | 259 | }; |
| ... | ... | @@ -264,7 +264,7 @@ test "pointer sentinel with enums" { |
| 264 | 264 | test "pointer sentinel with optional element" { |
| 265 | 265 | const S = struct { |
| 266 | 266 | fn doTheTest() void { |
| 267 | var ptr: [*:null]?i32 = &[_:null]?i32{ 1, 2, 3, 4 }; | |
| 267 | var ptr: [*:null]const ?i32 = &[_:null]?i32{ 1, 2, 3, 4 }; | |
| 268 | 268 | expect(ptr[4] == null); // TODO this should be comptime expect, see #3731 |
| 269 | 269 | } |
| 270 | 270 | }; |
| ... | ... | @@ -276,7 +276,7 @@ test "pointer sentinel with +inf" { |
| 276 | 276 | const S = struct { |
| 277 | 277 | fn doTheTest() void { |
| 278 | 278 | const inf = std.math.inf_f32; |
| 279 | var ptr: [*:inf]f32 = &[_:inf]f32{ 1.1, 2.2, 3.3, 4.4 }; | |
| 279 | var ptr: [*:inf]const f32 = &[_:inf]f32{ 1.1, 2.2, 3.3, 4.4 }; | |
| 280 | 280 | expect(ptr[4] == inf); // TODO this should be comptime expect, see #3731 |
| 281 | 281 | } |
| 282 | 282 | }; |
test/stage1/behavior/slice.zig+2-2| ... | ... | @@ -218,9 +218,9 @@ test "slice syntax resulting in pointer-to-array" { |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | 220 | fn testPointer0() void { |
| 221 | var pointer: [*]u0 = &[1]u0{0}; | |
| 221 | var pointer: [*]const u0 = &[1]u0{0}; | |
| 222 | 222 | var slice = pointer[0..1]; |
| 223 | comptime expect(@TypeOf(slice) == *[1]u0); | |
| 223 | comptime expect(@TypeOf(slice) == *const [1]u0); | |
| 224 | 224 | expect(slice[0] == 0); |
| 225 | 225 | } |
| 226 | 226 |