From 3160aa672bf2a149724f5f8d0e29cc2587a94179 Mon Sep 17 00:00:00 2001 From: StunxFS Date: Thu, 26 Dec 2024 20:38:35 -0400 Subject: [PATCH] sema: early return inside file stmts with errors --- compiler/ast/Stmt.v | 3 ++- compiler/context/mod.v | 7 ++++++- compiler/sema/mod.v | 19 +++++++++++++++---- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/compiler/ast/Stmt.v b/compiler/ast/Stmt.v index 5ffaea88..948e778a 100644 --- a/compiler/ast/Stmt.v +++ b/compiler/ast/Stmt.v @@ -31,7 +31,8 @@ pub: name_pos FilePos args []FnArg return_type Type - stmts []Stmt +pub mut: + stmts []Stmt } pub struct FnArg { diff --git a/compiler/context/mod.v b/compiler/context/mod.v index 48388492..55491f9d 100644 --- a/compiler/context/mod.v +++ b/compiler/context/mod.v @@ -185,8 +185,13 @@ pub fn (mut ctx CContext) load_primitive_types() { } } +@[inline] +pub fn (ctx &CContext) code_has_errors() bool { + return ctx.report.errors > 0 +} + pub fn (ctx &CContext) abort_if_errors() { - if ctx.report.errors > 0 { + if ctx.code_has_errors() { reason := if ctx.report.errors == 1 { 'aborting due to previous error' } else { diff --git a/compiler/sema/mod.v b/compiler/sema/mod.v index 9db4a06e..526934c6 100644 --- a/compiler/sema/mod.v +++ b/compiler/sema/mod.v @@ -42,15 +42,24 @@ fn (mut sema Sema) check_file(mut file ast.File) { sema.file.scope = ast.Scope.new(sema.ctx.universe, sema.sym) sema.scope = sema.file.scope - sema.first_pass = true - sema.stmts(mut sema.file.stmts) + sema.file_stmts(true) + if sema.ctx.code_has_errors() { + return + } - sema.first_pass = false - sema.stmts(mut sema.file.stmts) + sema.file_stmts(false) + if sema.ctx.code_has_errors() { + return + } sema.ctx.files << sema.file } +fn (mut sema Sema) file_stmts(first_pass bool) { + sema.first_pass = first_pass + sema.stmts(mut sema.file.stmts) +} + fn (mut sema Sema) stmts(mut stmts []ast.Stmt) { for mut stmt in stmts { sema.stmt(mut stmt) @@ -73,5 +82,7 @@ fn (mut sema Sema) fn_stmt(mut stmt ast.FnStmt) { args: stmt.args node: unsafe { stmt } }) or { context.error(err.msg(), stmt.name_pos) } + sema.stmts(mut stmt.stmts) + return } }