Skip to content

Commit

Permalink
AstGen: skip walking the AST when there are parse errors
Browse files Browse the repository at this point in the history
The AST -> ZIR lowering process assumes an AST that does not have any
parse errors.
  • Loading branch information
andrewrk committed Mar 15, 2023
1 parent 25c3878 commit 3b00e34
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ pub fn generate(gpa: Allocator, tree: Ast) Allocator.Error!Zir {
try astgen.extra.ensureTotalCapacity(gpa, tree.nodes.len + reserved_count);
astgen.extra.items.len += reserved_count;

try lowerAstErrors(&astgen);

var top_scope: Scope.Top = .{};

var gz_instructions: std.ArrayListUnmanaged(Zir.Inst.Index) = .{};
Expand All @@ -150,18 +148,24 @@ pub fn generate(gpa: Allocator, tree: Ast) Allocator.Error!Zir {
};
defer gz_instructions.deinit(gpa);

if (AstGen.structDeclInner(
&gen_scope,
&gen_scope.base,
0,
tree.containerDeclRoot(),
.Auto,
0,
)) |struct_decl_ref| {
assert(refToIndex(struct_decl_ref).? == 0);
} else |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.AnalysisFail => {}, // Handled via compile_errors below.
// The AST -> ZIR lowering process assumes an AST that does not have any
// parse errors.
if (tree.errors.len == 0) {
if (AstGen.structDeclInner(
&gen_scope,
&gen_scope.base,
0,
tree.containerDeclRoot(),
.Auto,
0,
)) |struct_decl_ref| {
assert(refToIndex(struct_decl_ref).? == 0);
} else |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
error.AnalysisFail => {}, // Handled via compile_errors below.
}
} else {
try lowerAstErrors(&astgen);
}

const err_index = @enumToInt(Zir.ExtraIndex.compile_errors);
Expand Down Expand Up @@ -12642,7 +12646,7 @@ fn emitDbgStmt(gz: *GenZir, line: u32, column: u32) !void {

fn lowerAstErrors(astgen: *AstGen) !void {
const tree = astgen.tree;
if (tree.errors.len == 0) return;
assert(tree.errors.len > 0);

const gpa = astgen.gpa;
const parse_err = tree.errors[0];
Expand Down

0 comments on commit 3b00e34

Please sign in to comment.