]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/c/c-convert.cc
c: Allow conversions of null pointer constants to nullptr_t
[thirdparty/gcc.git] / gcc / c / c-convert.cc
index dccd245dfc3c15f1a382ad60fd8187dd96e8c46e..0f35dc4fe9a93bc14a5ba368c80de8f8e7e8d837 100644 (file)
@@ -157,6 +157,19 @@ c_convert (tree type, tree expr, bool init_const)
       ret = convert_to_pointer (type, e);
       goto maybe_fold;
 
+    case NULLPTR_TYPE:
+      /* A null pointer constant or value of type nullptr_t may be
+        converted to nullptr_t.  The latter case has already been
+        handled.  build_c_cast will create an additional NOP_EXPR to
+        ensure the result of the conversion is not itself a null
+        pointer constant.  */
+      if (null_pointer_constant_p (expr))
+       {
+         ret = build_int_cst (type, 0);
+         goto maybe_fold;
+       }
+      break;
+
     case REAL_TYPE:
       ret = convert_to_real (type, e);
       goto maybe_fold;
@@ -201,12 +214,14 @@ c_convert (tree type, tree expr, bool init_const)
     }
 
   /* If we are converting to nullptr_t, don't say "non-scalar type" because
-     the nullptr_t type is a scalar type.  Only nullptr_t shall be converted
-     to nullptr_t.  */
+     the nullptr_t type is a scalar type.  Only nullptr_t or a null pointer
+     constant shall be converted to nullptr_t.  */
   if (code == NULLPTR_TYPE)
     {
       error ("conversion from %qT to %qT", TREE_TYPE (e), type);
-      inform (input_location, "only %qT can be converted to %qT", type, type);
+      inform (input_location,
+             "only %qT or a null pointer constant can be converted to %qT",
+             type, type);
     }
   else
     error ("conversion to non-scalar type requested");