From adc444ceeb91c06a6ee84dc4e4874294a41dee45 Mon Sep 17 00:00:00 2001 From: Vexu Date: Mon, 4 May 2020 14:28:58 +0300 Subject: [PATCH] fix missing compile error on call assigned to const --- src/ir.cpp | 10 ++++++++++ test/compile_errors.zig | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/ir.cpp b/src/ir.cpp index 479564554..b636ac6f7 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -20000,6 +20000,11 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr, if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) { return result_loc; } + if (result_loc->value->type->data.pointer.is_const) { + ir_add_error(ira, source_instr, buf_sprintf("cannot assign to constant")); + return ira->codegen->invalid_inst_gen; + } + IrInstGen *dummy_value = ir_const(ira, source_instr, impl_fn_type_id->return_type); dummy_value->value->special = ConstValSpecialRuntime; IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr, @@ -20138,6 +20143,11 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr, if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) { return result_loc; } + if (result_loc->value->type->data.pointer.is_const) { + ir_add_error(ira, source_instr, buf_sprintf("cannot assign to constant")); + return ira->codegen->invalid_inst_gen; + } + IrInstGen *dummy_value = ir_const(ira, source_instr, return_type); dummy_value->value->special = ConstValSpecialRuntime; IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr, diff --git a/test/compile_errors.zig b/test/compile_errors.zig index fd27b8e66..3d7677ab7 100644 --- a/test/compile_errors.zig +++ b/test/compile_errors.zig @@ -2,6 +2,29 @@ const tests = @import("tests.zig"); const std = @import("std"); pub fn addCases(cases: *tests.CompileErrorContext) void { + cases.add("call assigned to constant", + \\const Foo = struct { + \\ x: i32, + \\}; + \\fn foo() Foo { + \\ return .{ .x = 42 }; + \\} + \\fn bar(val: var) Foo { + \\ return .{ .x = val }; + \\} + \\export fn entry() void { + \\ const baz: Foo = undefined; + \\ baz = foo(); + \\} + \\export fn entry1() void { + \\ const baz: Foo = undefined; + \\ baz = bar(42); + \\} + , &[_][]const u8{ + "tmp.zig:12:14: error: cannot assign to constant", + "tmp.zig:16:14: error: cannot assign to constant", + }); + cases.add("invalid pointer syntax", \\export fn foo() void { \\ var guid: *:0 const u8 = undefined;