public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression
@ 2020-04-05  2:18 bisqwit at iki dot fi
  2020-04-07 22:39 ` [Bug c++/94490] " mpolacek at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: bisqwit at iki dot fi @ 2020-04-05  2:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94490
           Summary: Ternary expression with 3 consts is “not” a constant
                    expression
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bisqwit at iki dot fi
  Target Milestone: ---

Versions tested: GCC 9.3.0, GCC 10.0.1 20200324 (experimental) [master revision
596c90d3559:023579257f5:906b3eb9df6c577d3f6e9c3ea5c9d7e4d1e90536]

Error message:

test6.cc: In instantiation of ‘auto Mul(const T&, const T2&) [with T =
std::array<float, 2>; T2 = std::array<float, 2>]’:
test6.cc:20:58:   required from here
test6.cc:16:78: error: ‘(false ?  std::tuple_size_v<std::array<float, 2> > :
std::tuple_size_v<std::array<float, 2> >)’ is not a constant expression
   16 |              return typename arith_result<T,T2>::type {
std::get<P>(vec) ... };
      |                                                                        
     ^
test6.cc:20:6: error: ‘void x’ has incomplete type
   20 | auto x = Mul(std::array<float,2>{}, std::array<float,2>{});
      |      ^


Compiler is erroneously claiming that an expression of type (x ? y : z) where
all of x,y,z are constant expressions, is not a constant expression.



Code:

#include <tuple>
#include <functional>

template<class T, class T2, std::size_t A=std::tuple_size_v<T>, std::size_t
B=std::tuple_size_v<T>, std::size_t N = std::min(A,B), class S =
std::make_index_sequence<(A<B)?A:B>>
struct arith_result
{
    using type = std::conditional_t<requires(T t){t[0]; },
    decltype([]<std::size_t...I>(std::index_sequence<I...>) ->
std::array<std::common_type_t<std::tuple_element_t<I,T>...,
std::tuple_element_t<I,T2>...>, N>{}(S{})),
    decltype([]<std::size_t...I>(std::index_sequence<I...>) ->
std::tuple<std::common_type_t<std::tuple_element_t<I,T>,
std::tuple_element_t<I,T2>>...>{}(S{}))>;
};

template<typename T = std::array<float,3>, typename T2 = T>
auto Mul(const T& vec, const T2& val)
{
    return [&]<std::size_t... P>(std::index_sequence<P...>) {
             return typename arith_result<T,T2>::type { std::get<P>(vec) ... };
        } (std::make_index_sequence<2>{});
}

auto x = Mul(std::array<float,2>{}, std::array<float,2>{});


Note that if I replace the Mul function with this (inline the lambda call), the
problem goes away:

template<typename T = std::array<float,3>, typename T2 = T>
auto Mul(const T& vec, const T2& val)
{
    return typename arith_result<T,T2>::type {
std::get<0>(vec),std::get<1>(vec) };
}

Somehow the compiler forgets to do constant folding while it is processing the
lambda.

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

* [Bug c++/94490] Ternary expression with 3 consts is “not” a constant expression
  2020-04-05  2:18 [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression bisqwit at iki dot fi
@ 2020-04-07 22:39 ` mpolacek at gcc dot gnu.org
  2020-04-08 13:43 ` ensadc at mailnesia dot com
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-04-07 22:39 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

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

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
In expand_integer_pack tsubst_copy_and_build produces
*(0 ? tuple_size_v : tuple_size_v)
and cxx_constant_value complains, because we turn tuple_size_v into '2' and so
we're dereferencing an INTEGER_CST.

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

* [Bug c++/94490] Ternary expression with 3 consts is “not” a constant expression
  2020-04-05  2:18 [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression bisqwit at iki dot fi
  2020-04-07 22:39 ` [Bug c++/94490] " mpolacek at gcc dot gnu.org
