public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "foom at fuhm dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug libstdc++/104602] New: std::source_location::current uses cast from void*
Date: Sat, 19 Feb 2022 03:31:47 +0000	[thread overview]
Message-ID: <bug-104602-4@http.gcc.gnu.org/bugzilla/> (raw)

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.

             reply	other threads:[~2022-02-19  3:31 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-19  3:31 foom at fuhm dot net [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-104602-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).