public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails
@ 2022-01-05 23:16 jengelh at inai dot de
  2022-01-05 23:19 ` [Bug libstdc++/103923] " pinskia at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: jengelh at inai dot de @ 2022-01-05 23:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103923
           Summary: is_invocable<const T &, ...> inexplicably fails
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jengelh at inai dot de
  Target Milestone: ---

Input:

#include <cstdio>
#include <typeinfo>
#include <type_traits>
#include <unordered_map>
struct T {
        struct K {
                bool operator==(const K &) const { return 0; }
                bool operator<(const K &) const { return 0; }
        };
        struct H {
                auto operator()(const K &) const { return 0; }
        };
        std::unordered_map<T::K, int, T::H> m;
};
int main()
{
        printf("%d\n", std::is_invocable_v<const T::H &, const T::K &>);
        printf("%d\n", std::is_invocable_v<T::H, const T::K &>);
        //T().m[T::K()]; // for extra fun
}

Output:

GNU C++17 (SUSE Linux) version 11.2.1 20211124 [revision
7510c23c1ec53aa4a62705f0384079661342ff7b] (x86_64-suse-linux)
        compiled by GNU C version 11.2.1 20211124 [revision
7510c23c1ec53aa4a62705f0384079661342ff7b], GMP version 6.2.1, MPFR version
4.1.0-p7, MPC version 1.2.1, isl version isl-0.24-GMP

0
1

Expected output:

1
1

Expectation based on it being possible to invoke on a const H &:
{ T::H h; const T::H &hh = h; hh(T::K()); }

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

* [Bug libstdc++/103923] is_invocable<const T &, ...> inexplicably fails
  2022-01-05 23:16 [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails jengelh at inai dot de
@ 2022-01-05 23:19 ` pinskia at gcc dot gnu.org
  2022-01-06  9:23 ` redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-05 23:19 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|c++                         |libstdc++

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This looks like a libstdc++ issue as if I use clang with libstdc++ I get:

0
1

But if I use LLVM's libc++ I get:

1
1

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

* [Bug libstdc++/103923] is_invocable<const T &, ...> inexplicably fails
  2022-01-05 23:16 [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails jengelh at inai dot de
  2022-01-05 23:19 ` [Bug libstdc++/103923] " pinskia at gcc dot gnu.org
