D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15389 - extern(C++) forward referencing problem
Summary: extern(C++) forward referencing problem
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 blocker
Assignee: No Owner
URL:
Keywords: C++, industry
Depends on:
Blocks:
 
Reported: 2015-11-29 10:13 UTC by Manu
Modified: 2017-08-02 08:07 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Manu 2015-11-29 10:13:55 UTC
This doesn't work, but it should:
  alias X = NS.X;
  extern (C++, NS) struct X {}

This works fine:
  extern (C++, NS) struct X {}
  alias X = NS.X;
Comment 1 Manu 2016-01-03 07:10:49 UTC
This may be related to apparent circular referencing issues:

module x;
import y;
extern(C++, ns) class X { Y v; }


module y;
import x;
extern(C++, ns) class Y { X v; }

y.d(3): Error: undefined identifier 'X'


extern(C++, ns) class Y { x.X v; }

y.d(3): Error: undefined identifier 'X' in module 'x'


extern(C++, ns) class Y { x.ns.X v; }

y.d(3): Error: identifier 'X' of 'x.ns.X' is not defined
y.d(3): Error: x.ns.X is used as a type


import x : XX = X;
extern(C++, ns) class Y { XX v; }

y.d(2): Error: module x import 'X' not found


import x : NS = ns;
extern(C++, ns) class Y { NS.X v; }

y.d(3): Error: identifier 'X' of 'NS.X' is not defined
y.d(3): Error: NS.X is used as a type


import x : XX = ns.X;
extern(C++, ns) class Y { XX v; }

y.d(2): Error: ';' expected
y.d(2): Error: no identifier for declarator X


I dunno!
Comment 2 Walter Bright 2016-01-03 08:14:53 UTC
(In reply to Manu from comment #1)
> This may be related to apparent circular referencing issues:
> 
> module x;
> import y;
> extern(C++, ns) class X { Y v; }
> 
> 
> module y;
> import x;
> extern(C++, ns) class Y { X v; }
> 
> y.d(3): Error: undefined identifier 'X'

A misunderstanding of how namespace lookup works in D. Namespaces in D:

1. follow D lookup rules
2. follow C++ mangling rules

The example of:
    Y y;
is clearly assuming that C++ namespace lookup rules are being followed. They are not, D lookup rules are. Y is declared in x.y.ns, not x.x.ns. As far as the compiler is concerned, x.y.ns and x.x.ns are different namespaces (although they will mangle the same). Thus, "Y" should be "x.y.ns.Y".

You can see this if you replace "extern(C++, ns)" with "struct ns". You'll get the SAME error message! 

Not a D bug.


> extern(C++, ns) class Y { x.X v; }
> 
> y.d(3): Error: undefined identifier 'X' in module 'x'

Same issue. Should be "x.ns.X", as X is in the scope of x.ns. Not a D bug.

> extern(C++, ns) class Y { x.ns.X v; }
> 
> y.d(3): Error: identifier 'X' of 'x.ns.X' is not defined
> y.d(3): Error: x.ns.X is used as a type

Filed as https://issues.dlang.org/show_bug.cgi?id=15503


> import x : XX = X;
> extern(C++, ns) class Y { XX v; }
> 
> y.d(2): Error: module x import 'X' not found

Because it's ns.X, not X. Not a D bug.

> import x : NS = ns;
> extern(C++, ns) class Y { NS.X v; }
> 
> y.d(3): Error: identifier 'X' of 'NS.X' is not defined
> y.d(3): Error: NS.X is used as a type

Probably the same issue as https://issues.dlang.org/show_bug.cgi?id=15503

> import x : XX = ns.X;
> extern(C++, ns) class Y { XX v; }
> 
> y.d(2): Error: ';' expected
> y.d(2): Error: no identifier for declarator X

Supporting the . in the import would be an enhancement request.
Comment 4 Manu 2016-01-04 11:04:37 UTC
Thanks Walter!

Does this also address the case in the initial bug report?
Comment 5 github-bugzilla 2016-01-06 10:57:18 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/11d309ff1c766715e06671bbb586809a9b0d5af8
fix Issue 15389 - extern(C++) forward referencing problem

https://github.com/D-Programming-Language/dmd/commit/b21432a31061b7106c3d2d9f0d4b42a2faa070e0
Merge pull request #5330 from WalterBright/fix15389

fix Issue 15389 - extern(C++) forward referencing problem
Comment 6 github-bugzilla 2016-01-06 17:40:37 UTC
Commit pushed to stable at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/8399e29190a31746f695ae517cc28ec29806ae8a
Merge pull request #5330 from WalterBright/fix15389

fix Issue 15389 - extern(C++) forward referencing problem
Comment 7 github-bugzilla 2016-01-24 22:47:50 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/8399e29190a31746f695ae517cc28ec29806ae8a
Merge pull request #5330 from WalterBright/fix15389
Comment 8 github-bugzilla 2016-03-19 20:21:30 UTC
Commits pushed to stable at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/11d309ff1c766715e06671bbb586809a9b0d5af8
fix Issue 15389 - extern(C++) forward referencing problem

https://github.com/D-Programming-Language/dmd/commit/b21432a31061b7106c3d2d9f0d4b42a2faa070e0
Merge pull request #5330 from WalterBright/fix15389
Comment 9 github-bugzilla 2017-08-02 08:07:38 UTC
Commit pushed to dmd-cxx at https://github.com/dlang/dmd

https://github.com/dlang/dmd/commit/0fbec9bdf7d959db71e7b0a9b12d89e44493f340
Issue 15389 - extern(C++) forward referencing problem