public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.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	[thread overview]
Message-ID: <bug-97518-4-FkBBZKze0T@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-97518-4@http.gcc.gnu.org/bugzilla/>

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.

  parent reply	other threads:[~2020-11-10 20:08 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-21 14:57 [Bug c++/97518] New: " 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 [this message]
2020-11-10 20:09 ` mpolacek at gcc dot gnu.org
2020-11-11 17:37 ` cvs-commit at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-97518-4-FkBBZKze0T@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).