From a4d11f51ff409901e92a35983ace8b9b2a7ec2d3 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 12 Nov 2024 00:04:56 -0500 Subject: Fixes for 'if' blocks with nested 'else if' that declare variables --- typecheck.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'typecheck.c') diff --git a/typecheck.c b/typecheck.c index f844d30d..915e2b5c 100644 --- a/typecheck.c +++ b/typecheck.c @@ -1088,15 +1088,16 @@ type_t *get_type(env_t *env, ast_t *ast) env_t *falsey_scope = env; if (if_->condition->tag == Declare) { type_t *condition_type = get_type(env, Match(if_->condition, Declare)->value); - falsey_scope = fresh_scope(env); - bind_statement(falsey_scope, if_->condition); - const char *varname = Match(Match(if_->condition, Declare)->var, Var)->name; - if (!streq(varname, "_")) { - truthy_scope = fresh_scope(env); + if (streq(varname, "_")) + code_err(if_->condition, "To use `if var := ...:`, you must choose a real variable name, not `_`"); + + truthy_scope = fresh_scope(env); + if (condition_type->tag == OptionalType) set_binding(truthy_scope, varname, new(binding_t, .type=Match(condition_type, OptionalType)->type)); - } + else + set_binding(truthy_scope, varname, new(binding_t, .type=condition_type)); } else if (if_->condition->tag == Var) { type_t *condition_type = get_type(env, if_->condition); if (condition_type->tag == OptionalType) { -- cgit v1.2.3