From 7f6b0ba6eafd566a5a9243286ba59fc0e4587d30 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 7 Feb 2016 01:25:04 -0700 Subject: [PATCH] ability to explicitly cast maybe pointers to each other --- src/analyze.cpp | 9 +++++++++ test/self_hosted.zig | 7 +++++++ 2 files changed, 16 insertions(+) diff --git a/src/analyze.cpp b/src/analyze.cpp index bd31abd6a7..e37885f353 100644 --- a/src/analyze.cpp +++ b/src/analyze.cpp @@ -3702,6 +3702,15 @@ static TypeTableEntry *analyze_cast_expr(CodeGen *g, ImportTableEntry *import, B return resolve_cast(g, context, node, expr_node, wanted_type, CastOpPointerReinterpret, false); } + // explicit cast from maybe pointer to another maybe pointer + if (actual_type->id == TypeTableEntryIdMaybe && + actual_type->data.maybe.child_type->id == TypeTableEntryIdPointer && + wanted_type->id == TypeTableEntryIdMaybe && + wanted_type->data.maybe.child_type->id == TypeTableEntryIdPointer) + { + return resolve_cast(g, context, node, expr_node, wanted_type, CastOpPointerReinterpret, false); + } + // explicit cast from child type of maybe type to maybe type if (wanted_type->id == TypeTableEntryIdMaybe) { if (types_match_const_cast_only(wanted_type->data.maybe.child_type, actual_type)) { diff --git a/test/self_hosted.zig b/test/self_hosted.zig index 1f937b441b..8d7183990b 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -208,3 +208,10 @@ fn wants_fn_with_void(f: fn()) { } fn fn_with_unreachable() -> unreachable { unreachable {} } + + +#attribute("test") +fn explicit_cast_maybe_pointers() { + const a: ?&i32 = undefined; + const b: ?&f32 = (?&f32)(a); +}