Summary: | Improving static_assert diagnostics | ||
---|---|---|---|
Product: | gcc | Reporter: | Barry Revzin <barry.revzin> |
Component: | c++ | Assignee: | Marek Polacek <mpolacek> |
Status: | RESOLVED FIXED | ||
Severity: | normal | CC: | mpolacek, webrown.cpp |
Priority: | P3 | Keywords: | diagnostic |
Version: | 10.0 | ||
Target Milestone: | --- | ||
Host: | Target: | ||
Build: | Known to work: | ||
Known to fail: | Last reconfirmed: | 2020-10-21 00:00:00 |
Description
Barry Revzin
2020-10-21 14:57:27 UTC
Yes, I've wanted this too! Maybe I'll find time to implement this in GCC 11; it should not be that difficult. 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". 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); | ~~~~~~~~~~^~~~ 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. Improved in GCC 11. 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. |