https://gcc.gnu.org/g:430d39d890d1828b33781724b76eef421885deb5

commit r16-8478-g430d39d890d1828b33781724b76eef421885deb5
Author: Eczbek <[email protected]>
Date:   Mon Apr 6 11:15:02 2026 -0400

    c++: Fix unary negation of nullptr [PR123087]
    
    [expr.unary.op] paragraph 9 says that the operand is
    contextually converted to bool, so this should be legal.
    
            PR c++/123087
    
    gcc/cp/ChangeLog:
    
            * typeck.cc (cp_build_unary_op): For TRUTH_NOT_EXPR, replace
            call to perform_implicit_conversion with call to
            contextual_conv_bool.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/DRs/dr1423.C: Additonal test.
    
    Reviewed-by: Jason Merrill <[email protected]>

Diff:
---
 gcc/cp/typeck.cc                  | 3 +--
 gcc/testsuite/g++.dg/DRs/dr1423.C | 1 +
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index 79eb3b5ba286..e019d6e22a96 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -7854,8 +7854,7 @@ cp_build_unary_op (enum tree_code code, tree xarg, bool 
noconvert,
       if (gnu_vector_type_p (TREE_TYPE (arg)))
        return cp_build_binary_op (input_location, EQ_EXPR, arg,
                                   build_zero_cst (TREE_TYPE (arg)), complain);
-      arg = perform_implicit_conversion (boolean_type_node, arg,
-                                        complain);
+      arg = contextual_conv_bool (arg, complain);
       if (arg != error_mark_node)
        {
          if (processing_template_decl)
diff --git a/gcc/testsuite/g++.dg/DRs/dr1423.C 
b/gcc/testsuite/g++.dg/DRs/dr1423.C
index d82baae9985e..75bc5e0bc99c 100644
--- a/gcc/testsuite/g++.dg/DRs/dr1423.C
+++ b/gcc/testsuite/g++.dg/DRs/dr1423.C
@@ -5,3 +5,4 @@ bool b = nullptr; // { dg-error "converting to .bool. from 
.std::nullptr_t. requ
 bool b2(nullptr);
 bool b3{nullptr};
 bool b4 = { nullptr }; // { dg-error "converting to .bool. from 
.std::nullptr_t. requires direct-initialization" }
+bool b5 = !nullptr;

Reply via email to