Bug 19002 – __FUNCTION__ and __PRETTY_FUNCTION__ cannot be used as C string literals

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2018-06-18T12:38:54Z
Last change time
2021-07-26T13:32:35Z
Keywords
pull, rejects-valid
Assigned to
No Owner
Creator
Steven Schveighoffer

Comments

Comment #0 by schveiguy — 2018-06-18T12:38:54Z
void main() { import core.stdc.stdio; printf(__FUNCTION__); } Error: function core.stdc.stdio.printf(scope const(char*) format, ...) is not callable using argument types (string) __FUNCTION__ should be equivalent to a string literal here. Using __FILE__ or __MODULE__ works fine.
Comment #1 by greensunny12 — 2018-06-27T07:20:21Z
This is expected as all string literals should be zero-terminated D strings. So the following works as expected: ``` void main() { import core.stdc.stdio; printf(__FUNCTION__.ptr); } ``` The interesting bit is why __FILE__ and __MODULE__ can be passed directly to printf.
Comment #2 by schveiguy — 2018-06-27T15:17:15Z
No, this is not expected. I can use printf with a string literal, and it works, even if it's via an enum, or some const folding. The compiler implicitly can cast a string literal to a const(char*), otherwise using C functions with string literals would be painful. For example: printf("hello" ~ "\n"); // ok enum x = "hello"; printf(x); // ok enum y = __FUNCTION__; printf(y); // nope enum z = ctfeFunction(); printf(z); // nope I think all of these should work, they are all string literals, and should be 0 terminated.
Comment #3 by b2.temp — 2019-05-30T15:06:21Z
Comment #4 by dlang-bot — 2019-05-30T15:26:14Z
@Basile-z updated dlang/dmd pull request #9920 "fix issue 19002 - __FUNCTION__ and __PRETTY_FUNCTION__ cannot be used as C string literals" fixing this issue: - fix issue 19002 - __FUNCTION__ and __PRETTY_FUNCTION__ cannot be used as C string literals https://github.com/dlang/dmd/pull/9920
Comment #5 by dlang-bot — 2019-05-30T23:24:05Z
dlang/dmd pull request #9920 "fix issue 19002 - __FUNCTION__ and __PRETTY_FUNCTION__ cannot be used as C string literals" was merged into stable: - 748fee406f8be6279d687be4acafa119fcdecaa4 by Basile Burg: fix issue 19002 - __FUNCTION__ and __PRETTY_FUNCTION__ cannot be used as C string literals https://github.com/dlang/dmd/pull/9920
Comment #6 by dlang-bot — 2021-07-26T13:32:35Z
dlang/dmd pull request #12923 "[dmd-cxx] fix issue 19002 - __FUNCTION__ and __PRETTY_FUNCTION__ cannot be used as C string literals" was merged into dmd-cxx: - c42fda99f18c0258bebdbb9958cf86a8c602a1bf by Basile Burg: [dmd-cxx] fix issue 19002 - __FUNCTION__ and __PRETTY_FUNCTION__ cannot be used as C string literals https://github.com/dlang/dmd/pull/12923