From 3818460a473bd3ffcd35f09eb6429e1b0f00bc07 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Sun, 26 Jul 2026 19:15:54 +0000 Subject: fix: top-level RETURN and READFILE designator targets error cleanly MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - RETURN outside any FUNCTION/PROCEDURE used to emit an i64/void ret inside i32 main — a verifier dump instead of a diagnostic. It is now a clean compile error (no signature means top level: pseudocode cannot name a function 'main', it is a reserved runtime symbol). - READFILE "f.txt", r.f mis-parsed into an error cascade the same way INPUT r.f did before its fix; the target now consumes the designator and reports one clear 'must be a plain STRING variable' error. Co-Authored-By: Claude Fable 5 --- lib/CodeGen/CodeGen.cc | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'lib/CodeGen/CodeGen.cc') diff --git a/lib/CodeGen/CodeGen.cc b/lib/CodeGen/CodeGen.cc index 0e372d0..fbf807b 100644 --- a/lib/CodeGen/CodeGen.cc +++ b/lib/CodeGen/CodeGen.cc @@ -1017,21 +1017,23 @@ void CodeGen::emitStmt(StmtAST *Stmt) { if (auto *Ret = dynamic_cast(Stmt)) { Function *F = Builder->GetInsertBlock()->getParent(); const FuncSig *Sig = FuncGen->getSignature(F->getName().str()); - if (Sig && Sig->ReturnTypeName == "VOID" && Ret->getRetVal()) { + // Pseudocode cannot name a function "main" (reserved runtime symbol), + // so no signature means we are emitting the top level. + if (!Sig) { + reportError("RETURN is only allowed inside a FUNCTION or PROCEDURE"); + return; + } + if (Sig->ReturnTypeName == "VOID" && Ret->getRetVal()) { reportError("RETURN with a value is only allowed in a FUNCTION"); return; } - if (Sig && Sig->ReturnTypeName != "VOID" && !Ret->getRetVal()) { + if (Sig->ReturnTypeName != "VOID" && !Ret->getRetVal()) { reportError("RETURN in a FUNCTION requires a value"); return; } Value *RetVal = nullptr; if (Ret->getRetVal()) { - if (Sig) { - RetVal = emitCoercedExpr(Ret->getRetVal(), resolveType(Sig->ReturnTypeName)); - } else { - RetVal = emitExpr(Ret->getRetVal()); - } + RetVal = emitCoercedExpr(Ret->getRetVal(), resolveType(Sig->ReturnTypeName)); if (!RetVal) return; } FuncGen->emitReturn(Ret, RetVal); -- cgit v1.3.1-4-g156e