From fe70048b33baebdfbf7071059bd4742ec16ab408 Mon Sep 17 00:00:00 2001 From: _ov4 Date: Thu, 22 Oct 2026 15:17:31 +0000 Subject: fix(classes): defer NEW for DECLARE without a parameterless constructor A plain DECLARE, field or array element now calls NEW only when the class has a parameterless one; otherwise the zeroed object waits for x <- NEW C(...), as in guide section 10.2. DECLARE with parentheses still requires a matching constructor. Co-Authored-By: Claude Sonnet 5.5 --- lib/CodeGen/ConstructorGen.cc | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'lib/CodeGen/ConstructorGen.cc') diff --git a/lib/CodeGen/ConstructorGen.cc b/lib/CodeGen/ConstructorGen.cc index 8ca0258..286435e 100644 --- a/lib/CodeGen/ConstructorGen.cc +++ b/lib/CodeGen/ConstructorGen.cc @@ -114,8 +114,16 @@ bool CodeGen::emitConstruct(const std::string &TypeName, Value *Addr, return true; } +// Guide 10.2 creation pattern: a plain DECLARE only runs NEW when the class has +// a parameterless one; otherwise the object stays zeroed (fields constructed) +// and a later `x <- NEW C(...)` binds it. bool CodeGen::emitDefaultConstruct(const std::string &TypeName, Value *Addr, int Line) { - return emitConstruct(TypeName, Addr, {}, Line); + const TypeInfo *Info = resolveType(TypeName); + if (!Info) return false; + for (const FuncSig *Sig : FuncGen->getOverloads(TypeName + ".NEW")) + if (Sig->Params.empty()) return emitConstruct(TypeName, Addr, {}, Line); + SaveAndRestore Location(DiagnosticLine, Line); + return !Info->asRecord() || emitFieldDefaults(Info, Addr, Line); } // Default-constructs, in declaration order, each field of the record at Addr -- cgit v1.3.1-4-g156e