You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Default arguments bypass most attributes check (pure, @safe, @nogc) #18670
int x = 7;
void foo() pure
{
// Does not detect use of mutable global and compiles when it shouldn't.
bar();
// Correctly detects the use of a mutable global and gives an errorbaz(x);
}
void bar(int a = x) pure {}
void baz(int a) pure {}
void main() {}
andrej.mitrovich (@AndrejMitrovic) commented on 2013-09-16T08:48:37Z
So should the declaration of such a function be denied if it's pure, or should only calls be denied where a global is used? E.g.:
// 1. ban this declaration?
void bar(int a = x) pure {}
// 2. or just this call?
bar();
// If #2, then this could be considered ok:
bar(1);
issues.dlang (@jmdavis) commented on 2013-09-16T09:35:22Z
The default argument is only a problem if the pure function is called from a pure function. It's perfectly fine if it's called from an impure one. The problem is not that the default argument references a global but that the caller does not detect that the default argument of the function it's calling is a global. So, the caller does not detect that the call violates purity.
The simplest solution is probably to ban the declaration, because then the caller doesn't have to worry about whether the pure function that it's calling has any default arguments which would violate the purity of the caller, but ideally, it's just the call which would be illegal, because the default argument is just fine so long as the caller isn't pure.
pro.mathias.lang (@Geod24) commented on 2020-11-09T00:21:32Z
This applies to all attributes but `nothrow`, but only because `nothrow` is the very last to be checked.
E.g. this compiles:
```int doit() { return 42; }void f(int i = doit()) nothrow @safe pure @nogc {}void main() /* nothrow */ @safe pure @nogc { f(); }```
ibuclaw (@ibuclaw) commented on 2022-07-16T08:10:58Z
I just encountered this when reviewing some @safe code in the dmd compiler implementation.
The crux of which boils down to:
```int findCondition() @system;int parseGeneric(int param = findCondition()) @safe{ return param;}int parseSpec() @safe{ return parseGeneric(); // Compiles!}```
Call the function directly in @safe code, and you get the expected error.
This is 12 years old. I only just noticed this, but now I've done an audit of my code and I found this affects me in several places!
This is a super-critical issue; just trivially and unknowingly violating attributes is not something that can be accepted.
It's hard to imagine what implementation bug could even cause this? Why would a default-arg need special internal logic? Why wouldn't it just lower to a regular call to init the function argument prior to the call? What's special about default args that make a lowering inappropriate?
It would be really simple to just insert a syntaxCopy() of the default argument at the call site, but that's not according to spec, as it would evaluate to a different value at different usage sites (see the example in the spec).
I think the spec is just poorly worded here, as the example is about visibility. Point 3 seems to hint that this bug is valid, as it deals with a semantic error.
Oh there's no question that the bug is valid, I just answered Manu's question why the implementation doesn't / can't treat a default argument exactly the same as writing the expression yourself at the call site.
as it would evaluate to a different value at different usage sites...
How could it? The type of the function argument doesn't change at the call-site, unless it's a template, but if the argument is T then it infers the exact return type of the default arg, no?
Are you aware of any particular corner cases?
Maybe the spec is wrong...
Syntax copy at the call-site is the only logical option that anyone would imagine; unless there's some really bad side effect of that, then we're violating the rule of least surprise... and also inviting super-bad bugs, as evidenced by the fact this issue exists.
Also you could read that spec sentence several ways; like, the function to be called is resolved "in the context of the function declaration" (like, the scope used for resolution and local symbols for template instantiation), but that doesn't mean the actual call must be resolved there. You should be able to get the resolved function SYMBOL, and still put a normal call expression at the call-site... which should confirm attribute correctness?
On a tangent, that line in the spec is the reason fun(int l = __LINE__, string f = __FILE__) doesn't work, which is one of the worst common-sense fails that ever happened in D!
How could it? The type of the function argument doesn't change at the call-site, unless it's a template, but if the argument is T then it infers the exact return type of the default arg, no?
Because the function declaration and the function call may be in different scopes, identifiers can resolve to different values.
modulem;
enumHELLO = "HELLO";
stringgetHello(string x = HELLO) => x;
On a tangent, that line in the spec is the reason fun(int l = __LINE__, string f = __FILE__) doesn't work, which is one of the worst common-sense fails that ever happened in D!
That has always worked. For a long time expressions containing those keywords would fail (e.g. __FILE__.ptr would point to the declaration file, not the call site file), but that has been fixed since last year (#15968).
Though there's still a 'limitation' that special keywords are resolved eagerly in template arguments, because currently, functions arguments are eagerly type checked at the declaration.
voidf(int x = T!__LINE__); // is the type correct? Depends on where it's called from.
How could it? The type of the function argument doesn't change at the call-site, unless it's a template, but if the argument is T then it infers the exact return type of the default arg, no?
Because the function declaration and the function call may be in different scopes, identifiers can resolve to different values.
Yeah, but that's what I just said above; it must obviously resolve the symbol in the declaration's scope - it couldn't work any other way, and my reading of the spec is that's what that means.
I don't see how that has anything to do with the call itself though? You take the pre-resolved symbol and insert the call at the callsite. alias-es carry around symbols resolved elsewhere all the time; it's extremely normal in D to resolve a symbol in one scope and then transport it around for use somewhere else. I feel like that's all that's happening here?
For a long time expressions containing those keywords would fail (e.g. __FILE__.ptr would point to the declaration file, not the call site file), but that has been fixed since last year (#15968).
🤯
I had no idea! That's amazing; and I'm astonished someone had the balls to fix that! That would have surely fallen under the evil category of "mildly-breaking-change" ;)
Activity
dlangBugzillaToGithub commentedon Sep 16, 2013
andrej.mitrovich (@AndrejMitrovic) commented on 2013-09-16T08:48:37Z
So should the declaration of such a function be denied if it's pure, or should only calls be denied where a global is used? E.g.: // 1. ban this declaration? void bar(int a = x) pure {} // 2. or just this call? bar(); // If #2, then this could be considered ok: bar(1);dlangBugzillaToGithub commentedon Sep 16, 2013
issues.dlang (@jmdavis) commented on 2013-09-16T09:35:22Z
dlangBugzillaToGithub commentedon Nov 9, 2020
pro.mathias.lang (@Geod24) commented on 2020-11-09T00:21:32Z
dlangBugzillaToGithub commentedon Nov 9, 2020
pro.mathias.lang (@Geod24) commented on 2020-11-09T00:22:24Z
dlangBugzillaToGithub commentedon Jul 16, 2022
ibuclaw (@ibuclaw) commented on 2022-07-16T08:00:04Z
dlangBugzillaToGithub commentedon Jul 16, 2022
ibuclaw (@ibuclaw) commented on 2022-07-16T08:10:58Z
dlangBugzillaToGithub commentedon Jul 16, 2022
dlang-bot commented on 2022-07-16T09:16:48Z
TurkeyMan commentedon Apr 16, 2025
This is 12 years old. I only just noticed this, but now I've done an audit of my code and I found this affects me in several places!
This is a super-critical issue; just trivially and unknowingly violating attributes is not something that can be accepted.
It's hard to imagine what implementation bug could even cause this? Why would a default-arg need special internal logic? Why wouldn't it just lower to a regular call to init the function argument prior to the call? What's special about default args that make a lowering inappropriate?
dkorpel commentedon Apr 16, 2025
Because:
https://dlang.org/spec/function.html#function-default-args
It would be really simple to just insert a syntaxCopy() of the default argument at the call site, but that's not according to spec, as it would evaluate to a different value at different usage sites (see the example in the spec).
Geod24 commentedon Apr 16, 2025
I think the spec is just poorly worded here, as the example is about visibility. Point 3 seems to hint that this bug is valid, as it deals with a semantic error.
dkorpel commentedon Apr 16, 2025
Oh there's no question that the bug is valid, I just answered Manu's question why the implementation doesn't / can't treat a default argument exactly the same as writing the expression yourself at the call site.
TurkeyMan commentedon Apr 16, 2025
How could it? The type of the function argument doesn't change at the call-site, unless it's a template, but if the argument is T then it infers the exact return type of the default arg, no?
Are you aware of any particular corner cases?
Maybe the spec is wrong...
TurkeyMan commentedon Apr 16, 2025
Syntax copy at the call-site is the only logical option that anyone would imagine; unless there's some really bad side effect of that, then we're violating the rule of least surprise... and also inviting super-bad bugs, as evidenced by the fact this issue exists.
TurkeyMan commentedon Apr 16, 2025
Also you could read that spec sentence several ways; like, the function to be called is resolved "in the context of the function declaration" (like, the scope used for resolution and local symbols for template instantiation), but that doesn't mean the actual call must be resolved there. You should be able to get the resolved function SYMBOL, and still put a normal call expression at the call-site... which should confirm attribute correctness?
On a tangent, that line in the spec is the reason
fun(int l = __LINE__, string f = __FILE__)doesn't work, which is one of the worst common-sense fails that ever happened in D!dkorpel commentedon Apr 16, 2025
Because the function declaration and the function call may be in different scopes, identifiers can resolve to different values.
dkorpel commentedon Apr 16, 2025
That has always worked. For a long time expressions containing those keywords would fail (e.g.
__FILE__.ptrwould point to the declaration file, not the call site file), but that has been fixed since last year (#15968).Though there's still a 'limitation' that special keywords are resolved eagerly in template arguments, because currently, functions arguments are eagerly type checked at the declaration.
TurkeyMan commentedon Apr 16, 2025
Yeah, but that's what I just said above; it must obviously resolve the symbol in the declaration's scope - it couldn't work any other way, and my reading of the spec is that's what that means.
I don't see how that has anything to do with the call itself though? You take the pre-resolved symbol and insert the call at the callsite.
alias-es carry around symbols resolved elsewhere all the time; it's extremely normal in D to resolve a symbol in one scope and then transport it around for use somewhere else. I feel like that's all that's happening here?TurkeyMan commentedon Apr 16, 2025
🤯
I had no idea! That's amazing; and I'm astonished someone had the balls to fix that! That would have surely fallen under the evil category of "mildly-breaking-change" ;)
Fix dlang#18670 - Default arguments bypass most attribute checks
Fix dlang#18670 - Default arguments bypass most attribute checks
Fix dlang#18670 - Default arguments bypass most attribute checks