Bug 94490 - Ternary expression with 3 consts is “not” a constant expression
Summary: Ternary expression with 3 consts is “not” a constant expression
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 10.0
: P3 normal
Target Milestone: ---
Assignee: Marek Polacek
URL:
Keywords: rejects-valid
Depends on: 94489
Blocks:
  Show dependency treegraph
 
Reported: 2020-04-05 02:18 UTC by Joel Yliluoma
Modified: 2023-11-16 16:04 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-08-04 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joel Yliluoma 2020-04-05 02:18:20 UTC
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.
Comment 1 Marek Polacek 2020-04-07 22:39:03 UTC
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.
Comment 2 ensadc 2020-04-08 13:43:03 UTC
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);
```
Comment 3 Patrick Palka 2021-05-27 19:15:36 UTC
Marek, it looks like your https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94489#c2 patch also fixes this PR.
Comment 4 Marek Polacek 2021-05-27 21:03:38 UTC
Nice, thanks.  I'll take this one then; hopefully I'll get to it soon.
Comment 5 Marek Polacek 2021-12-01 17:23:03 UTC
Patch finally posted: https://gcc.gnu.org/pipermail/gcc-patches/2021-December/585932.html
Comment 6 GCC Commits 2021-12-02 13:21:54 UTC
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.
Comment 7 GCC Commits 2021-12-02 13:26:40 UTC
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)
Comment 8 Marek Polacek 2021-12-02 13:28:07 UTC
Fixed in GCC 11.3 and 12.1.