commit f127dee47424f4c87a4ed5026a2d5be5cce7a135 (tree)
parent 75b699b2c624d62a4b6717f00bd0856b86c3f990
Author: Vexu <git@vexu.eu>
Date: Mon, 4 May 2020 21:45:15 +0300
Merge pull request #5267 from Vexu/const-call
Fix missing compile error on call assigned to const
Diffstat:
3 files changed, 35 insertions(+), 1 deletion(-)
diff --git a/lib/std/crypto/blake3.zig b/lib/std/crypto/blake3.zig
@@ -338,7 +338,7 @@ pub const Blake3 = struct {
}
// Section 5.1.2 of the BLAKE3 spec explains this algorithm in more detail.
- fn add_chunk_chaining_value(self: *Blake3, new_cv: [8]u32, total_chunks: u64) void {
+ fn add_chunk_chaining_value(self: *Blake3, first_cv: [8]u32, total_chunks: u64) void {
// This chunk might complete some subtrees. For each completed subtree,
// its left child will be the current top entry in the CV stack, and
// its right child will be the current value of `new_cv`. Pop each left
@@ -346,6 +346,7 @@ pub const Blake3 = struct {
// with the result. After all these merges, push the final value of
// `new_cv` onto the stack. The number of completed subtrees is given
// by the number of trailing 0-bits in the new total number of chunks.
+ var new_cv = first_cv;
var chunk_counter = total_chunks;
while (chunk_counter & 1 == 0) {
new_cv = parent_cv(self.pop_cv(), new_cv, self.key, self.flags);
diff --git a/src/ir.cpp b/src/ir.cpp
@@ -19999,6 +19999,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,
@@ -20137,6 +20142,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
@@ -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;