authorgravatar for yujiri@disroot.orgEvin Yulo <yujiri@disroot.org> 2023-05-31 08:05:55-04:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-05-31 19:18:36+03:00
log3085e2af4197193f08ea95d80afb2cf982227334
tree5bdd8be63ea03f2396e1d35fe5a50697f7ce6710
parentcd1417dbdf098634641e87dba4c3be2806d76250

add missing note "operation is runtime due to this operand"


3 files changed, 42 insertions(+), 23 deletions(-)

src/Sema.zig+16-6
......@@ -17961,20 +17961,21 @@ fn finishStructInit(
1796117961 return sema.failWithOwnedErrorMsg(msg);
1796217962 }
1796317963
17964 const is_comptime = for (field_inits) |field_init| {
17964 // Find which field forces the expression to be runtime, if any.
17965 const opt_runtime_index = for (field_inits, 0..) |field_init, i| {
1796517966 if (!(try sema.isComptimeKnown(field_init))) {
17966 break false;
17967 break i;
1796717968 }
17968 } else true;
17969 } else null;
1796917970
17970 if (is_comptime) {
17971 const runtime_index = opt_runtime_index orelse {
1797117972 const values = try sema.arena.alloc(Value, field_inits.len);
1797217973 for (field_inits, 0..) |field_init, i| {
1797317974 values[i] = (sema.resolveMaybeUndefVal(field_init) catch unreachable).?;
1797417975 }
1797517976 const struct_val = try Value.Tag.aggregate.create(sema.arena, values);
1797617977 return sema.addConstantMaybeRef(block, struct_ty, struct_val, is_ref);
17977 }
17978 };
1797817979
1797917980 if (is_ref) {
1798017981 try sema.resolveStructLayout(struct_ty);
......@@ -17994,7 +17995,15 @@ fn finishStructInit(
1799417995 return sema.makePtrConst(block, alloc);
1799517996 }
1799617997
17997 try sema.requireRuntimeBlock(block, dest_src, null);
17998 sema.requireRuntimeBlock(block, .unneeded, null) catch |err| switch (err) {
17999 error.NeededSourceLocation => {
18000 const decl = sema.mod.declPtr(block.src_decl);
18001 const field_src = Module.initSrc(dest_src.node_offset.x, sema.gpa, decl, runtime_index);
18002 try sema.requireRuntimeBlock(block, dest_src, field_src);
18003 unreachable;
18004 },
18005 else => |e| return e,
18006 };
1799818007 try sema.queueFullTypeResolution(struct_ty);
1799918008 return block.addAggregateInit(struct_ty, field_inits);
1800018009}
......@@ -18014,6 +18023,7 @@ fn zirStructInitAnon(
1801418023 defer fields.deinit(sema.gpa);
1801518024 try fields.ensureUnusedCapacity(sema.gpa, types.len);
1801618025
18026 // Find which field forces the expression to be runtime, if any.
1801718027 const opt_runtime_index = rs: {
1801818028 var runtime_index: ?usize = null;
1801918029 var extra_index = extra.end;
test/cases/compile_errors/unable_to_evaluate_comptime_expr.zig created+26
......@@ -0,0 +1,26 @@
1var n: u8 = 5;
2
3const S = struct {
4 a: u8,
5};
6
7var a: S = .{ .a = n };
8
9pub export fn entry1() void {
10 _ = a;
11}
12
13var b: S = S{ .a = n };
14
15pub export fn entry2() void {
16 _ = b;
17}
18
19// error
20// backend=stage2
21// target=native
22//
23// :7:13: error: unable to evaluate comptime expression
24// :7:16: note: operation is runtime due to this operand
25// :13:13: error: unable to evaluate comptime expression
26// :13:16: note: operation is runtime due to this operand
test/cases/compile_errors/unable_to_evaluate_comptime_expr_with_inferred_type.zig deleted-17
......@@ -1,17 +0,0 @@
1const A = struct {
2 a: u8,
3};
4
5var n: u8 = 5;
6var a: A = .{ .a = n };
7
8pub export fn entry() void {
9 _ = a;
10}
11
12// error
13// backend=stage2
14// target=native
15//
16// :6:13: error: unable to evaluate comptime expression
17// :6:16: note: operation is runtime due to this operand