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(
66396639 defer tracy.end();
66406640
66416641 const inst_data = sema.code.instructions.items(.data)[inst].un_node;
6642 const src: LazySrcLoc = .{ .node_offset_bin_op = inst_data.src_node };
6643 const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node };
6644 const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node };
6642 const src = inst_data.src();
6643 const lhs_src = src;
6644 const rhs_src = src; // TODO better source location
66456645 const lhs = sema.resolveInst(.zero);
66466646 const rhs = sema.resolveInst(inst_data.operand);
66476647
......@@ -9909,7 +9909,8 @@ fn zirCDefine(
99099909 const src: LazySrcLoc = .{ .node_offset = extra.node };
99109910
99119911 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) {
99139914 const value = try sema.resolveConstString(block, src, extra.rhs);
99149915 try block.c_import_buf.?.writer().print("#define {s} {s}\n", .{ name, value });
99159916 } else {
......@@ -12085,16 +12086,19 @@ fn resolvePeerTypes(
1208512086 const chosen_ty = sema.typeOf(chosen);
1208612087 if (candidate_ty.eql(chosen_ty))
1208712088 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)
1208912093 continue;
12090 if (chosen_ty.zigTypeTag() == .NoReturn) {
12094 if (chosen_ty_tag == .NoReturn) {
1209112095 chosen = candidate;
1209212096 chosen_i = candidate_i + 1;
1209312097 continue;
1209412098 }
12095 if (candidate_ty.zigTypeTag() == .Undefined)
12099 if (candidate_ty_tag == .Undefined)
1209612100 continue;
12097 if (chosen_ty.zigTypeTag() == .Undefined) {
12101 if (chosen_ty_tag == .Undefined) {
1209812102 chosen = candidate;
1209912103 chosen_i = candidate_i + 1;
1210012104 continue;
......@@ -12117,30 +12121,41 @@ fn resolvePeerTypes(
1211712121 continue;
1211812122 }
1211912123
12120 if (chosen_ty.zigTypeTag() == .ComptimeInt and candidate_ty.isInt()) {
12124 if (chosen_ty_tag == .ComptimeInt and candidate_ty.isInt()) {
1212112125 chosen = candidate;
1212212126 chosen_i = candidate_i + 1;
1212312127 continue;
1212412128 }
1212512129
12126 if (chosen_ty.isInt() and candidate_ty.zigTypeTag() == .ComptimeInt) {
12130 if (chosen_ty.isInt() and candidate_ty_tag == .ComptimeInt) {
1212712131 continue;
1212812132 }
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 {
1213112137 chosen = candidate;
1213212138 chosen_i = candidate_i + 1;
1213312139 continue;
1213412140 }
12135
12136 if (chosen_ty.isRuntimeFloat() and candidate_ty.zigTypeTag() == .ComptimeFloat) {
12141 if (chosen_ty.isRuntimeFloat() and
12142 (candidate_ty_tag == .ComptimeFloat or candidate_ty_tag == .ComptimeInt))
12143 {
1213712144 continue;
1213812145 }
1213912146
12140 if (chosen_ty.zigTypeTag() == .Enum and candidate_ty.zigTypeTag() == .EnumLiteral) {
12147 if (chosen_ty_tag == .Enum and candidate_ty_tag == .EnumLiteral) {
1214112148 continue;
1214212149 }
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) {
1214412159 chosen = candidate;
1214512160 chosen_i = candidate_i + 1;
1214612161 continue;
test/behavior/eval.zig+34
......@@ -183,3 +183,37 @@ test "@setEvalBranchQuota" {
183183 try expect(sum == 500500);
184184 }
185185}
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 {
4141 };
4242}
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
7844test "statically initialized struct" {
7945 st_init_str_foo.x += 1;
8046 try expect(st_init_str_foo.x == 14);