@ 2022-01-06  9:23 ` redi at gcc dot gnu.org
  2022-01-06  9:31 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-06  9:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The instantiation of unordered_map<K, int, H> instantiates this alias:

  template<typename _Tp, typename _Hash>
    using __cache_default
      =  __not_<__and_<// Do not cache for fast hasher.
                       __is_fast_hash<_Hash>,
                       // Mandatory to have erase not throwing.
                       __is_nothrow_invocable<const _Hash&, const _Tp&>>>;

That causes the instantiations of is_invocable<const H&, const K&> before the
types are complete, which gives the wrong answer. Then the value of that trait
is memoized by the compiler, and remains false when printed out in main().

I think this is technically undefined behaviour, because the standard says that
it's undefined to instantiate std::unordered_map (and most templates in
namespace std) with incomplete types. A nested class is not complete until the
enclosing class is complete.

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

* [Bug libstdc++/103923] is_invocable<const T &, ...> inexplicably fails
  2022-01-05 23:16 [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails jengelh at inai dot de
  2022-01-05 23:19 ` [Bug libstdc++/103923] " pinskia at gcc dot gnu.org
  2022-01-06  9:23 ` redi at gcc dot gnu.org
@ 2022-01-06  9:31 ` redi at gcc dot gnu.org
  2022-01-06  9:37 ` redi at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-06  9:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
If I change the __cache_default trait to be:

  template<typename _Tp, typename _Hash>
    using __cache_default
      = __bool_constant<!(__is_fast_hash<_Hash>::value // Do not cache for fast
hasher.
           // Mandatory to have erase not throwing.
           && noexcept(std::declval<const _Hash&>()(std::declval<const
_Tp&>())))>;


Then you get an error from the instantiation of std::unordered_map, confirming
what I said above:

In file included from /home/jwakely/gcc/12/include/c++/12.0.0/unordered_map:46,
                 from 103923.C:4:
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h: In instantiation of
'class std::unordered_map<T::K, int, T::H>':
103923.C:13:45:   required from here
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
   50 |            && noexcept(std::declval<const _Hash&>()(std::declval<const
_Tp&>())))>;
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
In file included from /home/jwakely/gcc/12/include/c++/12.0.0/unordered_map:47,
                 from 103923.C:4:
/home/jwakely/gcc/12/include/c++/12.0.0/bits/unordered_map.h:874:42: error:
using invalid field 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::_M_h'
  874 |         find(const _Kt& __x) -> decltype(_M_h._M_find_tr(__x))
      |                                          ^~~~
/home/jwakely/gcc/12/include/c++/12.0.0/bits/unordered_map.h:885:48: error:
using invalid field 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::_M_h'
  885 |         find(const _Kt& __x) const -> decltype(_M_h._M_find_tr(__x))
      |                                                ^~~~
/home/jwakely/gcc/12/include/c++/12.0.0/bits/unordered_map.h:907:49: error:
using invalid field 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::_M_h'
  907 |         count(const _Kt& __x) const -> decltype(_M_h._M_count_tr(__x))
      |                                                 ^~~~
/home/jwakely/gcc/12/include/c++/12.0.0/bits/unordered_map.h:926:21: error:
using invalid field 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::_M_h'
  926 |         -> decltype(_M_h._M_find_tr(__x), void(), true)
      |                     ^~~~
In file included from /home/jwakely/gcc/12/include/c++/12.0.0/unordered_map:46,
                 from 103923.C:4:
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
   50 |            && noexcept(std::declval<const _Hash&>()(std::declval<const
_Tp&>())))>;
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~^~
In file included from /home/jwakely/gcc/12/include/c++/12.0.0/unordered_map:47,
                 from 103923.C:4:
/home/jwakely/gcc/12/include/c++/12.0.0/bits/unordered_map.h:948:21: error:
using invalid field 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::_M_h'
  948 |         -> decltype(_M_h._M_equal_range_tr(__x))
      |                     ^~~~
In file included from /home/jwakely/gcc/12/include/c++/12.0.0/unordered_map:46,
                 from 103923.C:4:
/home/jwakely/gcc/12/include/c++/12.0.0/bits/hashtable.h:50:50: error: use of
'auto T::H::operator()(const T::K&) const' before deduction of 'auto'
   50 |            && noexcept(std::declval<const _Hash&>()(std::declval<const
_Tp&>())))>;
      |                        ~~~~~~~~~~~~~~~~~~~~~~~~~~^~
In file included from /home/jwakely/gcc/12/include/c++/12.0.0/unordered_map:47,
                 from 103923.C:4:
/home/jwakely/gcc/12/include/c++/12.0.0/bits/unordered_map.h:960:21: error:
using invalid field 'std::unordered_map<_Key, _Tp, _Hash, _Pred, _Alloc>::_M_h'
  960 |         -> decltype(_M_h._M_equal_range_tr(__x))
      |                     ^~~~

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

* [Bug libstdc++/103923] is_invocable<const T &, ...> inexplicably fails
  2022-01-05 23:16 [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails jengelh at inai dot de
                   ` (2 preceding siblings ...)
  2022-01-06  9:31 ` redi at gcc dot gnu.org
@ 2022-01-06  9:37 ` redi at gcc dot gnu.org
  2022-01-06  9:39 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-06  9:37 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-01-06
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The problem is actually that the auto return type can't be deduced while the
enclosing class is incomplete, not that H is incomplete.

N.B. You can fix your code by simply changing the return type to not use auto:

        struct H {
                int operator()(const K &) const { return 0; }
        };

In any case, I think changing libstdc++ to make the code ill-formed would be an
improvement than silently compiling it but getting the traits wrong.

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

