Bug 15907 – Unjustified "is not visible from module" deprecation warning when using getMember trait
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2016-04-09T22:01:00Z
Last change time
2016-11-04T09:05:01Z
Keywords
industry
Assigned to
code
Creator
m.bierlee
Comments
Comment #0 by m.bierlee — 2016-04-09T22:01:26Z
Some libraries use the "allMembers" trait to get access to all members of an instance, including private members. The change "Import access checks for fully qualified names were fixed.", introduced in DMD 2.071.0, now shows a deprecation warning in cases where the library module accesses members of classes (and structs, probably) which were not imported in that module.
Consider the following code:
------------------------------
module app;
import library;
class FirstClass {}
class SecondClass {
private FirstClass firstClass;
}
void main() {
auto manipulator= new PrivateMemberManipulator();
auto instance = new SecondClass();
manipulator.process!SecondClass(instance);
}
------------------------------
module library;
import std.traits;
class PrivateMemberManipulator {
public void process(Type)(Type instance) {
foreach (member ; __traits(allMembers, Type)) {
static if (__traits(getProtection, __traits(getMember, instance, member)) == "private") {
// Do things
}
}
}
}
-----------------------------
When compiled, the following deprecation warning is shown:
src\library.d(8,39): Deprecation: app.SecondClass.firstClass is not visible from module library
This is because the results of "allMembers" are fully qualified (as they should be).
I feel this usage is perfectly legal as there is no way a library can (or should) import all application classes.
Comment #1 by m.bierlee — 2016-04-09T22:09:45Z
I forgot to add that it's actually "getMember" which causes the deprecation warning and "allMembers" is not fully qualified.
Comment #2 by me — 2016-04-24T14:21:14Z
Just ran into this issue as well - are there any "decent" workarounds? All I've found is manually excluding the problem objects before using getMember, but that's not really a scalable fix.
Comment #3 by acehreli — 2016-07-26T22:36:47Z
Here is another case with two modules (and with a WORKAROUND):
// ----- a.d:
mixin template MyMixin(alias MODULE) {
shared static this() {
initFunc!(mixin(MODULE))();
}
}
void initFunc(alias MODULE)() {
foreach (member; __traits(allMembers, MODULE)) {
static if(__traits(hasMember, __traits(getMember, MODULE, member), "someProperty")) {
}
}
}
// ----- b.d:
import a;
mixin MyMixin!__MODULE__;
void main() {
}
getMember inside a.d causes the following warnings:
a.d(9): Deprecation: b.object is not visible from module a
a.d(9): Deprecation: b.a is not visible from module a
Here is a WORKAROUND. Make the following changes in a.d:
1) Convert initFunc to a mixin template
2) Mix it in where it's called
3) Call it after it's mixed in
mixin template MyMixin(alias MODULE) {
shared static this() {
mixin initFunc!(mixin(MODULE)); // (2)
initFunc(); // (3)
}
}
mixin template initFunc(alias MODULE) { // (1)
void initFunc() {
foreach (member; __traits(allMembers, MODULE)) {
static if(__traits(hasMember, __traits(getMember, MODULE, member), "someProperty")) {
}
}
}
}
Ali
Comment #4 by code — 2016-08-24T17:42:25Z
*** Issue 15877 has been marked as a duplicate of this issue. ***
Comment #5 by github-bugzilla — 2016-08-29T17:03:58Z
Comment #7 by github-bugzilla — 2016-08-29T17:36:36Z
Commits pushed to stable at https://github.com/dlang/dmdhttps://github.com/dlang/dmd/commit/f899b4b59620e354b6ba0acfe843efb559202cd8
fix Issue 15907 - unjustified deprecation with getMember
- fixed by changing the semantics of allMembers and derivedMembers to
respect visibility
- even though the private members were listed before, they haven't been
accessible
- it's possible to mixin a template in order to use it with
private symbols (example given in changelog)
- fixed ice10598 test b/c it was relying on the presence of the private
object import
https://github.com/dlang/dmd/commit/49cb97213303c902844b7189ced1cf8833214437
Merge pull request #6078 from MartinNowak/fix15907
fix Issue 15907 - unjustified deprecation with getMember
Comment #8 by github-bugzilla — 2016-08-30T19:33:05Z