From 3daf154d0db7b6d7dc7ab9c1bf43ab831ffe9e17 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Fri, 15 May 2026 19:41:16 +0800 Subject: [PATCH] fixes #25803: add genCppConstructorExpr for full type-prefixed expression --- compiler/ccgtypes.nim | 18 +++++++++++++ compiler/cgen.nim | 2 +- tests/cpp/tcpp_default_ctor_assignment.h | 30 ++++++++++++++++++++++ tests/cpp/tcpp_default_ctor_assignment.nim | 14 ++++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 tests/cpp/tcpp_default_ctor_assignment.h create mode 100644 tests/cpp/tcpp_default_ctor_assignment.nim diff --git a/compiler/ccgtypes.nim b/compiler/ccgtypes.nim index 8e6b44c81d..d382e8faa1 100644 --- a/compiler/ccgtypes.nim +++ b/compiler/ccgtypes.nim @@ -696,6 +696,24 @@ proc genCppInitializer(m: BModule, prc: BProc; typ: PType; didGenTemp: var bool) if prc == nil: assert p.blocks.len == 0, "BProc belongs to a struct doesnt have blocks" +# Unlike genCppInitializer which returns just the braced value list (e.g. "{a, b}"), +# genCppConstructorExpr returns a full type-prefixed expression (e.g. "Foo(a, b)"). +# This is used when a standalone construction expression is needed — e.g. on the +# right-hand side of an assignment — whereas genCppInitializer is used in variable +# declarations where the type is already written separately before the initializer. +proc genCppConstructorExpr(m: BModule, prc: BProc; typ: PType; didGenTemp: var bool): Snippet = + var params = "" + if typ.itemId in m.g.graph.initializersPerType: + let call = m.g.graph.initializersPerType[typ.itemId] + if call != nil: + var p = prc + if p == nil: + p = BProc(module: m) + params = genCppParamsForCtor(p, call, didGenTemp) + if prc == nil: + assert p.blocks.len == 0, "BProc belongs to a struct doesnt have blocks" + result = getTypeDesc(m, typ, dkVar) & "(" & params & ")" + proc genRecordFieldsAux(m: BModule; n: PNode, rectype: PType, check: var IntSet; result: var Builder; unionPrefix = "") = diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 8d3b486ca3..cd673141fa 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -526,7 +526,7 @@ proc resetLoc(p: BProc, loc: var TLoc) = if isImportedCppType(typ): var didGenTemp = false let rl = rdLoc(loc) - let init = genCppInitializer(p.module, p, typ, didGenTemp) + let init = genCppConstructorExpr(p.module, p, typ, didGenTemp) p.s(cpsStmts).addAssignment(rl, init) return if optSeqDestructors in p.config.globalOptions and typ.kind in {tyString, tySequence}: diff --git a/tests/cpp/tcpp_default_ctor_assignment.h b/tests/cpp/tcpp_default_ctor_assignment.h new file mode 100644 index 0000000000..356cbb9381 --- /dev/null +++ b/tests/cpp/tcpp_default_ctor_assignment.h @@ -0,0 +1,30 @@ +#ifndef TCPP_DEFAULT_CTOR_ASSIGNMENT_H +#define TCPP_DEFAULT_CTOR_ASSIGNMENT_H + +struct AmbiguousAssign { + int x; + const char* y; + + AmbiguousAssign(): x(0), y(nullptr) {} + AmbiguousAssign(int x, const char* y): x(x), y(y) {} + + AmbiguousAssign& operator=(int v) { + x = v; + y = nullptr; + return *this; + } + + AmbiguousAssign& operator=(const char* s) { + x = 0; + y = s; + return *this; + } + + AmbiguousAssign& operator=(const AmbiguousAssign& other) { + x = other.x; + y = other.y; + return *this; + } +}; + +#endif \ No newline at end of file diff --git a/tests/cpp/tcpp_default_ctor_assignment.nim b/tests/cpp/tcpp_default_ctor_assignment.nim new file mode 100644 index 0000000000..88d9671ae6 --- /dev/null +++ b/tests/cpp/tcpp_default_ctor_assignment.nim @@ -0,0 +1,14 @@ +discard """ + cmd: "nim cpp $file" +""" + +type + AmbiguousAssign {.importcpp, header: "tcpp_default_ctor_assignment.h".} = object + x: cint + y: cstring + +proc main = + var xs = newSeq[AmbiguousAssign](3) + doAssert xs.len == 3 + +main() \ No newline at end of file