Bug 6180 – Private has no effect on types in modules

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2011-06-19T17:36:00Z
Last change time
2012-09-06T04:21:38Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2011-06-19T17:36:05Z
Example: foo.d: module foo; private { int x; class Foo {} } main.d: module main; import foo; void main() { // x = 5; // Error: module main foo.x is private auto foo = new Foo(); // accepted!! } Currently I have a problem with clashing symbols between std.concurrency.MessageBox which is a private class and the Windows bindings win32.winuser.MessageBox, which is a public function. It's a very commonly used function btw. My workaround is this after the last import: alias win32.winuser.MessageBox MessageBox;
Comment #1 by peter.alexander.au — 2011-06-21T08:05:56Z
I'm working on fixing the fact that protection is ignored on user-defined types, but until someone says otherwise, I won't be changing the overload resolution rules to account for protection. Unless I've missed it, no where in TDPL or on the website does it say that private symbols are invisible from other modules, it only says that they cannot be accessed.
Comment #2 by andrej.mitrovich — 2011-06-25T09:46:08Z
I don't really understand this notion of "visibility" vs "accessibility". Can someone show me some code that shows the difference between the two?
Comment #3 by issues.dlang — 2011-06-25T10:06:39Z
In general, if a function is visible, then the compiler knows about it. If it weren't visible, then the compiler wouldn't really see it when a module imported the module that it's in. If it's visible but inaccessible, then the compiler can see it, but it won't allow you to use it. So, for instance, if you tried to use a private function, then it could complain about the function being private, so you can't use it. But if it were not visible, then the compiler would have to complain that you were trying to use a symbol that it didn't know about. Private symbols are perfectly visibile. They're just not accessible. All that public, package, protected, and private affect is accessibility, not visibility. They're _access_ modifiers. And overload resolution occurs _before_ accessibility is checked, which is part of the reason that you've been running into issues with private stuff conflicting with public stuff. You can check out this explanation with regards to C++ (which is essentially the same): http://meditation-art.blogspot.com/2007/11/c-function-visibility-and-accessibility.html Also, look into NVI (Non-Virtual Inheritance). Herb Sutter discusses it in C++ here: http://www.gotw.ca/publications/mill18.htm Assuming that private functions are overridable (which according to TDPL, they should be, but they aren't currently in D), then derived classes can override private functions without being able to call them. Andrei discusses it starting at page 213 of TDPL. That wouldn't be possible if private functions weren't visible.
Comment #4 by andrej.mitrovich — 2011-06-25T10:13:15Z
Thanks Jon, that sheds the light on the issue. I guess this will be difficult to solve then.
Comment #5 by kamm-removethis — 2011-08-20T23:22:20Z
*** This issue has been marked as a duplicate of issue 1441 ***
Comment #6 by bugzilla — 2012-09-04T19:54:26Z
Jonathan's summary is correct.
Comment #7 by issues.dlang — 2012-09-04T20:00:57Z
However, it _would_ be really nice if we could at least make it so that private functions weren't considered in overload resolution. As it stands, things like private aliases are completely useless because they essentially pollute the global namespace (in the sense that they can cause overload conflicts, forcing you to use the full path for the function, even though the private one is inaccessible).
Comment #8 by code — 2012-09-05T06:03:29Z
>And overload resolution occurs _before_ accessibility is checked Not until we fix Bug 3254. The same bug thing now applies to the template access checks. >Private symbols are perfectly visibile. They're just not accessible. >... >You can check out this explanation with regards to C++ (which is essentially the same) >However, it _would_ be really nice if we could at least make it so that private functions weren't considered in overload resolution. In C++ headers are common and allow to hide implementation symbols. D's current workaround are handwritten di files (object, core.thread) but we need a better solution that is scalable and doesn't add redundancy. So far I only came up with HideModuleMembers which hides protected module level symbols but keeps access checks and overloading for nested scopes. https://github.com/D-Programming-Language/dmd/pull/739 Probably it's time to rediscuss this on the mailing list. http://www.digitalmars.com/d/archives/digitalmars/D/visibility_vs._accessibility_of_protected_symbols_157598.html
Comment #9 by timon.gehr — 2012-09-05T17:59:31Z
(In reply to comment #6) > Jonathan's summary is correct. It shouldn't be. The summary obviously describes horribly broken behaviour.
Comment #10 by issues.dlang — 2012-09-05T18:16:11Z
> It shouldn't be. The summary obviously describes horribly broken behaviour. It describes how it works in C++ which is exactly how it works in D. It's that way by design, and anything else would require that the language be changed. What many have argued for is that private symbols should be hidden (or at least not be considered in overload resolution when not accessible or otherwise cause conflicts with accessible symbols), which I think would be a major improvement, but that's not the way that it currently works or is ever expected to work unless you can convince Walter to change it.
Comment #11 by timon.gehr — 2012-09-05T18:49:03Z
(In reply to comment #10) > > It shouldn't be. The summary obviously describes horribly broken behaviour. > > It describes how it works in C++ Irrelevant. > which is exactly how it works in D. It's that way by design, This does not matter either. It is an incorrect design. > and anything else would require that the language be changed. > > What many have argued for is that private symbols should be hidden (or at least > not be considered in overload resolution when not accessible or otherwise cause > conflicts with accessible symbols), which I think would be a major improvement, These are not conflicts. The compiler is deliberately lying about this. > but that's not the way that it currently works or is ever expected to work > unless you can convince Walter to change it. Introducing a private module scope symbol currently is a breaking interface change (even in code that does not use any metaprogramming!) This is ridiculous. I assume that Walter will figure it out.
Comment #12 by issues.dlang — 2012-09-05T19:26:03Z
D takes it access modifier design from C++, and it's purely a question of accessibility, not visibility, even if many people expect it to affect visibility. AFAIK, Walter doesn't think that there's anything wrong with it. I have no idea how easy it will be to convince him otherwise. But if you don't like the current design, then start a discussion in the newsgroup on it and convince Walter.
Comment #13 by code — 2012-09-06T03:52:07Z
>D takes it access modifier design from C++, and it's purely a question of accessibility, not visibility, even if many people expect it to affect visibility. C++ doesn't have access modifiers on global scope, only within classes/structs. It's primarily needed to restrict access while making all fields visible for the memory layout. The C++ way of hiding symbols is to not expose them in headers and using anonymous namespaces for sources. Using access modifiers for types and functions in D's source only modules has no equivalence in C++. And because unqualified imports are the default we're getting namespaces clashes as with header includes. The hijack protection is only a safety net but it doesn't protect against breaking software. Reading the first paragraph of http://dlang.org/hijack.html it becomes clear that we missed an important design goal. >The mod­ule developers must be able to maintain and improve those modules without inadvertently stepping on the behavior of modules over which they cannot have knowledge of.
Comment #14 by code — 2012-09-06T04:21:38Z