diff --git a/flang/lib/Evaluate/intrinsics.cpp b/flang/lib/Evaluate/intrinsics.cpp --- a/flang/lib/Evaluate/intrinsics.cpp +++ b/flang/lib/Evaluate/intrinsics.cpp @@ -2616,6 +2616,20 @@ } else if (!arguments[2] && fptrRank > 0) { context.messages().Say( "SHAPE= argument to C_F_POINTER() must appear when FPTR= is an array"_err_en_US); + } else if (arguments[2]) { + if (const auto *argExpr{arguments[2].value().UnwrapExpr()}) { + if (argExpr->Rank() > 1) { + context.messages().Say(arguments[2]->sourceLocation(), + "SHAPE= argument to C_F_POINTER() must be a rank-one array."_err_en_US); + } else if (argExpr->Rank() == 1) { + if (auto constShape{GetConstantShape(context, *argExpr)}) { + if (constShape->At(ConstantSubscripts{1}).ToInt64() != fptrRank) { + context.messages().Say(arguments[2]->sourceLocation(), + "SHAPE= argument to C_F_POINTER() must have size equal to the rank of FPTR="_err_en_US); + } + } + } + } } } } diff --git a/flang/test/Semantics/c_f_pointer.f90 b/flang/test/Semantics/c_f_pointer.f90 --- a/flang/test/Semantics/c_f_pointer.f90 +++ b/flang/test/Semantics/c_f_pointer.f90 @@ -8,9 +8,11 @@ integer, pointer :: p end type type(with_pointer) :: coindexed[*] - integer, pointer :: scalarIntF, arrayIntF(:) + integer, pointer :: scalarIntF, arrayIntF(:), multiDimIntF(:,:) character(len=:), pointer :: charDeferredF integer :: j + integer, dimension(2, 2) :: rankTwoArray + rankTwoArray = reshape([1, 2, 3, 4], shape(rankTwoArray)) call c_f_pointer(scalarC, scalarIntF) ! ok call c_f_pointer(scalarC, arrayIntF, [1_8]) ! ok call c_f_pointer(shape=[1_8], cptr=scalarC, fptr=arrayIntF) ! ok @@ -31,4 +33,8 @@ call c_f_pointer(scalarC, coindexed[0]%p) !ERROR: FPTR= argument to C_F_POINTER() must have a type call c_f_pointer(scalarC, null()) + !ERROR: SHAPE= argument to C_F_POINTER() must have size equal to the rank of FPTR= + call c_f_pointer(scalarC, multiDimIntF, shape=[1_8]) + !ERROR: SHAPE= argument to C_F_POINTER() must be a rank-one array. + call c_f_pointer(scalarC, multiDimIntF, shape=rankTwoArray) end program