public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94511] New: User-defined type in non-type template parameter yields an incorrect value
@ 2020-04-07  9:00 pacoarjonilla at yahoo dot es
  2020-04-07  9:41 ` [Bug c++/94511] " jakub at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: pacoarjonilla at yahoo dot es @ 2020-04-07  9:00 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94511
           Summary: User-defined type in non-type template parameter
                    yields an incorrect value
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pacoarjonilla at yahoo dot es
  Target Milestone: ---

g++ --std=c++2a code.cc && ./a.out

The following snippet misbehaves with GCC9 and GCC10 master

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#include <cstddef>
#include <iostream>

struct A {
    std::byte data [6] {static_cast<std::byte>(0x00)};

    constexpr A(char ch) {
        data[ch/8] |= static_cast<std::byte>(0x01 << (ch % 8));
    }
};

template <A chars> struct B { char ch; };

void print(A a) {
    for (int i = 0; i < 6; i++)
        std::cout << (unsigned)a.data[i] << ' ';
    std::cout << std::endl;
}

using Bn = B<{'\n'}>; // XXX Replacing \n by any other character less than 48
corrects the issue XXX
using Ba = B<{'.'}>;

template <A T> void foo () {
    print(T);
}

int main() {
    foo<'*'>(); // XXX Replacing the * also corrects the issue XXX
    print(A{'*'});
}

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Expected output

0 0 0 0 0 4 
0 0 0 0 0 4 

Actual output

0 4 0 0 0 0 
0 0 0 0 0 4

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

* [Bug c++/94511] User-defined type in non-type template parameter yields an incorrect value
  2020-04-07  9:00 [Bug c++/94511] New: User-defined type in non-type template parameter yields an incorrect value pacoarjonilla at yahoo dot es
@ 2020-04-07  9:41 ` jakub at gcc dot gnu.org
  2020-04-07  9:45 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-04-07  9:41 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Adjusted testcase without system headers:
namespace std {
  enum class byte : unsigned char {};
  constexpr byte
  operator| (byte __l, byte __r) noexcept
  { return (byte)(unsigned char)((unsigned)__l | (unsigned)__r); }
  constexpr byte&
  operator|= (byte& __l, byte __r) noexcept
  { return __l = __l | __r; }
}

struct A {
  std::byte data [6] {static_cast<std::byte> (0x00)};
  constexpr A (char ch) { data[ch/8] |= static_cast<std::byte>(0x01 << (ch %
8)); }
};

template <A chars> struct B { char ch; };

void
check (A a)
{
  for (int i = 0; i < 6; i++)
    if ((unsigned) a.data[i] != (i == 5 ? 4 : 0))
      __builtin_abort ();
}

using Bn = B<{'\n'}>;

template <A T>
void
foo ()
{
  check (T);
}

int
main ()
{
  foo<'*'> ();
  check (A{'*'});
}

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

* [Bug c++/94511] User-defined type in non-type template parameter yields an incorrect value
  2020-04-07  9:00 [Bug c++/94511] New: User-defined type in non-type template parameter yields an incorrect value pacoarjonilla at yahoo dot es
  2020-04-07  9:41 ` [Bug c++/94511] " jakub at gcc dot gnu.org
@ 2020-04-07  9:45 ` jakub at gcc dot gnu.org
  2020-04-07  9:51 ` jakub at gcc dot gnu.org
  2021-08-23 20:58 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-04-07  9:45 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-04-07
                 CC|                            |mpolacek at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
This is miscompiled since r9-5271-g96e768c3fecf0f65af3f289293145d5740971e2a
when it started to be accepted.

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

* [Bug c++/94511] User-defined type in non-type template parameter yields an incorrect value
  2020-04-07  9:00 [Bug c++/94511] New: User-defined type in non-type template parameter yields an incorrect value pacoarjonilla at yahoo dot es
  2020-04-07  9:41 ` [Bug c++/94511] " jakub at gcc dot gnu.org
  2020-04-07  9:45 ` jakub at gcc dot gnu.org
@ 2020-04-07  9:51 ` jakub at gcc dot gnu.org
  2021-08-23 20:58 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2020-04-07  9:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Note, the testcase isn't portable, so either would need to be restricted for
ascii compatible execution charsets only, or the check would need to be
replaced with i == ('*' / 8) ? 1U << ('*' % 8) : 0
(and deal with '*' >= 64 where it would be a compile time error).
Or use '\x0a' and '\x2a' instead of '\n' and '*'.

namespace std {
  enum class byte : unsigned char {};
  constexpr byte
  operator| (byte __l, byte __r) noexcept
  { return (byte)(unsigned char)((unsigned)__l | (unsigned)__r); }
  constexpr byte&
  operator|= (byte& __l, byte __r) noexcept
  { return __l = __l | __r; }
}

struct A {
  std::byte data [6] {static_cast<std::byte> (0x00)};
  constexpr A (char ch) { data[ch/8] |= static_cast<std::byte>(0x01 << (ch %
8)); }
};

template <A chars> struct B { char ch; };

void
check (A a)
{
  for (int i = 0; i < 6; i++)
    if ((unsigned) a.data[i] != (i == 5 ? 4 : 0))
      __builtin_abort ();
}

using Bn = B<{'\x0a'}>;

template <A T>
void
foo ()
{
  check (T);
}

int
main ()
{
  foo<'\x2a'> ();
  check (A{'\x2a'});
}

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

* [Bug c++/94511] User-defined type in non-type template parameter yields an incorrect value
  2020-04-07  9:00 [Bug c++/94511] New: User-defined type in non-type template parameter yields an incorrect value pacoarjonilla at yahoo dot es
                   ` (2 preceding siblings ...)
  2020-04-07  9:51 ` jakub at gcc dot gnu.org
@ 2021-08-23 20:58 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-23 20:58 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2020-04-07 00:00:00         |2021-8-23

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note the cleaned up reduced testcase is rejected by clang but a slight change
can allow it to be accepted:
Change the using Bn line to be:
using Bn = B< A{'\x0a'} >;

I don't know if it was valid without the A there or not.

Also here is a further reduction, the operators don't make a difference.
Also this is a more portable testcase which also shows what is going on easier.


#define element 0
#define value 1
#define altelement (element+1)

#define r (value)|((element)<<4)
#define r1 (value)|((altelement)<<4)

struct A {
  unsigned data [16] {};
  constexpr A (unsigned ch) { data[(ch>>4)&0xf] = (ch&0xf); }
};
template <A chars> struct B {  };
void
check (A a)
{
  for (int i = 0; i < 16; i++)
    if ((unsigned) a.data[i] != (i == element ? value : 0))
      __builtin_abort ();
}
using Bn = B<A{r1}>;
template <A T> void foo () {  check (T); }
int
main ()
{
  foo<r> ();
  check (A{r});
}

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

end of thread, other threads:[~2021-08-23 20:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-07  9:00 [Bug c++/94511] New: User-defined type in non-type template parameter yields an incorrect value pacoarjonilla at yahoo dot es
2020-04-07  9:41 ` [Bug c++/94511] " jakub at gcc dot gnu.org
2020-04-07  9:45 ` jakub at gcc dot gnu.org
2020-04-07  9:51 ` jakub at gcc dot gnu.org
2021-08-23 20:58 ` pinskia 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).