zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit b9f88c3552c0ac892aa6dba7ed8518a0aee51305 (tree)
parent ce96323ba15309c08b868c442ef4511c244e2e83
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Sun, 24 Nov 2019 20:25:14 -0500

fix compile errors for array sentinels mismatching

Diffstat:
Msrc/ir.cpp | 14++++++++++----
Mtest/compile_errors.zig | 18+++++++++++++++---
2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/src/ir.cpp b/src/ir.cpp @@ -12661,21 +12661,27 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa { Buf *txt_msg = buf_sprintf("destination pointer requires a terminating '"); render_const_value(ira->codegen, txt_msg, wanted_type->data.pointer.sentinel); - buf_appendf(txt_msg, "' sentinel value"); + buf_appendf(txt_msg, "' sentinel"); if (actual_type->data.pointer.sentinel != nullptr) { buf_appendf(txt_msg, ", but source pointer has a terminating '"); render_const_value(ira->codegen, txt_msg, actual_type->data.pointer.sentinel); - buf_appendf(txt_msg, "' sentinel value"); + buf_appendf(txt_msg, "' sentinel"); } add_error_note(ira->codegen, parent_msg, source_node, txt_msg); } break; } case ConstCastResultIdSentinelArrays: { + ZigType *actual_type = cast_result->data.sentinel_arrays->actual_type; ZigType *wanted_type = cast_result->data.sentinel_arrays->wanted_type; Buf *txt_msg = buf_sprintf("destination array requires a terminating '"); - render_const_value(ira->codegen, txt_msg, wanted_type->data.pointer.sentinel); - buf_appendf(txt_msg, "' sentinel value"); + render_const_value(ira->codegen, txt_msg, wanted_type->data.array.sentinel); + buf_appendf(txt_msg, "' sentinel"); + if (actual_type->data.array.sentinel != nullptr) { + buf_appendf(txt_msg, ", but source array has a terminating '"); + render_const_value(ira->codegen, txt_msg, actual_type->data.array.sentinel); + buf_appendf(txt_msg, "' sentinel"); + } add_error_note(ira->codegen, parent_msg, source_node, txt_msg); break; } diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -3,18 +3,30 @@ const builtin = @import("builtin"); pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add( - "incompatible pointer sentinels", + "incompatible sentinels", \\export fn entry1(ptr: [*:255]u8) [*:0]u8 { \\ return ptr; \\} \\export fn entry2(ptr: [*]u8) [*:0]u8 { \\ return ptr; \\} + \\export fn entry3() void { + \\ var array: [2:0]u8 = [_:255]u8{1, 2}; + \\} + \\export fn entry4() void { + \\ var array: [2:0]u8 = [_]u8{1, 2}; + \\} , "tmp.zig:2:12: error: expected type '[*:0]u8', found '[*:255]u8'", - "tmp.zig:2:12: note: destination pointer requires a terminating '0' sentinel value, but source pointer has a terminating '255' sentinel value", + "tmp.zig:2:12: note: destination pointer requires a terminating '0' sentinel, but source pointer has a terminating '255' sentinel", "tmp.zig:5:12: error: expected type '[*:0]u8', found '[*]u8'", - "tmp.zig:5:12: note: destination pointer requires a terminating '0' sentinel value", + "tmp.zig:5:12: note: destination pointer requires a terminating '0' sentinel", + + "tmp.zig:8:35: error: expected type '[2:0]u8', found '[2:255]u8'", + "tmp.zig:8:35: note: destination array requires a terminating '0' sentinel, but source array has a terminating '255' sentinel", + "tmp.zig:11:31: error: expected type '[2:0]u8', found '[2]u8'", + "tmp.zig:11:31: note: destination array requires a terminating '0' sentinel", + ); cases.add(