From 94ed28b4d19692cccc9da7640931ed45d31e47c6 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 9 Mar 2025 17:52:48 -0400 Subject: Add better typechecking for Abort (and add `Abort` as a user-reachable type) and check for unreachable code --- typecheck.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'typecheck.c') diff --git a/typecheck.c b/typecheck.c index 1ab20177..0530ff7b 100644 --- a/typecheck.c +++ b/typecheck.c @@ -920,8 +920,18 @@ type_t *get_type(env_t *env, ast_t *ast) } env_t *block_env = fresh_scope(env); + for (ast_list_t *stmt = block->statements; stmt; stmt = stmt->next) { + prebind_statement(block_env, stmt->ast); + } for (ast_list_t *stmt = block->statements; stmt; stmt = stmt->next) { bind_statement(block_env, stmt->ast); + if (stmt->next) { // Check for unreachable code: + if (stmt->ast->tag == Return) + code_err(stmt->ast, "This statement will always return, so the rest of the code in this block is unreachable!"); + type_t *statement_type = get_type(block_env, stmt->ast); + if (statement_type && statement_type->tag == AbortType && stmt->next) + code_err(stmt->ast, "This statement will always abort, so the rest of the code in this block is unreachable!"); + } } return get_type(block_env, last->ast); } -- cgit v1.2.3