gcc/libcc1
Marek Polacek 8c0c83feb0 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.
2020-11-10 15:08:06 -05:00
..
ChangeLog Daily bump. 2020-11-07 00:16:39 +00:00
Makefile.am libcc1: Enable Intel CET on Intel CET enabled host 2020-05-12 09:12:37 -07:00
Makefile.in libcc1: Enable Intel CET on Intel CET enabled host 2020-05-12 09:12:37 -07:00
aclocal.m4 libcc1: Enable Intel CET on Intel CET enabled host 2020-05-12 09:12:37 -07:00
callbacks.cc Update copyright years. 2020-01-01 12:51:42 +01:00
callbacks.hh Update copyright years. 2020-01-01 12:51:42 +01:00
cc1plugin-config.h.in
compiler-name.hh Update copyright years. 2020-01-01 12:51:42 +01:00
configure Require CET support only for the final GCC build 2020-07-30 05:36:24 -07:00
configure.ac libcc1: Enable Intel CET on Intel CET enabled host 2020-05-12 09:12:37 -07:00
connection.cc Update copyright years. 2020-01-01 12:51:42 +01:00
connection.hh Update copyright years. 2020-01-01 12:51:42 +01:00
findcomp.cc Update copyright years. 2020-01-01 12:51:42 +01:00
findcomp.hh Update copyright years. 2020-01-01 12:51:42 +01:00
libcc1.cc Update copyright years. 2020-01-01 12:51:42 +01:00
libcc1.sym
libcc1plugin.cc core: Rename DECL_IS_BUILTIN -> DECL_IS_UNDECLARED_BUILTIN 2020-11-06 10:50:51 -08:00
libcc1plugin.sym
libcp1.cc Update copyright years. 2020-01-01 12:51:42 +01:00
libcp1plugin.cc c++: Improve static_assert diagnostic [PR97518] 2020-11-10 15:08:06 -05:00
libcp1plugin.sym
marshall-c.hh Update copyright years. 2020-01-01 12:51:42 +01:00
marshall-cp.hh Update copyright years. 2020-01-01 12:51:42 +01:00
marshall.cc Update copyright years. 2020-01-01 12:51:42 +01:00
marshall.hh Update copyright years. 2020-01-01 12:51:42 +01:00
names.cc Update copyright years. 2020-01-01 12:51:42 +01:00
names.hh Update copyright years. 2020-01-01 12:51:42 +01:00
rpc.hh Update copyright years. 2020-01-01 12:51:42 +01:00
status.hh Update copyright years. 2020-01-01 12:51:42 +01:00