* [Bug libstdc++/103923] is_invocable<const T &, ...> inexplicably fails
  2022-01-05 23:16 [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails jengelh at inai dot de
                   ` (3 preceding siblings ...)
  2022-01-06  9:37 ` redi at gcc dot gnu.org
@ 2022-01-06  9:39 ` redi at gcc dot gnu.org
  2022-01-06  9:46 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-06  9:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Here's a minimal program showing the underlying problem:

template<typename T, bool x = noexcept(T{}())>
struct C
{
};

struct T {
  struct H {
    auto operator()() const { return 0; }
  };
  C<H> c;
};

T t;

103923.C:1:43: error: use of 'auto T::H::operator()() const' before deduction
of 'auto'
    1 | template<typename T, bool x = noexcept(T{}())>
      |                                        ~~~^~
103923.C:10:12: error: template argument 2 is invalid
   10 |         C<H> c;
      |            ^

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

* [Bug libstdc++/103923] is_invocable<const T &, ...> inexplicably fails
  2022-01-05 23:16 [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails jengelh at inai dot de
                   ` (4 preceding siblings ...)
  2022-01-06  9:39 ` redi at gcc dot gnu.org
@ 2022-01-06  9:46 ` redi at gcc dot gnu.org
  2022-01-06 14:30 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-06  9:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is another case of https://wg21.link/cwg2335

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

* [Bug libstdc++/103923] is_invocable<const T &, ...> inexplicably fails
  2022-01-05 23:16 [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails jengelh at inai dot de
                   ` (5 preceding siblings ...)
  2022-01-06  9:46 ` redi at gcc dot gnu.org
@ 2022-01-06 14:30 ` redi at gcc dot gnu.org
  2022-01-06 14:53 ` redi at gcc dot gnu.org
  2022-01-06 16:00 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-06 14:30 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

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

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Ugh, this is a big can of worms.

The current code is wrong, because __cache_default<Key, Hash> can silently give
the wrong answer, and cause ODR violations.

But if we change __cache_default to give the right answer, we break existing
tests:

FAIL: 23_containers/unordered_map/48101_neg.cc  (test for errors, line 27)
FAIL: 23_containers/unordered_set/85965.cc (test for excess errors)

We have tests that depend on NOT checking the hash function until all types are
complete. But that means it is impossible for __cache_default to do the right
thing, because it requires completeness.

The reason that __cache_default checks the hash function is to see whether it
throws or not. The code comments say:

                      // Mandatory to have erase not throwing.
                      __is_nothrow_invocable<const _Hash&, const _Tp&>>>;

and elsewhere:

      // Getting a bucket index from a node shall not throw because it is used
      // in methods (erase, swap...) that shall not throw. Need a complete
      // type to check this, so do it in the destructor not at class scope.
      static_assert(noexcept(declval<const __hash_code_base_access&>()
                        ._M_bucket_index(declval<const __node_value_type&>(),
                                         (std::size_t)0)),
                    "Cache the hash code or qualify your functors involved"
                    " in hash code and bucket index computation with
noexcept");

But this is nonsense. erase is not noexcept. [unord.req.except] specifically
says "erase(k) does not throw an exception unless that exception is thrown by
the container’s Hash or Pred object (if any)".

So why are we doing all this work (and causing bugs) to avoid throwing in
erase, when we are allowed to throw in erase?! swap isn't allowed to call a
throwing hash function, which seems to be a problem because we use
_M_update_bbegin() in swap, but maybe we can do that another way that doesn't
require calling _M_bucket_index.

I'm not sure how we can change the definition of __cache_default now, without
causing ABI breaks. But the current caching policy just seems wrong.

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

* [Bug libstdc++/103923] is_invocable<const T &, ...> inexplicably fails
  2022-01-05 23:16 [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails jengelh at inai dot de
                   ` (6 preceding siblings ...)
  2022-01-06 14:30 ` redi at gcc dot gnu.org
@ 2022-01-06 14:53 ` redi at gcc dot gnu.org
  2022-01-06 16:00 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-06 14:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I think libc++ just always caches the hash code, which keeps things much
simpler.

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

* [Bug libstdc++/103923] is_invocable<const T &, ...> inexplicably fails
  2022-01-05 23:16 [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails jengelh at inai dot de
                   ` (7 preceding siblings ...)
  2022-01-06 14:53 ` redi at gcc dot gnu.org
@ 2022-01-06 16:00 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2022-01-06 16:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Maybe we don't actually need to do anything here. As the original testcase
shows, trying to actually use the map is still ill-formed:

        //T().m[T::K()]; // for extra fun

I'm not going to work on this for now, because the code is undefined, it's just
that we don't give a clear diagnostic about what the problem is. There's a
simple workaround, which is to not use the 'auto' return type.

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

end of thread, other threads:[~2022-01-06 16:00 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-05 23:16 [Bug c++/103923] New: is_invocable<const T &, ...> inexplicably fails jengelh at inai dot de
2022-01-05 23:19 ` [Bug libstdc++/103923] " pinskia at gcc dot gnu.org
2022-01-06  9:23 ` redi at gcc dot gnu.org
2022-01-06  9:31 ` redi at gcc dot gnu.org
2022-01-06  9:37 ` redi at gcc dot gnu.org
2022-01-06  9:39 ` redi at gcc dot gnu.org
2022-01-06  9:46 ` redi at gcc dot gnu.org
2022-01-06 14:30 ` redi at gcc dot gnu.org
2022-01-06 14:53 ` redi at gcc dot gnu.org
2022-01-06 16:00 ` redi 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).