Skip to content

Default arguments bypass most attributes check (pure, @safe, @nogc) #18670

Open

Description

@dlangBugzillaToGithub

Jonathan M Davis (@jmdavis) reported this on 2013-09-15T14:54:15Z

Transferred from https://issues.dlang.org/show_bug.cgi?id=11048

CC List

Description

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 error
    baz(x);
}

void bar(int a = x) pure {}

void baz(int a) pure {}

void main() {}

Activity

  1. dlangBugzillaToGithub commented on Sep 16, 2013

    @dlangBugzillaToGithub
    Author

    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);
  2. dlangBugzillaToGithub commented on Sep 16, 2013

    @dlangBugzillaToGithub
    Author

    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.
  3. dlangBugzillaToGithub commented on Nov 9, 2020

    @dlangBugzillaToGithub
    Author

    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(); }
    ```
  4. dlangBugzillaToGithub commented on Nov 9, 2020

    @dlangBugzillaToGithub
    Author

    pro.mathias.lang (@Geod24) commented on 2020-11-09T00:22:24Z

    *** Issue 13442 has been marked as a duplicate of this issue. ***
  5. dlangBugzillaToGithub commented on Jul 16, 2022

    @dlangBugzillaToGithub
    Author

    ibuclaw (@ibuclaw) commented on 2022-07-16T08:00:04Z

    *** Issue 19645 has been marked as a duplicate of this issue. ***
  6. dlangBugzillaToGithub commented on Jul 16, 2022

    @dlangBugzillaToGithub
    Author

    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.
  7. dlangBugzillaToGithub commented on Jul 16, 2022

    @dlangBugzillaToGithub
    Author

    dlang-bot commented on 2022-07-16T09:16:48Z

    @ibuclaw created dlang/dmd pull request #14309 "fix Issue 11048 - Default arguments bypass most attributes check (pure, @safe, @nogc)" fixing this issue:
    
    - fix Issue 11048 - Default arguments bypass most attributes check (pure, @safe, @nogc)
    
    https://github.com/dlang/dmd/pull/14309
  8. TurkeyMan commented on Apr 16, 2025

    @TurkeyMan
    Contributor

    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?

  9. dkorpel commented on Apr 16, 2025

    @dkorpel
    Contributor

    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?

    Because:

    Default parameters are resolved and semantically checked in the context of the function declaration.

    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).

  10. Geod24 commented on Apr 16, 2025

    @Geod24
    Member

    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.

  11. dkorpel commented on Apr 16, 2025

    @dkorpel
    Contributor

    Point 3 seems to hint that this bug is valid

    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.

  12. TurkeyMan commented on Apr 16, 2025

    @TurkeyMan
    Contributor

    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...

  13. TurkeyMan commented on Apr 16, 2025

    @TurkeyMan
    Contributor

    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.

  14. TurkeyMan commented on Apr 16, 2025

    @TurkeyMan
    Contributor

    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!

  15. dkorpel commented on Apr 16, 2025

    @dkorpel
    Contributor

    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.

    module m;
    enum HELLO = "HELLO";
    string getHello(string x = HELLO) => x;
    import m;
    
    enum HELLO = "GOODBYE"; // hijack default arg
    pragma(msg, getHello(HELLO)); // GOODBYE
    pragma(msg, getHello()); // HELLO
  16. dkorpel commented on Apr 16, 2025

    @dkorpel
    Contributor

    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.

    void f(int x = T!__LINE__); // is the type correct? Depends on where it's called from.
  17. TurkeyMan commented on Apr 16, 2025

    @TurkeyMan
    Contributor

    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?

  18. TurkeyMan commented on Apr 16, 2025

    @TurkeyMan
    Contributor

    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" ;)

  19. added a commit that references this issue on Apr 10, 2026
    e3abe9a
  20. added a commit that references this issue on Jun 17, 2026
    9dfa490
  21. added a commit that references this issue on Jul 10, 2026
    3ebe669
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions