authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-02-01 13:38:56-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-02-01 13:38:56-05:00
log217025e9f0a3c3ce3c0d761352d0fa8b41aa5d3b
treec4b3d86e2dfed1a9cbf4af1f9c9c3a06b42fe4c3
parent3e99495ed8d2a384501338edb4885e709d51bf74
parent4188d541303c9375a7ce9533a8cfe72f69466b4c
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10751 from mitchellh/stage2/cmp-eq-undefined

stage2: cmp between untyped undefines values results in undef bool

2 files changed, 21 insertions(+), 24 deletions(-)

src/Sema.zig+21-22
......@@ -1163,7 +1163,7 @@ fn resolveConstBool(
11631163 zir_ref: Zir.Inst.Ref,
11641164) !bool {
11651165 const air_inst = sema.resolveInst(zir_ref);
1166 const wanted_type = Type.initTag(.bool);
1166 const wanted_type = Type.bool;
11671167 const coerced_inst = try sema.coerce(block, wanted_type, air_inst, src);
11681168 const val = try sema.resolveConstValue(block, src, coerced_inst);
11691169 return val.toBool();
......@@ -8058,7 +8058,7 @@ fn zirOverflowArithmetic(
80588058 return switch (result.overflowed) {
80598059 .yes => Air.Inst.Ref.bool_true,
80608060 .no => Air.Inst.Ref.bool_false,
8061 .undef => try sema.addConstUndef(Type.initTag(.bool)),
8061 .undef => try sema.addConstUndef(Type.bool),
80628062 };
80638063}
80648064
......@@ -9125,7 +9125,7 @@ fn zirCmpEq(
91259125 if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lval| {
91269126 if (try sema.resolveMaybeUndefVal(block, rhs_src, rhs)) |rval| {
91279127 if (lval.isUndef() or rval.isUndef()) {
9128 return sema.addConstUndef(Type.initTag(.bool));
9128 return sema.addConstUndef(Type.bool);
91299129 }
91309130 // TODO optimisation opportunity: evaluate if mem.eql is faster with the names,
91319131 // or calling to Module.getErrorValue to get the values and then compare them is
......@@ -9244,9 +9244,9 @@ fn cmpSelf(
92449244 const resolved_type = sema.typeOf(casted_lhs);
92459245 const runtime_src: LazySrcLoc = src: {
92469246 if (try sema.resolveMaybeUndefVal(block, lhs_src, casted_lhs)) |lhs_val| {
9247 if (lhs_val.isUndef()) return sema.addConstUndef(resolved_type);
9247 if (lhs_val.isUndef()) return sema.addConstUndef(Type.bool);
92489248 if (try sema.resolveMaybeUndefVal(block, rhs_src, casted_rhs)) |rhs_val| {
9249 if (rhs_val.isUndef()) return sema.addConstUndef(resolved_type);
9249 if (rhs_val.isUndef()) return sema.addConstUndef(Type.bool);
92509250
92519251 if (lhs_val.compare(op, rhs_val, resolved_type)) {
92529252 return Air.Inst.Ref.bool_true;
......@@ -9265,7 +9265,7 @@ fn cmpSelf(
92659265 // bool eq/neq more efficiently.
92669266 if (resolved_type.zigTypeTag() == .Bool) {
92679267 if (try sema.resolveMaybeUndefVal(block, rhs_src, casted_rhs)) |rhs_val| {
9268 if (rhs_val.isUndef()) return sema.addConstUndef(resolved_type);
9268 if (rhs_val.isUndef()) return sema.addConstUndef(Type.bool);
92699269 return sema.runtimeBoolCmp(block, op, casted_lhs, rhs_val.toBool(), lhs_src);
92709270 }
92719271 }
......@@ -9300,7 +9300,7 @@ fn runtimeBoolCmp(
93009300) CompileError!Air.Inst.Ref {
93019301 if ((op == .neq) == rhs) {
93029302 try sema.requireRuntimeBlock(block, runtime_src);
9303 return block.addTyOp(.not, Type.initTag(.bool), lhs);
9303 return block.addTyOp(.not, Type.bool, lhs);
93049304 } else {
93059305 return lhs;
93069306 }
......@@ -9545,9 +9545,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
95459545 // alignment: comptime_int,
95469546 field_values[1] = try Value.Tag.int_u64.create(sema.arena, ty.abiAlignment(target));
95479547 // is_generic: bool,
9548 field_values[2] = if (info.is_generic) Value.initTag(.bool_true) else Value.initTag(.bool_false);
9548 field_values[2] = if (info.is_generic) Value.@"true" else Value.@"false";
95499549 // is_var_args: bool,
9550 field_values[3] = if (info.is_var_args) Value.initTag(.bool_true) else Value.initTag(.bool_false);
9550 field_values[3] = if (info.is_var_args) Value.@"true" else Value.@"false";
95519551 // return_type: ?type,
95529552 field_values[4] = try Value.Tag.ty.create(sema.arena, ty.fnReturnType());
95539553 // args: []const FnArg,
......@@ -9599,9 +9599,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
95999599 // size: Size,
96009600 field_values[0] = try Value.Tag.enum_field_index.create(sema.arena, @enumToInt(info.size));
96019601 // is_const: bool,
9602 field_values[1] = if (!info.mutable) Value.initTag(.bool_true) else Value.initTag(.bool_false);
9602 field_values[1] = if (!info.mutable) Value.@"true" else Value.@"false";
96039603 // is_volatile: bool,
9604 field_values[2] = if (info.@"volatile") Value.initTag(.bool_true) else Value.initTag(.bool_false);
9604 field_values[2] = if (info.@"volatile") Value.@"true" else Value.@"false";
96059605 // alignment: comptime_int,
96069606 field_values[3] = try Value.Tag.int_u64.create(sema.arena, info.@"align");
96079607 // address_space: AddressSpace
......@@ -9609,7 +9609,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
96099609 // child: type,
96109610 field_values[5] = try Value.Tag.ty.create(sema.arena, info.pointee_type);
96119611 // is_allowzero: bool,
9612 field_values[6] = if (info.@"allowzero") Value.initTag(.bool_true) else Value.initTag(.bool_false);
9612 field_values[6] = if (info.@"allowzero") Value.@"true" else Value.@"false";
96139613 // sentinel: anytype,
96149614 field_values[7] = if (info.sentinel) |some| try Value.Tag.opt_payload.create(sema.arena, some) else Value.@"null";
96159615
......@@ -10060,18 +10060,17 @@ fn zirBoolNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1006010060 const operand_src = src; // TODO put this on the operand, not the `!`
1006110061 const uncasted_operand = sema.resolveInst(inst_data.operand);
1006210062
10063 const bool_type = Type.initTag(.bool);
10064 const operand = try sema.coerce(block, bool_type, uncasted_operand, operand_src);
10063 const operand = try sema.coerce(block, Type.bool, uncasted_operand, operand_src);
1006510064 if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |val| {
1006610065 return if (val.isUndef())
10067 sema.addConstUndef(bool_type)
10066 sema.addConstUndef(Type.bool)
1006810067 else if (val.toBool())
1006910068 Air.Inst.Ref.bool_false
1007010069 else
1007110070 Air.Inst.Ref.bool_true;
1007210071 }
1007310072 try sema.requireRuntimeBlock(block, src);
10074 return block.addTyOp(.not, bool_type, operand);
10073 return block.addTyOp(.not, Type.bool, operand);
1007510074}
1007610075
1007710076fn zirBoolBr(
......@@ -10229,7 +10228,7 @@ fn zirCondbr(
1022910228 const else_body = sema.code.extra[extra.end + then_body.len ..][0..extra.data.else_body_len];
1023010229
1023110230 const uncasted_cond = sema.resolveInst(extra.data.condition);
10232 const cond = try sema.coerce(parent_block, Type.initTag(.bool), uncasted_cond, cond_src);
10231 const cond = try sema.coerce(parent_block, Type.bool, uncasted_cond, cond_src);
1023310232
1023410233 if (try sema.resolveDefinedValue(parent_block, src, cond)) |cond_val| {
1023510234 const body = if (cond_val.toBool()) then_body else else_body;
......@@ -15537,7 +15536,7 @@ fn analyzeIsNull(
1553715536 operand: Air.Inst.Ref,
1553815537 invert_logic: bool,
1553915538) CompileError!Air.Inst.Ref {
15540 const result_ty = Type.initTag(.bool);
15539 const result_ty = Type.bool;
1554115540 if (try sema.resolveMaybeUndefVal(block, src, operand)) |opt_val| {
1554215541 if (opt_val.isUndef()) {
1554315542 return sema.addConstUndef(result_ty);
......@@ -15566,7 +15565,7 @@ fn analyzeIsNonErr(
1556615565 if (ot != .ErrorSet and ot != .ErrorUnion) return Air.Inst.Ref.bool_true;
1556715566 if (ot == .ErrorSet) return Air.Inst.Ref.bool_false;
1556815567 assert(ot == .ErrorUnion);
15569 const result_ty = Type.initTag(.bool);
15568 const result_ty = Type.bool;
1557015569 if (try sema.resolveMaybeUndefVal(block, src, operand)) |err_union| {
1557115570 if (err_union.isUndef()) {
1557215571 return sema.addConstUndef(result_ty);
......@@ -15769,7 +15768,7 @@ fn cmpNumeric(
1576915768 if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lhs_val| {
1577015769 if (try sema.resolveMaybeUndefVal(block, rhs_src, rhs)) |rhs_val| {
1577115770 if (lhs_val.isUndef() or rhs_val.isUndef()) {
15772 return sema.addConstUndef(Type.initTag(.bool));
15771 return sema.addConstUndef(Type.bool);
1577315772 }
1577415773 if (Value.compareHetero(lhs_val, op, rhs_val)) {
1577515774 return Air.Inst.Ref.bool_true;
......@@ -15840,7 +15839,7 @@ fn cmpNumeric(
1584015839 var lhs_bits: usize = undefined;
1584115840 if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lhs_val| {
1584215841 if (lhs_val.isUndef())
15843 return sema.addConstUndef(Type.initTag(.bool));
15842 return sema.addConstUndef(Type.bool);
1584415843 const is_unsigned = if (lhs_is_float) x: {
1584515844 var bigint_space: Value.BigIntSpace = undefined;
1584615845 var bigint = try lhs_val.toBigInt(&bigint_space).toManaged(sema.gpa);
......@@ -15875,7 +15874,7 @@ fn cmpNumeric(
1587515874 var rhs_bits: usize = undefined;
1587615875 if (try sema.resolveMaybeUndefVal(block, rhs_src, rhs)) |rhs_val| {
1587715876 if (rhs_val.isUndef())
15878 return sema.addConstUndef(Type.initTag(.bool));
15877 return sema.addConstUndef(Type.bool);
1587915878 const is_unsigned = if (rhs_is_float) x: {
1588015879 var bigint_space: Value.BigIntSpace = undefined;
1588115880 var bigint = try rhs_val.toBigInt(&bigint_space).toManaged(sema.gpa);
test/behavior/math.zig-2
......@@ -1024,8 +1024,6 @@ test "vector comparison" {
10241024}
10251025
10261026test "compare undefined literal with comptime_int" {
1027 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
1028
10291027 var x = undefined == 1;
10301028 // x is now undefined with type bool
10311029 x = true;