From a0c44e806b70021203f5b9cf9af3b5049c40a75a Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Thu, 7 Oct 2021 17:11:27 -0700 Subject: [PATCH] stage2: fix comptime_float negation --- src/Sema.zig | 51 ++++++++++++++++++++++------------- test/behavior/eval.zig | 34 +++++++++++++++++++++++ test/behavior/eval_stage1.zig | 34 ----------------------- 3 files changed, 67 insertions(+), 52 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 8fc25a9278..0d0faf2a4b 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -6639,9 +6639,9 @@ fn zirNegate( defer tracy.end(); const inst_data = sema.code.instructions.items(.data)[inst].un_node; - const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node }; - const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node }; - const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node }; + const src = inst_data.src(); + const lhs_src = src; + const rhs_src = src; // TODO better source location const lhs = sema.resolveInst(.zero); const rhs = sema.resolveInst(inst_data.operand); @@ -9909,7 +9909,8 @@ fn zirCDefine( const src: LazySrcLoc = .{ .node_offset = extra.node }; const name = try sema.resolveConstString(block, src, extra.lhs); - if (sema.typeOf(extra.rhs).zigTypeTag() != .Void) { + const rhs = sema.resolveInst(extra.rhs); + if (sema.typeOf(rhs).zigTypeTag() != .Void) { const value = try sema.resolveConstString(block, src, extra.rhs); try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value }); } else { @@ -12085,16 +12086,19 @@ fn resolvePeerTypes( const chosen_ty = sema.typeOf(chosen); if (candidate_ty.eql(chosen_ty)) continue; - if (candidate_ty.zigTypeTag() == .NoReturn) + const candidate_ty_tag = candidate_ty.zigTypeTag(); + const chosen_ty_tag = chosen_ty.zigTypeTag(); + + if (candidate_ty_tag == .NoReturn) continue; - if (chosen_ty.zigTypeTag() == .NoReturn) { + if (chosen_ty_tag == .NoReturn) { chosen = candidate; chosen_i = candidate_i + 1; continue; } - if (candidate_ty.zigTypeTag() == .Undefined) + if (candidate_ty_tag == .Undefined) continue; - if (chosen_ty.zigTypeTag() == .Undefined) { + if (chosen_ty_tag == .Undefined) { chosen = candidate; chosen_i = candidate_i + 1; continue; @@ -12117,30 +12121,41 @@ fn resolvePeerTypes( continue; } - if (chosen_ty.zigTypeTag() == .ComptimeInt and candidate_ty.isInt()) { + if (chosen_ty_tag == .ComptimeInt and candidate_ty.isInt()) { chosen = candidate; chosen_i = candidate_i + 1; continue; } - if (chosen_ty.isInt() and candidate_ty.zigTypeTag() == .ComptimeInt) { + if (chosen_ty.isInt() and candidate_ty_tag == .ComptimeInt) { continue; } - if (chosen_ty.zigTypeTag() == .ComptimeFloat and candidate_ty.isRuntimeFloat()) { + if ((chosen_ty_tag == .ComptimeFloat or chosen_ty_tag == .ComptimeInt) and + candidate_ty.isRuntimeFloat()) + { + chosen = candidate; + chosen_i = candidate_i + 1; + continue; + } + if (chosen_ty.isRuntimeFloat() and + (candidate_ty_tag == .ComptimeFloat or candidate_ty_tag == .ComptimeInt)) + { + continue; + } + + if (chosen_ty_tag == .Enum and candidate_ty_tag == .EnumLiteral) { + continue; + } + if (chosen_ty_tag == .EnumLiteral and candidate_ty_tag == .Enum) { chosen = candidate; chosen_i = candidate_i + 1; continue; } - if (chosen_ty.isRuntimeFloat() and candidate_ty.zigTypeTag() == .ComptimeFloat) { + if (chosen_ty_tag == .ComptimeFloat and candidate_ty_tag == .ComptimeInt) continue; - } - - if (chosen_ty.zigTypeTag() == .Enum and candidate_ty.zigTypeTag() == .EnumLiteral) { - continue; - } - if (chosen_ty.zigTypeTag() == .EnumLiteral and candidate_ty.zigTypeTag() == .Enum) { + if (chosen_ty_tag == .ComptimeInt and candidate_ty_tag == .ComptimeFloat) { chosen = candidate; chosen_i = candidate_i + 1; continue; diff --git a/test/behavior/eval.zig b/test/behavior/eval.zig index 6acdf15e89..d30c911678 100644 --- a/test/behavior/eval.zig +++ b/test/behavior/eval.zig @@ -183,3 +183,37 @@ test "@setEvalBranchQuota" { try expect(sum == 500500); } } + +test "constant struct with negation" { + try expect(vertices[0].x == @as(f32, -0.6)); +} +const Vertex = struct { + x: f32, + y: f32, + r: f32, + g: f32, + b: f32, +}; +const vertices = [_]Vertex{ + Vertex{ + .x = -0.6, + .y = -0.4, + .r = 1.0, + .g = 0.0, + .b = 0.0, + }, + Vertex{ + .x = 0.6, + .y = -0.4, + .r = 0.0, + .g = 1.0, + .b = 0.0, + }, + Vertex{ + .x = 0.0, + .y = 0.6, + .r = 0.0, + .g = 0.0, + .b = 1.0, + }, +}; diff --git a/test/behavior/eval_stage1.zig b/test/behavior/eval_stage1.zig index 4e945d7af0..6440558abc 100644 --- a/test/behavior/eval_stage1.zig +++ b/test/behavior/eval_stage1.zig @@ -41,40 +41,6 @@ pub fn vec3(x: f32, y: f32, z: f32) Vec3 { }; } -test "constant struct with negation" { - try expect(vertices[0].x == -0.6); -} -const Vertex = struct { - x: f32, - y: f32, - r: f32, - g: f32, - b: f32, -}; -const vertices = [_]Vertex{ - Vertex{ - .x = -0.6, - .y = -0.4, - .r = 1.0, - .g = 0.0, - .b = 0.0, - }, - Vertex{ - .x = 0.6, - .y = -0.4, - .r = 0.0, - .g = 1.0, - .b = 0.0, - }, - Vertex{ - .x = 0.0, - .y = 0.6, - .r = 0.0, - .g = 0.0, - .b = 1.0, - }, -}; - test "statically initialized struct" { st_init_str_foo.x += 1; try expect(st_init_str_foo.x == 14);