D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21211 - `__FILE__.ptr` as default argument gives wrong value
Summary: `__FILE__.ptr` as default argument gives wrong value
Status: RESOLVED DUPLICATE of issue 18919
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-08-31 00:58 UTC by Mathias LANG
Modified: 2021-02-05 16:46 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Mathias LANG 2020-08-31 00:58:36 UTC
Using `__FILE__.ptr` will get const-folded, meaning it will always be the caller's site showing up:

```
// a.d
import b;

void main ()
{
    foo();
}
```

```
// b.d
import core.stdc.stdio;

void foo(const(char)* file = __FILE__.ptr)
{
    printf("Called from %s\n", file);
}
```

Using the latest DMD (v2.093.1):
```
$ dmd -i -run a.d
Called from b.d
```

But why would one ever use `__FILE__.ptr`, might you ask ? Just use `__FILE__` !
Well I would, but in my original code, `foo` is `extern(C++)`...
Comment 1 Mathias LANG 2020-08-31 01:09:10 UTC
Side note: `__FUNCTION__` and `__PRETTY_FUNCTION__` work correctly.
Comment 2 Dennis 2021-02-05 16:46:34 UTC

*** This issue has been marked as a duplicate of issue 18919 ***