Bug 10815 – Allow access of a symbol in a template instance if instantiator module has access to the symbol
Status
RESOLVED
Resolution
WORKSFORME
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-08-13T05:13:21Z
Last change time
2023-01-04T14:01:02Z
Assigned to
No Owner
Creator
Andrej Mitrovic
Comments
Comment #0 by andrej.mitrovich — 2013-08-13T05:13:21Z
Currently the compiler disallows the following:
-----
module test;
import std.algorithm;
private int foo(int input) { return input; }
package int bar(int input) { return input; }
void main()
{
map!foo([1, 2, 3]);
map!bar([1, 2, 3]);
}
-----
C:\dmd-git\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(410): Error: function test.foo is not accessible from module algorithm
C:\dmd-git\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(410): Error: function test.bar is not accessible from module algorithm
The 'test' module explicitly passed a function that this module has access to, which should effectively give access of this function symbol to the instantiated template.
I propose we allow the instantiated template to get access to the passed symbol if it's passed via an alias symbol, and if the module that instantiates the template has access to this symbol.
Note that it is already possible to pass private functions via a pointer:
-----
module test;
import bar;
private void func() { }
void main()
{
call(&func);
}
-----
-----
module bar;
void call(void function() func) { func(); }
-----
This compiles. But with the relaxed access rule so should this (which currently doesn't compile):
-----
module test;
import bar;
private void func() { }
void main()
{
call!func();
}
-----
-----
module bar;
void call(alias func)() { func(); }
-----
Comment #1 by public — 2013-08-13T06:36:12Z
I have always thought it is intentional:
"TemplateInstantances are always performed in the scope of where the TemplateDeclaration is declared, with the addition of the template parameters being declared as aliases for their deduced types."
(http://dlang.org/template.html)
Comment #2 by andrej.mitrovich — 2013-08-13T11:43:44Z
Well it may be intentional, but it makes many Phobos functions unusable with internal library routines.
Comment #3 by razvan.nitu1305 — 2023-01-04T14:01:02Z
I cannot reproduce this. It seems that it has been fixed in git master.