authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-06 14:08:23-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-04-06 14:09:27-07:00
log7bb67b1fd0ca56a04778083f1a01583d839be9b1
tree2eadc6f2c7ce6cb3d2adca58c6dbcd16ef05ee2a
parent22ef416d4de6e37fbe8428351c4043e5acc3c818

ability to compare function pointers at compile time


2 files changed, 19 insertions(+), 0 deletions(-)

src/analyze.cpp+10
...@@ -2902,6 +2902,16 @@ static TypeTableEntry *analyze_bool_bin_op_expr(CodeGen *g, ImportTableEntry *im...@@ -2902,6 +2902,16 @@ static TypeTableEntry *analyze_bool_bin_op_expr(CodeGen *g, ImportTableEntry *im
2902 } else if (resolved_type->id == TypeTableEntryIdPureError) {2902 } else if (resolved_type->id == TypeTableEntryIdPureError) {
2903 bool are_equal = op1_val->data.x_err.err == op2_val->data.x_err.err;2903 bool are_equal = op1_val->data.x_err.err == op2_val->data.x_err.err;
29042904
2905 if (bin_op_type == BinOpTypeCmpEq) {
2906 answer = are_equal;
2907 } else if (bin_op_type == BinOpTypeCmpNotEq) {
2908 answer = !are_equal;
2909 } else {
2910 zig_unreachable();
2911 }
2912 } else if (resolved_type->id == TypeTableEntryIdFn) {
2913 bool are_equal = (op1_val->data.x_fn == op2_val->data.x_fn);
2914
2905 if (bin_op_type == BinOpTypeCmpEq) {2915 if (bin_op_type == BinOpTypeCmpEq) {
2906 answer = are_equal;2916 answer = are_equal;
2907 } else if (bin_op_type == BinOpTypeCmpNotEq) {2917 } else if (bin_op_type == BinOpTypeCmpNotEq) {
test/self_hosted.zig+9
...@@ -522,6 +522,15 @@ fn max(T: type)(a: T, b: T) -> T {...@@ -522,6 +522,15 @@ fn max(T: type)(a: T, b: T) -> T {
522}522}
523523
524524
525#attribute("test")
526fn constant_equal_function_pointers() {
527 const alias = empty_fn;
528 assert(@const_eval(empty_fn == alias));
529}
530
531fn empty_fn() {}
532
533
525fn assert(b: bool) {534fn assert(b: bool) {
526 if (!b) unreachable{}535 if (!b) unreachable{}
527}536}