Bug 97518 - Improving static_assert diagnostics
Summary: Improving static_assert diagnostics
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: diagnostic
Depends on:
Blocks:
 
Reported: 2020-10-21 14:57 UTC by Barry Revzin
Modified: 2020-11-11 17:37 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-10-21 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Barry Revzin 2020-10-21 14:57:27 UTC
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.
Comment 1 Marek Polacek 2020-10-21 15:01:12 UTC
Yes, I've wanted this too!  Maybe I'll find time to implement this in GCC 11; it should not be that difficult.
Comment 2 Jonathan Wakely 2020-10-21 15:30:17 UTC
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".
Comment 3 Marek Polacek 2020-11-06 19:29:02 UTC
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);
      |                  ~~~~~~~~~~^~~~
Comment 4 GCC Commits 2020-11-10 20:08:56 UTC
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.
Comment 5 Marek Polacek 2020-11-10 20:09:37 UTC
Improved in GCC 11.
Comment 6 GCC Commits 2020-11-11 17:37:23 UTC
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.