Commit adc444ceeb

Vexu <git@vexu.eu>
2020-05-04 13:28:58
fix missing compile error on call assigned to const
1 parent e72f454
Changed files (2)
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,
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;