c++: Fix unary negation of nullptr [PR123087]

Message ID ff801c38-75fc-4876-8733-291a3cee33d2@gmail.com
State New
Headers
Series c++: Fix unary negation of nullptr [PR123087] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 fail Patch failed to apply
linaro-tcwg-bot/tcwg_gcc_build--master-arm fail Patch failed to apply

Commit Message

Eczbek April 4, 2026, 10:30 p.m. UTC
  From 2ace27c73fd513bc63f16d28bc7c13dfd33be552 Mon Sep 17 00:00:00 2001
From: Eczbek <eczbek.void@gmail.com>
Date: Sat, 21 Mar 2026 04:23:13 -0400
Subject: [PATCH] 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.
---
  gcc/cp/typeck.cc                  | 3 +--
  gcc/testsuite/g++.dg/DRs/dr1423.C | 1 +
  2 files changed, 2 insertions(+), 2 deletions(-)
  

Patch

diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index 79eb3b5ba28..e019d6e22a9 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 d82baae9985..75bc5e0bc99 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;