@ 2020-04-08 13:43 ` ensadc at mailnesia dot com
  2021-05-27 19:15 ` ppalka at gcc dot gnu.org
                   ` (5 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: ensadc at mailnesia dot com @ 2020-04-08 13:43 UTC (permalink / raw)
  To: gcc-bugs

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

ensadc at mailnesia dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |ensadc at mailnesia dot com

--- Comment #2 from ensadc at mailnesia dot com ---
reduced & modified:

```
template<class T>
constexpr int fake_tuple_size_v = 3;
template<int...> struct intseq {};
template<int N> using genseq = intseq<__integer_pack(N)...>;

template<int A, class S = genseq<0 ? A : A>>
struct arith_result
{ };

template<typename T>
auto Mul(const T&)
{
    return [](auto) { return arith_result<fake_tuple_size_v<T>> { }; }(0);
}

auto x = Mul(0);
```

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

* [Bug c++/94490] Ternary expression with 3 consts is “not” a constant expression
  2020-04-05  2:18 [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression bisqwit at iki dot fi
  2020-04-07 22:39 ` [Bug c++/94490] " mpolacek at gcc dot gnu.org
  2020-04-08 13:43 ` ensadc at mailnesia dot com
@ 2021-05-27 19:15 ` ppalka at gcc dot gnu.org
  2021-05-27 21:03 ` mpolacek at gcc dot gnu.org
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: ppalka at gcc dot gnu.org @ 2021-05-27 19:15 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-05-27
                 CC|                            |ppalka at gcc dot gnu.org

--- Comment #3 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Marek, it looks like your https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94489#c2
patch also fixes this PR.

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

* [Bug c++/94490] Ternary expression with 3 consts is “not” a constant expression
  2020-04-05  2:18 [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression bisqwit at iki dot fi
                   ` (2 preceding siblings ...)
  2021-05-27 19:15 ` ppalka at gcc dot gnu.org
@ 2021-05-27 21:03 ` mpolacek at gcc dot gnu.org
  2021-12-01 17:23 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-05-27 21:03 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Nice, thanks.  I'll take this one then; hopefully I'll get to it soon.

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

