]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fold-const: Fix implicit cast to nullptr_t typedef [PR123790]
authorJakub Jelinek <jakub@redhat.com>
Fri, 30 Jan 2026 10:40:17 +0000 (11:40 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Fri, 30 Jan 2026 10:40:17 +0000 (11:40 +0100)
The following testcase is incorrectly rejected, because since r15-6744
it adds build_nop to preserve the exact typedef type and
cxx_eval_constant_expression can't fold NOP_EXPR from integer_zerop of one
NULLPTR_TYPE to another NULLPTR_TYPE, while cxx_eval_constant_expression
relies on fold to do such folding.

I see 3 options to fix that, one is deal with this in the r15-6744 spot
and special case NULLPTR_TYPE there and build_zero_cst instead of build_nop
(with similar condition like in the patch below), another possibility is
special case this in cxx_eval_constant_expression, and
another one (done in this patch) is to handle this in fold-const.cc -
fold_convert_loc and also in fold_convert_const.

2026-01-30  Jakub Jelinek  <jakub@redhat.com>

PR c++/123790
* fold-const.cc (fold_convert_const): Handle conversion of
integer_zerop to NULLPTR_TYPE.
(fold_convert_loc): Likewise.

* g++.dg/cpp0x/nullptr47.C: New test.

gcc/fold-const.cc
gcc/testsuite/g++.dg/cpp0x/nullptr47.C [new file with mode: 0644]

index 47cd0053d31a2210939f31ec8f9f63c97303b3d3..82d8ef5092e827c7d349db3bd77b20c321cde02c 100644 (file)
@@ -2566,6 +2566,8 @@ fold_convert_const (enum tree_code code, tree type, tree arg1)
          return v.build ();
        }
     }
+  else if (TREE_CODE (type) == NULLPTR_TYPE && integer_zerop (arg1))
+    return build_zero_cst (type);
   return NULL_TREE;
 }
 
@@ -2788,6 +2790,10 @@ fold_convert_loc (location_t loc, tree type, tree arg)
       tem = fold_ignored_result (arg);
       return fold_build1_loc (loc, NOP_EXPR, type, tem);
 
+    case NULLPTR_TYPE:
+      if (integer_zerop (arg))
+       return build_zero_cst (type);
+      /* FALLTHRU */
     default:
       if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
        return fold_build1_loc (loc, NOP_EXPR, type, arg);
diff --git a/gcc/testsuite/g++.dg/cpp0x/nullptr47.C b/gcc/testsuite/g++.dg/cpp0x/nullptr47.C
new file mode 100644 (file)
index 0000000..4c4071b
--- /dev/null
@@ -0,0 +1,10 @@
+// PR c++/123790
+// { dg-do compile { target c++11 } }
+
+int
+main ()
+{
+  using nullptr_t = decltype (nullptr);
+  constexpr nullptr_t zero = nullptr;
+  constexpr nullptr_t other_zero = zero;
+}