Thread overview
private method callable from other module
Oct 08, 2022
kdevel
Oct 08, 2022
kdevel
Oct 08, 2022
kdevel
Oct 08, 2022
Jack Pope
Oct 08, 2022
kdevel
Oct 08, 2022
kdevel
October 08, 2022

Is this a known bug?

// file b.d
import std.stdio;

struct S {
   private void foo (ubyte c)
   {
      writeln (__PRETTY_FUNCTION__);
   }
   void foo ()
   {
   }
}
// file a.d
unittest {
   import b;
   auto s = S ();
   s.foo ('x');
}
$ dmd -g -unittest -main a.d b.d
$ ./a
void b.S.foo(ubyte c)
1 modules passed unittests
October 08, 2022

On Saturday, 8 October 2022 at 09:46:59 UTC, kdevel wrote:

>
$ dmd -g -unittest -main a.d b.d
$ ./a
void b.S.foo(ubyte c)
1 modules passed unittests

2.086.1
a.d(5): Error: struct b.S member foo is not accessible

2.090.1
compiles

October 08, 2022

On Saturday, 8 October 2022 at 09:59:17 UTC, kdevel wrote:

>

On Saturday, 8 October 2022 at 09:46:59 UTC, kdevel wrote:

>
$ dmd -g -unittest -main a.d b.d
$ ./a
void b.S.foo(ubyte c)
1 modules passed unittests

2.086.1
a.d(5): Error: struct b.S member foo is not accessible

2.090.1
compiles

Interestingly (again with dmd v2.100.2) -run runs into a link error:

$ dmd -g -unittest -main -run a.d b.d
a.o: In function `_D1a16__unittest_L2_C1FZv':
(...)./a.d:5: undefined reference to `_D1b1S3fooMFhZv'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1
October 08, 2022

Altering the definition sequence in b.d to have the private version second has the desired result. Otherwise, when following private foo, it looks like public foo gets adopted as a public constructor. The same for classes.

October 08, 2022

On Saturday, 8 October 2022 at 10:05:33 UTC, kdevel wrote:

>
$ dmd -g -unittest -main -run a.d b.d
a.o: In function `_D1a16__unittest_L2_C1FZv':
(...)./a.d:5: undefined reference to `_D1b1S3fooMFhZv'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1

b.d is the cmd line arg to the executable. The correct order of arguments to dmd is: dmd -g -unittest -main b.d -run a.d

October 08, 2022

On Saturday, 8 October 2022 at 15:56:01 UTC, Jack Pope wrote:

>

Altering the definition sequence in b.d to have the private version second has the desired result. Otherwise, when following private foo, it looks like public foo gets adopted as a public constructor. The same for classes.

Created Issue 23397 - private method callable from other module