authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-09 18:08:54-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:14-07:00
log3b00e341fd8d1f12a3a14ee79a675a36549e716b
tree23f3ee1522e9e9b980810b3516b90595beee73a8
parent25c3878c00b92ffc884d89d32817ca9c244f7972

AstGen: skip walking the AST when there are parse errors

The AST -> ZIR lowering process assumes an AST that does not have any parse errors.

1 files changed, 19 insertions(+), 15 deletions(-)

src/AstGen.zig+19-15
......@@ -133,8 +133,6 @@ pub fn generate(gpa: Allocator, tree: Ast) Allocator.Error!Zir {
133133 try astgen.extra.ensureTotalCapacity(gpa, tree.nodes.len + reserved_count);
134134 astgen.extra.items.len += reserved_count;
135135
136 try lowerAstErrors(&astgen);
137
138136 var top_scope: Scope.Top = .{};
139137
140138 var gz_instructions: std.ArrayListUnmanaged(Zir.Inst.Index) = .{};
......@@ -150,18 +148,24 @@ pub fn generate(gpa: Allocator, tree: Ast) Allocator.Error!Zir {
150148 };
151149 defer gz_instructions.deinit(gpa);
152150
153 if (AstGen.structDeclInner(
154 &gen_scope,
155 &gen_scope.base,
156 0,
157 tree.containerDeclRoot(),
158 .Auto,
159 0,
160 )) |struct_decl_ref| {
161 assert(refToIndex(struct_decl_ref).? == 0);
162 } else |err| switch (err) {
163 error.OutOfMemory => return error.OutOfMemory,
164 error.AnalysisFail => {}, // Handled via compile_errors below.
151 // The AST -> ZIR lowering process assumes an AST that does not have any
152 // parse errors.
153 if (tree.errors.len == 0) {
154 if (AstGen.structDeclInner(
155 &gen_scope,
156 &gen_scope.base,
157 0,
158 tree.containerDeclRoot(),
159 .Auto,
160 0,
161 )) |struct_decl_ref| {
162 assert(refToIndex(struct_decl_ref).? == 0);
163 } else |err| switch (err) {
164 error.OutOfMemory => return error.OutOfMemory,
165 error.AnalysisFail => {}, // Handled via compile_errors below.
166 }
167 } else {
168 try lowerAstErrors(&astgen);
165169 }
166170
167171 const err_index = @enumToInt(Zir.ExtraIndex.compile_errors);
......@@ -12642,7 +12646,7 @@ fn emitDbgStmt(gz: *GenZir, line: u32, column: u32) !void {
1264212646
1264312647fn lowerAstErrors(astgen: *AstGen) !void {
1264412648 const tree = astgen.tree;
12645 if (tree.errors.len == 0) return;
12649 assert(tree.errors.len > 0);
1264612650
1264712651 const gpa = astgen.gpa;
1264812652 const parse_err = tree.errors[0];