diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2018-07-13 12:34:42 -0400 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2018-07-13 12:34:42 -0400 |
| commit | 5354d1f5fc496beb8313488ea1690e02e9c630fa (patch) | |
| tree | 4b46b803efbc09944d2a0cb47904b452ea666fbd /src | |
| parent | ac096c294976b7cb6433a7939adcd664af770201 (diff) | |
| download | zig-5354d1f5fc496beb8313488ea1690e02e9c630fa.tar.gz zig-5354d1f5fc496beb8313488ea1690e02e9c630fa.zip | |
allow == for comparing optional pointers
closes #658
Diffstat (limited to 'src')
| -rw-r--r-- | src/codegen.cpp | 4 | ||||
| -rw-r--r-- | src/ir.cpp | 30 |
2 files changed, 14 insertions, 20 deletions
diff --git a/src/codegen.cpp b/src/codegen.cpp index 54e2da7d61..3f54c120b4 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2249,10 +2249,8 @@ static LLVMValueRef ir_render_bin_op(CodeGen *g, IrExecutable *executable, return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, ""); } else if (type_entry->id == TypeTableEntryIdEnum || type_entry->id == TypeTableEntryIdErrorSet || - type_entry->id == TypeTableEntryIdPointer || type_entry->id == TypeTableEntryIdBool || - type_entry->id == TypeTableEntryIdPromise || - type_entry->id == TypeTableEntryIdFn) + get_codegen_ptr_type(type_entry) != nullptr) { LLVMIntPredicate pred = cmp_op_to_int_predicate(op_id, false); return LLVMBuildICmp(g->builder, pred, op1_value, op2_value, ""); diff --git a/src/ir.cpp b/src/ir.cpp index eb62cc8bdf..f452ef43e0 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -11147,7 +11147,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp if (type_is_invalid(resolved_type)) return resolved_type; - + bool operator_allowed; switch (resolved_type->id) { case TypeTableEntryIdInvalid: zig_unreachable(); // handled above @@ -11156,6 +11156,7 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp case TypeTableEntryIdComptimeInt: case TypeTableEntryIdInt: case TypeTableEntryIdFloat: + operator_allowed = true; break; case TypeTableEntryIdBool: @@ -11170,19 +11171,8 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp case TypeTableEntryIdBoundFn: case TypeTableEntryIdArgTuple: case TypeTableEntryIdPromise: - if (!is_equality_cmp) { - ir_add_error_node(ira, source_node, - buf_sprintf("operator not allowed for type '%s'", buf_ptr(&resolved_type->name))); - return ira->codegen->builtin_types.entry_invalid; - } - break; - case TypeTableEntryIdEnum: - if (!is_equality_cmp) { - ir_add_error_node(ira, source_node, - buf_sprintf("operator not allowed for type '%s'", buf_ptr(&resolved_type->name))); - return ira->codegen->builtin_types.entry_invalid; - } + operator_allowed = is_equality_cmp; break; case TypeTableEntryIdUnreachable: @@ -11190,12 +11180,18 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp case TypeTableEntryIdStruct: case TypeTableEntryIdUndefined: case TypeTableEntryIdNull: - case TypeTableEntryIdOptional: case TypeTableEntryIdErrorUnion: case TypeTableEntryIdUnion: - ir_add_error_node(ira, source_node, - buf_sprintf("operator not allowed for type '%s'", buf_ptr(&resolved_type->name))); - return ira->codegen->builtin_types.entry_invalid; + operator_allowed = false; + break; + case TypeTableEntryIdOptional: + operator_allowed = is_equality_cmp && get_codegen_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))); + return ira->codegen->builtin_types.entry_invalid; } IrInstruction *casted_op1 = ir_implicit_cast(ira, op1, resolved_type); |
