Skip to content

Compile-time comparison of optionals (== / !=) #5044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 94 additions & 55 deletions src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16381,18 +16381,51 @@ static IrInstGen *ir_analyze_bin_op_cmp_numeric(IrAnalyze *ira, IrInst *source_i
return ir_build_bin_op_gen(ira, source_instr, result_type, op_id, casted_op1, casted_op2, true);
}

static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_instruction) {
IrInstGen *op1 = bin_op_instruction->op1->child;
if (type_is_invalid(op1->value->type))
return ira->codegen->invalid_inst_gen;
static bool ir_is_cmp_allowed(IrBinOp op_id, ZigType *op_type) {
bool is_equality_cmp = (op_id == IrBinOpCmpEq || op_id == IrBinOpCmpNotEq);
switch (op_type->id) {
case ZigTypeIdInvalid:
zig_unreachable();

IrInstGen *op2 = bin_op_instruction->op2->child;
if (type_is_invalid(op2->value->type))
return ira->codegen->invalid_inst_gen;
case ZigTypeIdComptimeFloat:
case ZigTypeIdComptimeInt:
case ZigTypeIdInt:
case ZigTypeIdFloat:
return true;

AstNode *source_node = bin_op_instruction->base.base.source_node;
case ZigTypeIdVector:
// vectors of bool trigger this code path
case ZigTypeIdBool:
case ZigTypeIdMetaType:
case ZigTypeIdVoid:
case ZigTypeIdErrorSet:
case ZigTypeIdFn:
case ZigTypeIdOpaque:
case ZigTypeIdBoundFn:
case ZigTypeIdEnum:
case ZigTypeIdEnumLiteral:
case ZigTypeIdAnyFrame:
return is_equality_cmp;
case ZigTypeIdPointer:
return is_equality_cmp || (op_type->data.pointer.ptr_len == PtrLenC);
case ZigTypeIdUnreachable:
case ZigTypeIdArray:
case ZigTypeIdStruct:
case ZigTypeIdUndefined:
case ZigTypeIdNull:
case ZigTypeIdErrorUnion:
case ZigTypeIdUnion:
case ZigTypeIdFnFrame:
return false;
case ZigTypeIdOptional:
return is_equality_cmp && ir_is_cmp_allowed(op_id, op_type->data.maybe.child_type);
}
}

static IrInstGen *ir_analyze_bin_op_cmp_seperate_ops(IrAnalyze *ira, IrInstSrcBinOp *bin_op_instruction, IrInstGen *op1, IrInstGen *op2) {
AstNode *source_node = bin_op_instruction->base.base.source_node;
IrBinOp op_id = bin_op_instruction->op_id;

bool is_equality_cmp = (op_id == IrBinOpCmpEq || op_id == IrBinOpCmpNotEq);
if (is_equality_cmp && op1->value->type->id == ZigTypeIdNull && op2->value->type->id == ZigTypeIdNull) {
return ir_const_bool(ira, &bin_op_instruction->base.base, (op_id == IrBinOpCmpEq));
Expand Down Expand Up @@ -16457,6 +16490,43 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i
} else {
return is_non_null;
}
} else if (is_equality_cmp &&
(op1->value->type->id == ZigTypeIdOptional && op2->value->type->id == ZigTypeIdOptional) &&
(op1->value->type->data.maybe.child_type->id != ZigTypeIdPointer) &&
(op2->value->type->data.maybe.child_type->id != ZigTypeIdPointer))
{
IrInst *bin_op_base = &bin_op_instruction->base.base;
if (instr_is_comptime(op1) && instr_is_comptime(op2)) {
ZigValue *op1_val = ir_resolve_const(ira, op1, UndefBad);
if (!op1_val)
return ira->codegen->invalid_inst_gen;

ZigValue *op2_val = ir_resolve_const(ira, op2, UndefBad);
if (!op2_val)
return ira->codegen->invalid_inst_gen;

bool op1_is_null = optional_value_is_null(op1_val);
bool op2_is_null = optional_value_is_null(op2_val);

if (op1_is_null && op2_is_null) {
return ir_const_bool(ira, bin_op_base, op_id == IrBinOpCmpEq);
} else if (!op1_is_null && !op2_is_null) {
IrInstGen *op1_unwrapped = ir_analyze_optional_value_payload_value(ira, bin_op_base, op1, false);
if (type_is_invalid(op1_unwrapped->value->type))
return ira->codegen->invalid_inst_gen;

IrInstGen *op2_unwrapped = ir_analyze_optional_value_payload_value(ira, bin_op_base, op2, false);
if (type_is_invalid(op2_unwrapped->value->type))
return ira->codegen->invalid_inst_gen;

assert(instr_is_comptime(op1_unwrapped) && instr_is_comptime(op2_unwrapped));
return ir_analyze_bin_op_cmp_seperate_ops(ira, bin_op_instruction, op1_unwrapped, op2_unwrapped);
} else {
return ir_const_bool(ira, bin_op_base, op_id != IrBinOpCmpEq);
}
}
ir_add_error_node(ira, source_node, buf_sprintf("comparison of non-comptime, non-pointer optionals. TODO: allow runtime comparison of non-pointer optionals"));
return ira->codegen->invalid_inst_gen;
} else if (op1->value->type->id == ZigTypeIdNull || op2->value->type->id == ZigTypeIdNull) {
ZigType *non_null_type = (op1->value->type->id == ZigTypeIdNull) ? op2->value->type : op1->value->type;
ir_add_error_node(ira, source_node, buf_sprintf("comparison of '%s' with null",
Expand Down Expand Up @@ -16583,7 +16653,7 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i
}

return ir_build_bin_op_gen(ira, &bin_op_instruction->base.base, ira->codegen->builtin_types.entry_bool,
op_id, op1, op2, bin_op_instruction->safety_check_on);
op_id, op1, op2, bin_op_instruction->safety_check_on);
}

if (type_is_numeric(op1->value->type) && type_is_numeric(op2->value->type)) {
Expand All @@ -16598,51 +16668,8 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i
if (type_is_invalid(resolved_type))
return ira->codegen->invalid_inst_gen;

bool operator_allowed;
switch (resolved_type->id) {
case ZigTypeIdInvalid:
zig_unreachable(); // handled above

case ZigTypeIdComptimeFloat:
case ZigTypeIdComptimeInt:
case ZigTypeIdInt:
case ZigTypeIdFloat:
zig_unreachable(); // handled with the type_is_numeric checks above

case ZigTypeIdVector:
// Not every case is handled by the type_is_numeric checks above,
// vectors of bool trigger this code path
case ZigTypeIdBool:
case ZigTypeIdMetaType:
case ZigTypeIdVoid:
case ZigTypeIdErrorSet:
case ZigTypeIdFn:
case ZigTypeIdOpaque:
case ZigTypeIdBoundFn:
case ZigTypeIdEnum:
case ZigTypeIdEnumLiteral:
case ZigTypeIdAnyFrame:
operator_allowed = is_equality_cmp;
break;

case ZigTypeIdPointer:
operator_allowed = is_equality_cmp || (resolved_type->data.pointer.ptr_len == PtrLenC);
break;
bool operator_allowed = ir_is_cmp_allowed(op_id, resolved_type);

case ZigTypeIdUnreachable:
case ZigTypeIdArray:
case ZigTypeIdStruct:
case ZigTypeIdUndefined:
case ZigTypeIdNull:
case ZigTypeIdErrorUnion:
case ZigTypeIdUnion:
case ZigTypeIdFnFrame:
operator_allowed = false;
break;
case ZigTypeIdOptional:
operator_allowed = is_equality_cmp && get_src_ptr_type(resolved_type) != nullptr;
break;
}
if (!operator_allowed) {
ir_add_error_node(ira, source_node,
buf_sprintf("operator not allowed for type '%s'", buf_ptr(&resolved_type->name)));
Expand Down Expand Up @@ -16681,7 +16708,7 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i
IrInstGen *result = ir_const(ira, &bin_op_instruction->base.base,
get_vector_type(ira->codegen, resolved_type->data.vector.len, ira->codegen->builtin_types.entry_bool));
result->value->data.x_array.data.s_none.elements =
ira->codegen->pass1_arena->allocate<ZigValue>(resolved_type->data.vector.len);
ira->codegen->pass1_arena->allocate<ZigValue>(resolved_type->data.vector.len);

expand_undef_array(ira->codegen, result->value);
for (size_t i = 0;i < resolved_type->data.vector.len;i++) {
Expand All @@ -16698,7 +16725,19 @@ static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_i
get_vector_type(ira->codegen, resolved_type->data.vector.len, ira->codegen->builtin_types.entry_bool) :
ira->codegen->builtin_types.entry_bool;
return ir_build_bin_op_gen(ira, &bin_op_instruction->base.base, res_type,
op_id, casted_op1, casted_op2, bin_op_instruction->safety_check_on);
op_id, casted_op1, casted_op2, bin_op_instruction->safety_check_on);
}

static IrInstGen *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstSrcBinOp *bin_op_instruction) {
IrInstGen *op1 = bin_op_instruction->op1->child;
if (type_is_invalid(op1->value->type))
return ira->codegen->invalid_inst_gen;

IrInstGen *op2 = bin_op_instruction->op2->child;
if (type_is_invalid(op2->value->type))
return ira->codegen->invalid_inst_gen;

return ir_analyze_bin_op_cmp_seperate_ops(ira, bin_op_instruction, op1, op2);
}

static ErrorMsg *ir_eval_math_op_scalar(IrAnalyze *ira, IrInst* source_instr, ZigType *type_entry,
Expand Down
25 changes: 25 additions & 0 deletions test/stage1/behavior/optional.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,31 @@ fn testNullPtrsEql() void {
expect(&number == x);
}

test "equality compare nullable integers" {
// testNullableIntEql(); uncomment when runtime checking is complete
comptime testNullableIntEql();
}

fn testNullableIntEql() void {
comptime { // these do not work at runtime yet. remove comptime when implementation of runtime comparison is done
var a: ?i32 = 10;
var b: ?i32 = 20;
var n: ?i32 = null;

expect(a == a);
expect(n == n);
expect(a != n);
expect(a != b);

var ao: ??i32 = a;
var no: ??i32 = n;

expect(ao == ao);
expect(no == no);
expect(ao != no);
}
}

test "address of unwrap optional" {
const S = struct {
const Foo = struct {
Expand Down