public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97518] New: Improving static_assert diagnostics
@ 2020-10-21 14:57 barry.revzin at gmail dot com
  2020-10-21 15:01 ` [Bug c++/97518] " mpolacek at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: barry.revzin at gmail dot com @ 2020-10-21 14:57 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97518
           Summary: Improving static_assert diagnostics
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: barry.revzin at gmail dot com
  Target Milestone: ---

Consider the following:

template <typename T, typename U> struct is_same { static constexpr bool value
= false; };
template <typename T> struct is_same<T, T> { static constexpr bool value =
true; };

template <typename T> using some_metafunction_t = T;

template <typename T>
void foo(T ) {
    using X = T*;
    using Y = some_metafunction_t<T>;

    static_assert(is_same<X, Y>::value);
}

void bar() {
    foo(0);
}

gcc emits:

<source>: In instantiation of 'void foo(T) [with T = int]':
<source>:15:10:   required from here
<source>:11:34: error: static assertion failed
   11 |     static_assert(is_same<X, Y>::value);
      |        

Notably, it does not tell me what either X or Y are. All I know is that they're
not the same. I get T, but the computation of X and Y could be fairly
complicated and T may not help (or even be relevant, necessarily). This ends up
being useless for me, to the point where I actually created my own verify_same
type such that verify_same<T, U> is only defined when T == U, and create a
variable like:

[[maybe_unused]] verify_same<T, U> _;

It would be a lot cooler if gcc could diagnose all the types and values that
were used in a static_assert condition. 

clang, for instance, gives me:

<source>:11:5: error: static_assert failed due to requirement 'is_same<int *,
int>::value'
    static_assert(is_same<X, Y>::value);
    ^             ~~~~~~~~~~~~~~~~~~~~
<source>:15:5: note: in instantiation of function template specialization
'foo<int>' requested here
    foo(0);
    ^

Which, while it doesn't tell me that X=int* and Y=int, at least clearly
illustrates both types, and is a much more useful error diagnostic.

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

* [Bug c++/97518] Improving static_assert diagnostics
  2020-10-21 14:57 [Bug c++/97518] New: Improving static_assert diagnostics barry.revzin at gmail dot com
@ 2020-10-21 15:01 ` mpolacek at gcc dot gnu.org
  2020-10-21 15:30 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-10-21 15:01 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org
           Keywords|                            |diagnostic
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2020-10-21

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Yes, I've wanted this too!  Maybe I'll find time to implement this in GCC 11;
it should not be that difficult.

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

* [Bug c++/97518] Improving static_assert diagnostics
  2020-10-21 14:57 [Bug c++/97518] New: Improving static_assert diagnostics barry.revzin at gmail dot com
  2020-10-21 15:01 ` [Bug c++/97518] " mpolacek at gcc dot gnu.org
@ 2020-10-21 15:30 ` redi at gcc dot gnu.org
  2020-11-06 19:29 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2020-10-21 15:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
A related case I encounter often is:

static_assert( sizeof(T) == 4 );

if the assertion fails I would like to know what the size is, rather than just
"not 4", so I have to add something like Barry's verify_same:

template<size_t> struct undefined;

undefined<sizeof(T)> _;

and read the diagnostics.

If static_assert was able to tell me it failed because 8 == 4 failed, that
would be awesome.

Concepts do help, the diagnostic for std::same_as<T, U> does show the types.
But writing std::same_as<std::integral_constant<std::size_t, sizeof(T)>,
std::integral_constant<std::size_t, 4>> as the assertion just to get sizeof(T)
in the diagnostics is "suboptimal".

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

* [Bug c++/97518] Improving static_assert diagnostics
  2020-10-21 14:57 [Bug c++/97518] New: Improving static_assert diagnostics barry.revzin at gmail dot com
  2020-10-21 15:01 ` [Bug c++/97518] " mpolacek at gcc dot gnu.org
  2020-10-21 15:30 ` redi at gcc dot gnu.org
