From adc444ceeb91c06a6ee84dc4e4874294a41dee45 Mon Sep 17 00:00:00 2001 From: Vexu Date: Mon, 4 May 2020 14:28:58 +0300 Subject: [PATCH 1/2] 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 4795645544..b636ac6f76 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 fd27b8e666..3d7677ab75 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; From 85fd484f0778b5f158dc76bf51b820314b86292d Mon Sep 17 00:00:00 2001 From: Vexu Date: Mon, 4 May 2020 14:45:36 +0300 Subject: [PATCH 2/2] std: fix blake3 assignment to constant --- lib/std/crypto/blake3.zig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/std/crypto/blake3.zig b/lib/std/crypto/blake3.zig index 7c79ffcf55..08479d65a5 100644 --- 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);