public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/104602] New: std::source_location::current uses cast from void*
@ 2022-02-19  3:31 foom at fuhm dot net
  2022-02-19  3:50 ` [Bug libstdc++/104602] " pinskia at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: foom at fuhm dot net @ 2022-02-19  3:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104602

            Bug ID: 104602
           Summary: std::source_location::current uses cast from void*
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: foom at fuhm dot net
  Target Milestone: ---

I'm working on implementing __builtin_source_location() in Clang
(https://reviews.llvm.org/D120159).

In testing it against the libstdc++ <source_location> header, I ran into a
minor issue.

"current()" in GNU libstdc++ is defined as so:

    static consteval source_location
    current(const void* __p = __builtin_source_location()) noexcept
    {
      source_location __ret;
      __ret._M_impl = static_cast <const __impl*>(__p);
      return __ret;
    }

But! A static_cast from a `const void*` parameter to `const __impl*` is not
permitted in constexpr evaluation:
"""
5. An expression E is a core constant expression unless the evaluation of E,
[...] would evaluate one of the following:
[...]
5.15. a conversion from type cv void* to a pointer-to-object type;"
"""
http://eel.is/c++draft/expr.const#5.15

Clang diagnoses this rule, but GCC apparently does not. (it's not really clear
to me why this rule really needs to exist in the standard -- why bother to
police which kinds of pointer casts you're allowed to do, instead of just
raising an error upon _access_ through the wrong type?)

Anyhow, to workaround this issue, I plan to simply hardcode an exception to the
check in Clang for casts which occur in a "std::source_location::current"
method. Yet, although it's perhaps too late to avoid this workaround, it'd be
nice if libstdc++ didn't require the use of an invalid cast.

In clang (in my proposed change), __builtin_source_location already returns the
expected `const __impl*` type, rather than `const void*` as it does in GCC. So,
the issue is only the cast TO `void*` and back again in libstdc++. ISTM this
would be fixed by moving the `static_cast <const __impl*>` into the default
parameter expression. That would then be a no-op cast on clang, and an (invalid
but undiagnosed) cast from void in GCC.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2022-07-07 23:33 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-19  3:31 [Bug libstdc++/104602] New: std::source_location::current uses cast from void* foom at fuhm dot net
2022-02-19  3:50 ` [Bug libstdc++/104602] " pinskia at gcc dot gnu.org
2022-02-19 12:06 ` redi at gcc dot gnu.org
2022-02-19 12:56 ` jakub at gcc dot gnu.org
2022-02-21 14:58 ` foom at fuhm dot net
2022-02-21 15:05 ` jakub at gcc dot gnu.org
2022-02-21 21:06 ` redi at gcc dot gnu.org
2022-02-24 23:46 ` cvs-commit at gcc dot gnu.org
2022-02-24 23:47 ` redi at gcc dot gnu.org
2022-02-24 23:48 ` redi at gcc dot gnu.org
2022-07-07 23:31 ` cvs-commit at gcc dot gnu.org
2022-07-07 23:33 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).