D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 9795 - cast fails for C's forward referenced structure pointer
Summary: cast fails for C's forward referenced structure pointer
Status: RESOLVED DUPLICATE of issue 10451
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Linux
: P2 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-03-23 09:59 UTC by Sarath Kumar Kodali
Modified: 2013-10-01 19:24 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Sarath Kumar Kodali 2013-03-23 09:59:23 UTC
With DMD32 D Compiler v2.062,

$ cat libA.d
module libA;

extern (C)
{
        struct Opaque;  // forward reference
        Opaque* getObject();
}

$ cat libB.d
module libB;

extern (C)
{
        struct Opaque;  // forward reference
        void doAction(Opaque *);
}

$ cat bug.d
import libA, libB;

int main(string[] args)
{
        auto opaque = libA.getObject();
        libB.doAction(cast(libB.Opaque *)opaque);
        return 0;
}

$ rdmd bug.dlibB.d(5): Error: struct libB.Opaque unknown size
libB.d(5): Error: struct libB.Opaque no size yet for forward reference
libB.d(5): Error: struct libB.Opaque unknown size
libB.d(5): Error: struct libB.Opaque no size yet for forward reference
libA.d(5): Error: struct libA.Opaque unknown size
libA.d(5): Error: struct libA.Opaque no size yet for forward reference
Failed: 'dmd' '-v' '-o-' 'bug.d' '-I.'
$

If we declare Opaque in a separate file and import it in libA and libB, then there is no compiler error. Also if we remove the cast in bug.d, the error message would be:

bug.d(6): Error: function libB.doAction (Opaque*) is not callable using argument types (Opaque*)
bug.d(6): Error: cannot implicitly convert expression (opaque) of type Opaque* to Opaque*

The compiler should have qualified Opaque so that it is easier to understand that it cannot convert libA.Opaque* to libB.Opaque*
Comment 1 Kenji Hara 2013-10-01 19:24:03 UTC
This is a dup of bug 10451.

With 2.064 git-head, the OP code correctly reports link-time errors.

$ dmd libA.d libB.d bug.d
DMD v2.064 DEBUG
OPTLINK (R) for Win32  Release 8.00.12
Copyright (C) Digital Mars 1989-2010  All rights reserved.
http://www.digitalmars.com/ctg/optlink.html
libA.obj(libA)
 Error 42: Symbol Undefined _getObject
libA.obj(libA)
 Error 42: Symbol Undefined _doAction
--- errorlevel 2

*** This issue has been marked as a duplicate of issue 10451 ***