ir: Fix lazy comparison between @alignOf and zero

Closes #4527
master
LemonBoy 2020-02-23 09:44:19 +01:00 committed by Andrew Kelley
parent 648f94c027
commit 14bbb82832
No known key found for this signature in database
GPG Key ID: 7C5F548F728501A9
2 changed files with 34 additions and 2 deletions

View File

@ -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;

View File

@ -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);
}
}