Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #13093 C++ Atomics: operator= is implicitly deleted because the d… #21712

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions compiler/cgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,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:
Expand Down
6 changes: 5 additions & 1 deletion compiler/semstmts.nim
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,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:
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/concurrency/atomics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 24 additions & 0 deletions tests/cpp/t13093.nim
Original file line number Diff line number Diff line change
@@ -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()