Commit 14bbb82832

LemonBoy <thatlemon@gmail.com>
2020-02-23 09:44:19
ir: Fix lazy comparison between @alignOf and zero
Closes #4527
1 parent 648f94c
Changed files (2)
src
test
stage1
behavior
src/ir.cpp
@@ -15565,9 +15565,20 @@ static Error lazy_cmp_zero(CodeGen *codegen, AstNode *source_node, ZigValue *val
             switch (val->data.x_lazy->id) {
                 case LazyValueIdInvalid:
                     zig_unreachable();
-                case LazyValueIdAlignOf:
-                    *result = CmpGT;
+                case LazyValueIdAlignOf: {
+                    LazyValueAlignOf *lazy_align_of = reinterpret_cast<LazyValueAlignOf *>(val->data.x_lazy);
+                    IrAnalyze *ira = lazy_align_of->ira;
+
+                    uint32_t abi_align;
+                    if ((err = type_val_resolve_abi_align(ira->codegen, source_node, lazy_align_of->target_type->value,
+                        &abi_align)))
+                    {
+                        return err;
+                    }
+
+                    *result = (abi_align == 0) ? CmpEQ : CmpGT;
                     return ErrorNone;
+                }
                 case LazyValueIdSizeOf: {
                     LazyValueSizeOf *lazy_size_of = reinterpret_cast<LazyValueSizeOf *>(val->data.x_lazy);
                     IrAnalyze *ira = lazy_size_of->ira;
test/stage1/behavior/alignof.zig
@@ -15,3 +15,24 @@ test "@alignOf(T) before referencing T" {
         comptime expect(@alignOf(Foo) == 4);
     }
 }
+
+test "comparison of @alignOf(T) against zero" {
+    {
+        const T = struct { x: u32 };
+        expect(!(@alignOf(T) == 0));
+        expect(@alignOf(T) != 0);
+        expect(!(@alignOf(T) < 0));
+        expect(!(@alignOf(T) <= 0));
+        expect(@alignOf(T) > 0);
+        expect(@alignOf(T) >= 0);
+    }
+    {
+        const T = struct {};
+        expect(@alignOf(T) == 0);
+        expect(!(@alignOf(T) != 0));
+        expect(!(@alignOf(T) < 0));
+        expect(@alignOf(T) <= 0);
+        expect(!(@alignOf(T) > 0));
+        expect(@alignOf(T) >= 0);
+    }
+}