authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-07 17:11:27-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-07 17:11:27-07:00
loga0c44e806b70021203f5b9cf9af3b5049c40a75a
tree4713c75b5ffcc004f80b9cf1d3df778a2aee8276
parent3df19b765d3b33cfda43b875adb714487bfb8c6b

stage2: fix comptime_float negation


3 files changed, 64 insertions(+), 49 deletions(-)

src/Sema.zig+30-15
...@@ -6639,9 +6639,9 @@ fn zirNegate(...@@ -6639,9 +6639,9 @@ fn zirNegate(
6639 defer tracy.end();6639 defer tracy.end();
66406640
6641 const inst_data = sema.code.instructions.items(.data)[inst].un_node;6641 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
6642 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };6642 const src = inst_data.src();
6643 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };6643 const lhs_src = src;
6644 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };6644 const rhs_src = src; // TODO better source location
6645 const lhs = sema.resolveInst(.zero);6645 const lhs = sema.resolveInst(.zero);
6646 const rhs = sema.resolveInst(inst_data.operand);6646 const rhs = sema.resolveInst(inst_data.operand);
66476647
...@@ -9909,7 +9909,8 @@ fn zirCDefine(...@@ -9909,7 +9909,8 @@ fn zirCDefine(
9909 const src: LazySrcLoc = .{ .node_offset = extra.node };9909 const src: LazySrcLoc = .{ .node_offset = extra.node };
99109910
9911 const name = try sema.resolveConstString(block, src, extra.lhs);9911 const name = try sema.resolveConstString(block, src, extra.lhs);
9912 if (sema.typeOf(extra.rhs).zigTypeTag() != .Void) {9912 const rhs = sema.resolveInst(extra.rhs);
9913 if (sema.typeOf(rhs).zigTypeTag() != .Void) {
9913 const value = try sema.resolveConstString(block, src, extra.rhs);9914 const value = try sema.resolveConstString(block, src, extra.rhs);
9914 try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value });9915 try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value });
9915 } else {9916 } else {
...@@ -12085,16 +12086,19 @@ fn resolvePeerTypes(...@@ -12085,16 +12086,19 @@ fn resolvePeerTypes(
12085 const chosen_ty = sema.typeOf(chosen);12086 const chosen_ty = sema.typeOf(chosen);
12086 if (candidate_ty.eql(chosen_ty))12087 if (candidate_ty.eql(chosen_ty))
12087 continue;12088 continue;
12088 if (candidate_ty.zigTypeTag() == .NoReturn)12089 const candidate_ty_tag = candidate_ty.zigTypeTag();
12090 const chosen_ty_tag = chosen_ty.zigTypeTag();
12091
12092 if (candidate_ty_tag == .NoReturn)
12089 continue;12093 continue;
12090 if (chosen_ty.zigTypeTag() == .NoReturn) {12094 if (chosen_ty_tag == .NoReturn) {
12091 chosen = candidate;12095 chosen = candidate;
12092 chosen_i = candidate_i + 1;12096 chosen_i = candidate_i + 1;
12093 continue;12097 continue;
12094 }12098 }
12095 if (candidate_ty.zigTypeTag() == .Undefined)12099 if (candidate_ty_tag == .Undefined)
12096 continue;12100 continue;
12097 if (chosen_ty.zigTypeTag() == .Undefined) {12101 if (chosen_ty_tag == .Undefined) {
12098 chosen = candidate;12102 chosen = candidate;
12099 chosen_i = candidate_i + 1;12103 chosen_i = candidate_i + 1;
12100 continue;12104 continue;
...@@ -12117,30 +12121,41 @@ fn resolvePeerTypes(...@@ -12117,30 +12121,41 @@ fn resolvePeerTypes(
12117 continue;12121 continue;
12118 }12122 }
1211912123
12120 if (chosen_ty.zigTypeTag() == .ComptimeInt and candidate_ty.isInt()) {12124 if (chosen_ty_tag == .ComptimeInt and candidate_ty.isInt()) {
12121 chosen = candidate;12125 chosen = candidate;
12122 chosen_i = candidate_i + 1;12126 chosen_i = candidate_i + 1;
12123 continue;12127 continue;
12124 }12128 }
1212512129
12126 if (chosen_ty.isInt() and candidate_ty.zigTypeTag() == .ComptimeInt) {12130 if (chosen_ty.isInt() and candidate_ty_tag == .ComptimeInt) {
12127 continue;12131 continue;
12128 }12132 }
1212912133
12130 if (chosen_ty.zigTypeTag() == .ComptimeFloat and candidate_ty.isRuntimeFloat()) {12134 if ((chosen_ty_tag == .ComptimeFloat or chosen_ty_tag == .ComptimeInt) and
12135 candidate_ty.isRuntimeFloat())
12136 {
12131 chosen = candidate;12137 chosen = candidate;
12132 chosen_i = candidate_i + 1;12138 chosen_i = candidate_i + 1;
12133 continue;12139 continue;
12134 }12140 }
1213512141 if (chosen_ty.isRuntimeFloat() and
12136 if (chosen_ty.isRuntimeFloat() and candidate_ty.zigTypeTag() == .ComptimeFloat) {12142 (candidate_ty_tag == .ComptimeFloat or candidate_ty_tag == .ComptimeInt))
12143 {
12137 continue;12144 continue;
12138 }12145 }
1213912146
12140 if (chosen_ty.zigTypeTag() == .Enum and candidate_ty.zigTypeTag() == .EnumLiteral) {12147 if (chosen_ty_tag == .Enum and candidate_ty_tag == .EnumLiteral) {
12141 continue;12148 continue;
12142 }12149 }
12143 if (chosen_ty.zigTypeTag() == .EnumLiteral and candidate_ty.zigTypeTag() == .Enum) {12150 if (chosen_ty_tag == .EnumLiteral and candidate_ty_tag == .Enum) {
12151 chosen = candidate;
12152 chosen_i = candidate_i + 1;
12153 continue;
12154 }
12155
12156 if (chosen_ty_tag == .ComptimeFloat and candidate_ty_tag == .ComptimeInt)
12157 continue;
12158 if (chosen_ty_tag == .ComptimeInt and candidate_ty_tag == .ComptimeFloat) {
12144 chosen = candidate;12159 chosen = candidate;
12145 chosen_i = candidate_i + 1;12160 chosen_i = candidate_i + 1;
12146 continue;12161 continue;
test/behavior/eval.zig+34
...@@ -183,3 +183,37 @@ test "@setEvalBranchQuota" {...@@ -183,3 +183,37 @@ test "@setEvalBranchQuota" {
183 try expect(sum == 500500);183 try expect(sum == 500500);
184 }184 }
185}185}
186
187test "constant struct with negation" {
188 try expect(vertices[0].x == @as(f32, -0.6));
189}
190const Vertex = struct {
191 x: f32,
192 y: f32,
193 r: f32,
194 g: f32,
195 b: f32,
196};
197const vertices = [_]Vertex{
198 Vertex{
199 .x = -0.6,
200 .y = -0.4,
201 .r = 1.0,
202 .g = 0.0,
203 .b = 0.0,
204 },
205 Vertex{
206 .x = 0.6,
207 .y = -0.4,
208 .r = 0.0,
209 .g = 1.0,
210 .b = 0.0,
211 },
212 Vertex{
213 .x = 0.0,
214 .y = 0.6,
215 .r = 0.0,
216 .g = 0.0,
217 .b = 1.0,
218 },
219};
test/behavior/eval_stage1.zig-34
...@@ -41,40 +41,6 @@ pub fn vec3(x: f32, y: f32, z: f32) Vec3 {...@@ -41,40 +41,6 @@ pub fn vec3(x: f32, y: f32, z: f32) Vec3 {
41 };41 };
42}42}
4343
44test "constant struct with negation" {
45 try expect(vertices[0].x == -0.6);
46}
47const Vertex = struct {
48 x: f32,
49 y: f32,
50 r: f32,
51 g: f32,
52 b: f32,
53};
54const vertices = [_]Vertex{
55 Vertex{
56 .x = -0.6,
57 .y = -0.4,
58 .r = 1.0,
59 .g = 0.0,
60 .b = 0.0,
61 },
62 Vertex{
63 .x = 0.6,
64 .y = -0.4,
65 .r = 0.0,
66 .g = 1.0,
67 .b = 0.0,
68 },
69 Vertex{
70 .x = 0.0,
71 .y = 0.6,
72 .r = 0.0,
73 .g = 0.0,
74 .b = 1.0,
75 },
76};
77
78test "statically initialized struct" {44test "statically initialized struct" {
79 st_init_str_foo.x += 1;45 st_init_str_foo.x += 1;
80 try expect(st_init_str_foo.x == 14);46 try expect(st_init_str_foo.x == 14);