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 @@ -2572,6 +2572,17 @@ } 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) { + 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,7 +8,7 @@ integer, pointer :: p end type type(with_pointer) :: coindexed[*] - integer, pointer :: scalarIntF, arrayIntF(:) + integer, pointer :: scalarIntF, arrayIntF(:), multiDimIntF(:,:) character(len=:), pointer :: charDeferredF integer :: j call c_f_pointer(scalarC, scalarIntF) ! ok @@ -31,4 +31,6 @@ 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()) + !SHAPE= argument to C_F_POINTER() must have size equal to the rank of FPTR= + call c_f_pointer(scalarC, multiDimIntF, shape=[1_8]) end program