c++: Fix unary negation of nullptr [PR123087]
Checks
Commit Message
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(-)
@@ -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)
@@ -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;