authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-07 15:12:37+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-04-07 15:25:44+03:00
loge62671f643a5072fb8e56306a87a2772dc357e9c
treef024d8472379260d7351e8b4f6c43b7e4cf82188
parentab05766674b88dc7715d27f7d590d6832ee005ca
signaturelock-open Commit is signed but in an unrecognized format.

fix missing const on address of literal


4 files changed, 42 insertions(+), 13 deletions(-)

src/all_types.hpp-2
......@@ -3519,8 +3519,6 @@ struct IrInstSrcRef {
35193519 IrInstSrc base;
35203520
35213521 IrInstSrc *value;
3522 bool is_const;
3523 bool is_volatile;
35243522};
35253523
35263524struct IrInstGenRef {
src/ir.cpp+13-8
......@@ -3290,13 +3290,9 @@ static IrInstSrc *ir_build_import(IrBuilderSrc *irb, Scope *scope, AstNode *sour
32903290 return &instruction->base;
32913291}
32923292
3293static IrInstSrc *ir_build_ref_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, IrInstSrc *value,
3294 bool is_const, bool is_volatile)
3295{
3293static IrInstSrc *ir_build_ref_src(IrBuilderSrc *irb, Scope *scope, AstNode *source_node, IrInstSrc *value) {
32963294 IrInstSrcRef *instruction = ir_build_instruction<IrInstSrcRef>(irb, scope, source_node);
32973295 instruction->value = value;
3298 instruction->is_const = is_const;
3299 instruction->is_volatile = is_volatile;
33003296
33013297 ir_ref_instruction(value, irb->current_basic_block);
33023298
......@@ -5938,7 +5934,7 @@ static IrInstSrc *ir_gen_symbol(IrBuilderSrc *irb, Scope *scope, AstNode *node,
59385934 } else {
59395935 IrInstSrc *value = ir_build_const_type(irb, scope, node, primitive_type);
59405936 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);
59425938 } else {
59435939 return ir_expr_wrap(irb, scope, value, result_loc);
59445940 }
......@@ -7486,7 +7482,7 @@ static IrInstSrc *ir_lval_wrap(IrBuilderSrc *irb, Scope *scope, IrInstSrc *value
74867482 if (lval == LValPtr) {
74877483 // We needed a pointer to a value, but we got a value. So we create
74887484 // 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);
74907486 } else if (result_loc != nullptr) {
74917487 return ir_expr_wrap(irb, scope, value, result_loc);
74927488 } else {
......@@ -23348,7 +23344,16 @@ static IrInstGen *ir_analyze_instruction_ref(IrAnalyze *ira, IrInstSrcRef *ref_i
2334823344 IrInstGen *value = ref_instruction->value->child;
2334923345 if (type_is_invalid(value->value->type))
2335023346 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);
2335223357}
2335323358
2335423359static 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) {
14761476}
14771477
14781478static 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 ");
14821480 ir_print_other_inst_src(irp, instruction->value);
14831481}
14841482
test/compile_errors.zig+28
......@@ -2,6 +2,34 @@ const tests = @import("tests.zig");
22const std = @import("std");
33
44pub 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
533 cases.addTest("cast between ?T where T is not a pointer",
634 \\pub const fnty1 = ?fn (i8) void;
735 \\pub const fnty2 = ?fn (u64) void;