public inbox for gdb-prs@sourceware.org
help / color / mirror / Atom feed
From: "simon.marchi at polymtl dot ca" <sourceware-bugzilla@sourceware.org>
To: gdb-prs@sourceware.org
Subject: [Bug gdb/31331] Wenum-constexpr-conversion should be fixed, soon treated as a hard error
Date: Sun, 04 Feb 2024 18:57:29 +0000	[thread overview]
Message-ID: <bug-31331-4717-AuxFZTzUqP@http.sourceware.org/bugzilla/> (raw)
In-Reply-To: <bug-31331-4717@http.sourceware.org/bugzilla/>

https://sourceware.org/bugzilla/show_bug.cgi?id=31331

--- Comment #4 from Simon Marchi <simon.marchi at polymtl dot ca> ---
(In reply to Tom Tromey from comment #2)
> I changed enum-flags.h to use std::underlying_type, with the idea
> that if there were any errors, I'd simply change the base
> enum to use an unsigned type.  (I tried auditing the existing
> uses by hand but I lost interest partway through.)

I think that the code explicitly avoids using underlying_type on the enum type,
to get around this behavior (I'll paste the code to generate this table at the
end).

Type             is_signed_v<T>      T(-1)  T(0)  T(-1) < T(0)
Implicit         false                  -1     0  true  
ExplicitSigned   true                   -1     0  true  
ExplicitUnsigned false          4294967295     0  false 

That is, an enum that doesn't specific a base type (Implicit above) has
unsigned underlying type, according to std::underlying_type, but it decays to
an integer, it appears to behave like a signed type (perhaps some language guru
can explain why).

The expression "T (-1) < T (0)" in enum-flags.h is ultimately used to defined
the traits type EnumIsUnsigned and EnumIsSigned.  And those are used to enable
or disable the use of operator~ on enum / enum flag types that behave like
signed types.  I guess because operator~ is not well-defined for them?

So by doing the std::underlying_type change, here's the difference.  Without
your change, trying to use operator~ on let's say STEP_OVER_BREAKPOINT would
give:

  CXX    infrun.o
/home/simark/src/binutils-gdb/gdb/infrun.c:1470:12: error: overload resolution
selected deleted operator '~'
  auto x = ~STEP_OVER_BREAKPOINT;
           ^~~~~~~~~~~~~~~~~~~~~
/home/simark/src/binutils-gdb/gdb/../gdbsupport/enum-flags.h:408:16: note:
candidate function [with enum_type = step_over_what_flag, $1 = void, $2 = void]
has been explicitly deleted
constexpr void operator~ (enum_type e) = delete;
               ^

If I change the enum_underlying_type definition to be:

template<typename T>
struct enum_underlying_type
{
  typedef typename integer_for_size<
    sizeof (T), static_cast<bool> (
                  std::is_signed_v<std::underlying_type_t<T>>)>::type type;
};

... then the use of operator~ is allowed.

I don't know if this complexity is needed in the end, I just wanted to explain
why (if I understand correctly) we have what we have today.

---

#include <fmt/core.h>
#include <sstream>

enum Implicit {
  A1 = 1,
  A2 = 2,
  A3 = 4,
};
enum ExplicitSigned : signed {
  B1 = 1,
  B2 = 2,
  B3 = 4,
};
enum ExplicitUnsigned : unsigned {
  C1 = 1,
  C2 = 2,
  C3 = 4,
};

template <typename T> void printInfosAboutT(const char *typeName) {
  fmt::print("{: <16} ", typeName);
  fmt::print("{: <14} ", std::is_signed_v<std::underlying_type_t<T>>);

  {
    std::ostringstream os;
    os << T(-1);
    fmt::print("{: >10}  ", os.str());
  }

  {
    std::ostringstream os;
    os << T(0);
    fmt::print("{: >4}  ", os.str());
  } 

  fmt::print("{: <5} ", T(-1) < T(0));
  fmt::print("\n");
}

int main() {
  fmt::print("Type             is_signed_v<T>      T(-1)  T(0)  T(-1) <
T(0)\n");
  printInfosAboutT<Implicit>("Implicit");
  printInfosAboutT<ExplicitSigned>("ExplicitSigned");
  printInfosAboutT<ExplicitUnsigned>("ExplicitUnsigned");
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.

  parent reply	other threads:[~2024-02-04 18:57 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-02 20:36 [Bug gdb/31331] New: " carlosgalvezp at gmail dot com
2024-02-03  2:40 ` [Bug gdb/31331] " tromey at sourceware dot org
2024-02-03 21:14 ` tromey at sourceware dot org
2024-02-03 21:30 ` sam at gentoo dot org
2024-02-04 18:10 ` carlosgalvezp at gmail dot com
2024-02-04 18:57 ` simon.marchi at polymtl dot ca [this message]
2024-02-04 19:56 ` tromey at sourceware dot org
2024-02-04 19:58 ` tromey at sourceware dot org
2024-02-04 21:02 ` tromey at sourceware dot org
2024-02-04 21:19 ` tromey at sourceware dot org
2024-02-04 21:26 ` carlosgalvezp at gmail dot com
2024-02-05 19:28 ` carlosgalvezp at gmail dot com
2024-02-05 19:29 ` carlosgalvezp at gmail dot com
2024-02-05 19:30 ` carlosgalvezp at gmail dot com
2024-02-05 19:47 ` carlosgalvezp at gmail dot com
2024-02-05 19:53 ` simon.marchi at polymtl dot ca
2024-02-05 20:04 ` carlosgalvezp at gmail dot com
2024-02-05 20:08 ` simon.marchi at polymtl dot ca
2024-02-05 20:09 ` simon.marchi at polymtl dot ca
2024-02-05 20:11 ` simon.marchi at polymtl dot ca
2024-02-05 20:43 ` carlosgalvezp at gmail dot com
2024-02-05 20:55 ` carlosgalvezp at gmail dot com
2024-02-05 21:03 ` carlosgalvezp at gmail dot com
2024-02-05 21:19 ` simon.marchi at polymtl dot ca
2024-02-06 18:32 ` carlosgalvezp at gmail dot com
2024-02-07 20:47 ` carlosgalvezp at gmail dot com
2024-02-11 10:02 ` carlosgalvezp at gmail dot com
2024-02-11 17:16 ` tromey at sourceware dot org
2024-02-11 17:19 ` tromey at sourceware dot org
2024-02-11 18:21 ` carlosgalvezp at gmail dot com
2024-02-11 18:44 ` tromey at sourceware dot org
2024-02-11 20:40 ` carlosgalvezp at gmail dot com
2024-02-13 23:54 ` tromey at sourceware dot org
2024-03-26 15:37 ` tromey at sourceware dot org
2024-04-16 22:57 ` tromey at sourceware dot org
2024-04-17 10:41 ` carlosgalvezp at gmail dot com
2024-04-20 18:22 ` brobecker at gnat dot com
2024-05-08 22:12 ` carlosgalvezp at gmail dot com
2024-05-10 20:35 ` carlosgalvezp at gmail dot com
2024-05-19  3:57 ` carlosgalvezp at gmail dot com

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-31331-4717-AuxFZTzUqP@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=gdb-prs@sourceware.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).