diff --git a/flang/include/flang/Evaluate/initial-image.h b/flang/include/flang/Evaluate/initial-image.h --- a/flang/include/flang/Evaluate/initial-image.h +++ b/flang/include/flang/Evaluate/initial-image.h @@ -22,12 +22,7 @@ class InitialImage { public: - enum Result { - Ok, - NotAConstant, - OutOfRange, - SizeMismatch, - }; + enum Result { Ok, NotAConstant, OutOfRange, SizeMismatch }; explicit InitialImage(std::size_t bytes) : data_(bytes) {} InitialImage(InitialImage &&that) = default; diff --git a/flang/lib/Evaluate/fold.cpp b/flang/lib/Evaluate/fold.cpp --- a/flang/lib/Evaluate/fold.cpp +++ b/flang/lib/Evaluate/fold.cpp @@ -275,11 +275,15 @@ if (totalBytes < std::size_t{1000000} && (elements == 0 || totalBytes / elements == *sourceBytes)) { InitialImage image{*sourceBytes}; - InitialImage::Result imageResult{ - image.Add(0, *sourceBytes, *source, context)}; - CHECK(imageResult == InitialImage::Ok); - return image.AsConstant( - context, *moldType, moldLength, *extents, true /*pad with 0*/); + auto status{image.Add(0, *sourceBytes, *source, context)}; + if (status == InitialImage::Ok) { + return image.AsConstant( + context, *moldType, moldLength, *extents, true /*pad with 0*/); + } else { + // Can fail due to an allocatable or automatic component; + // a warning will also have been produced. + CHECK(status == InitialImage::NotAConstant); + } } } return std::nullopt; diff --git a/flang/lib/Evaluate/initial-image.cpp b/flang/lib/Evaluate/initial-image.cpp --- a/flang/lib/Evaluate/initial-image.cpp +++ b/flang/lib/Evaluate/initial-image.cpp @@ -35,12 +35,10 @@ AddPointer(offset + component.offset(), indExpr.value()); } else if (IsAllocatable(component) || IsAutomatic(component)) { return NotAConstant; - } else { - Result added{Add(offset + component.offset(), component.size(), - indExpr.value(), context)}; - if (added != Ok) { - return added; - } + } else if (auto result{Add(offset + component.offset(), + component.size(), indExpr.value(), context)}; + result != Ok) { + return result; } } offset += elementBytes; diff --git a/flang/test/Semantics/null01.f90 b/flang/test/Semantics/null01.f90 --- a/flang/test/Semantics/null01.f90 +++ b/flang/test/Semantics/null01.f90 @@ -1,4 +1,4 @@ -! RUN: %python %S/test_errors.py %s %flang_fc1 +! RUN: %python %S/test_errors.py %s %flang_fc1 -pedantic ! NULL() intrinsic function error tests subroutine test @@ -103,6 +103,8 @@ print *, sin(null(rp0)) !ERROR: A NULL() pointer is not allowed for 'source=' intrinsic argument print *, transfer(null(rp0),ip0) + !WARNING: Source of TRANSFER contains allocatable or pointer component %ra0 + print *, transfer(dt4(null()),[0]) !ERROR: NULL() may not be used as an expression in this context select case(null(ip0)) end select