Skip to content

Commit 466f3b4

Browse files
Fix minor internal error.
1 parent 1d3b5b5 commit 466f3b4

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/env.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,19 +228,25 @@ bool env_set_alias_direct(Env* env, const char* name,
228228
/* Disallow aliasing to frozen / permafrozen target */
229229
if (cur->frozen || cur->permafrozen) return false;
230230

231-
/* Find or create local entry */
231+
/* Type compatibility */
232+
if (type != TYPE_UNKNOWN && type != cur->decl_type) return false;
233+
234+
/* Find existing local entry (but don't create it yet). Only create
235+
the local entry after all validation succeeds to avoid leaving a
236+
declared-but-uninitialized binding when alias setup fails. */
232237
EnvEntry* entry = env_find_local(env, name);
233238
if (!entry) {
234239
if (!declare_if_missing) return false;
240+
/* create now */
235241
if (!env_define_direct(env, name, type)) return false;
236242
entry = env_find_local(env, name);
243+
if (!entry) return false;
237244
}
238245

239246
/* Respect frozen state on the entry itself */
240247
if (entry->frozen || entry->permafrozen) return false;
241248

242-
/* Type compatibility */
243-
if (type != TYPE_UNKNOWN && type != cur->decl_type) return false;
249+
/* Overwrite declared type with target's type */
244250
entry->decl_type = cur->decl_type;
245251

246252
/* Clear any stored value and set alias */

src/interpreter.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,6 @@ static ExecResult exec_stmt(Interpreter* interp, Stmt* stmt, Env* env, LabelMap*
16251625
if (!tgt) return make_error("Invalid pointer literal", stmt->line, stmt->column);
16261626
if (stmt->as.assign.has_type) {
16271627
DeclType expected = stmt->as.assign.decl_type;
1628-
env_define(env, stmt->as.assign.name, expected);
16291628
if (!env_set_alias(env, stmt->as.assign.name, tgt, expected, true)) {
16301629
char buf[256];
16311630
snprintf(buf, sizeof(buf), "Cannot create alias '%s' -> '%s'", stmt->as.assign.name, tgt);

0 commit comments

Comments
 (0)