From 29e7e7609121ebe1349cbbc2dad716c91dce5d83 Mon Sep 17 00:00:00 2001 From: 0z5a Date: Thu, 19 Nov 2026 08:16:23 +0100 Subject: [PATCH] Report non-identifiers on the left of := b[0], flag := foo() gave "Unknown identifier flag", because it was parsed as a designator list. Fixes #564. --- src/umka_expr.c | 4 ++++ src/umka_stmt.c | 5 ++++- src/umka_stmt.h | 1 + tests/negative.um | 13 +++++++++++++ 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/umka_expr.c b/src/umka_expr.c index c5e59a91..2518d277 100644 --- a/src/umka_expr.c +++ b/src/umka_expr.c @@ -2576,6 +2576,10 @@ void parseDesignatorList(Umka *umka, const Type **type, Const *constant, bool *i lexNext(&umka->lex); + // Not a designator list, but a short variable declaration with a non-identifier on the left, e.g., a[0], b := f() + if (doShortVarDeclLookahead(umka)) + umka->error.handler(umka->error.context, "Identifier expected on the left side of :="); + bool fieldIsVar, fieldIsCall, fieldIsCompLit; parseDesignator(umka, &fieldType, NULL, &fieldIsVar, &fieldIsCall, &fieldIsCompLit, NULL); diff --git a/src/umka_stmt.c b/src/umka_stmt.c index 99a5b646..3c7647ce 100644 --- a/src/umka_stmt.c +++ b/src/umka_stmt.c @@ -93,7 +93,7 @@ void doResolveExtern(Umka *umka) } -static bool doShortVarDeclLookahead(Umka *umka) +bool doShortVarDeclLookahead(Umka *umka) { // ident {"," ident} ":=" Lexer lookaheadLex = umka->lex; @@ -410,6 +410,9 @@ static void parseSimpleStmt(Umka *umka) TokenKind op = umka->lex.tok.kind; + if (op == TOK_COLONEQ) + umka->error.handler(umka->error.context, "Identifier expected on the left side of :="); + if (typeExprListStruct(type) && !isCall && op != TOK_EQ) umka->error.handler(umka->error.context, "List assignment expected"); diff --git a/src/umka_stmt.h b/src/umka_stmt.h index ccb64050..342b98cc 100644 --- a/src/umka_stmt.h +++ b/src/umka_stmt.h @@ -6,6 +6,7 @@ void doZeroVar(Umka *umka, const Ident *ident); void doResolveExtern(Umka *umka); +bool doShortVarDeclLookahead(Umka *umka); void parseAssignmentStmt(Umka *umka, const Type *type, Const *varPtrConstList); void parseDeclAssignmentStmt(Umka *umka, IdentName *names, const bool *exported, int num, bool constExpr); diff --git a/tests/negative.um b/tests/negative.um index 0a009ac3..29127b22 100644 --- a/tests/negative.um +++ b/tests/negative.um @@ -63,6 +63,19 @@ fn main() { s[0] = 'x' }`}, + {.compileError, "Identifier expected on the left side of :=", ` +fn f(): (int, bool) {return 42, true} +fn main() { + a := [2]int{5, 7} + a[0], ok := f() +}`}, + + {.compileError, "Identifier expected on the left side of :=", ` +fn main() { + a := [2]int{5, 7} + a[0] := 1 +}`}, + {.compileError, "Constant must be ordinal, or real, or string, or closure", ` const c = []int{1} fn main() {}`},