Skip to content

Commit b85b68a

Browse files
committedJun 2, 2018
better compile error for error sets behind nullable
·
0.15.20.3.0
1 parent e514454 commit b85b68a

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed
 

‎src/ir.cpp‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7934,11 +7934,20 @@ static ImplicitCastMatchResult ir_types_match_with_implicit_cast(IrAnalyze *ira,
79347934
return ImplicitCastMatchResultReportedError;
79357935
}
79367936

7937+
// implicit conversion from ?T to ?U
7938+
if (expected_type->id == TypeTableEntryIdMaybe && actual_type->id == TypeTableEntryIdMaybe) {
7939+
ImplicitCastMatchResult res = ir_types_match_with_implicit_cast(ira, expected_type->data.maybe.child_type,
7940+
actual_type->data.maybe.child_type, value);
7941+
if (res != ImplicitCastMatchResultNo)
7942+
return res;
7943+
}
7944+
79377945
// implicit conversion from non maybe type to maybe type
7938-
if (expected_type->id == TypeTableEntryIdMaybe &&
7939-
ir_types_match_with_implicit_cast(ira, expected_type->data.maybe.child_type, actual_type, value))
7940-
{
7941-
return ImplicitCastMatchResultYes;
7946+
if (expected_type->id == TypeTableEntryIdMaybe) {
7947+
ImplicitCastMatchResult res = ir_types_match_with_implicit_cast(ira, expected_type->data.maybe.child_type,
7948+
actual_type, value);
7949+
if (res != ImplicitCastMatchResultNo)
7950+
return res;
79427951
}
79437952

79447953
// implicit conversion from null literal to maybe type

‎test/compile_errors.zig‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
const tests = @import("tests.zig");
22

33
pub fn addCases(cases: *tests.CompileErrorContext) void {
4+
cases.add(
5+
"invalid deref on switch target",
6+
\\const NextError = error{NextError};
7+
\\const OtherError = error{OutOfMemory};
8+
\\
9+
\\export fn entry() void {
10+
\\ const a: ?NextError!i32 = foo();
11+
\\}
12+
\\
13+
\\fn foo() ?OtherError!i32 {
14+
\\ return null;
15+
\\}
16+
,
17+
".tmp_source.zig:5:34: error: expected 'NextError!i32', found 'OtherError!i32'",
18+
".tmp_source.zig:2:26: note: 'error.OutOfMemory' not a member of destination error set",
19+
);
20+
421
cases.add(
522
"invalid deref on switch target",
623
\\comptime {

0 commit comments

Comments
 (0)
Please sign in to comment.