public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106143] New: Add fix-it for missing ::value on trait with std::integral_constant base
@ 2022-06-30 8:31 redi at gcc dot gnu.org
2022-06-30 8:36 ` [Bug c++/106143] " jakub at gcc dot gnu.org
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2022-06-30 8:31 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106143
Bug ID: 106143
Summary: Add fix-it for missing ::value on trait with
std::integral_constant base
Product: gcc
Version: unknown
Status: UNCONFIRMED
Keywords: diagnostic
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: redi at gcc dot gnu.org
Target Milestone: ---
#include <type_traits>
static_assert( std::is_void<void>, "void is void is void" );
constexpr bool b = std::is_void<void>;
v.C:2:34: error: expected primary-expression before ',' token
2 | static_assert( std::is_void<void>, "void is void is void" );
| ^
v.C:3:38: error: expected primary-expression before ';' token
3 | constexpr bool b = std::is_void<void>;
| ^
I do this all the time: forget to take the ::value of the trait and just use
the type itself. The diagnostic could be improved for this case, by detecting
that the type has a base that is a specialization of std::integral_constant and
suggesting adding ::value as a fix-it:
v.C:2:34: error: expected primary-expression before ',' token
2 | static_assert( std::is_void<void>, "void is void is void" );
| ^
| ::value
v.C:3:38: error: expected primary-expression before ';' token
3 | constexpr bool b = std::is_void<void>;
| ^
| ::value
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/106143] Add fix-it for missing ::value on trait with std::integral_constant base
2022-06-30 8:31 [Bug c++/106143] New: Add fix-it for missing ::value on trait with std::integral_constant base redi at gcc dot gnu.org
@ 2022-06-30 8:36 ` jakub at gcc dot gnu.org
2022-06-30 9:07 ` redi at gcc dot gnu.org
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-30 8:36 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106143
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org
--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
But the other suggestion could be to use std::is_void_v<void> instead (at least
for C++17 and later), no?
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/106143] Add fix-it for missing ::value on trait with std::integral_constant base
2022-06-30 8:31 [Bug c++/106143] New: Add fix-it for missing ::value on trait with std::integral_constant base redi at gcc dot gnu.org
2022-06-30 8:36 ` [Bug c++/106143] " jakub at gcc dot gnu.org
@ 2022-06-30 9:07 ` redi at gcc dot gnu.org
2022-06-30 9:08 ` redi at gcc dot gnu.org
2023-05-18 16:45 ` pinskia at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2022-06-30 9:07 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106143
--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Clang gives a different error, but no more helpful:
v.C:2:34: error: expected '(' for function-style cast or type construction
static_assert( std::is_void<void>, "void is void is void" );
~~~~~~~~~~~~~~~~~~^
v.C:3:38: error: expected '(' for function-style cast or type construction
constexpr bool b = std::is_void<void>;
~~~~~~~~~~~~~~~~~~^
2 errors generated.
This is pointing out that std::is_void<void>() would fix it, which is true. And
it's maybe more helpful than just saying "expected primary-expression" (which
means nothing to most users). But for the specific case of type traits we can
do better: using ::value to access the static member is more efficient to
compile than constructing a temporary and then finding its implicit conversion
to bool. So we should suggest ::value and not suggest ()
Maybe for C++17 we could also recognise when the derived trait type is in
namespace std and suggest the std::is_void_v variable template instead, on the
assumption that every std:: trait has a corresponding variable template now.
Ville suggests that instead of checking for a std::integral_constant base we
could just check if there's a nested ::value that is convertible to bool. That
would make it also work for boost-style traits that use an enumerator instead
of a static member, e.g.
struct is_foo { enum value = true; }; };
So maybe if we're using C++17, and it's a type defined in namespace std that
has a base class of std::integral_constant<bool, B>, then suggest is_foo_v<T>.
Otherwise, if is_foo<T>::value exists and is convertible to bool, then suggest
is_foo<T>::value.
Otherwise, either print the current diagnostic, or maybe something more
user-friendly that says "you used a type where a value is needed", like EDG
does:
"v.C", line 2: error: type name is not allowed
static_assert( std::is_void<void>, "void is void is void" );
^
"v.C", line 3: error: type name is not allowed
constexpr bool b = std::is_void<void>;
^
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/106143] Add fix-it for missing ::value on trait with std::integral_constant base
2022-06-30 8:31 [Bug c++/106143] New: Add fix-it for missing ::value on trait with std::integral_constant base redi at gcc dot gnu.org
2022-06-30 8:36 ` [Bug c++/106143] " jakub at gcc dot gnu.org
2022-06-30 9:07 ` redi at gcc dot gnu.org
@ 2022-06-30 9:08 ` redi at gcc dot gnu.org
2023-05-18 16:45 ` pinskia at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: redi at gcc dot gnu.org @ 2022-06-30 9:08 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106143
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #1)
> But the other suggestion could be to use std::is_void_v<void> instead (at
> least for C++17 and later), no?
Indeed :-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* [Bug c++/106143] Add fix-it for missing ::value on trait with std::integral_constant base
2022-06-30 8:31 [Bug c++/106143] New: Add fix-it for missing ::value on trait with std::integral_constant base redi at gcc dot gnu.org
` (2 preceding siblings ...)
2022-06-30 9:08 ` redi at gcc dot gnu.org
@ 2023-05-18 16:45 ` pinskia at gcc dot gnu.org
3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-18 16:45 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=106143
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Severity|normal |enhancement
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-05-18 16:45 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-30 8:31 [Bug c++/106143] New: Add fix-it for missing ::value on trait with std::integral_constant base redi at gcc dot gnu.org
2022-06-30 8:36 ` [Bug c++/106143] " jakub at gcc dot gnu.org
2022-06-30 9:07 ` redi at gcc dot gnu.org
2022-06-30 9:08 ` redi at gcc dot gnu.org
2023-05-18 16:45 ` pinskia 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).