public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares
@ 2020-04-10 18:58 myriachan at gmail dot com
  2020-04-10 20:20 ` [Bug c++/94554] " mpolacek at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: myriachan at gmail dot com @ 2020-04-10 18:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554

            Bug ID: 94554
           Summary: spurious -Waddress warning within "if constexpr"
                    function-null compares
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: myriachan at gmail dot com
  Target Milestone: ---

The following with -std=c++17 -Waddress:

int meow() { return 1; }
void kitty(int);
template <int (*F)()>
void test() {
    if constexpr (F) {
        kitty(F());
    } else {
        kitty(2);
    }
}
template void test<nullptr>();
template void test<meow>();

gives a spurious/pointless warning:

<source>: In instantiation of 'void test() [with int (* F)() = meow]':
<source>:12:26:   required from here
<source>:5:5: warning: the address of 'int meow()' will never be NULL
[-Waddress]
    5 |     if constexpr (F) {
      |     ^~

The warning should be suppressed in "if constexpr" contexts, because of course
it's going to be always true or always false.

Clang errors on this case, so it's possible that my code is invalid: Is it
legal to compare a function pointer against null in a constant-expression?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/94554] spurious -Waddress warning within "if constexpr" function-null compares
  2020-04-10 18:58 [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares myriachan at gmail dot com
@ 2020-04-10 20:20 ` mpolacek at gcc dot gnu.org
  2020-04-10 20:22 ` myriachan at gmail dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-04-10 20:20 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
I don't think this warning should be suppressed in "if constexpr" contexts. 
What's the point of such code?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/94554] spurious -Waddress warning within "if constexpr" function-null compares
  2020-04-10 18:58 [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares myriachan at gmail dot com
  2020-04-10 20:20 ` [Bug c++/94554] " mpolacek at gcc dot gnu.org
@ 2020-04-10 20:22 ` myriachan at gmail dot com
  2020-04-14 21:56 ` daniel.kruegler at googlemail dot com
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: myriachan at gmail dot com @ 2020-04-10 20:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554

