From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 823DB385782E; Tue, 10 Nov 2020 20:08:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 823DB385782E From: "cvs-commit at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/97518] Improving static_assert diagnostics Date: Tue, 10 Nov 2020 20:08:56 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 10.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: cvs-commit at gcc dot gnu.org X-Bugzilla-Status: ASSIGNED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: mpolacek at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Nov 2020 20:08:56 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D97518 --- Comment #4 from CVS Commits --- The master branch has been updated by Marek Polacek : https://gcc.gnu.org/g:8c0c83feb04d7486ccf9cbe86dcd5668f0a21ef9 commit r11-4891-g8c0c83feb04d7486ccf9cbe86dcd5668f0a21ef9 Author: Marek Polacek 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::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::value' evaluates to false which I think is much better. However, always printing the condition t= hat 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 fals= e" 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::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.=