authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-04 14:28:58+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-04 14:28:58+03:00
logadc444ceeb91c06a6ee84dc4e4874294a41dee45
treed69253d7044ad741b64ee61abed86d8a2cb7a890
parente72f45475d05fb446e48d3e34e6c8e367916bd50
signaturelock-open Commit is signed but in an unrecognized format.

fix missing compile error on call assigned to const


2 files changed, 33 insertions(+), 0 deletions(-)

src/ir.cpp+10
...@@ -20000,6 +20000,11 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -20000,6 +20000,11 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
20000 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {20000 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
20001 return result_loc;20001 return result_loc;
20002 }20002 }
20003 if (result_loc->value->type->data.pointer.is_const) {
20004 ir_add_error(ira, source_instr, buf_sprintf("cannot assign to constant"));
20005 return ira->codegen->invalid_inst_gen;
20006 }
20007
20003 IrInstGen *dummy_value = ir_const(ira, source_instr, impl_fn_type_id->return_type);20008 IrInstGen *dummy_value = ir_const(ira, source_instr, impl_fn_type_id->return_type);
20004 dummy_value->value->special = ConstValSpecialRuntime;20009 dummy_value->value->special = ConstValSpecialRuntime;
20005 IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr,20010 IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr,
...@@ -20138,6 +20143,11 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -20138,6 +20143,11 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
20138 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {20143 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
20139 return result_loc;20144 return result_loc;
20140 }20145 }
20146 if (result_loc->value->type->data.pointer.is_const) {
20147 ir_add_error(ira, source_instr, buf_sprintf("cannot assign to constant"));
20148 return ira->codegen->invalid_inst_gen;
20149 }
20150
20141 IrInstGen *dummy_value = ir_const(ira, source_instr, return_type);20151 IrInstGen *dummy_value = ir_const(ira, source_instr, return_type);
20142 dummy_value->value->special = ConstValSpecialRuntime;20152 dummy_value->value->special = ConstValSpecialRuntime;
20143 IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr,20153 IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr,
test/compile_errors.zig+23
...@@ -2,6 +2,29 @@ const tests = @import("tests.zig");...@@ -2,6 +2,29 @@ const tests = @import("tests.zig");
2const std = @import("std");2const std = @import("std");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("call assigned to constant",
6 \\const Foo = struct {
7 \\ x: i32,
8 \\};
9 \\fn foo() Foo {
10 \\ return .{ .x = 42 };
11 \\}
12 \\fn bar(val: var) Foo {
13 \\ return .{ .x = val };
14 \\}
15 \\export fn entry() void {
16 \\ const baz: Foo = undefined;
17 \\ baz = foo();
18 \\}
19 \\export fn entry1() void {
20 \\ const baz: Foo = undefined;
21 \\ baz = bar(42);
22 \\}
23 , &[_][]const u8{
24 "tmp.zig:12:14: error: cannot assign to constant",
25 "tmp.zig:16:14: error: cannot assign to constant",
26 });
27
5 cases.add("invalid pointer syntax",28 cases.add("invalid pointer syntax",
6 \\export fn foo() void {29 \\export fn foo() void {
7 \\ var guid: *:0 const u8 = undefined;30 \\ var guid: *:0 const u8 = undefined;