authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-02 15:20:51-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2018-06-02 15:20:51-04:00
logb85b68a7fd175169e0f07ab733bde6d5654b1044
tree309934a6d7db1864710ac3cdf237431f06bad8b1
parente514454c0e092794171d4a62260971cc8de6f200

better compile error for error sets behind nullable


2 files changed, 30 insertions(+), 4 deletions(-)

src/ir.cpp+13-4
......@@ -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+17
......@@ -1,6 +1,23 @@
11const tests = @import("tests.zig");
22
33pub 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 {