authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-07-28 17:28:37+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-08-01 23:37:01+03:00
logf1768b40b2468d63355c8cf83d3614ae23a54317
treede545d2d3b1c91c08f4223d67cb00157718adade
parent57f9405a8fcaec6043d680fa47ae0e98709160c2

stage2: better source location for var decls


11 files changed, 131 insertions(+), 51 deletions(-)

src/AstGen.zig+2-1
......@@ -7424,7 +7424,8 @@ fn builtinCall(
74247424 const token_starts = tree.tokens.items(.start);
74257425 const node_start = token_starts[tree.firstToken(node)];
74267426 astgen.advanceSourceCursor(node_start);
7427 const result = try gz.addExtendedPayload(.builtin_src, Zir.Inst.LineColumn{
7427 const result = try gz.addExtendedPayload(.builtin_src, Zir.Inst.Src{
7428 .node = gz.nodeIndexToRelative(node),
74287429 .line = astgen.source_line,
74297430 .column = astgen.source_column,
74307431 });
src/Module.zig+94-13
......@@ -2161,6 +2161,10 @@ pub const SrcLoc = struct {
21612161 .local_var_decl => tree.localVarDecl(node),
21622162 .simple_var_decl => tree.simpleVarDecl(node),
21632163 .aligned_var_decl => tree.alignedVarDecl(node),
2164 .@"usingnamespace" => {
2165 const node_data = tree.nodes.items(.data);
2166 return nodeToSpan(tree, node_data[node].lhs);
2167 },
21642168 else => unreachable,
21652169 };
21662170 if (full.ast.type_node != 0) {
......@@ -2171,6 +2175,58 @@ pub const SrcLoc = struct {
21712175 const end = start + @intCast(u32, tree.tokenSlice(tok_index).len);
21722176 return Span{ .start = start, .end = end, .main = start };
21732177 },
2178 .node_offset_var_decl_align => |node_off| {
2179 const tree = try src_loc.file_scope.getTree(gpa);
2180 const node = src_loc.declRelativeToNodeIndex(node_off);
2181 const node_tags = tree.nodes.items(.tag);
2182 const full: Ast.full.VarDecl = switch (node_tags[node]) {
2183 .global_var_decl => tree.globalVarDecl(node),
2184 .local_var_decl => tree.localVarDecl(node),
2185 .simple_var_decl => tree.simpleVarDecl(node),
2186 .aligned_var_decl => tree.alignedVarDecl(node),
2187 else => unreachable,
2188 };
2189 return nodeToSpan(tree, full.ast.align_node);
2190 },
2191 .node_offset_var_decl_section => |node_off| {
2192 const tree = try src_loc.file_scope.getTree(gpa);
2193 const node = src_loc.declRelativeToNodeIndex(node_off);
2194 const node_tags = tree.nodes.items(.tag);
2195 const full: Ast.full.VarDecl = switch (node_tags[node]) {
2196 .global_var_decl => tree.globalVarDecl(node),
2197 .local_var_decl => tree.localVarDecl(node),
2198 .simple_var_decl => tree.simpleVarDecl(node),
2199 .aligned_var_decl => tree.alignedVarDecl(node),
2200 else => unreachable,
2201 };
2202 return nodeToSpan(tree, full.ast.section_node);
2203 },
2204 .node_offset_var_decl_addrspace => |node_off| {
2205 const tree = try src_loc.file_scope.getTree(gpa);
2206 const node = src_loc.declRelativeToNodeIndex(node_off);
2207 const node_tags = tree.nodes.items(.tag);
2208 const full: Ast.full.VarDecl = switch (node_tags[node]) {
2209 .global_var_decl => tree.globalVarDecl(node),
2210 .local_var_decl => tree.localVarDecl(node),
2211 .simple_var_decl => tree.simpleVarDecl(node),
2212 .aligned_var_decl => tree.alignedVarDecl(node),
2213 else => unreachable,
2214 };
2215 return nodeToSpan(tree, full.ast.addrspace_node);
2216 },
2217 .node_offset_var_decl_init => |node_off| {
2218 const tree = try src_loc.file_scope.getTree(gpa);
2219 const node = src_loc.declRelativeToNodeIndex(node_off);
2220 const node_tags = tree.nodes.items(.tag);
2221 const full: Ast.full.VarDecl = switch (node_tags[node]) {
2222 .global_var_decl => tree.globalVarDecl(node),
2223 .local_var_decl => tree.localVarDecl(node),
2224 .simple_var_decl => tree.simpleVarDecl(node),
2225 .aligned_var_decl => tree.alignedVarDecl(node),
2226 else => unreachable,
2227 };
2228 return nodeToSpan(tree, full.ast.init_node);
2229 },
21742230 .node_offset_builtin_call_arg0 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 0),
21752231 .node_offset_builtin_call_arg1 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 1),
21762232 .node_offset_builtin_call_arg2 => |n| return src_loc.byteOffsetBuiltinCallArg(gpa, n, 2),
......@@ -2857,6 +2913,18 @@ pub const LazySrcLoc = union(enum) {
28572913 /// to the type expression.
28582914 /// The Decl is determined contextually.
28592915 node_offset_var_decl_ty: i32,
2916 /// The source location points to the alignment expression of a var decl.
2917 /// The Decl is determined contextually.
2918 node_offset_var_decl_align: i32,
2919 /// The source location points to the linksection expression of a var decl.
2920 /// The Decl is determined contextually.
2921 node_offset_var_decl_section: i32,
2922 /// The source location points to the addrspace expression of a var decl.
2923 /// The Decl is determined contextually.
2924 node_offset_var_decl_addrspace: i32,
2925 /// The source location points to the initializer of a var decl.
2926 /// The Decl is determined contextually.
2927 node_offset_var_decl_init: i32,
28602928 /// The source location points to a for loop condition expression,
28612929 /// found by taking this AST node index offset from the containing
28622930 /// Decl AST node, which points to a for loop AST node. Next, navigate
......@@ -3098,6 +3166,10 @@ pub const LazySrcLoc = union(enum) {
30983166 .node_offset,
30993167 .node_offset_initializer,
31003168 .node_offset_var_decl_ty,
3169 .node_offset_var_decl_align,
3170 .node_offset_var_decl_section,
3171 .node_offset_var_decl_addrspace,
3172 .node_offset_var_decl_init,
31013173 .node_offset_for_cond,
31023174 .node_offset_builtin_call_arg0,
31033175 .node_offset_builtin_call_arg1,
......@@ -4414,17 +4486,26 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
44144486 const body = zir.extra[extra.end..][0..extra.data.body_len];
44154487 const result_ref = (try sema.analyzeBodyBreak(&block_scope, body)).?.operand;
44164488 try wip_captures.finalize();
4417 const src = LazySrcLoc.nodeOffset(0);
4418 const decl_tv = try sema.resolveInstValue(&block_scope, .unneeded, result_ref, undefined);
4489 const align_src: LazySrcLoc = .{ .node_offset_var_decl_align = 0 };
4490 const section_src: LazySrcLoc = .{ .node_offset_var_decl_section = 0 };
4491 const address_space_src: LazySrcLoc = .{ .node_offset_var_decl_addrspace = 0 };
4492 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 };
4493 const init_src: LazySrcLoc = .{ .node_offset_var_decl_init = 0 };
4494 const decl_tv = try sema.resolveInstValue(&block_scope, init_src, result_ref, undefined);
44194495 const decl_align: u32 = blk: {
44204496 const align_ref = decl.zirAlignRef();
44214497 if (align_ref == .none) break :blk 0;
4422 break :blk try sema.resolveAlign(&block_scope, src, align_ref);
4498 break :blk try sema.resolveAlign(&block_scope, align_src, align_ref);
44234499 };
44244500 const decl_linksection: ?[*:0]const u8 = blk: {
44254501 const linksection_ref = decl.zirLinksectionRef();
44264502 if (linksection_ref == .none) break :blk null;
4427 const bytes = try sema.resolveConstString(&block_scope, src, linksection_ref, "linksection must be comptime known");
4503 const bytes = try sema.resolveConstString(&block_scope, section_src, linksection_ref, "linksection must be comptime known");
4504 if (mem.indexOfScalar(u8, bytes, 0) != null) {
4505 return sema.fail(&block_scope, section_src, "linksection cannot contain null bytes", .{});
4506 } else if (bytes.len == 0) {
4507 return sema.fail(&block_scope, section_src, "linksection cannot be empty", .{});
4508 }
44284509 break :blk (try decl_arena_allocator.dupeZ(u8, bytes)).ptr;
44294510 };
44304511 const target = sema.mod.getTarget();
......@@ -4442,27 +4523,27 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
44424523 .constant => target_util.defaultAddressSpace(target, .global_constant),
44434524 else => unreachable,
44444525 },
4445 else => |addrspace_ref| try sema.analyzeAddrspace(&block_scope, src, addrspace_ref, addrspace_ctx),
4526 else => |addrspace_ref| try sema.analyzeAddrspace(&block_scope, address_space_src, addrspace_ref, addrspace_ctx),
44464527 };
44474528 };
44484529
44494530 // Note this resolves the type of the Decl, not the value; if this Decl
44504531 // is a struct, for example, this resolves `type` (which needs no resolution),
44514532 // not the struct itself.
4452 try sema.resolveTypeLayout(&block_scope, src, decl_tv.ty);
4533 try sema.resolveTypeLayout(&block_scope, ty_src, decl_tv.ty);
44534534
44544535 const decl_arena_state = try decl_arena_allocator.create(std.heap.ArenaAllocator.State);
44554536
44564537 if (decl.is_usingnamespace) {
44574538 if (!decl_tv.ty.eql(Type.type, mod)) {
4458 return sema.fail(&block_scope, src, "expected type, found {}", .{
4539 return sema.fail(&block_scope, ty_src, "expected type, found {}", .{
44594540 decl_tv.ty.fmt(mod),
44604541 });
44614542 }
44624543 var buffer: Value.ToTypeBuffer = undefined;
44634544 const ty = try decl_tv.val.toType(&buffer).copy(decl_arena_allocator);
44644545 if (ty.getNamespace() == null) {
4465 return sema.fail(&block_scope, src, "type {} has no namespace", .{ty.fmt(mod)});
4546 return sema.fail(&block_scope, ty_src, "type {} has no namespace", .{ty.fmt(mod)});
44664547 }
44674548
44684549 decl.ty = Type.type;
......@@ -4508,7 +4589,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
45084589 decl.analysis = .complete;
45094590 decl.generation = mod.generation;
45104591
4511 const has_runtime_bits = try sema.fnHasRuntimeBits(&block_scope, src, decl.ty);
4592 const has_runtime_bits = try sema.fnHasRuntimeBits(&block_scope, ty_src, decl.ty);
45124593
45134594 if (has_runtime_bits) {
45144595 // We don't fully codegen the decl until later, but we do need to reserve a global
......@@ -4525,7 +4606,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
45254606
45264607 const is_inline = decl.ty.fnCallingConvention() == .Inline;
45274608 if (decl.is_exported) {
4528 const export_src = src; // TODO make this point at `export` token
4609 const export_src: LazySrcLoc = .{ .token_offset = @boolToInt(decl.is_pub) };
45294610 if (is_inline) {
45304611 return sema.fail(&block_scope, export_src, "export of inline function", .{});
45314612 }
......@@ -4588,14 +4669,14 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
45884669 decl.generation = mod.generation;
45894670
45904671 const has_runtime_bits = is_extern or
4591 (queue_linker_work and try sema.typeHasRuntimeBits(&block_scope, src, decl.ty));
4672 (queue_linker_work and try sema.typeHasRuntimeBits(&block_scope, ty_src, decl.ty));
45924673
45934674 if (has_runtime_bits) {
45944675 log.debug("queue linker work for {*} ({s})", .{ decl, decl.name });
45954676
45964677 // Needed for codegen_decl which will call updateDecl and then the
45974678 // codegen backend wants full access to the Decl Type.
4598 try sema.resolveTypeFully(&block_scope, src, decl.ty);
4679 try sema.resolveTypeFully(&block_scope, ty_src, decl.ty);
45994680
46004681 try mod.comp.bin_file.allocateDeclIndexes(decl_index);
46014682 try mod.comp.work_queue.writeItem(.{ .codegen_decl = decl_index });
......@@ -4606,7 +4687,7 @@ fn semaDecl(mod: *Module, decl_index: Decl.Index) !bool {
46064687 }
46074688
46084689 if (decl.is_exported) {
4609 const export_src = src; // TODO point to the export token
4690 const export_src: LazySrcLoc = .{ .token_offset = @boolToInt(decl.is_pub) };
46104691 // The scope needs to have the decl in it.
46114692 const options: std.builtin.ExportOptions = .{ .name = mem.sliceTo(decl.name, 0) };
46124693 try sema.analyzeExport(&block_scope, export_src, options, decl_index);
src/Sema.zig+7-15
......@@ -2971,7 +2971,7 @@ fn zirAllocExtended(
29712971 const extra = sema.code.extraData(Zir.Inst.AllocExtended, extended.operand);
29722972 const src = LazySrcLoc.nodeOffset(extra.data.src_node);
29732973 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = extra.data.src_node };
2974 const align_src = src; // TODO better source location
2974 const align_src: LazySrcLoc = .{ .node_offset_var_decl_align = extra.data.src_node };
29752975 const small = @bitCast(Zir.Inst.AllocExtended.Small, extended.small);
29762976
29772977 var extra_index: usize = extra.end;
......@@ -8046,7 +8046,7 @@ fn zirPtrToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai
80468046 if (try sema.resolveMaybeUndefValIntable(block, ptr_src, ptr)) |ptr_val| {
80478047 return sema.addConstant(Type.usize, ptr_val);
80488048 }
8049 try sema.requireRuntimeBlock(block, ptr_src, ptr_src);
8049 try sema.requireRuntimeBlock(block, inst_data.src(), ptr_src);
80508050 return block.addUnOp(.ptrtoint, ptr);
80518051}
80528052
......@@ -13174,8 +13174,8 @@ fn zirBuiltinSrc(
1317413174 const tracy = trace(@src());
1317513175 defer tracy.end();
1317613176
13177 const src = sema.src; // TODO better source location
13178 const extra = sema.code.extraData(Zir.Inst.LineColumn, extended.operand).data;
13177 const extra = sema.code.extraData(Zir.Inst.Src, extended.operand).data;
13178 const src = LazySrcLoc.nodeOffset(extra.node);
1317913179 const func = sema.func orelse return sema.fail(block, src, "@src outside function", .{});
1318013180 const fn_owner_decl = sema.mod.declPtr(func.owner_decl);
1318113181
......@@ -18959,10 +18959,8 @@ fn zirVarExtended(
1895918959 extended: Zir.Inst.Extended.InstData,
1896018960) CompileError!Air.Inst.Ref {
1896118961 const extra = sema.code.extraData(Zir.Inst.ExtendedVar, extended.operand);
18962 const src = sema.src;
18963 const ty_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at type
18964 const name_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at the name token
18965 const init_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at init expr
18962 const ty_src: LazySrcLoc = .{ .node_offset_var_decl_ty = 0 };
18963 const init_src: LazySrcLoc = .{ .node_offset_var_decl_init = 0 };
1896618964 const small = @bitCast(Zir.Inst.ExtendedVar.Small, extended.small);
1896718965
1896818966 var extra_index: usize = extra.end;
......@@ -18976,12 +18974,6 @@ fn zirVarExtended(
1897618974 // ZIR supports encoding this information but it is not used; the information
1897718975 // is encoded via the Decl entry.
1897818976 assert(!small.has_align);
18979 //const align_val: Value = if (small.has_align) blk: {
18980 // const align_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
18981 // extra_index += 1;
18982 // const align_tv = try sema.resolveInstConst(block, align_src, align_ref);
18983 // break :blk align_tv.val;
18984 //} else Value.@"null";
1898518977
1898618978 const uncasted_init: Air.Inst.Ref = if (small.has_init) blk: {
1898718979 const init_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
......@@ -19005,7 +18997,7 @@ fn zirVarExtended(
1900518997 return sema.failWithNeededComptime(block, init_src, "container level variable initializers must be comptime known");
1900618998 } else Value.initTag(.unreachable_value);
1900718999
19008 try sema.validateVarType(block, name_src, var_ty, small.is_extern);
19000 try sema.validateVarType(block, ty_src, var_ty, small.is_extern);
1900919001
1901019002 const new_var = try sema.gpa.create(Module.Var);
1901119003 errdefer sema.gpa.destroy(new_var);
src/Zir.zig+6
......@@ -3548,6 +3548,12 @@ pub const Inst = struct {
35483548 ty: Ref,
35493549 init_count: u32,
35503550 };
3551
3552 pub const Src = struct {
3553 node: i32,
3554 line: u32,
3555 column: u32,
3556 };
35513557};
35523558
35533559pub const SpecialProng = enum { none, @"else", under };
test/cases/compile_errors/global_variable_alignment_non_power_of_2.zig created+8
......@@ -0,0 +1,8 @@
1const some_data: [100]u8 align(3) = undefined;
2export fn entry() usize { return @sizeOf(@TypeOf(some_data)); }
3
4// error
5// backend=stage2
6// target=native
7//
8// :1:32: error: alignment value '3' is not a power of two
test/cases/compile_errors/src_outside_function.zig created+9
......@@ -0,0 +1,9 @@
1comptime {
2 @src();
3}
4
5// error
6// backend=stage2
7// target=native
8//
9// :2:5: error: @src outside function
test/cases/compile_errors/stage1/obj/global_variable_alignment_non_power_of_2.zig deleted-8
......@@ -1,8 +0,0 @@
1const some_data: [100]u8 align(3) = undefined;
2export fn entry() usize { return @sizeOf(@TypeOf(some_data)); }
3
4// error
5// backend=stage1
6// target=native
7//
8// tmp.zig:1:32: error: alignment value 3 is not a power of 2
test/cases/compile_errors/stage1/obj/src_outside_function.zig deleted-9
......@@ -1,9 +0,0 @@
1comptime {
2 @src();
3}
4
5// error
6// backend=stage1
7// target=native
8//
9// tmp.zig:2:5: error: @src outside function
test/cases/compile_errors/type_variables_must_be_constant.zig+2-2
......@@ -7,5 +7,5 @@ export fn entry() foo {
77// backend=stage2
88// target=native
99//
10// :1:1: error: variable of type 'type' must be const or comptime
11// :1:1: note: types are not available at runtime
10// :1:5: error: variable of type 'type' must be const or comptime
11// :1:5: note: types are not available at runtime
test/cases/compile_errors/use_invalid_number_literal_as_array_index.zig+2-2
......@@ -8,5 +8,5 @@ export fn entry() void {
88// backend=stage2
99// target=native
1010//
11// :1:1: error: variable of type 'comptime_int' must be const or comptime
12// :1:1: note: to modify this variable at runtime, it must be given an explicit fixed-size number type
11// :1:5: error: variable of type 'comptime_int' must be const or comptime
12// :1:5: note: to modify this variable at runtime, it must be given an explicit fixed-size number type
test/cases/compile_errors/usingnamespace_with_wrong_type.zig+1-1
......@@ -4,4 +4,4 @@ usingnamespace void;
44// backend=stage2
55// target=native
66//
7// :1:1: error: type void has no namespace
7// :1:16: error: type void has no namespace