zig

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

commit b85b68a7fd175169e0f07ab733bde6d5654b1044 (tree)
parent e514454c0e092794171d4a62260971cc8de6f200
Author: Andrew Kelley <superjoe30@gmail.com>
Date:   Sat,  2 Jun 2018 15:20:51 -0400

better compile error for error sets behind nullable

Diffstat:
Msrc/ir.cpp | 17+++++++++++++----
Mtest/compile_errors.zig | 17+++++++++++++++++
2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/src/ir.cpp b/src/ir.cpp @@ -7934,11 +7934,20 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira, return ImplicitCastMatchResultReportedError; } + // implicit conversion from ?T to ?U + if (expected_type->id == TypeTableEntryIdMaybe && actual_type->id == TypeTableEntryIdMaybe) { + ImplicitCastMatchResult res = ir_types_match_with_implicit_cast(ira, expected_type->data.maybe.child_type, + actual_type->data.maybe.child_type, value); + if (res != ImplicitCastMatchResultNo) + return res; + } + // implicit conversion from non maybe type to maybe type - if (expected_type->id == TypeTableEntryIdMaybe && - ir_types_match_with_implicit_cast(ira, expected_type->data.maybe.child_type, actual_type, value)) - { - return ImplicitCastMatchResultYes; + if (expected_type->id == TypeTableEntryIdMaybe) { + ImplicitCastMatchResult res = ir_types_match_with_implicit_cast(ira, expected_type->data.maybe.child_type, + actual_type, value); + if (res != ImplicitCastMatchResultNo) + return res; } // implicit conversion from null literal to maybe type diff --git a/test/compile_errors.zig b/test/compile_errors.zig @@ -3,6 +3,23 @@ const tests = @import("tests.zig"); pub fn addCases(cases: *tests.CompileErrorContext) void { cases.add( "invalid deref on switch target", + \\const NextError = error{NextError}; + \\const OtherError = error{OutOfMemory}; + \\ + \\export fn entry() void { + \\ const a: ?NextError!i32 = foo(); + \\} + \\ + \\fn foo() ?OtherError!i32 { + \\ return null; + \\} + , + ".tmp_source.zig:5:34: error: expected 'NextError!i32', found 'OtherError!i32'", + ".tmp_source.zig:2:26: note: 'error.OutOfMemory' not a member of destination error set", + ); + + cases.add( + "invalid deref on switch target", \\comptime { \\ var tile = Tile.Empty; \\ switch (tile.*) {