authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-05-04 21:45:15+03:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-05-04 21:45:15+03:00
logf127dee47424f4c87a4ed5026a2d5be5cce7a135
tree46ccfd32c7990bb0772fb05f9aa804ec1e6932a8
parent75b699b2c624d62a4b6717f00bd0856b86c3f990
parent85fd484f0778b5f158dc76bf51b820314b86292d
signature Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #5267 from Vexu/const-call

Fix missing compile error on call assigned to const

3 files changed, 35 insertions(+), 1 deletions(-)

lib/std/crypto/blake3.zig+2-1
......@@ -338,7 +338,7 @@ pub const Blake3 = struct {
338338 }
339339
340340 // Section 5.1.2 of the BLAKE3 spec explains this algorithm in more detail.
341 fn add_chunk_chaining_value(self: *Blake3, new_cv: [8]u32, total_chunks: u64) void {
341 fn add_chunk_chaining_value(self: *Blake3, first_cv: [8]u32, total_chunks: u64) void {
342342 // This chunk might complete some subtrees. For each completed subtree,
343343 // its left child will be the current top entry in the CV stack, and
344344 // its right child will be the current value of `new_cv`. Pop each left
......@@ -346,6 +346,7 @@ pub const Blake3 = struct {
346346 // with the result. After all these merges, push the final value of
347347 // `new_cv` onto the stack. The number of completed subtrees is given
348348 // by the number of trailing 0-bits in the new total number of chunks.
349 var new_cv = first_cv;
349350 var chunk_counter = total_chunks;
350351 while (chunk_counter & 1 == 0) {
351352 new_cv = parent_cv(self.pop_cv(), new_cv, self.key, self.flags);
src/ir.cpp+10
......@@ -19999,6 +19999,11 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
1999919999 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
2000020000 return result_loc;
2000120001 }
20002 if (result_loc->value->type->data.pointer.is_const) {
20003 ir_add_error(ira, source_instr, buf_sprintf("cannot assign to constant"));
20004 return ira->codegen->invalid_inst_gen;
20005 }
20006
2000220007 IrInstGen *dummy_value = ir_const(ira, source_instr, impl_fn_type_id->return_type);
2000320008 dummy_value->value->special = ConstValSpecialRuntime;
2000420009 IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr,
......@@ -20137,6 +20142,11 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
2013720142 if (type_is_invalid(result_loc->value->type) || result_loc->value->type->id == ZigTypeIdUnreachable) {
2013820143 return result_loc;
2013920144 }
20145 if (result_loc->value->type->data.pointer.is_const) {
20146 ir_add_error(ira, source_instr, buf_sprintf("cannot assign to constant"));
20147 return ira->codegen->invalid_inst_gen;
20148 }
20149
2014020150 IrInstGen *dummy_value = ir_const(ira, source_instr, return_type);
2014120151 dummy_value->value->special = ConstValSpecialRuntime;
2014220152 IrInstGen *dummy_result = ir_implicit_cast2(ira, source_instr,
test/compile_errors.zig+23
......@@ -2,6 +2,29 @@ const tests = @import("tests.zig");
22const std = @import("std");
33
44pub 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
528 cases.add("invalid pointer syntax",
629 \\export fn foo() void {
730 \\ var guid: *:0 const u8 = undefined;