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

C++ Atomics in union: ::<unnamed union>::<unnamed struct>::field with constructor not allowed in anonymous aggregate #13062

Closed
mratsim opened this issue Jan 7, 2020 · 2 comments

Comments

@mratsim
Copy link
Collaborator

mratsim commented Jan 7, 2020

The following compile with the C backend but not with C++

import atomics

type
  Pledge* {.exportc.} = object
    p: PledgePtr

  PledgeKind {.exportc.} = enum
    Single
    Iteration

  PledgePtr {.exportc.} = ptr object
    case kind: PledgeKind
    of Single:
      impl: PledgeImpl
    of Iteration:
      discard

  PledgeImpl {.exportc.} = object
    fulfilled: Atomic[bool]

var x: Pledge
echo x.repr

C++ code

  struct Pledge;
struct tyObject_PledgePtrcolonObjectType___yxB804f8EWpdMmtSWWPeFA;
...
struct PledgeImpl;
struct Pledge {
tyObject_PledgePtrcolonObjectType___yxB804f8EWpdMmtSWWPeFA* p;
};
...
typedef NU8 PledgeKind;
typedef std::atomic<NIM_BOOL>  TY__Z2iuRDrwY0WX5bGI2o2sLQ;
struct PledgeImpl {
TY__Z2iuRDrwY0WX5bGI2o2sLQ fulfilled;
};
struct tyObject_PledgePtrcolonObjectType___yxB804f8EWpdMmtSWWPeFA {
PledgeKind kind;
union{
struct {PledgeImpl impl;
};
};
};

Error:

error: member ‘PledgeImpl tyObject_PledgePtrcolonObjectType___yxB804f8EWpdMmtSWWPeFA::<unnamed union>::<unnamed struct>::impl’ with constructor not allowed in anonymous aggregate
   75 | struct {PledgeImpl impl;
      |                    ^~~~
@Araq
Copy link
Member

Araq commented Jan 8, 2020

Workaround, use

type
  PledgePtr {.exportc.} = ptr object
    kind: PledgeKind
    impl: PledgeImpl

@ringabout
Copy link
Member

ringabout commented Nov 13, 2020

It works in Nim 1.2.6 and above(with c and cpp backend)

D:\QQPCmgr\Desktop\Nim>choosenim 1.2.6
      Info: Version 1.2.6 already selected

D:\QQPCmgr\Desktop\Nim>nim c -r tests/ccgbugs/t13062.nim
[p = nil]

D:\QQPCmgr\Desktop\Nim>nim cpp -r tests/ccgbugs/t13062.nim
[p = nil]

@Araq Araq closed this as completed in d802a4a Nov 13, 2020
narimiran pushed a commit that referenced this issue Nov 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants