Bug 2383 – default arguments can implicitly access private global variables that are not visible at call site

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2008-10-01T18:55:00Z
Last change time
2015-06-09T05:15:22Z
Keywords
accepts-invalid, spec
Assigned to
bugzilla
Creator
tomas

Comments

Comment #0 by tomas — 2008-10-01T18:55:37Z
Consider the following testcase: ------------------------ 1st module: module privateconst1; extern(C) int rand(); private const int abc; static this() { abc = rand(); } void foo(int i = abc) {} ------------------------ 2nd module: module privateconst2; import privateconst1; void main() { foo(); } ------ this is accepted by dmd and works. however this makes it impossible for another compiler to mark 'abc' as internal (or in C terms, static) disabling a lot of valuable optimizations. in the 2nd module the call to 'foo' has to load the value of 'abc' which is private and should not be accessible. I can't find anywhere in the spec where it says this is allowed, hence marking both 'spec' and 'accepts-invalid'
Comment #1 by bugzilla — 2008-10-02T03:49:46Z
It's legal because the default parameter value is evaluated in the context where it appears, and so has access to private values where it appears. I'll clarify the documentation.
Comment #2 by matti.niemenmaa+dbugzilla — 2008-10-02T04:39:40Z
"The default parameter value is evaluated in the context where it appears" - should I reopen Issue 191, then?
Comment #3 by tomas — 2008-10-02T05:24:57Z
how can I detect this and still make private globals internal to the module in the normal case ?
Comment #4 by tomas — 2008-10-02T05:25:21Z
I mean in terms of DMD...
Comment #5 by matti.niemenmaa+dbugzilla — 2008-10-02T05:36:54Z
Isn't the case just the same as the following? (I wish default args were always semantically equivalent to this, but oh well...) private const int abc; void foo() { foo(abc); } void foo(int i) {} That shouldn't require making abc non-private, or?
Comment #6 by tomas — 2008-10-02T05:42:10Z
reason I'm asking this is that DMD puts a the private global value in the AST at the call site. thus I can't make this specific global internal and I can't find any information to tell me that this specific global does not follow usual symbol visibility rules.
Comment #7 by bugzilla — 2008-10-02T17:14:40Z
(In reply to comment #2) > "The default parameter value is evaluated in the context > where it appears" - should I reopen Issue 191, then? No, because a 'this' pointer would still be needed.
Comment #8 by tomas — 2008-10-16T07:25:07Z
*bump* I could really use some hints on how to fix this in LDC .. Walter? How do you do it in DMD? As I've already said, the callsite references the global variable, so I need some way to figure out that this in fact is not a real private.
Comment #9 by bugzilla — 2008-10-20T22:20:06Z
Fixed dmd 1.036 and 2.020