Skip to content

Commit 4064c0e

Browse files
[flang] Implemented a warning about contiguity of compile time constant values (#161084)
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".
1 parent 280abaf commit 4064c0e

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

flang/include/flang/Support/Fortran-features.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ ENUM_CLASS(UsageWarning, Portability, PointerToUndefinable,
7676
IndexVarRedefinition, IncompatibleImplicitInterfaces, CdefinedInit,
7777
VectorSubscriptFinalization, UndefinedFunctionResult, UselessIomsg,
7878
MismatchingDummyProcedure, SubscriptedEmptyArray, UnsignedLiteralTruncation,
79-
CompatibleDeclarationsFromDistinctModules,
79+
CompatibleDeclarationsFromDistinctModules, ConstantIsContiguous,
8080
NullActualForDefaultIntentAllocatable, UseAssociationIntoSameNameSubprogram,
8181
HostAssociatedIntentOutInSpecExpr, NonVolatilePointerToVolatile,
8282
RealConstantWidening, VolatileOrAsynchronousTemporary)

flang/lib/Evaluate/fold-logical.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,12 +799,20 @@ Expr<Type<TypeCategory::Logical, KIND>> FoldIntrinsicFunction(
799799
}
800800
} else if (name == "is_contiguous") {
801801
if (args.at(0)) {
802+
auto warnContiguous{[&]() {
803+
if (auto source{args[0]->sourceLocation()}) {
804+
context.Warn(common::UsageWarning::ConstantIsContiguous, *source,
805+
"is_contiguous() is always true for named constants and subobjects of named constants"_warn_en_US);
806+
}
807+
}};
802808
if (auto *expr{args[0]->UnwrapExpr()}) {
803809
if (auto contiguous{IsContiguous(*expr, context)}) {
810+
warnContiguous();
804811
return Expr<T>{*contiguous};
805812
}
806813
} else if (auto *assumedType{args[0]->GetAssumedTypeDummy()}) {
807814
if (auto contiguous{IsContiguous(*assumedType, context)}) {
815+
warnContiguous();
808816
return Expr<T>{*contiguous};
809817
}
810818
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic -Werror
2+
integer, parameter :: num = 3
3+
integer, parameter :: arr(num)=[(i, i=1,num)]
4+
!WARNING: is_contiguous() is always true for named constants and subobjects of named constants [-Wconstant-is-contiguous]
5+
logical, parameter :: result=is_contiguous(arr(num:1:-1))
6+
end

0 commit comments

Comments
 (0)