From b380c1486ee262c78726d0f3f3c769a7a4ed3dfe Mon Sep 17 00:00:00 2001 From: Eugene Epshteyn Date: Sat, 27 Sep 2025 23:33:30 -0400 Subject: [PATCH 1/5] [flang] Implemented a warning about contiguity of compile time constant values Implemented common::UsageWarning::ConstantIsContiguous to warn about the following case: ``` integer, parameter :: num = 3 integer, parameter :: arr(num)=[(i, i=1,num)] logical, parameter :: result=is_contiguous(arr(num:1:-1)) end ``` Here, while array section is discontiguous, `arr` is a compile time constant, so array section created at compile time will end up being contiguous and `result` will be "true". If `arr` wasn't a constant, the result at runtime would have been "false". --- flang/include/flang/Support/Fortran-features.h | 2 +- flang/lib/Evaluate/fold-logical.cpp | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/flang/include/flang/Support/Fortran-features.h b/flang/include/flang/Support/Fortran-features.h index 2bbc2385777da..51364d552be64 100644 --- a/flang/include/flang/Support/Fortran-features.h +++ b/flang/include/flang/Support/Fortran-features.h @@ -76,7 +76,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable, IndexVarRedefinition, IncompatibleImplicitInterfaces, CdefinedInit, VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg, MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation, - CompatibleDeclarationsFromDistinctModules, + CompatibleDeclarationsFromDistinctModules, ConstantIsContiguous, NullActualForDefaultIntentAllocatable, UseAssociationIntoSameNameSubprogram, HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile, RealConstantWidening, VolatileOrAsynchronousTemporary) diff --git a/flang/lib/Evaluate/fold-logical.cpp b/flang/lib/Evaluate/fold-logical.cpp index c64f79e06a8ac..705902aa3d8ed 100644 --- a/flang/lib/Evaluate/fold-logical.cpp +++ b/flang/lib/Evaluate/fold-logical.cpp @@ -799,12 +799,21 @@ Expr> FoldIntrinsicFunction( } } else if (name == "is_contiguous") { if (args.at(0)) { + auto warnContiguous = [&]() { + if (auto source{args[0]->sourceLocation()}) { + context.Warn(common::UsageWarning::ConstantIsContiguous, + *source, + "constant values constructed at compile time are likely to be contiguous"_warn_en_US); + } + }; if (auto *expr{args[0]->UnwrapExpr()}) { if (auto contiguous{IsContiguous(*expr, context)}) { + warnContiguous(); return Expr{*contiguous}; } } else if (auto *assumedType{args[0]->GetAssumedTypeDummy()}) { if (auto contiguous{IsContiguous(*assumedType, context)}) { + warnContiguous(); return Expr{*contiguous}; } } From fb8269dc65c31b9b148b3ada11ea8e5c765cc4f6 Mon Sep 17 00:00:00 2001 From: Eugene Epshteyn Date: Sat, 27 Sep 2025 23:43:29 -0400 Subject: [PATCH 2/5] clang-format --- flang/lib/Evaluate/fold-logical.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/flang/lib/Evaluate/fold-logical.cpp b/flang/lib/Evaluate/fold-logical.cpp index 705902aa3d8ed..4bb1dc99ebec4 100644 --- a/flang/lib/Evaluate/fold-logical.cpp +++ b/flang/lib/Evaluate/fold-logical.cpp @@ -801,8 +801,7 @@ Expr> FoldIntrinsicFunction( if (args.at(0)) { auto warnContiguous = [&]() { if (auto source{args[0]->sourceLocation()}) { - context.Warn(common::UsageWarning::ConstantIsContiguous, - *source, + context.Warn(common::UsageWarning::ConstantIsContiguous, *source, "constant values constructed at compile time are likely to be contiguous"_warn_en_US); } }; From a74f608dd2648252432089f4b2d8f6da101fe709 Mon Sep 17 00:00:00 2001 From: Eugene Epshteyn Date: Sat, 27 Sep 2025 23:47:50 -0400 Subject: [PATCH 3/5] added test --- flang/test/Semantics/contiguous-warn.f90 | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 flang/test/Semantics/contiguous-warn.f90 diff --git a/flang/test/Semantics/contiguous-warn.f90 b/flang/test/Semantics/contiguous-warn.f90 new file mode 100644 index 0000000000000..5d87089a36778 --- /dev/null +++ b/flang/test/Semantics/contiguous-warn.f90 @@ -0,0 +1,6 @@ +! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror +integer, parameter :: num = 3 +integer, parameter :: arr(num)=[(i, i=1,num)] +!WARNING: constant values constructed at compile time are likely to be contiguous [-Wconstant-is-contiguous] +logical, parameter :: result=is_contiguous(arr(num:1:-1)) +end From e3d64114a667853131f0d0a913ca00f27c9a04b6 Mon Sep 17 00:00:00 2001 From: Eugene Epshteyn Date: Mon, 29 Sep 2025 18:00:01 -0400 Subject: [PATCH 4/5] Code review feedback --- flang/lib/Evaluate/fold-logical.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/flang/lib/Evaluate/fold-logical.cpp b/flang/lib/Evaluate/fold-logical.cpp index 4bb1dc99ebec4..449c316802d6a 100644 --- a/flang/lib/Evaluate/fold-logical.cpp +++ b/flang/lib/Evaluate/fold-logical.cpp @@ -799,12 +799,12 @@ Expr> FoldIntrinsicFunction( } } else if (name == "is_contiguous") { if (args.at(0)) { - auto warnContiguous = [&]() { + auto warnContiguous{[&]() { if (auto source{args[0]->sourceLocation()}) { context.Warn(common::UsageWarning::ConstantIsContiguous, *source, - "constant values constructed at compile time are likely to be contiguous"_warn_en_US); + "is_contiguous() is always true for named constants and subobjects of named constants"_warn_en_US); } - }; + }}; if (auto *expr{args[0]->UnwrapExpr()}) { if (auto contiguous{IsContiguous(*expr, context)}) { warnContiguous(); From ca108a34f14ce245a9bfda3f389ac2a0512260e6 Mon Sep 17 00:00:00 2001 From: Eugene Epshteyn Date: Mon, 29 Sep 2025 18:54:19 -0400 Subject: [PATCH 5/5] Updated test with the new error message --- flang/test/Semantics/contiguous-warn.f90 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flang/test/Semantics/contiguous-warn.f90 b/flang/test/Semantics/contiguous-warn.f90 index 5d87089a36778..2eb1f1c0857f7 100644 --- a/flang/test/Semantics/contiguous-warn.f90 +++ b/flang/test/Semantics/contiguous-warn.f90 @@ -1,6 +1,6 @@ ! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror integer, parameter :: num = 3 integer, parameter :: arr(num)=[(i, i=1,num)] -!WARNING: constant values constructed at compile time are likely to be contiguous [-Wconstant-is-contiguous] +!WARNING: is_contiguous() is always true for named constants and subobjects of named constants [-Wconstant-is-contiguous] logical, parameter :: result=is_contiguous(arr(num:1:-1)) end