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 @@ -2570,6 +2570,19 @@ } 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 shapeShape{GetShape(context, *arguments[2])}) { + if (auto constShape{AsConstantShape(context, *shapeShape)}) { + if (constShape->At(ConstantSubscripts{1}).ToInt64() != fptrRank) { + context.messages().Say(arguments[2]->sourceLocation(), + "SHAPE= argument to C_F_POINTER() must have the same rank as 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()) + !ERROR: SHAPE= argument to C_F_POINTER() must have the same rank as FPTR= + call c_f_pointer(scalarC, multiDimIntF, shape=[1_8]) end program