@ 2020-11-06 19:29 ` mpolacek at gcc dot gnu.org
  2020-11-10 20:08 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-11-06 19:29 UTC (permalink / raw)
  To: gcc-bugs

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

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 #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I have a trivial patch that improves

template<typename T>
void foo()
{
  static_assert (sizeof(T) == 4);
}

void
g ()
{
  foo<char>();
}

from

97518-2.C: In instantiation of ‘void foo() [with T = char]’:
97518-2.C:10:13:   required from here
97518-2.C:4:28: error: static assertion failed
    4 |   static_assert (sizeof(T) == 4);
      |                  ~~~~~~~~~~^~~~

to

97518-2.C: In instantiation of ‘void foo() [with T = char]’:
97518-2.C:10:13:   required from here
97518-2.C:4:28: error: static assertion failed due to requirement ‘(sizeof
(char) == 4)’
    4 |   static_assert (sizeof(T) == 4);
      |                  ~~~~~~~~~~^~~~

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

* [Bug c++/97518] Improving static_assert diagnostics
  2020-10-21 14:57 [Bug c++/97518] New: Improving static_assert diagnostics barry.revzin at gmail dot com
                   ` (2 preceding siblings ...)
  2020-11-06 19:29 ` mpolacek at gcc dot gnu.org
@ 2020-11-10 20:08 ` cvs-commit at gcc dot gnu.org
  2020-11-10 20:09 ` mpolacek at gcc dot gnu.org
  2020-11-11 17:37 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-11-10 20:08 UTC (permalink / raw)
  To: gcc-bugs

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

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

https://gcc.gnu.org/g:8c0c83feb04d7486ccf9cbe86dcd5668f0a21ef9

commit r11-4891-g8c0c83feb04d7486ccf9cbe86dcd5668f0a21ef9
Author: Marek Polacek <polacek@redhat.com>
Date:   Fri Nov 6 15:21:13 2020 -0500

    c++: Improve static_assert diagnostic [PR97518]

    Currently, when a static_assert fails, we only say "static assertion
failed".
    It would be more useful if we could also print the expression that
    evaluated to false; this is especially useful when the condition uses
    template parameters.  Consider the motivating example, in which we have
    this line:

      static_assert(is_same<X, Y>::value);

    if this fails, the user has to play dirty games to get the compiler to
    print the template arguments.  With this patch, we say:

      error: static assertion failed
      note: 'is_same<int*, int>::value' evaluates to false

    which I think is much better.  However, always printing the condition that
    evaluated to 'false' wouldn't be very useful: e.g. noexcept(fn) is
    always parsed to true/false, so we would say "'false' evaluates to false"
    which doesn't help.  So I wound up only printing the condition when it was
    instantiation-dependent, that is, we called finish_static_assert from
    tsubst_expr.

    Moreover, this patch also improves the diagnostic when the condition
    consists of a logical AND.  Say you have something like this:

      static_assert(fn1() && fn2() && fn3() && fn4() && fn5());

    where fn4() evaluates to false and the other ones to true.  Highlighting
    the whole thing is not that helpful because it won't say which clause
    evaluated to false.  With the find_failing_clause tweak in this patch
    we emit:

      error: static assertion failed
        6 | static_assert(fn1() && fn2() && fn3() && fn4() && fn5());
          |                                          ~~~^~

    so you know right away what's going on.  Unfortunately, when you combine
    both things, that is, have an instantiation-dependent expr and && in
    a static_assert, we can't yet quite point to the clause that failed.  It
    is because when we tsubstitute something like is_same<X, Y>::value, we
    generate a VAR_DECL that doesn't have any location.  It would be awesome
    if we could wrap it with a location wrapper, but I didn't see anything
    obvious.

    In passing, I've cleaned up some things:
    * use iloc_sentinel when appropriate,
    * it's nicer to call contextual_conv_bool instead of the rather verbose
      perform_implicit_conversion_flags,
    * no need to check for INTEGER_CST before calling integer_zerop.

    gcc/cp/ChangeLog:

            PR c++/97518
            * cp-tree.h (finish_static_assert): Adjust declaration.
            * parser.c (cp_parser_static_assert): Pass false to
            finish_static_assert.
            * pt.c (tsubst_expr): Pass true to finish_static_assert.
            * semantics.c (find_failing_clause_r): New function.
            (find_failing_clause): New function.
            (finish_static_assert): Add a bool parameter.  Use
            iloc_sentinel.  Call contextual_conv_bool instead of
            perform_implicit_conversion_flags.  Don't check for INTEGER_CST
before
            calling integer_zerop.  Call find_failing_clause and maybe use its
            location.  Print the original condition or the failing clause if
            SHOW_EXPR_P.

    gcc/testsuite/ChangeLog:

            PR c++/97518
            * g++.dg/diagnostic/pr87386.C: Adjust expected output.
            * g++.dg/diagnostic/static_assert1.C: New test.
            * g++.dg/diagnostic/static_assert2.C: New test.

    libcc1/ChangeLog:

            PR c++/97518
            * libcp1plugin.cc (plugin_add_static_assert): Pass false to
            finish_static_assert.

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

* [Bug c++/97518] Improving static_assert diagnostics
  2020-10-21 14:57 [Bug c++/97518] New: Improving static_assert diagnostics barry.revzin at gmail dot com
                   ` (3 preceding siblings ...)
  2020-11-10 20:08 ` cvs-commit at gcc dot gnu.org
@ 2020-11-10 20:09 ` mpolacek at gcc dot gnu.org
  2020-11-11 17:37 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-11-10 20:09 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Improved in GCC 11.

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

* [Bug c++/97518] Improving static_assert diagnostics
  2020-10-21 14:57 [Bug c++/97518] New: Improving static_assert diagnostics barry.revzin at gmail dot com
                   ` (4 preceding siblings ...)
  2020-11-10 20:09 ` mpolacek at gcc dot gnu.org
@ 2020-11-11 17:37 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-11-11 17:37 UTC (permalink / raw)
  To: gcc-bugs

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

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

https://gcc.gnu.org/g:d6e5745a9a88314e27f387b2277299076862af67

commit r11-4924-gd6e5745a9a88314e27f387b2277299076862af67
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Nov 10 16:39:19 2020 -0500

    c++: Tweak tsubst_qualified_id location.

    Retain the location when tsubstituting a qualified-id so that our
    static_assert diagnostic can benefit.  Don't create useless location
    wrappers for temporary variables.

    gcc/ChangeLog:

            PR c++/97518
            * tree.c (maybe_wrap_with_location): Don't add a location
            wrapper around an artificial and ignored decl.

    gcc/cp/ChangeLog:

            PR c++/97518
            * pt.c (tsubst_qualified_id): Use EXPR_LOCATION of the
qualified-id.
            Use it to maybe_wrap_with_location the final expression.

    gcc/testsuite/ChangeLog:

            PR c++/97518
            * g++.dg/diagnostic/static_assert3.C: New test.

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

end of thread, other threads:[~2020-11-11 17:37 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-21 14:57 [Bug c++/97518] New: Improving static_assert diagnostics barry.revzin at gmail dot com
2020-10-21 15:01 ` [Bug c++/97518] " mpolacek at gcc dot gnu.org
2020-10-21 15:30 ` redi at gcc dot gnu.org
2020-11-06 19:29 ` mpolacek at gcc dot gnu.org
2020-11-10 20:08 ` cvs-commit at gcc dot gnu.org
2020-11-10 20:09 ` mpolacek at gcc dot gnu.org
2020-11-11 17:37 ` cvs-commit 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).