@@ -3091,10 +3091,10 @@ void CastOperation::CheckCStyleCast() {
30913091 return ;
30923092 }
30933093
3094- // C23 6.5.4p4 :
3095- // The type nullptr_t shall not be converted to any type other than void,
3096- // bool, or a pointer type. No type other than nullptr_t shall be converted
3097- // to nullptr_t.
3094+ // C23 6.5.5p4 :
3095+ // ... The type nullptr_t shall not be converted to any type other than
3096+ // void, bool or a pointer type.If the target type is nullptr_t, the cast
3097+ // expression shall be a null pointer constant or have type nullptr_t.
30983098 if (SrcType->isNullPtrType ()) {
30993099 // FIXME: 6.3.2.4p2 says that nullptr_t can be converted to itself, but
31003100 // 6.5.4p4 is a constraint check and nullptr_t is not void, bool, or a
@@ -3115,11 +3115,20 @@ void CastOperation::CheckCStyleCast() {
31153115 Self.CurFPFeatureOverrides ());
31163116 }
31173117 }
3118+
31183119 if (DestType->isNullPtrType () && !SrcType->isNullPtrType ()) {
3119- Self.Diag (SrcExpr.get ()->getExprLoc (), diag::err_nullptr_cast)
3120- << /* type to nullptr*/ 1 << SrcType;
3121- SrcExpr = ExprError ();
3122- return ;
3120+ if (!SrcExpr.get ()->isNullPointerConstant (Self.Context ,
3121+ Expr::NPC_NeverValueDependent)) {
3122+ Self.Diag (SrcExpr.get ()->getExprLoc (), diag::err_nullptr_cast)
3123+ << /* type to nullptr*/ 1 << SrcType;
3124+ SrcExpr = ExprError ();
3125+ return ;
3126+ }
3127+ // Need to convert the source from whatever its type is to a null pointer
3128+ // type first.
3129+ SrcExpr = ImplicitCastExpr::Create (Self.Context , DestType, CK_NullToPointer,
3130+ SrcExpr.get (), nullptr , VK_PRValue,
3131+ Self.CurFPFeatureOverrides ());
31233132 }
31243133
31253134 if (DestType->isExtVectorType ()) {
0 commit comments