public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/49136] New: [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields
@ 2011-05-24  8:35 daniel.kruegler at googlemail dot com
  2011-05-24 11:39 ` [Bug c++/49136] " jakub at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2011-05-24  8:35 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49136

           Summary: [C++0x][constexpr] Incorrect constexpr c'tor
                    evaluation with bitfields
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: daniel.kruegler@googlemail.com
                CC: jason@redhat.com


gcc 4.7.0 20110521 (experimental) in C++0x mode fires a static assertion at the
line marked with #:

//----
struct day {
  unsigned d : 5;
  unsigned n : 3;
  constexpr explicit day(int dd) : d(dd), n(7) {}
};

struct date {
  int d;
  constexpr date(day dd) : d((dd.n != 7) ? 7 : dd.d) {}
};

constexpr day d(0);
constexpr date dt(d);
static_assert(dt.d == 0, "Error"); // #
//----

Further testing shows, that dt.d has the value 7 instead of 0. The error only
occurs, if the day object d is defined as constexpr variable. E.g. given the
above shown types day and date the following program

//---
extern "C" int printf(const char*, ...);

int main()
{
  constexpr day d(0);
  date dt(d);
  printf("%d\n", d.d);  // Prints 0
  printf("%d", dt.d);   // Prints 7
}
//---

still produces the calculation error. If we remove the constexpr specifier of
the local object d (or if we replace it by a const specifier), the calculation
is correct, the output is 

0
0

as expected. Interestingly, the internal value of the object d itself is always
correct. The defect seems to happen if a constexpr day object is used in
another constexpr constructor. It seems that the type date itself is required
as well, I could not replace the constexpr date constructor call by a constexpr
function like this

constexpr int date_func(day dd) { return (dd.n != 7) ? 7 : dd.d; }

to produce the same error. While trying to simplify the test case I found that
the struct day must contain at least two such bitfields.

Let me add that this problem occurred while trying to "constexpressify" an
existing date library of Howard Hinnant, so this is not just a theoretical
case.


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

* [Bug c++/49136] [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields
  2011-05-24  8:35 [Bug c++/49136] New: [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields daniel.kruegler at googlemail dot com
@ 2011-05-24 11:39 ` jakub at gcc dot gnu.org
  2011-05-24 11:52 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-24 11:39 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49136

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2011.05.24 10:03:34
                 CC|                            |jakub at gcc dot gnu.org
     Ever Confirmed|0                           |1

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-24 10:03:34 UTC ---
The problem is that fold optimizes dd.n != 7 comparison into
(bit_field_ref <dd, 8, 0> & 224) != 224 - see optimize_bit_field_compare -
and cxx_eval_bit_field_ref doesn't expect a bit_field_ref which covers more
than one bit field.  One possible fix would be to check also the size in
cxx_eval_bit_field_ref and or in at the right position all the bit fields in
the range, another would be make sure such folding doesn't happen during
constexpr processing.


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

* [Bug c++/49136] [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields
  2011-05-24  8:35 [Bug c++/49136] New: [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields daniel.kruegler at googlemail dot com
  2011-05-24 11:39 ` [Bug c++/49136] " jakub at gcc dot gnu.org
@ 2011-05-24 11:52 ` jakub at gcc dot gnu.org
  2011-05-24 13:28 ` jakub at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-24 11:52 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49136

--- Comment #2 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-24 10:49:09 UTC ---
Another testcase, this one ICEs:

// PR c++/49136
// { dg-do compile }
// { dg-options "-std=c++0x" }

struct S
{
  unsigned : 1; unsigned s : 27; unsigned : 4;
  constexpr S (unsigned int x) : s(x) {}
};

struct T
{
  unsigned int t;
  constexpr T (S s) : t(s.s != 77 ? 0 : s.s) {}
};

constexpr S s (77);
constexpr T t (s);
static_assert (t.t == 77, "Error");


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

* [Bug c++/49136] [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields
  2011-05-24  8:35 [Bug c++/49136] New: [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields daniel.kruegler at googlemail dot com
  2011-05-24 11:39 ` [Bug c++/49136] " jakub at gcc dot gnu.org
  2011-05-24 11:52 ` jakub at gcc dot gnu.org
@ 2011-05-24 13:28 ` jakub at gcc dot gnu.org
  2011-05-24 17:39 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-24 13:28 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49136

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-24 12:50:10 UTC ---
Created attachment 24341
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24341
gcc47-pr49136.patch

WIP patch, works on little-endian, but doesn't on big endian, so some
correction somewhere is needed.


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

* [Bug c++/49136] [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields
  2011-05-24  8:35 [Bug c++/49136] New: [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields daniel.kruegler at googlemail dot com
                   ` (2 preceding siblings ...)
  2011-05-24 13:28 ` jakub at gcc dot gnu.org
@ 2011-05-24 17:39 ` jakub at gcc dot gnu.org
  2011-05-24 21:04 ` jason at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-24 17:39 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49136

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
         AssignedTo|unassigned at gcc dot       |jakub at gcc dot gnu.org
                   |gnu.org                     |

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-24 17:08:22 UTC ---
Created attachment 24345
  --> http://gcc.gnu.org/bugzilla/attachment.cgi?id=24345
gcc47-pr49136.patch

Adjusted fix that passes also on powerpc on the testcases.


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

* [Bug c++/49136] [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields
  2011-05-24  8:35 [Bug c++/49136] New: [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields daniel.kruegler at googlemail dot com
                   ` (3 preceding siblings ...)
  2011-05-24 17:39 ` jakub at gcc dot gnu.org
@ 2011-05-24 21:04 ` jason at gcc dot gnu.org
  2011-05-25  7:30 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: jason at gcc dot gnu.org @ 2011-05-24 21:04 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49136

Jason Merrill <jason at gcc dot gnu.org> changed:

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

--- Comment #5 from Jason Merrill <jason at gcc dot gnu.org> 2011-05-24 20:24:47 UTC ---
(In reply to comment #4)
> Created attachment 24345 [details]
> gcc47-pr49136.patch
> 
> Adjusted fix that passes also on powerpc on the testcases.

OK.


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

* [Bug c++/49136] [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields
  2011-05-24  8:35 [Bug c++/49136] New: [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields daniel.kruegler at googlemail dot com
                   ` (4 preceding siblings ...)
  2011-05-24 21:04 ` jason at gcc dot gnu.org
@ 2011-05-25  7:30 ` jakub at gcc dot gnu.org
  2011-05-25  7:33 ` jakub at gcc dot gnu.org
  2011-05-25  8:09 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-25  7:30 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49136

--- Comment #6 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-25 07:00:06 UTC ---
Author: jakub
Date: Wed May 25 07:00:01 2011
New Revision: 174168

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=174168
Log:
    PR c++/49136
    * semantics.c (cxx_eval_bit_field_ref): Handle the
    case when BIT_FIELD_REF doesn't cover only a single field.

    * g++.dg/cpp0x/constexpr-bitfield2.C: New test.
    * g++.dg/cpp0x/constexpr-bitfield3.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield2.C
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield3.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/semantics.c
    trunk/gcc/testsuite/ChangeLog


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

* [Bug c++/49136] [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields
  2011-05-24  8:35 [Bug c++/49136] New: [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields daniel.kruegler at googlemail dot com
                   ` (5 preceding siblings ...)
  2011-05-25  7:30 ` jakub at gcc dot gnu.org
@ 2011-05-25  7:33 ` jakub at gcc dot gnu.org
  2011-05-25  8:09 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-25  7:33 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49136

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|                            |FIXED

--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-25 07:16:03 UTC ---
Fixed.


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

* [Bug c++/49136] [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields
  2011-05-24  8:35 [Bug c++/49136] New: [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields daniel.kruegler at googlemail dot com
                   ` (6 preceding siblings ...)
  2011-05-25  7:33 ` jakub at gcc dot gnu.org
@ 2011-05-25  8:09 ` jakub at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: jakub at gcc dot gnu.org @ 2011-05-25  8:09 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=49136

--- Comment #7 from Jakub Jelinek <jakub at gcc dot gnu.org> 2011-05-25 07:03:28 UTC ---
Author: jakub
Date: Wed May 25 07:03:25 2011
New Revision: 174169

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=174169
Log:
    PR c++/49136
    * semantics.c (cxx_eval_bit_field_ref): Handle the
    case when BIT_FIELD_REF doesn't cover only a single field.

    * g++.dg/cpp0x/constexpr-bitfield2.C: New test.
    * g++.dg/cpp0x/constexpr-bitfield3.C: New test.

Added:
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield2.C
    branches/gcc-4_6-branch/gcc/testsuite/g++.dg/cpp0x/constexpr-bitfield3.C
Modified:
    branches/gcc-4_6-branch/gcc/cp/ChangeLog
    branches/gcc-4_6-branch/gcc/cp/semantics.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog


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

end of thread, other threads:[~2011-05-25  7:33 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-05-24  8:35 [Bug c++/49136] New: [C++0x][constexpr] Incorrect constexpr c'tor evaluation with bitfields daniel.kruegler at googlemail dot com
2011-05-24 11:39 ` [Bug c++/49136] " jakub at gcc dot gnu.org
2011-05-24 11:52 ` jakub at gcc dot gnu.org
2011-05-24 13:28 ` jakub at gcc dot gnu.org
2011-05-24 17:39 ` jakub at gcc dot gnu.org
2011-05-24 21:04 ` jason at gcc dot gnu.org
2011-05-25  7:30 ` jakub at gcc dot gnu.org
2011-05-25  7:33 ` jakub at gcc dot gnu.org
2011-05-25  8:09 ` jakub 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).