[flang] Fixed crash on forward referenced `len` parameter
authorPete Steinfeld <psteinfeld@nvidia.com>
Tue, 26 May 2020 22:12:15 +0000 (15:12 -0700)
committerPete Steinfeld <psteinfeld@nvidia.com>
Thu, 4 Jun 2020 20:12:11 +0000 (13:12 -0700)
Summary:
Using a forward reference to define a `len` parameter causes a crash.
The underlying cause was that a previously declared type had an
erroneous expression for its `LEN` param value.  When this expression
was referenced to evaluate a subsequent expression, bad things happened.
I fixed this by putting in code to detect this case.

Reviewers: tskeith, klausler, DavidTruby

Subscribers: llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D80593

flang/lib/Evaluate/variable.cpp
flang/test/Semantics/resolve91.f90

index c7b261d..3a6cf56 100644 (file)
@@ -257,8 +257,14 @@ DescriptorInquiry::DescriptorInquiry(NamedEntity &&base, Field field, int dim)
 static std::optional<Expr<SubscriptInteger>> SymbolLEN(const Symbol &sym) {
   if (auto dyType{DynamicType::From(sym)}) {
     if (const semantics::ParamValue * len{dyType->charLength()}) {
-      if (auto intExpr{len->GetExplicit()}) {
-        return ConvertToType<SubscriptInteger>(*std::move(intExpr));
+      if (len->isExplicit()) {
+        if (auto intExpr{len->GetExplicit()}) {
+          return ConvertToType<SubscriptInteger>(*std::move(intExpr));
+        } else {
+          // There was an error constructing this symbol's type.  It should
+          // have a length expression, but we couldn't retrieve it
+          return std::nullopt;
+        }
       } else {
         return Expr<SubscriptInteger>{
             DescriptorInquiry{NamedEntity{sym}, DescriptorInquiry::Field::Len}};
index f55ca86..0a375b8 100644 (file)
@@ -44,3 +44,10 @@ module m4
     real, dimension(:), pointer :: realArray => localArray
   end type
 end module m4
+
+module m5
+  !ERROR: Actual argument for 'string=' has bad type 'REAL(4)'
+  character(len=len(a)) :: b
+  !ERROR: The type of 'a' has already been implicitly declared
+  character(len=len(b)) :: a
+end module m5