Merge pull request #5267 from Vexu/const-call

Fix missing compile error on call assigned to const
This commit is contained in:
Vexu 2020-05-04 21:45:15 +03:00 committed by GitHub
commit f127dee474
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 1 deletions

View File

@ -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);

View File

@ -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,

View File

@ -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;