public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/67333] New: [C++11][constexpr] constexpr functions incorrectly prohibit taking references to volatile types
@ 2015-08-24  4:39 myriachan at gmail dot com
  2015-08-24  5:50 ` [Bug c++/67333] " miyuki at gcc dot gnu.org
  2015-08-29 22:23 ` miyuki at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: myriachan at gmail dot com @ 2015-08-24  4:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 67333
           Summary: [C++11][constexpr] constexpr functions incorrectly
                    prohibit taking references to volatile types
           Product: gcc
           Version: 5.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: myriachan at gmail dot com
  Target Milestone: ---

GCC 4.7.3 (at least) through GCC 6.0 reject the following due to "meow has
side-effects":

#include <cstddef>
#include <type_traits>

template <typename T, std::size_t S>
constexpr std::size_t lengthof(const volatile T (&)[S])
{
    return S;
}

int main()
{
    volatile int meow[4];
    static_cast<void>(meow); // shut up warning
    return static_cast<int>(std::integral_constant<std::size_t,
        lengthof(meow)>::value);
}

I believe that this is legal per [expr.const] in the Standard, because the
volatile parameter is never used in an lvalue-to-rvalue conversion, which is
what [expr.const] disallows in constant-expressions for volatile types.  Clang
versions that understand this code accept it; Visual C++ 2015 does as well.

Someone who replied to my question on the "std-discussion" mailing list
suggested that this is also technically legal as well:

#include <type_traits>

constexpr int Test(int x)
{
    volatile int v = x;
    return x;
}

int main()
{
    return std::integral_constant<int, Test(2)>::value;
}

GCC also rejects this, but Clang accepts this as well.  Any attempt to read v
will fail, though, so Clang is enforcing the rule.  I'm not on my Windows
machine as I write this, so I can't check MSVC.


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

* [Bug c++/67333] [C++11][constexpr] constexpr functions incorrectly prohibit taking references to volatile types
  2015-08-24  4:39 [Bug c++/67333] New: [C++11][constexpr] constexpr functions incorrectly prohibit taking references to volatile types myriachan at gmail dot com
@ 2015-08-24  5:50 ` miyuki at gcc dot gnu.org
  2015-08-29 22:23 ` miyuki at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: miyuki at gcc dot gnu.org @ 2015-08-24  5:50 UTC (permalink / raw)
  To: gcc-bugs

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

Mikhail Maltsev <miyuki at gcc dot gnu.org> changed:

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

--- Comment #1 from Mikhail Maltsev <miyuki at gcc dot gnu.org> ---
EDG accepts the first testcase. The second one is rejected, but presumably due
to incomplete C++14 support.

In potential_constant_expression_1 we have a check:

  if (TREE_THIS_VOLATILE (t))
    {
      if (flags & tf_error)
        error ("expression %qE has side-effects", t);

The comment in tree.h says:
/* Nonzero means this expression is volatile in the C sense:
   its address should be of type `volatile WHATEVER *'.
   In other words, the declared item is volatile qualified.
...
   If this bit is set in an expression, so is TREE_SIDE_EFFECTS.  */

In our case t is:
(gdb) p t
$10 = <var_decl 0x7ffff66a7cf0 meow>
(gdb) pt
warning: Expression is not an assignment (and might have no effect)
 <var_decl 0x7ffff66a7cf0 meow
    type <array_type 0x7ffff6800c78
        type <integer_type 0x7ffff6800a80 int volatile type_6 SI
            size <integer_cst 0x7ffff66bd0a8 constant 32>
            unit size <integer_cst 0x7ffff66bd0c0 constant 4>
            align 32 symtab 0 alias set -1 canonical type 0x7ffff6800a80
precision 32 min <integer_cst 0x7ffff66bd060 -2147483648> max <integer_cst
0x7ffff66bd078 2147483647>
            pointer_to_this <pointer_type 0x7ffff6800d20>>
        TI
        size <integer_cst 0x7ffff669bea0 constant 128>
        unit size <integer_cst 0x7ffff669beb8 constant 16>
        align 32 symtab 0 alias set -1 canonical type 0x7ffff6800c78
        domain <integer_type 0x7ffff6800b28 type <integer_type 0x7ffff669f1f8
sizetype>
            type_6 DI
            size <integer_cst 0x7ffff669be58 constant 64>
            unit size <integer_cst 0x7ffff669be70 constant 8>
            align 64 symtab 0 alias set -1 canonical type 0x7ffff6800b28
precision 64 min <integer_cst 0x7ffff669be88 0> max <integer_cst 0x7ffff680adc8
3>>
        pointer_to_this <pointer_type 0x7ffff68133f0>>
    side-effects addressable volatile used tree_1 decl_5 TI file
/home/miyuki/gcc/test/meow/meow.cc line 15 col 18 size <integer_cst
0x7ffff669bea0 128> unit size <integer_cst 0x7ffff669beb8 16>
    align 32 context <function_decl 0x7ffff67ffe00 main>>

I.e. the condition is probably wrong and we should also check that t is an
expression, not a declaration.


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

* [Bug c++/67333] [C++11][constexpr] constexpr functions incorrectly prohibit taking references to volatile types
  2015-08-24  4:39 [Bug c++/67333] New: [C++11][constexpr] constexpr functions incorrectly prohibit taking references to volatile types myriachan at gmail dot com
  2015-08-24  5:50 ` [Bug c++/67333] " miyuki at gcc dot gnu.org
@ 2015-08-29 22:23 ` miyuki at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: miyuki at gcc dot gnu.org @ 2015-08-29 22:23 UTC (permalink / raw)
  To: gcc-bugs

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

Mikhail Maltsev <miyuki at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2015-08-29
             Blocks|                            |55004
     Ever confirmed|0                           |1
      Known to fail|                            |6.0

--- Comment #2 from Mikhail Maltsev <miyuki at gcc dot gnu.org> ---
For the record: https://gcc.gnu.org/ml/gcc-patches/2015-08/msg01735.html


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55004
[Bug 55004] [meta-bug] constexpr issues


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

end of thread, other threads:[~2015-08-29 22:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-08-24  4:39 [Bug c++/67333] New: [C++11][constexpr] constexpr functions incorrectly prohibit taking references to volatile types myriachan at gmail dot com
2015-08-24  5:50 ` [Bug c++/67333] " miyuki at gcc dot gnu.org
2015-08-29 22:23 ` miyuki 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).