Skip to content

Commit

Permalink
better compile error for error sets behind nullable
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewrk committed Jun 2, 2018
1 parent e514454 commit b85b68a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/ir.cpp
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/compile_errors.zig
@@ -1,6 +1,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 {
Expand Down

0 comments on commit b85b68a

Please sign in to comment.