[Issue 22674] ImportC: compatible types declared in different translation units are not treated equivalent in D.

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Jan 22 04:53:11 UTC 2022


https://issues.dlang.org/show_bug.cgi?id=22674

Walter Bright <bugzilla at digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla at digitalmars.com
         Resolution|---                         |WONTFIX

--- Comment #1 from Walter Bright <bugzilla at digitalmars.com> ---
I looked into making an adjustment to the compiler, which would be regarding
types in C imports with the same name be the same type. Unfortunately, the D
compiler is designed around types are the same if they have the same address.
Changing this would be very difficult, and would impair speed even if it never
actually happens.

But there is a solution. Accessing things from D can be qualified, so:

  import maker;
  import freer;

  void do_foo(){
    auto f = cast(freer.FooRef)make_foo();
    free_foo(f);
  }

Doing this in ImportC is slightly trickier, as C doesn't have name
qualification:

  __import maker;
  __import freer : FooRef;

  void do_foo(){
    FooRef f = (FooRef)make_foo();
    free_foo(f);
  }

Once could go further by creating a D file that merges the duplicate
declarations:

  import maker;
  import freer;

  alias FooRef = maker.FooRef;
  extern (C) void free_foo(FooRef foo);

and then importing that module. This works because the declaration of
free_foo() will link without problems with the definition of free_foo() where
ever it may be, as C name mangling does not mangle in the types. This merged D
file may be imported either by D code or ImportC code.

Hence, I'm going to mark this as WONTFIX because it is too disruptive, and
accommodations are available that aren't too bad.

--


More information about the Digitalmars-d-bugs mailing list