diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-11-12 00:04:56 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-11-12 00:04:56 -0500 |
| commit | a4d11f51ff409901e92a35983ace8b9b2a7ec2d3 (patch) | |
| tree | 0b2bc4faab88e49b2d9a0f6350280700e359084e /typecheck.c | |
| parent | 5b8f7179ad28e615d1cc40753125f4f296dd5b59 (diff) | |
Fixes for 'if' blocks with nested 'else if' that declare variables
Diffstat (limited to 'typecheck.c')
| -rw-r--r-- | typecheck.c | 13 |
1 files changed, 7 insertions, 6 deletions
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) { |
