authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-09-18 21:25:37+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-18 22:12:22-04:00
logf92d01c8a86b525c2caaf07497e16841905cea8c
treed75b73ef1734ea5b11cbdfe8ae400f7b4bc7604b
parent4fbf9f7f79c8e0df525f9932878995e6a6782ac4

stage1: Fix edge case in casting between optional types

Closes #6370

2 files changed, 28 insertions(+), 1 deletions(-)

src/ir.cpp+23-1
......@@ -63,6 +63,7 @@ enum ConstCastResultId {
6363 ConstCastResultIdPointerChild,
6464 ConstCastResultIdSliceChild,
6565 ConstCastResultIdOptionalChild,
66 ConstCastResultIdOptionalShape,
6667 ConstCastResultIdErrorUnionPayload,
6768 ConstCastResultIdErrorUnionErrorSet,
6869 ConstCastResultIdFnAlign,
......@@ -11946,8 +11947,22 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, ZigType *wanted
1194611947 }
1194711948 }
1194811949
11949 // maybe
11950 // optional types
1195011951 if (wanted_type->id == ZigTypeIdOptional && actual_type->id == ZigTypeIdOptional) {
11952 // Consider the case where the wanted type is ??[*]T and the actual one
11953 // is ?[*]T, we cannot turn the former into the latter even though the
11954 // child types are compatible (?[*]T and [*]T are both represented as a
11955 // pointer). The extra level of indirection in ??[*]T means it's
11956 // represented as a regular, fat, optional type and, as a consequence,
11957 // has a different shape than the one of ?[*]T.
11958 if ((wanted_ptr_type != nullptr) != (actual_ptr_type != nullptr)) {
11959 // The use of type_mismatch is intentional
11960 result.id = ConstCastResultIdOptionalShape;
11961 result.data.type_mismatch = heap::c_allocator.allocate_nonzero<ConstCastTypeMismatch>(1);
11962 result.data.type_mismatch->wanted_type = wanted_type;
11963 result.data.type_mismatch->actual_type = actual_type;
11964 return result;
11965 }
1195111966 ConstCastOnly child = types_match_const_cast_only(ira, wanted_type->data.maybe.child_type,
1195211967 actual_type->data.maybe.child_type, source_node, wanted_is_mutable);
1195311968 if (child.id == ConstCastResultIdInvalid)
......@@ -14549,6 +14564,13 @@ static void report_recursive_error(IrAnalyze *ira, AstNode *source_node, ConstCa
1454914564 report_recursive_error(ira, source_node, &cast_result->data.optional->child, msg);
1455014565 break;
1455114566 }
14567 case ConstCastResultIdOptionalShape: {
14568 add_error_note(ira->codegen, parent_msg, source_node,
14569 buf_sprintf("optional type child '%s' cannot cast into optional type '%s'",
14570 buf_ptr(&cast_result->data.type_mismatch->actual_type->name),
14571 buf_ptr(&cast_result->data.type_mismatch->wanted_type->name)));
14572 break;
14573 }
1455214574 case ConstCastResultIdErrorUnionErrorSet: {
1455314575 ErrorMsg *msg = add_error_note(ira->codegen, parent_msg, source_node,
1455414576 buf_sprintf("error set '%s' cannot cast into error set '%s'",
test/stage1/behavior/cast.zig+5
......@@ -849,3 +849,8 @@ test "comptime float casts" {
849849 expect(b == 2);
850850 expect(@TypeOf(b) == comptime_int);
851851}
852
853test "cast from ?[*]T to ??[*]T" {
854 const a: ??[*]u8 = @as(?[*]u8, null);
855 expect(a != null and a.? == null);
856}