From fb7561a7f20872b63f2bef25fb72fddf8c0442ae Mon Sep 17 00:00:00 2001 From: Bung Date: Sun, 23 Apr 2023 16:28:43 +0800 Subject: [PATCH] fix #13093 C++ Atomics: operator= is implicitly deleted because the default definition would be ill-formed --- compiler/cgen.nim | 4 ++++ compiler/semstmts.nim | 6 +++++- lib/pure/concurrency/atomics.nim | 2 +- tests/cpp/t13093.nim | 24 ++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/cpp/t13093.nim diff --git a/compiler/cgen.nim b/compiler/cgen.nim index f6457f1e015c..96decbaf1514 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -491,6 +491,10 @@ proc constructLoc(p: BProc, loc: var TLoc, isTemp = false) = nilLoc.r = rope("NIM_NIL") genRefAssign(p, loc, nilLoc) else: + if typ.kind == tyVar: + let tt = typ.skipTypes({tyVar}) + if isImportedType(tt) and tfRequiresInit in tt.flags: + return linefmt(p, cpsStmts, "$1 = ($2)0;$n", [rdLoc(loc), getTypeDesc(p.module, typ, mapTypeChooser(loc))]) else: diff --git a/compiler/semstmts.nim b/compiler/semstmts.nim index 96883255c4fd..60170176e5a5 100644 --- a/compiler/semstmts.nim +++ b/compiler/semstmts.nim @@ -717,7 +717,11 @@ proc semVarOrLet(c: PContext, n: PNode, symkind: TSymKind): PNode = tyUserTypeClassInst}) if actualType.kind in {tyObject, tyDistinct} and actualType.requiresInit: - defaultConstructionError(c, v.typ, v.info) + # imported type use requiresInit pragma prevent implicit initialization + if (tfRequiresInit in actualType.flags and sfImportc in actualType.sym.flags): + discard + else: + defaultConstructionError(c, v.typ, v.info) else: checkNilable(c, v) # allow let to not be initialised if imported from C: diff --git a/lib/pure/concurrency/atomics.nim b/lib/pure/concurrency/atomics.nim index c7b881bc55c1..49204bd4d530 100644 --- a/lib/pure/concurrency/atomics.nim +++ b/lib/pure/concurrency/atomics.nim @@ -89,7 +89,7 @@ when defined(cpp) or defined(nimdoc): ## with other moSequentiallyConsistent operations. type - Atomic*[T] {.importcpp: "std::atomic", completeStruct.} = object + Atomic*[T] {.importcpp: "std::atomic", completeStruct, requiresInit.} = object ## An atomic object with underlying type `T`. raw: T diff --git a/tests/cpp/t13093.nim b/tests/cpp/t13093.nim new file mode 100644 index 000000000000..17c730d16a73 --- /dev/null +++ b/tests/cpp/t13093.nim @@ -0,0 +1,24 @@ +discard """ + targets: "cpp" + action: reject + errormsg: "The PledgeObj type requires the following fields to be initialized: refCount" +""" + +import atomics + +type + Pledge* = object + p: PledgePtr + + PledgePtr = ptr PledgeObj + PledgeObj = object + refCount: Atomic[int32] + +proc main() = + var pledge: Pledge + pledge.p = createShared(PledgeObj) + let tmp = PledgeObj() # <---- not allowed: atomics are not copyable + + pledge.p[] = tmp + +main()