authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-18 13:45:06+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-10-20 20:11:00+03:00
logc95a34b68f6075d7a9d305d17a6b03bc9fd1fff2
tree6e060e102426883b8b0894a0feea0dbdd0b19684
parent34e4b07d0c3cafd0aad15b217e8c56ab9af5de40

stage2: improve source location of assignment


19 files changed, 104 insertions(+), 40 deletions(-)

src/AstGen.zig+33-17
......@@ -232,7 +232,7 @@ pub const ResultLoc = union(enum) {
232232 coerced_ty: Zir.Inst.Ref,
233233 /// The expression must store its result into this typed pointer. The result instruction
234234 /// from the expression must be ignored.
235 ptr: Zir.Inst.Ref,
235 ptr: PtrResultLoc,
236236 /// The expression must store its result into this allocation, which has an inferred type.
237237 /// The result instruction from the expression must be ignored.
238238 /// Always an instruction with tag `alloc_inferred`.
......@@ -242,6 +242,11 @@ pub const ResultLoc = union(enum) {
242242 /// The result instruction from the expression must be ignored.
243243 block_ptr: *GenZir,
244244
245 const PtrResultLoc = struct {
246 inst: Zir.Inst.Ref,
247 src_node: ?Ast.Node.Index = null,
248 };
249
245250 pub const Strategy = struct {
246251 elide_store_to_block_ptr_instructions: bool,
247252 tag: Tag,
......@@ -1380,8 +1385,8 @@ fn arrayInitExpr(
13801385 const result = try arrayInitExprInner(gz, scope, node, array_init.ast.elements, types.array, types.elem, tag);
13811386 return rvalue(gz, rl, result, node);
13821387 },
1383 .ptr => |ptr_inst| {
1384 return arrayInitExprRlPtr(gz, scope, rl, node, ptr_inst, array_init.ast.elements, types.array);
1388 .ptr => |ptr_res| {
1389 return arrayInitExprRlPtr(gz, scope, rl, node, ptr_res.inst, array_init.ast.elements, types.array);
13851390 },
13861391 .inferred_ptr => |ptr_inst| {
13871392 if (types.array == .none) {
......@@ -1513,7 +1518,7 @@ fn arrayInitExprRlPtrInner(
15131518 });
15141519 astgen.extra.items[extra_index] = refToIndex(elem_ptr).?;
15151520 extra_index += 1;
1516 _ = try expr(gz, scope, .{ .ptr = elem_ptr }, elem_init);
1521 _ = try expr(gz, scope, .{ .ptr = .{ .inst = elem_ptr } }, elem_init);
15171522 }
15181523
15191524 const tag: Zir.Inst.Tag = if (gz.force_comptime)
......@@ -1631,7 +1636,7 @@ fn structInitExpr(
16311636 const result = try structInitExprRlTy(gz, scope, node, struct_init, inner_ty_inst, .struct_init);
16321637 return rvalue(gz, rl, result, node);
16331638 },
1634 .ptr => |ptr_inst| return structInitExprRlPtr(gz, scope, rl, node, struct_init, ptr_inst),
1639 .ptr => |ptr_res| return structInitExprRlPtr(gz, scope, rl, node, struct_init, ptr_res.inst),
16351640 .inferred_ptr => |ptr_inst| {
16361641 if (struct_init.ast.type_expr == 0) {
16371642 // We treat this case differently so that we don't get a crash when
......@@ -1739,7 +1744,7 @@ fn structInitExprRlPtrInner(
17391744 });
17401745 astgen.extra.items[extra_index] = refToIndex(field_ptr).?;
17411746 extra_index += 1;
1742 _ = try expr(gz, scope, .{ .ptr = field_ptr }, field_init);
1747 _ = try expr(gz, scope, .{ .ptr = .{ .inst = field_ptr } }, field_init);
17431748 }
17441749
17451750 const tag: Zir.Inst.Tag = if (gz.force_comptime)
......@@ -2998,7 +3003,7 @@ fn varDecl(
29983003 }
29993004 };
30003005 gz.rl_ty_inst = type_inst;
3001 break :a .{ .alloc = alloc, .result_loc = .{ .ptr = alloc } };
3006 break :a .{ .alloc = alloc, .result_loc = .{ .ptr = .{ .inst = alloc } } };
30023007 } else a: {
30033008 const alloc = alloc: {
30043009 if (align_inst == .none) {
......@@ -3098,7 +3103,10 @@ fn assign(gz: *GenZir, scope: *Scope, infix_node: Ast.Node.Index) InnerError!voi
30983103 }
30993104 }
31003105 const lvalue = try lvalExpr(gz, scope, lhs);
3101 _ = try expr(gz, scope, .{ .ptr = lvalue }, rhs);
3106 _ = try expr(gz, scope, .{ .ptr = .{
3107 .inst = lvalue,
3108 .src_node = infix_node,
3109 } }, rhs);
31023110}
31033111
31043112fn assignOp(
......@@ -6729,7 +6737,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
67296737 }
67306738
67316739 const rl: ResultLoc = if (nodeMayNeedMemoryLocation(tree, operand_node, true)) .{
6732 .ptr = try gz.addNode(.ret_ptr, node),
6740 .ptr = .{ .inst = try gz.addNode(.ret_ptr, node) },
67336741 } else .{
67346742 .ty = try gz.addNode(.ret_type, node),
67356743 };
......@@ -6748,7 +6756,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
67486756 },
67496757 .always => {
67506758 // Value is always an error. Emit both error defers and regular defers.
6751 const err_code = if (rl == .ptr) try gz.addUnNode(.load, rl.ptr, node) else operand;
6759 const err_code = if (rl == .ptr) try gz.addUnNode(.load, rl.ptr.inst, node) else operand;
67526760 try genDefers(gz, defer_outer, scope, .{ .both = err_code });
67536761 try emitDbgStmt(gz, ret_line, ret_column);
67546762 try gz.addRet(rl, operand, node);
......@@ -6765,7 +6773,7 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
67656773 }
67666774
67676775 // Emit conditional branch for generating errdefers.
6768 const result = if (rl == .ptr) try gz.addUnNode(.load, rl.ptr, node) else operand;
6776 const result = if (rl == .ptr) try gz.addUnNode(.load, rl.ptr.inst, node) else operand;
67696777 const is_non_err = try gz.addUnNode(.is_non_err, result, node);
67706778 const condbr = try gz.addCondBr(.condbr, node);
67716779
......@@ -7337,7 +7345,10 @@ fn as(
73377345 const result = try reachableExpr(gz, scope, .{ .ty = dest_type }, rhs, node);
73387346 return rvalue(gz, rl, result, node);
73397347 },
7340 .ptr, .inferred_ptr => |result_ptr| {
7348 .ptr => |result_ptr| {
7349 return asRlPtr(gz, scope, rl, node, result_ptr.inst, rhs, dest_type);
7350 },
7351 .inferred_ptr => |result_ptr| {
73417352 return asRlPtr(gz, scope, rl, node, result_ptr, rhs, dest_type);
73427353 },
73437354 .block_ptr => |block_scope| {
......@@ -9570,9 +9581,9 @@ fn rvalue(
95709581 }),
95719582 }
95729583 },
9573 .ptr => |ptr_inst| {
9574 _ = try gz.addPlNode(.store_node, src_node, Zir.Inst.Bin{
9575 .lhs = ptr_inst,
9584 .ptr => |ptr_res| {
9585 _ = try gz.addPlNode(.store_node, ptr_res.src_node orelse src_node, Zir.Inst.Bin{
9586 .lhs = ptr_res.inst,
95769587 .rhs = result,
95779588 });
95789589 return result;
......@@ -10445,11 +10456,16 @@ const GenZir = struct {
1044510456 gz.break_result_loc = parent_rl;
1044610457 },
1044710458
10448 .discard, .none, .ptr, .ref => {
10459 .discard, .none, .ref => {
1044910460 gz.rl_ty_inst = .none;
1045010461 gz.break_result_loc = parent_rl;
1045110462 },
1045210463
10464 .ptr => |ptr_res| {
10465 gz.rl_ty_inst = .none;
10466 gz.break_result_loc = .{ .ptr = .{ .inst = ptr_res.inst } };
10467 },
10468
1045310469 .inferred_ptr => |ptr| {
1045410470 gz.rl_ty_inst = .none;
1045510471 gz.rl_ptr = ptr;
......@@ -11610,7 +11626,7 @@ const GenZir = struct {
1161011626
1161111627 fn addRet(gz: *GenZir, rl: ResultLoc, operand: Zir.Inst.Ref, node: Ast.Node.Index) !void {
1161211628 switch (rl) {
11613 .ptr => |ret_ptr| _ = try gz.addUnNode(.ret_load, ret_ptr, node),
11629 .ptr => |ptr_res| _ = try gz.addUnNode(.ret_load, ptr_res.inst, node),
1161411630 .ty, .ty_shift_operand => _ = try gz.addUnNode(.ret_node, operand, node),
1161511631 else => unreachable,
1161611632 }
src/Module.zig+34
......@@ -2878,6 +2878,32 @@ pub const SrcLoc = struct {
28782878 };
28792879 return nodeToSpan(tree, full.ast.type_expr);
28802880 },
2881 .node_offset_store_ptr => |node_off| {
2882 const tree = try src_loc.file_scope.getTree(gpa);
2883 const node_tags = tree.nodes.items(.tag);
2884 const node_datas = tree.nodes.items(.data);
2885 const node = src_loc.declRelativeToNodeIndex(node_off);
2886
2887 switch (node_tags[node]) {
2888 .assign => {
2889 return nodeToSpan(tree, node_datas[node].lhs);
2890 },
2891 else => return nodeToSpan(tree, node),
2892 }
2893 },
2894 .node_offset_store_operand => |node_off| {
2895 const tree = try src_loc.file_scope.getTree(gpa);
2896 const node_tags = tree.nodes.items(.tag);
2897 const node_datas = tree.nodes.items(.data);
2898 const node = src_loc.declRelativeToNodeIndex(node_off);
2899
2900 switch (node_tags[node]) {
2901 .assign => {
2902 return nodeToSpan(tree, node_datas[node].rhs);
2903 },
2904 else => return nodeToSpan(tree, node),
2905 }
2906 },
28812907 }
28822908 }
28832909
......@@ -3213,6 +3239,12 @@ pub const LazySrcLoc = union(enum) {
32133239 /// The source location points to the type of an array or struct initializer.
32143240 /// The Decl is determined contextually.
32153241 node_offset_init_ty: i32,
3242 /// The source location points to the LHS of an assignment.
3243 /// The Decl is determined contextually.
3244 node_offset_store_ptr: i32,
3245 /// The source location points to the RHS of an assignment.
3246 /// The Decl is determined contextually.
3247 node_offset_store_operand: i32,
32163248
32173249 pub const nodeOffset = if (TracedOffset.want_tracing) nodeOffsetDebug else nodeOffsetRelease;
32183250
......@@ -3296,6 +3328,8 @@ pub const LazySrcLoc = union(enum) {
32963328 .node_offset_container_tag,
32973329 .node_offset_field_default,
32983330 .node_offset_init_ty,
3331 .node_offset_store_ptr,
3332 .node_offset_store_operand,
32993333 => .{
33003334 .file_scope = decl.getFileScope(),
33013335 .parent_decl_node = decl.src_node,
src/Sema.zig+2-2
......@@ -4639,8 +4639,8 @@ fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!v
46394639 try sema.addToInferredErrorSet(operand);
46404640 }
46414641
4642 const ptr_src = src; // TODO better soruce location
4643 const operand_src = src; // TODO better soruce location
4642 const ptr_src: LazySrcLoc = .{ .node_offset_store_ptr = inst_data.src_node };
4643 const operand_src: LazySrcLoc = .{ .node_offset_store_operand = inst_data.src_node };
46444644 const air_tag: Air.Inst.Tag = if (is_ret) .ret_ptr else .store;
46454645 return sema.storePtr2(block, src, ptr, ptr_src, operand, operand_src, air_tag);
46464646}
test/cases/compile_errors/any_typed_null_to_any_typed_optional.zig+2-2
......@@ -7,5 +7,5 @@ pub export fn entry() void {
77// backend=stage2
88// target=native
99//
10// :3:21: error: expected type '?*anyopaque', found '?usize'
11// :3:21: note: optional type child 'usize' cannot cast into optional type child '*anyopaque'
10// :3:9: error: expected type '?*anyopaque', found '?usize'
11// :3:9: note: optional type child 'usize' cannot cast into optional type child '*anyopaque'
test/cases/compile_errors/assign_through_constant_pointer.zig+1-1
......@@ -7,4 +7,4 @@ export fn f() void {
77// backend=stage2
88// target=native
99//
10// :3:13: error: cannot assign to constant
10// :3:7: error: cannot assign to constant
test/cases/compile_errors/assign_through_constant_slice.zig+1-1
......@@ -7,4 +7,4 @@ export fn f() void {
77// backend=stage2
88// target=native
99//
10// :3:13: error: cannot assign to constant
10// :3:7: error: cannot assign to constant
test/cases/compile_errors/assign_to_constant_field.zig+1-1
......@@ -10,4 +10,4 @@ export fn derp() void {
1010// backend=stage2
1111// target=native
1212//
13// :6:15: error: cannot assign to constant
13// :6:6: error: cannot assign to constant
test/cases/compile_errors/assign_to_constant_variable.zig+1-1
......@@ -75,7 +75,7 @@ export fn entry18() void {
7575// backend=stage2
7676// target=native
7777//
78// :3:9: error: cannot assign to constant
78// :3:5: error: cannot assign to constant
7979// :7:7: error: cannot assign to constant
8080// :11:7: error: cannot assign to constant
8181// :15:7: error: cannot assign to constant
test/cases/compile_errors/call_assigned_to_constant.zig+2-2
......@@ -20,5 +20,5 @@ export fn entry1() void {
2020// backend=stage2
2121// target=native
2222//
23// :12:14: error: cannot assign to constant
24// :16:14: error: cannot assign to constant
23// :12:5: error: cannot assign to constant
24// :16:5: error: cannot assign to constant
test/cases/compile_errors/comptime_store_in_comptime_switch_in_runtime_if.zig+1-1
......@@ -21,5 +21,5 @@ pub export fn entry() void {
2121// backend=stage2
2222// target=native
2323//
24// :13:27: error: store to comptime variable depends on runtime condition
24// :13:25: error: store to comptime variable depends on runtime condition
2525// :11:16: note: runtime condition here
test/cases/compile_errors/global_var_struct_init_in_comptim_block.zig created+14
......@@ -0,0 +1,14 @@
1const Foo = struct {
2 x: i32,
3};
4var x: Foo = .{ .x = 2 };
5comptime {
6 x = .{ .x = 3 };
7}
8
9// error
10// backend=stage2
11// target=native
12//
13// :6:17: error: unable to evaluate comptime expression
14// :6:17: note: operation is runtime due to this operand
test/cases/compile_errors/non-const_expression_function_call_with_struct_return_value_outside_function.zig+2-2
......@@ -14,6 +14,6 @@ export fn entry() usize { return @sizeOf(@TypeOf(a)); }
1414// backend=stage2
1515// target=native
1616//
17// :6:26: error: unable to evaluate comptime expression
18// :6:26: note: operation is runtime due to this operand
17// :6:24: error: unable to evaluate comptime expression
18// :6:5: note: operation is runtime due to this operand
1919// :4:17: note: called from here
test/cases/compile_errors/reassign_to_slice_parameter.zig+1-1
......@@ -9,4 +9,4 @@ export fn entry() void {
99// backend=llvm
1010// target=native
1111//
12// :2:10: error: cannot assign to constant
12// :2:5: error: cannot assign to constant
test/cases/compile_errors/reference_to_const_data.zig+4-4
......@@ -23,7 +23,7 @@ export fn qux() void {
2323// backend=stage2
2424// target=native
2525//
26// :3:14: error: cannot assign to constant
27// :7:13: error: cannot assign to constant
28// :11:13: error: cannot assign to constant
29// :19:13: error: cannot assign to constant
26// :3:8: error: cannot assign to constant
27// :7:8: error: cannot assign to constant
28// :11:8: error: cannot assign to constant
29// :19:8: error: cannot assign to constant
test/cases/compile_errors/write_to_const_global_variable.zig+1-1
......@@ -8,4 +8,4 @@ export fn entry() void { f(); }
88// backend=stage2
99// target=native
1010//
11// :3:9: error: cannot assign to constant
11// :3:5: error: cannot assign to constant
test/cases/x86_64-linux/comptime_var.0.zig+1-1
......@@ -8,5 +8,5 @@ pub fn main() void {
88// output_mode=Exe
99// target=x86_64-linux
1010//
11// :4:21: error: store to comptime variable depends on runtime condition
11// :4:19: error: store to comptime variable depends on runtime condition
1212// :4:11: note: runtime condition here
test/cases/x86_64-linux/comptime_var.1.zig+1-1
......@@ -9,5 +9,5 @@ pub fn main() void {
99
1010// error
1111//
12// :6:21: error: store to comptime variable depends on runtime condition
12// :6:19: error: store to comptime variable depends on runtime condition
1313// :4:13: note: runtime condition here
test/cases/x86_64-macos/comptime_var.0.zig+1-1
......@@ -8,5 +8,5 @@ pub fn main() void {
88// output_mode=Exe
99// target=x86_64-macos
1010//
11// :4:21: error: store to comptime variable depends on runtime condition
11// :4:19: error: store to comptime variable depends on runtime condition
1212// :4:11: note: runtime condition here
test/cases/x86_64-macos/comptime_var.1.zig+1-1
......@@ -9,5 +9,5 @@ pub fn main() void {
99
1010// error
1111//
12// :6:21: error: store to comptime variable depends on runtime condition
12// :6:19: error: store to comptime variable depends on runtime condition
1313// :4:13: note: runtime condition here