aboutsummaryrefslogtreecommitdiff
path: root/typecheck.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-03-09 17:52:48 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-03-09 17:52:48 -0400
commit94ed28b4d19692cccc9da7640931ed45d31e47c6 (patch)
tree1c57de73ec25e256d63250551c27cafdba1f63d7 /typecheck.c
parent6abd4e80244a7fa73a7213101b1d61e4df1b6689 (diff)
Add better typechecking for Abort (and add `Abort` as a user-reachable
type) and check for unreachable code
Diffstat (limited to 'typecheck.c')
-rw-r--r--typecheck.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/typecheck.c b/typecheck.c
index 1ab20177..0530ff7b 100644
--- a/typecheck.c
+++ b/typecheck.c
@@ -921,7 +921,17 @@ 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);
}