aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Kelley <superjoe30@gmail.com>2018-06-26 15:27:41 -0400
committerAndrew Kelley <superjoe30@gmail.com>2018-06-26 15:27:41 -0400
commit11ca38a4e9c637bf6ff635f4f62634edaf89f853 (patch)
treed5cce0cbb97bd4c7756b591066d5f10152aa8bb1
parentaf95e1557214df4a1a34a712efc2f8dafb502c82 (diff)
downloadzig-11ca38a4e9c637bf6ff635f4f62634edaf89f853.tar.gz
zig-11ca38a4e9c637bf6ff635f4f62634edaf89f853.zip
fix crash for optional pointer to empty struct
closes #1153
-rw-r--r--src/ir.cpp3
-rw-r--r--test/behavior.zig1
-rw-r--r--test/cases/optional.zig9
3 files changed, 12 insertions, 1 deletions
diff --git a/src/ir.cpp b/src/ir.cpp
index 1930bbb248..76178f2437 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -7985,9 +7985,10 @@ static ConstCastOnly types_match_const_cast_only(IrAnalyze *ira, TypeTableEntry
// * and [*] can do a const-cast-only to ?* and ?[*], respectively
// but not if there is a mutable parent pointer
+ // and not if the pointer is zero bits
if (!wanted_is_mutable && wanted_type->id == TypeTableEntryIdOptional &&
wanted_type->data.maybe.child_type->id == TypeTableEntryIdPointer &&
- actual_type->id == TypeTableEntryIdPointer)
+ actual_type->id == TypeTableEntryIdPointer && type_has_bits(actual_type))
{
ConstCastOnly child = types_match_const_cast_only(ira,
wanted_type->data.maybe.child_type, actual_type, source_node, wanted_is_mutable);
diff --git a/test/behavior.zig b/test/behavior.zig
index 3a2f706ad4..3766ed4305 100644
--- a/test/behavior.zig
+++ b/test/behavior.zig
@@ -35,6 +35,7 @@ comptime {
_ = @import("cases/math.zig");
_ = @import("cases/merge_error_sets.zig");
_ = @import("cases/misc.zig");
+ _ = @import("cases/optional.zig");
_ = @import("cases/namespace_depends_on_compile_var/index.zig");
_ = @import("cases/new_stack_call.zig");
_ = @import("cases/null.zig");
diff --git a/test/cases/optional.zig b/test/cases/optional.zig
new file mode 100644
index 0000000000..0129252dab
--- /dev/null
+++ b/test/cases/optional.zig
@@ -0,0 +1,9 @@
+const assert = @import("std").debug.assert;
+
+pub const EmptyStruct = struct {};
+
+test "optional pointer to size zero struct" {
+ var e = EmptyStruct{};
+ var o: ?*EmptyStruct = &e;
+ assert(o != null);
+}