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 <mpola...@gcc.gnu.org>:

https://gcc.gnu.org/g:8c0c83feb04d7486ccf9cbe86dcd5668f0a21ef9

commit r11-4891-g8c0c83feb04d7486ccf9cbe86dcd5668f0a21ef9
Author: Marek Polacek <pola...@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.

Reply via email to