diff --git a/flang/include/flang/Evaluate/integer.h b/flang/include/flang/Evaluate/integer.h --- a/flang/include/flang/Evaluate/integer.h +++ b/flang/include/flang/Evaluate/integer.h @@ -784,12 +784,12 @@ return {diff.value, overflow}; } - // MAX(X-Y, 0) - constexpr Integer DIM(const Integer &y) const { + // DIM(X,Y)=MAX(X-Y, 0) + constexpr ValueWithOverflow DIM(const Integer &y) const { if (CompareSigned(y) != Ordering::Greater) { return {}; } else { - return SubtractSigned(y).value; + return SubtractSigned(y); } } diff --git a/flang/lib/Evaluate/fold-integer.cpp b/flang/lib/Evaluate/fold-integer.cpp --- a/flang/lib/Evaluate/fold-integer.cpp +++ b/flang/lib/Evaluate/fold-integer.cpp @@ -568,8 +568,15 @@ cx->u)}; } } else if (name == "dim") { - return FoldElementalIntrinsic( - context, std::move(funcRef), &Scalar::DIM); + return FoldElementalIntrinsic(context, std::move(funcRef), + ScalarFunc([&context](const Scalar &x, + const Scalar &y) -> Scalar { + auto result{x.DIM(y)}; + if (result.overflow) { + context.messages().Say("DIM intrinsic folding overflow"_warn_en_US); + } + return result.value; + })); } else if (name == "dot_product") { return FoldDotProduct(context, std::move(funcRef)); } else if (name == "dshiftl" || name == "dshiftr") { diff --git a/flang/lib/Evaluate/fold-real.cpp b/flang/lib/Evaluate/fold-real.cpp --- a/flang/lib/Evaluate/fold-real.cpp +++ b/flang/lib/Evaluate/fold-real.cpp @@ -138,10 +138,14 @@ })); } else if (name == "dim") { return FoldElementalIntrinsic(context, std::move(funcRef), - ScalarFunc( - [](const Scalar &x, const Scalar &y) -> Scalar { - return x.DIM(y).value; - })); + ScalarFunc([&context](const Scalar &x, + const Scalar &y) -> Scalar { + ValueWithRealFlags> result{x.DIM(y)}; + if (result.flags.test(RealFlag::Overflow)) { + context.messages().Say("DIM intrinsic folding overflow"_warn_en_US); + } + return result.value; + })); } else if (name == "dot_product") { return FoldDotProduct(context, std::move(funcRef)); } else if (name == "dprod") { diff --git a/flang/test/Evaluate/errors01.f90 b/flang/test/Evaluate/errors01.f90 --- a/flang/test/Evaluate/errors01.f90 +++ b/flang/test/Evaluate/errors01.f90 @@ -156,6 +156,10 @@ real, parameter :: bad1 = scale(1.0, 99999) !CHECK: complex ABS intrinsic folding overflow real, parameter :: bad2 = abs(cmplx(huge(0.),huge(0.))) + !CHECK: warning: DIM intrinsic folding overflow + real, parameter :: bad3 = dim(huge(1.),-.5*huge(1.)) + !CHECK: warning: DIM intrinsic folding overflow + integer, parameter :: bad4 = dim(huge(1),-1) !CHECK: warning: overflow on REAL(8) to REAL(4) conversion x = 1.D40 end subroutine