authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-26 18:35:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-03-26 18:35:15-07:00
logda731e18c94fdd985812ec27cfdacff5199e55d2
tree47715840c4bef1fc8004c1b3a16c7976b50f4051
parenta72bfd00cf07da50a94a024d6f74167aa41382c5

stage2: implement source location: .node_offset_var_decl_ty


2 files changed, 36 insertions(+), 16 deletions(-)

src/Module.zig+23-3
...@@ -1608,7 +1608,27 @@ pub const SrcLoc = struct {...@@ -1608,7 +1608,27 @@ pub const SrcLoc = struct {
1608 const token_starts = tree.tokens.items(.start);1608 const token_starts = tree.tokens.items(.start);
1609 return token_starts[tok_index];1609 return token_starts[tok_index];
1610 },1610 },
1611 .node_offset_var_decl_ty => @panic("TODO"),1611 .node_offset_var_decl_ty => |node_off| {
1612 const decl = src_loc.container.decl;
1613 const node = decl.relativeToNodeIndex(node_off);
1614 const tree = decl.container.file_scope.base.tree();
1615 const node_tags = tree.nodes.items(.tag);
1616 const full = switch (node_tags[node]) {
1617 .global_var_decl => tree.globalVarDecl(node),
1618 .local_var_decl => tree.localVarDecl(node),
1619 .simple_var_decl => tree.simpleVarDecl(node),
1620 .aligned_var_decl => tree.alignedVarDecl(node),
1621 else => unreachable,
1622 };
1623 const tok_index = if (full.ast.type_node != 0) blk: {
1624 const main_tokens = tree.nodes.items(.main_token);
1625 break :blk main_tokens[full.ast.type_node];
1626 } else blk: {
1627 break :blk full.ast.mut_token + 1; // the name token
1628 };
1629 const token_starts = tree.tokens.items(.start);
1630 return token_starts[tok_index];
1631 },
1612 .node_offset_builtin_call_arg0 => @panic("TODO"),1632 .node_offset_builtin_call_arg0 => @panic("TODO"),
1613 .node_offset_builtin_call_arg1 => @panic("TODO"),1633 .node_offset_builtin_call_arg1 => @panic("TODO"),
1614 .node_offset_builtin_call_argn => unreachable, // Handled specially in `Sema`.1634 .node_offset_builtin_call_argn => unreachable, // Handled specially in `Sema`.
...@@ -1625,7 +1645,7 @@ pub const SrcLoc = struct {...@@ -1625,7 +1645,7 @@ pub const SrcLoc = struct {
1625 const node = decl.relativeToNodeIndex(node_off);1645 const node = decl.relativeToNodeIndex(node_off);
1626 const tree = decl.container.file_scope.base.tree();1646 const tree = decl.container.file_scope.base.tree();
1627 const node_tags = tree.nodes.items(.tag);1647 const node_tags = tree.nodes.items(.tag);
1628 const cond_expr = switch (node_tags[node]) {1648 const src_node = switch (node_tags[node]) {
1629 .if_simple => tree.ifSimple(node).ast.cond_expr,1649 .if_simple => tree.ifSimple(node).ast.cond_expr,
1630 .@"if" => tree.ifFull(node).ast.cond_expr,1650 .@"if" => tree.ifFull(node).ast.cond_expr,
1631 .while_simple => tree.whileSimple(node).ast.cond_expr,1651 .while_simple => tree.whileSimple(node).ast.cond_expr,
...@@ -1636,7 +1656,7 @@ pub const SrcLoc = struct {...@@ -1636,7 +1656,7 @@ pub const SrcLoc = struct {
1636 else => unreachable,1656 else => unreachable,
1637 };1657 };
1638 const main_tokens = tree.nodes.items(.main_token);1658 const main_tokens = tree.nodes.items(.main_token);
1639 const tok_index = main_tokens[cond_expr];1659 const tok_index = main_tokens[src_node];
1640 const token_starts = tree.tokens.items(.start);1660 const token_starts = tree.tokens.items(.start);
1641 return token_starts[tok_index];1661 return token_starts[tok_index];
1642 },1662 },
test/stage2/test.zig+13-13
...@@ -1232,11 +1232,11 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -1232,11 +1232,11 @@ pub fn addCases(ctx: *TestContext) !void {
1232 \\ foo: while (true) {}1232 \\ foo: while (true) {}
1233 \\}1233 \\}
1234 , &[_][]const u8{":2:5: error: unused while loop label"});1234 , &[_][]const u8{":2:5: error: unused while loop label"});
1235 //case.addError(1235 case.addError(
1236 // \\comptime {1236 \\comptime {
1237 // \\ foo: for ("foo") |_| {}1237 \\ foo: for ("foo") |_| {}
1238 // \\}1238 \\}
1239 //, &[_][]const u8{":2:5: error: unused for loop label"});1239 , &[_][]const u8{":2:5: error: unused for loop label"});
1240 case.addError(1240 case.addError(
1241 \\comptime {1241 \\comptime {
1242 \\ blk: {blk: {}}1242 \\ blk: {blk: {}}
...@@ -1247,14 +1247,14 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -1247,14 +1247,14 @@ pub fn addCases(ctx: *TestContext) !void {
1247 });1247 });
1248 }1248 }
12491249
1250 //{1250 {
1251 // var case = ctx.exe("bad inferred variable type", linux_x64);1251 var case = ctx.exe("bad inferred variable type", linux_x64);
1252 // case.addError(1252 case.addError(
1253 // \\export fn foo() void {1253 \\export fn foo() void {
1254 // \\ var x = null;1254 \\ var x = null;
1255 // \\}1255 \\}
1256 // , &[_][]const u8{":2:9: error: variable of type '@Type(.Null)' must be const or comptime"});1256 , &[_][]const u8{":2:9: error: variable of type '@Type(.Null)' must be const or comptime"});
1257 //}1257 }
12581258
1259 {1259 {
1260 var case = ctx.exe("compile error in inline fn call fixed", linux_x64);1260 var case = ctx.exe("compile error in inline fn call fixed", linux_x64);