Skip to content

fixes #25803: add genCppConstructorExpr for full type-prefixed expression#25817

Open
ringabout wants to merge 2 commits into
develfrom
pr_c++_2
Open

fixes #25803: add genCppConstructorExpr for full type-prefixed expression#25817
ringabout wants to merge 2 commits into
develfrom
pr_c++_2

Conversation

@ringabout
Copy link
Copy Markdown
Member

@ringabout ringabout commented May 15, 2026

fixes #25803

This pull request introduces a new approach for generating C++ constructor expressions in the Nim compiler's C++ backend, ensuring that type-prefixed construction is used when needed (such as in assignments), rather than just braced initializer lists. It also adds a new test case (with corresponding C++ header) to verify correct behavior when assigning to types with overloaded assignment operators and constructors.

C++ code generation improvements:

  • Added genCppConstructorExpr in ccgtypes.nim, which generates a full type-prefixed constructor expression (e.g., Foo(a, b)) for C++ code generation, as opposed to just a braced initializer list. This is important for contexts like assignments where the type must be explicit.
  • Updated resetLoc in cgen.nim to use genCppConstructorExpr instead of genCppInitializer when initializing imported C++ types, ensuring correct code generation for assignments.

Testing:

  • Added a new C++ header file, tcpp_default_ctor_assignment.h, defining a struct AmbiguousAssign with overloaded assignment operators and constructors to test ambiguous assignment scenarios.
  • Added a corresponding Nim test, tcpp_default_ctor_assignment.nim, which exercises construction and assignment for the imported C++ type, ensuring the new code generation logic works as intended.

Copilot AI review requested due to automatic review settings May 15, 2026 11:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to fix C++ code generation for default(T)/sequence initialization of importcpp types by avoiding untyped {} assignment expressions that can trigger ambiguous C++ assignment overload resolution.

Changes:

  • Adds genCppConstructorExpr for type-prefixed C++ construction expressions.
  • Updates resetLoc to use the new constructor expression for imported C++ types.
  • Adds a C++ regression test with an imported type that has ambiguous assignment overloads.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
compiler/cgen.nim Uses the new constructor-expression helper when resetting imported C++ types.
compiler/ccgtypes.nim Adds genCppConstructorExpr alongside existing C++ initializer generation.
tests/cpp/tcpp_default_ctor_assignment.nim Adds a regression test compiling newSeq for an imported C++ type.
tests/cpp/tcpp_default_ctor_assignment.h Defines the C++ type with ambiguous assignment overloads used by the test.
Comments suppressed due to low confidence (2)

compiler/ccgtypes.nim:715

  • Using a parenthesized constructor expression here changes C++ overload resolution from the braced initialization that genCppInitializer uses elsewhere. A typed braced construction expression would still disambiguate the RHS of assignments, while preserving initializer-list/default-list initialization semantics for imported C++ types that rely on std::initializer_list overloads or are only list-initializable.
        let k = lastSon(n[i])

compiler/ccgtypes.nim:714

  • The assertion message contains a typo: “doesnt” should be “doesn't”.
      of nkOfBranch, nkElse:

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[C++] default(T) initializer list may lead to ambiguous assignment and prevent compilation

2 participants