public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111004] New: Visitor and concept error message
@ 2023-08-12 19:19 deco33000 at yandex dot com
  2023-08-12 19:28 ` [Bug c++/111004] " pinskia at gcc dot gnu.org
  2023-08-13  0:21 ` [Bug libstdc++/111004] " redi at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: deco33000 at yandex dot com @ 2023-08-12 19:19 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111004
           Summary: Visitor and concept error message
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: deco33000 at yandex dot com
  Target Milestone: ---

The error code for the following situation is not clear.

The error is that struct A and B don't have a "bool activated;" member.

The issue is the variant does not reflect what the error is in an helpful way.
To ease your life, here is the godbolt:
https://godbolt.org/z/Wdr4zn5E1

Do you think it is possible to improve the diagnostic?

---------------------------
Reduced test case:
#include <iostream>
#include <variant>
#include <vector>

using namespace std;

template <class T>
concept My_concept = requires(T a) {
    { a.activated } -> std::same_as<bool &>;
};

struct A {

    bool not_activated;
};
struct B {

    bool not_activated;
    int other;
};
auto test(variant<A, B> &v) -> void {

    std::visit([](My_concept auto &&arg) { std::cout << "OK\n"; }, v);
}

int main() {

    variant<A, B> v;

    v = A();

    test(v);

    return 0;
}
------------------------
Thanks

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

* [Bug c++/111004] Visitor and concept error message
  2023-08-12 19:19 [Bug c++/111004] New: Visitor and concept error message deco33000 at yandex dot com
@ 2023-08-12 19:28 ` pinskia at gcc dot gnu.org
  2023-08-13  0:21 ` [Bug libstdc++/111004] " redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-12 19:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Clang's error message is similarly "bad" with GCC's libstdc++:
```
<source>:24:5: error: no matching function for call to 'visit'
   24 |     std::visit([](My_concept auto &&arg) { std::cout << "OK\n"; }, v);
      |     ^~~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/variant:1867:5:
note: candidate template ignored: couldn't infer template argument '_Res'
 1867 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/14.0.0/../../../../include/c++/14.0.0/variant:1827:5:
note: candidate template ignored: substitution failure [with _Visitor = (lambda
at <source>:24:16), _Variants = <variant<A, B> &>]: no type named 'type' in
'std::invoke_result<(lambda at <source>:24:16), A &>'
 1827 |     visit(_Visitor&& __visitor, _Variants&&... __variants)
      |     ^
```

Now LLVM's libc++ produces something which might be helpful:
```
In file included from <source>:2:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/iostream:43:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/ios:222:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/__locale:21:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/mutex:192:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/__condition_variable/condition_variable.h:17:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/__mutex/unique_lock.h:17:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/__system_error/system_error.h:14:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/__system_error/error_category.h:15:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/string:622:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/string_view:1059:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/algorithm:1960:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/iterator:683:
In file included from
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/__iterator/common_iterator.h:31:
/opt/compiler-explorer/clang-trunk-20230812/bin/../include/c++/v1/variant:680:19:
error: static assertion failed due to requirement 'is_invocable_v<(lambda at
<source>:24:16), A &>': `std::visit` requires the visitor to be exhaustive.
  680 |     static_assert(is_invocable_v<_Visitor, _Values...>,
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
```
But still no mention of why

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

* [Bug libstdc++/111004] Visitor and concept error message
  2023-08-12 19:19 [Bug c++/111004] New: Visitor and concept error message deco33000 at yandex dot com
  2023-08-12 19:28 ` [Bug c++/111004] " pinskia at gcc dot gnu.org
@ 2023-08-13  0:21 ` redi at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: redi at gcc dot gnu.org @ 2023-08-13  0:21 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
          Component|c++                         |libstdc++
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2023-08-13

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The static assert message for libc++ does say why, but not in user friendly
language.

I think we could change libstdc++ to use decltype(auto) for the return type of
visit, and then use a nice static assert (with better message) to diagnose the
invalid cases. We currently constrain the function using SFINAE, which isn't
actually required by the standard, and makes it harder to give a good error
here.

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

end of thread, other threads:[~2023-08-13  0:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-12 19:19 [Bug c++/111004] New: Visitor and concept error message deco33000 at yandex dot com
2023-08-12 19:28 ` [Bug c++/111004] " pinskia at gcc dot gnu.org
2023-08-13  0:21 ` [Bug libstdc++/111004] " 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).