authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-02 15:06:32-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-02 15:06:32-07:00
logd5f77c0babb83e5e9d651fa96b84f374dd3f0c57
treebaf1769e190bbddd727fe21f08491630612cf961
parent0611aa39858e6d7613cda3b3da981ff6b208a5e8

stage2: fix error reporting not loading AST

In the byteOffset function, compile errors may need to compute the AST from source bytes in order to resolve source locations. Previously there were a few lines trying to access the AST before it was loaded. Trivial fix, just move load the tree at the beginning.

2 files changed, 10 insertions(+), 11 deletions(-)

BRANCH_TODO-1
...@@ -1,4 +1,3 @@...@@ -1,4 +1,3 @@
1 * running build-exe with cached ZIR crashes
2 * implement lazy struct field resolution; don't resolve struct fields until1 * implement lazy struct field resolution; don't resolve struct fields until
3 they are needed.2 they are needed.
4 * decouple AstGen from Module, Compilation3 * decouple AstGen from Module, Compilation
src/Module.zig+10-10
...@@ -2186,14 +2186,14 @@ pub const SrcLoc = struct {...@@ -2186,14 +2186,14 @@ pub const SrcLoc = struct {
2186 return token_starts[src_loc.declSrcToken()] + byte_off;2186 return token_starts[src_loc.declSrcToken()] + byte_off;
2187 },2187 },
2188 .token_offset => |tok_off| {2188 .token_offset => |tok_off| {
2189 const tok_index = src_loc.declSrcToken() + tok_off;
2190 const tree = try src_loc.file_scope.getTree(gpa);2189 const tree = try src_loc.file_scope.getTree(gpa);
2190 const tok_index = src_loc.declSrcToken() + tok_off;
2191 const token_starts = tree.tokens.items(.start);2191 const token_starts = tree.tokens.items(.start);
2192 return token_starts[tok_index];2192 return token_starts[tok_index];
2193 },2193 },
2194 .node_offset, .node_offset_bin_op => |node_off| {2194 .node_offset, .node_offset_bin_op => |node_off| {
2195 const node = src_loc.declRelativeToNodeIndex(node_off);
2196 const tree = try src_loc.file_scope.getTree(gpa);2195 const tree = try src_loc.file_scope.getTree(gpa);
2196 const node = src_loc.declRelativeToNodeIndex(node_off);
2197 assert(src_loc.file_scope.tree_loaded);2197 assert(src_loc.file_scope.tree_loaded);
2198 const main_tokens = tree.nodes.items(.main_token);2198 const main_tokens = tree.nodes.items(.main_token);
2199 const tok_index = main_tokens[node];2199 const tok_index = main_tokens[node];
...@@ -2201,15 +2201,15 @@ pub const SrcLoc = struct {...@@ -2201,15 +2201,15 @@ pub const SrcLoc = struct {
2201 return token_starts[tok_index];2201 return token_starts[tok_index];
2202 },2202 },
2203 .node_offset_back2tok => |node_off| {2203 .node_offset_back2tok => |node_off| {
2204 const node = src_loc.declRelativeToNodeIndex(node_off);
2205 const tree = try src_loc.file_scope.getTree(gpa);2204 const tree = try src_loc.file_scope.getTree(gpa);
2205 const node = src_loc.declRelativeToNodeIndex(node_off);
2206 const tok_index = tree.firstToken(node) - 2;2206 const tok_index = tree.firstToken(node) - 2;
2207 const token_starts = tree.tokens.items(.start);2207 const token_starts = tree.tokens.items(.start);
2208 return token_starts[tok_index];2208 return token_starts[tok_index];
2209 },2209 },
2210 .node_offset_var_decl_ty => |node_off| {2210 .node_offset_var_decl_ty => |node_off| {
2211 const node = src_loc.declRelativeToNodeIndex(node_off);
2212 const tree = try src_loc.file_scope.getTree(gpa);2211 const tree = try src_loc.file_scope.getTree(gpa);
2212 const node = src_loc.declRelativeToNodeIndex(node_off);
2213 const node_tags = tree.nodes.items(.tag);2213 const node_tags = tree.nodes.items(.tag);
2214 const full = switch (node_tags[node]) {2214 const full = switch (node_tags[node]) {
2215 .global_var_decl => tree.globalVarDecl(node),2215 .global_var_decl => tree.globalVarDecl(node),
...@@ -2362,8 +2362,8 @@ pub const SrcLoc = struct {...@@ -2362,8 +2362,8 @@ pub const SrcLoc = struct {
2362 },2362 },
23632363
2364 .node_offset_for_cond, .node_offset_if_cond => |node_off| {2364 .node_offset_for_cond, .node_offset_if_cond => |node_off| {
2365 const node = src_loc.declRelativeToNodeIndex(node_off);
2366 const tree = try src_loc.file_scope.getTree(gpa);2365 const tree = try src_loc.file_scope.getTree(gpa);
2366 const node = src_loc.declRelativeToNodeIndex(node_off);
2367 const node_tags = tree.nodes.items(.tag);2367 const node_tags = tree.nodes.items(.tag);
2368 const src_node = switch (node_tags[node]) {2368 const src_node = switch (node_tags[node]) {
2369 .if_simple => tree.ifSimple(node).ast.cond_expr,2369 .if_simple => tree.ifSimple(node).ast.cond_expr,
...@@ -2381,8 +2381,8 @@ pub const SrcLoc = struct {...@@ -2381,8 +2381,8 @@ pub const SrcLoc = struct {
2381 return token_starts[tok_index];2381 return token_starts[tok_index];
2382 },2382 },
2383 .node_offset_bin_lhs => |node_off| {2383 .node_offset_bin_lhs => |node_off| {
2384 const node = src_loc.declRelativeToNodeIndex(node_off);
2385 const tree = try src_loc.file_scope.getTree(gpa);2384 const tree = try src_loc.file_scope.getTree(gpa);
2385 const node = src_loc.declRelativeToNodeIndex(node_off);
2386 const node_datas = tree.nodes.items(.data);2386 const node_datas = tree.nodes.items(.data);
2387 const src_node = node_datas[node].lhs;2387 const src_node = node_datas[node].lhs;
2388 const main_tokens = tree.nodes.items(.main_token);2388 const main_tokens = tree.nodes.items(.main_token);
...@@ -2391,8 +2391,8 @@ pub const SrcLoc = struct {...@@ -2391,8 +2391,8 @@ pub const SrcLoc = struct {
2391 return token_starts[tok_index];2391 return token_starts[tok_index];
2392 },2392 },
2393 .node_offset_bin_rhs => |node_off| {2393 .node_offset_bin_rhs => |node_off| {
2394 const node = src_loc.declRelativeToNodeIndex(node_off);
2395 const tree = try src_loc.file_scope.getTree(gpa);2394 const tree = try src_loc.file_scope.getTree(gpa);
2395 const node = src_loc.declRelativeToNodeIndex(node_off);
2396 const node_datas = tree.nodes.items(.data);2396 const node_datas = tree.nodes.items(.data);
2397 const src_node = node_datas[node].rhs;2397 const src_node = node_datas[node].rhs;
2398 const main_tokens = tree.nodes.items(.main_token);2398 const main_tokens = tree.nodes.items(.main_token);
...@@ -2402,8 +2402,8 @@ pub const SrcLoc = struct {...@@ -2402,8 +2402,8 @@ pub const SrcLoc = struct {
2402 },2402 },
24032403
2404 .node_offset_switch_operand => |node_off| {2404 .node_offset_switch_operand => |node_off| {
2405 const node = src_loc.declRelativeToNodeIndex(node_off);
2406 const tree = try src_loc.file_scope.getTree(gpa);2405 const tree = try src_loc.file_scope.getTree(gpa);
2406 const node = src_loc.declRelativeToNodeIndex(node_off);
2407 const node_datas = tree.nodes.items(.data);2407 const node_datas = tree.nodes.items(.data);
2408 const src_node = node_datas[node].lhs;2408 const src_node = node_datas[node].lhs;
2409 const main_tokens = tree.nodes.items(.main_token);2409 const main_tokens = tree.nodes.items(.main_token);
...@@ -2413,8 +2413,8 @@ pub const SrcLoc = struct {...@@ -2413,8 +2413,8 @@ pub const SrcLoc = struct {
2413 },2413 },
24142414
2415 .node_offset_switch_special_prong => |node_off| {2415 .node_offset_switch_special_prong => |node_off| {
2416 const switch_node = src_loc.declRelativeToNodeIndex(node_off);
2417 const tree = try src_loc.file_scope.getTree(gpa);2416 const tree = try src_loc.file_scope.getTree(gpa);
2417 const switch_node = src_loc.declRelativeToNodeIndex(node_off);
2418 const node_datas = tree.nodes.items(.data);2418 const node_datas = tree.nodes.items(.data);
2419 const node_tags = tree.nodes.items(.tag);2419 const node_tags = tree.nodes.items(.tag);
2420 const main_tokens = tree.nodes.items(.main_token);2420 const main_tokens = tree.nodes.items(.main_token);
...@@ -2439,8 +2439,8 @@ pub const SrcLoc = struct {...@@ -2439,8 +2439,8 @@ pub const SrcLoc = struct {
2439 },2439 },
24402440
2441 .node_offset_switch_range => |node_off| {2441 .node_offset_switch_range => |node_off| {
2442 const switch_node = src_loc.declRelativeToNodeIndex(node_off);
2443 const tree = try src_loc.file_scope.getTree(gpa);2442 const tree = try src_loc.file_scope.getTree(gpa);
2443 const switch_node = src_loc.declRelativeToNodeIndex(node_off);
2444 const node_datas = tree.nodes.items(.data);2444 const node_datas = tree.nodes.items(.data);
2445 const node_tags = tree.nodes.items(.tag);2445 const node_tags = tree.nodes.items(.tag);
2446 const main_tokens = tree.nodes.items(.main_token);2446 const main_tokens = tree.nodes.items(.main_token);