Skip to content

Commit

Permalink
sizeComplete pragma to workaround C++ Atomic [backport]
Browse files Browse the repository at this point in the history
  • Loading branch information
metagn committed Sep 30, 2024
1 parent b0e6d28 commit ec96591
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ type
tfCompleteStruct
# (for importc types); type is fully specified, allowing to compute
# sizeof, alignof, offsetof at CT
tfSizeComplete
tfExplicitCallConv
tfIsConstructor
tfEffectSystemWorkaround
Expand Down
1 change: 1 addition & 0 deletions compiler/condsyms.nim
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,4 @@ proc initDefines*(symbols: StringTableRef) =
defineSymbol("nimHasGenericsOpenSym2")
defineSymbol("nimHasGenericsOpenSym3")
defineSymbol("nimHasJsNoLambdaLifting")
defineSymbol("nimHasSizeComplete")
6 changes: 5 additions & 1 deletion compiler/pragmas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const
wGcSafe, wCodegenDecl, wNoInit, wCompileTime}
typePragmas* = declPragmas + {wMagic, wAcyclic,
wPure, wHeader, wCompilerProc, wCore, wFinal, wSize, wShallow,
wIncompleteStruct, wCompleteStruct, wByCopy, wByRef,
wIncompleteStruct, wCompleteStruct, wSizeComplete, wByCopy, wByRef,
wInheritable, wGensym, wInject, wRequiresInit, wUnchecked, wUnion, wPacked,
wCppNonPod, wBorrow, wGcSafe, wPartial, wExplain, wPackage, wCodegenDecl,
wSendable, wNoInit}
Expand Down Expand Up @@ -1216,6 +1216,10 @@ proc singlePragma(c: PContext, sym: PSym, n: PNode, i: var int,
noVal(c, it)
if sym.typ == nil: invalidPragma(c, it)
else: incl(sym.typ.flags, tfCompleteStruct)
of wSizeComplete:
noVal(c, it)
if sym.typ == nil: invalidPragma(c, it)
else: incl(sym.typ.flags, tfSizeComplete)
of wUnchecked:
noVal(c, it)
if sym.typ == nil or sym.typ.kind notin {tyArray, tyUncheckedArray}:
Expand Down
2 changes: 1 addition & 1 deletion compiler/sizealignoffsetimpl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ proc computeSizeAlign(conf: ConfigRef; typ: PType) =
let paddingAtEnd = int16(accum.finish())
if typ.sym != nil and
typ.sym.flags * {sfCompilerProc, sfImportc} == {sfImportc} and
tfCompleteStruct notin typ.flags:
typ.flags * {tfCompleteStruct, tfSizeComplete} == {}:
typ.size = szUnknownSize
typ.align = szUnknownSize
typ.paddingAtEnd = szUnknownSize
Expand Down
3 changes: 2 additions & 1 deletion compiler/wordrecg.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ type
wImportc = "importc", wImportJs = "importjs", wExportc = "exportc", wExportCpp = "exportcpp",
wExportNims = "exportnims",
wIncompleteStruct = "incompleteStruct", # deprecated
wCompleteStruct = "completeStruct", wRequiresInit = "requiresInit", wAlign = "align",
wCompleteStruct = "completeStruct", wSizeComplete = "sizeComplete",
wRequiresInit = "requiresInit", wAlign = "align",
wNodecl = "nodecl", wPure = "pure", wSideEffect = "sideEffect", wHeader = "header",
wNoSideEffect = "noSideEffect", wGcSafe = "gcsafe", wNoreturn = "noreturn",
wNosinks = "nosinks", wLib = "lib", wDynlib = "dynlib",
Expand Down
5 changes: 4 additions & 1 deletion lib/pure/concurrency/atomics.nim
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ when defined(cpp) or defined(nimdoc):
## Also guarantees that all threads observe the same total ordering
## with other moSequentiallyConsistent operations.

when not defined(nimHasSizeComplete):
{.pragma: sizeComplete, completeStruct.}

type
Atomic*[T] {.importcpp: "std::atomic", completeStruct.} = object
Atomic*[T] {.importcpp: "std::atomic", sizeComplete.} = object
## An atomic object with underlying type `T`.
raw: T

Expand Down
14 changes: 14 additions & 0 deletions tests/ccgbugs/tatomictypeinfo.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
discard """
matrix: "--mm:refc; --mm:orc"
targets: "c cpp"
"""

# issue #24159

import std/atomics

type N = object
u: ptr Atomic[int]
proc w(args: N) = discard
var e: Thread[N]
createThread(e, w, default(N))
1 change: 1 addition & 0 deletions tests/ccgbugs/tcgbug.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ success
M1 M2
ok
'''
targets: "c cpp"
matrix: "--mm:refc;--mm:orc"
"""

Expand Down

0 comments on commit ec96591

Please sign in to comment.