--- Comment #2 from Melissa <myriachan at gmail dot com> ---
Templates that take an optional function pointer as a template parameter.  It
lets you have templates that change behavior if a null function pointer is
passed.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/94554] spurious -Waddress warning within "if constexpr" function-null compares
  2020-04-10 18:58 [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares myriachan at gmail dot com
  2020-04-10 20:20 ` [Bug c++/94554] " mpolacek at gcc dot gnu.org
  2020-04-10 20:22 ` myriachan at gmail dot com
@ 2020-04-14 21:56 ` daniel.kruegler at googlemail dot com
  2020-07-10 15:13 ` lts-rudolph at gmx dot de
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2020-04-14 21:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554

--- Comment #3 from Daniel Krügler <daniel.kruegler at googlemail dot com> ---
(In reply to Melissa from comment #0)
> Clang errors on this case, so it's possible that my code is invalid: Is it
> legal to compare a function pointer against null in a constant-expression?

The example is ill-formed because the condition of 'if constexpr' is more
restricted than that of normal 'if': It expects "a contextually converted
constant expression of type bool" and [expr.const] p10 lists the allowed
conversions in this case. This list omits the boolean conversions
([conv.bool]).

But the example would become valid when rewritten as follows:

int meow() { return 1; }
void kitty(int);
template <int (*F)()>
void test() {
    if constexpr (bool(F)) {
        kitty(F());
    } else {
        kitty(2);
    }
}
template void test<nullptr>();
template void test<meow>();

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/94554] spurious -Waddress warning within "if constexpr" function-null compares
  2020-04-10 18:58 [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares myriachan at gmail dot com
                   ` (2 preceding siblings ...)
  2020-04-14 21:56 ` daniel.kruegler at googlemail dot com
@ 2020-07-10 15:13 ` lts-rudolph at gmx dot de
  2020-10-01 17:54 ` mail at 3v1n0 dot net
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: lts-rudolph at gmx dot de @ 2020-07-10 15:13 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554

Klaus Rudolph <lts-rudolph at gmx dot de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lts-rudolph at gmx dot de

--- Comment #4 from Klaus Rudolph <lts-rudolph at gmx dot de> ---
The code can be modified to be well formed if changed to:

if constexpr (F != nullptr )) {

g++ still emits the bogus warning in that case which is a bug I believe!

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/94554] spurious -Waddress warning within "if constexpr" function-null compares
  2020-04-10 18:58 [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares myriachan at gmail dot com
                   ` (3 preceding siblings ...)
  2020-07-10 15:13 ` lts-rudolph at gmx dot de
@ 2020-10-01 17:54 ` mail at 3v1n0 dot net
  2022-06-23  4:25 ` jason at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mail at 3v1n0 dot net @ 2020-10-01 17:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554

Marco Trevisan <mail at 3v1n0 dot net> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mail at 3v1n0 dot net

--- Comment #5 from Marco Trevisan <mail at 3v1n0 dot net> ---
I don't think g++ is totally wrong here,

Actually, if any, it should be stronger in making the error clearer and louder,
as per sé it's fine to consider that you're for real not comparing something
constant and the function address may not be known at compile time, so not
wrong to check.

However, something that will not error but work can be something like:

#include <iostream>
#include <type_traits>

using namespace std;

int meow() { return 1; }
void kitty(int i) { std::cout << "Mowed vlaue " << i << "\n"; }

template <int (*F)()>
void test() {
    using NullType = std::integral_constant<decltype(F), nullptr>;
    using ActualType = std::integral_constant<decltype(F), F>;

    if constexpr (!std::is_same_v<ActualType, NullType>) {
        kitty(F());
    } else {
        kitty(222222);
    }
}

int main()
{
    test<nullptr>();
    test<meow>();

    return 0;
}

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/94554] spurious -Waddress warning within "if constexpr" function-null compares
  2020-04-10 18:58 [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares myriachan at gmail dot com
                   ` (4 preceding siblings ...)
  2020-10-01 17:54 ` mail at 3v1n0 dot net
@ 2022-06-23  4:25 ` jason at gcc dot gnu.org
  2022-06-23  4:31 ` jason at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jason at gcc dot gnu.org @ 2022-06-23  4:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |jason at gcc dot gnu.org
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-06-23
             Status|UNCONFIRMED                 |ASSIGNED
                 CC|                            |jason at gcc dot gnu.org

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/94554] spurious -Waddress warning within "if constexpr" function-null compares
  2020-04-10 18:58 [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares myriachan at gmail dot com
                   ` (5 preceding siblings ...)
  2022-06-23  4:25 ` jason at gcc dot gnu.org
@ 2022-06-23  4:31 ` jason at gcc dot gnu.org
  2022-06-23 15:08 ` cvs-commit at gcc dot gnu.org
  2022-06-23 15:44 ` jason at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: jason at gcc dot gnu.org @ 2022-06-23  4:31 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554

--- Comment #6 from Jason Merrill <jason at gcc dot gnu.org> ---
(In reply to Daniel Krügler from comment #3)
> The example is ill-formed because the condition of 'if constexpr' is more
> restricted than that of normal 'if': It expects "a contextually converted
> constant expression of type bool" and [expr.const] p10 lists the allowed
> conversions in this case. This list omits the boolean conversions
> ([conv.bool]).

This was changed by wg21.link/p1401 so the conversion is allowed in this
context as well.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/94554] spurious -Waddress warning within "if constexpr" function-null compares
  2020-04-10 18:58 [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares myriachan at gmail dot com
                   ` (6 preceding siblings ...)
  2022-06-23  4:31 ` jason at gcc dot gnu.org
@ 2022-06-23 15:08 ` cvs-commit at gcc dot gnu.org
  2022-06-23 15:44 ` jason at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-06-23 15:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:124a9e08b7a83795bd4d09001955f0eef68ecd00

commit r13-1219-g124a9e08b7a83795bd4d09001955f0eef68ecd00
Author: Jason Merrill <jason@redhat.com>
Date:   Thu Jun 23 00:24:22 2022 -0400

    c++: -Waddress and if constexpr [PR94554]

    Like we avoid various warnings for seemingly tautological expressions when
    substituting a template, we should avoid warning for the implicit
conversion
    to bool in an if statement.  I considered also doing this for the
conditions
    in loop expressions, but that seems unnecessary, as a loop condition is
    unlikely to be a constant.

    The change to finish_if_stmt_cond isn't necessary since dependent_operand_p
    looks through IMPLICIT_CONV_EXPR, but makes it more constent with
    e.g. build_x_binary_op that determines the type of an expression and then
    builds it using the original operands.

            PR c++/94554

    gcc/cp/ChangeLog:

            * pt.cc (dependent_operand_p): Split out from...
            (tsubst_copy_and_build): ...here.
            (tsubst_expr) [IF_STMT]: Use it.
            * semantics.cc (finish_if_stmt_cond): Keep the pre-conversion
            condition in the template tree.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp1z/constexpr-if38.C: New test.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug c++/94554] spurious -Waddress warning within "if constexpr" function-null compares
  2020-04-10 18:58 [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares myriachan at gmail dot com
                   ` (7 preceding siblings ...)
  2022-06-23 15:08 ` cvs-commit at gcc dot gnu.org
@ 2022-06-23 15:44 ` jason at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: jason at gcc dot gnu.org @ 2022-06-23 15:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94554

Jason Merrill <jason at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |13.0
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #8 from Jason Merrill <jason at gcc dot gnu.org> ---
Fixed for GCC 13.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-06-23 15:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-10 18:58 [Bug c++/94554] New: spurious -Waddress warning within "if constexpr" function-null compares myriachan at gmail dot com
2020-04-10 20:20 ` [Bug c++/94554] " mpolacek at gcc dot gnu.org
2020-04-10 20:22 ` myriachan at gmail dot com
2020-04-14 21:56 ` daniel.kruegler at googlemail dot com
2020-07-10 15:13 ` lts-rudolph at gmx dot de
2020-10-01 17:54 ` mail at 3v1n0 dot net
2022-06-23  4:25 ` jason at gcc dot gnu.org
2022-06-23  4:31 ` jason at gcc dot gnu.org
2022-06-23 15:08 ` cvs-commit at gcc dot gnu.org
2022-06-23 15:44 ` jason at gcc dot gnu.org

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).