* [Bug c++/94490] Ternary expression with 3 consts is “not” a constant expression
  2020-04-05  2:18 [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression bisqwit at iki dot fi
                   ` (3 preceding siblings ...)
  2021-05-27 21:03 ` mpolacek at gcc dot gnu.org
@ 2021-12-01 17:23 ` mpolacek at gcc dot gnu.org
  2021-12-02 13:21 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-12-01 17:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Patch finally posted:
https://gcc.gnu.org/pipermail/gcc-patches/2021-December/585932.html

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

* [Bug c++/94490] Ternary expression with 3 consts is “not” a constant expression
  2020-04-05  2:18 [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression bisqwit at iki dot fi
                   ` (4 preceding siblings ...)
  2021-12-01 17:23 ` mpolacek at gcc dot gnu.org
@ 2021-12-02 13:21 ` cvs-commit at gcc dot gnu.org
  2021-12-02 13:26 ` cvs-commit at gcc dot gnu.org
  2021-12-02 13:28 ` mpolacek at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-12-02 13:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:9af081003f9c19f33457e0ed1aa14a764f462c3c

commit r12-5712-g9af081003f9c19f33457e0ed1aa14a764f462c3c
Author: Marek Polacek <polacek@redhat.com>
Date:   Sat Nov 6 18:10:39 2021 -0400

    c++: Fix bogus error with __integer_pack [PR94490]

    Here we issue a bogus:

    error: '(0 ? fake_tuple_size_v<int> : fake_tuple_size_v<int>)' is not a
constant expression

    because cxx_constant_value in expand_integer_pack gets

    *(0 ? VIEW_CONVERT_EXPR<const int>(fake_tuple_size_v) :
VIEW_CONVERT_EXPR<const int>(fake_tuple_size_v))

    which is a REFERENCE_REF_P and we evaluate its operand to 3, so we end
    up with *3 and that fails.  Sounds like we need to get rid of the
    REFERENCE_REF_P then.  That is what tsubst_copy_and_build/INDIRECT_REF
    will do:

            if (REFERENCE_REF_P (t))
              {
                /* A type conversion to reference type will be enclosed in
                   such an indirect ref, but the substitution of the cast
                   will have also added such an indirect ref.  */
                r = convert_from_reference (r);
              }

    so I think it's reasonable to call instantiate_non_dependent_expr_sfinae.

            PR c++/94490

    gcc/cp/ChangeLog:

            * pt.c (expand_integer_pack): Call
            instantiate_non_dependent_expr_sfinae.

    gcc/testsuite/ChangeLog:

            * g++.dg/ext/integer-pack5.C: New test.

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

* [Bug c++/94490] Ternary expression with 3 consts is “not” a constant expression
  2020-04-05  2:18 [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression bisqwit at iki dot fi
                   ` (5 preceding siblings ...)
  2021-12-02 13:21 ` cvs-commit at gcc dot gnu.org
@ 2021-12-02 13:26 ` cvs-commit at gcc dot gnu.org
  2021-12-02 13:28 ` mpolacek at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-12-02 13:26 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Marek Polacek
<mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:5746f9199c2b8f0d679f13e3a48d6f8546a974f6

commit r11-9350-g5746f9199c2b8f0d679f13e3a48d6f8546a974f6
Author: Marek Polacek <polacek@redhat.com>
Date:   Sat Nov 6 18:10:39 2021 -0400

    c++: Fix bogus error with __integer_pack [PR94490]

    Here we issue a bogus:

    error: '(0 ? fake_tuple_size_v<int> : fake_tuple_size_v<int>)' is not a
constant expression

    because cxx_constant_value in expand_integer_pack gets

    *(0 ? VIEW_CONVERT_EXPR<const int>(fake_tuple_size_v) :
VIEW_CONVERT_EXPR<const int>(fake_tuple_size_v))

    which is a REFERENCE_REF_P and we evaluate its operand to 3, so we end
    up with *3 and that fails.  Sounds like we need to get rid of the
    REFERENCE_REF_P then.  That is what tsubst_copy_and_build/INDIRECT_REF
    will do:

            if (REFERENCE_REF_P (t))
              {
                /* A type conversion to reference type will be enclosed in
                   such an indirect ref, but the substitution of the cast
                   will have also added such an indirect ref.  */
                r = convert_from_reference (r);
              }

    so I think it's reasonable to call instantiate_non_dependent_expr_sfinae.

            PR c++/94490

    gcc/cp/ChangeLog:

            * pt.c (expand_integer_pack): Call
            instantiate_non_dependent_expr_sfinae.

    gcc/testsuite/ChangeLog:

            * g++.dg/ext/integer-pack5.C: New test.

    (cherry picked from commit 9af081003f9c19f33457e0ed1aa14a764f462c3c)

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

* [Bug c++/94490] Ternary expression with 3 consts is “not” a constant expression
  2020-04-05  2:18 [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression bisqwit at iki dot fi
                   ` (6 preceding siblings ...)
  2021-12-02 13:26 ` cvs-commit at gcc dot gnu.org
@ 2021-12-02 13:28 ` mpolacek at gcc dot gnu.org
  7 siblings, 0 replies; 9+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-12-02 13:28 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

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

--- Comment #8 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed in GCC 11.3 and 12.1.

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

end of thread, other threads:[~2021-12-02 13:28 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-05  2:18 [Bug c++/94490] New: Ternary expression with 3 consts is “not” a constant expression bisqwit at iki dot fi
2020-04-07 22:39 ` [Bug c++/94490] " mpolacek at gcc dot gnu.org
2020-04-08 13:43 ` ensadc at mailnesia dot com
2021-05-27 19:15 ` ppalka at gcc dot gnu.org
2021-05-27 21:03 ` mpolacek at gcc dot gnu.org
2021-12-01 17:23 ` mpolacek at gcc dot gnu.org
2021-12-02 13:21 ` cvs-commit at gcc dot gnu.org
2021-12-02 13:26 ` cvs-commit at gcc dot gnu.org
2021-12-02 13:28 ` mpolacek 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).