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(...@@ -17961,20 +17961,21 @@ fn finishStructInit(
17961 return sema.failWithOwnedErrorMsg(msg);17961 return sema.failWithOwnedErrorMsg(msg);
17962 }17962 }
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| {
17965 if (!(try sema.isComptimeKnown(field_init))) {17966 if (!(try sema.isComptimeKnown(field_init))) {
17966 break false;17967 break i;
17967 }17968 }
17968 } else true;17969 } else null;
1796917970
17970 if (is_comptime) {17971 const runtime_index = opt_runtime_index orelse {
17971 const values = try sema.arena.alloc(Value, field_inits.len);17972 const values = try sema.arena.alloc(Value, field_inits.len);
17972 for (field_inits, 0..) |field_init, i| {17973 for (field_inits, 0..) |field_init, i| {
17973 values[i] = (sema.resolveMaybeUndefVal(field_init) catch unreachable).?;17974 values[i] = (sema.resolveMaybeUndefVal(field_init) catch unreachable).?;
17974 }17975 }
17975 const struct_val = try Value.Tag.aggregate.create(sema.arena, values);17976 const struct_val = try Value.Tag.aggregate.create(sema.arena, values);
17976 return sema.addConstantMaybeRef(block, struct_ty, struct_val, is_ref);17977 return sema.addConstantMaybeRef(block, struct_ty, struct_val, is_ref);
17977 }17978 };
1797817979
17979 if (is_ref) {17980 if (is_ref) {
17980 try sema.resolveStructLayout(struct_ty);17981 try sema.resolveStructLayout(struct_ty);
...@@ -17994,7 +17995,15 @@ fn finishStructInit(...@@ -17994,7 +17995,15 @@ fn finishStructInit(
17994 return sema.makePtrConst(block, alloc);17995 return sema.makePtrConst(block, alloc);
17995 }17996 }
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 };
17998 try sema.queueFullTypeResolution(struct_ty);18007 try sema.queueFullTypeResolution(struct_ty);
17999 return block.addAggregateInit(struct_ty, field_inits);18008 return block.addAggregateInit(struct_ty, field_inits);
18000}18009}
...@@ -18014,6 +18023,7 @@ fn zirStructInitAnon(...@@ -18014,6 +18023,7 @@ fn zirStructInitAnon(
18014 defer fields.deinit(sema.gpa);18023 defer fields.deinit(sema.gpa);
18015 try fields.ensureUnusedCapacity(sema.gpa, types.len);18024 try fields.ensureUnusedCapacity(sema.gpa, types.len);
1801618025
18026 // Find which field forces the expression to be runtime, if any.
18017 const opt_runtime_index = rs: {18027 const opt_runtime_index = rs: {
18018 var runtime_index: ?usize = null;18028 var runtime_index: ?usize = null;
18019 var extra_index = extra.end;18029 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