[flang-commits] [flang] 0ba2a3c - [flang] Replace negative known CHARACTER length with zero in type analysis

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Fri Sep 23 12:22:32 PDT 2022


Author: Peter Klausler
Date: 2022-09-23T12:22:19-07:00
New Revision: 0ba2a3c44f02ca6b31c339ee6e5dacbd9b8573a8

URL: https://github.com/llvm/llvm-project/commit/0ba2a3c44f02ca6b31c339ee6e5dacbd9b8573a8
DIFF: https://github.com/llvm/llvm-project/commit/0ba2a3c44f02ca6b31c339ee6e5dacbd9b8573a8.diff

LOG: [flang] Replace negative known CHARACTER length with zero in type analysis

When a DynamicType for CHARACTER has a known length, correct a negative
length value to its effective length of zero so that all such types
compare equal in interface compatibility checking.

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

Added: 
    

Modified: 
    flang/include/flang/Evaluate/type.h
    flang/lib/Evaluate/type.cpp
    flang/test/Semantics/separate-mp02.f90

Removed: 
    


################################################################################
diff  --git a/flang/include/flang/Evaluate/type.h b/flang/include/flang/Evaluate/type.h
index 8f469daa439d..876665bb8958 100644
--- a/flang/include/flang/Evaluate/type.h
+++ b/flang/include/flang/Evaluate/type.h
@@ -91,8 +91,11 @@ class DynamicType {
     CHECK(IsValidKindOfIntrinsicType(category_, kind_));
   }
   DynamicType(int charKind, const semantics::ParamValue &len);
+  // When a known length is presented, resolve it to its effective
+  // length of zero if it is negative.
   constexpr DynamicType(int k, std::int64_t len)
-      : category_{TypeCategory::Character}, kind_{k}, knownLength_{len} {
+      : category_{TypeCategory::Character}, kind_{k}, knownLength_{
+                                                          len >= 0 ? len : 0} {
     CHECK(IsValidKindOfIntrinsicType(category_, kind_));
   }
   explicit constexpr DynamicType(

diff  --git a/flang/lib/Evaluate/type.cpp b/flang/lib/Evaluate/type.cpp
index 97e707a34cc8..2a1cdd23d750 100644
--- a/flang/lib/Evaluate/type.cpp
+++ b/flang/lib/Evaluate/type.cpp
@@ -96,7 +96,7 @@ DynamicType::DynamicType(int k, const semantics::ParamValue &pv)
     : category_{TypeCategory::Character}, kind_{k} {
   CHECK(IsValidKindOfIntrinsicType(category_, kind_));
   if (auto n{ToInt64(pv.GetExplicit())}) {
-    knownLength_ = *n;
+    knownLength_ = *n > 0 ? *n : 0;
   } else {
     charLengthParamValue_ = &pv;
   }

diff  --git a/flang/test/Semantics/separate-mp02.f90 b/flang/test/Semantics/separate-mp02.f90
index 6d2f8f8d6ab1..fd9c4c3cc18f 100644
--- a/flang/test/Semantics/separate-mp02.f90
+++ b/flang/test/Semantics/separate-mp02.f90
@@ -305,9 +305,32 @@ pure elemental module subroutine s1
     end subroutine
   end interface
 end module
+
 submodule(m8) sm8
  contains
   !Ensure no spurious error about mismatching attributes
   module procedure s1
   end procedure
 end submodule
+
+module m9
+  interface
+    module subroutine sub1(s)
+      character(len=0) s
+    end subroutine
+    module subroutine sub2(s)
+      character(len=0) s
+    end subroutine
+  end interface
+end module
+
+submodule(m9) sm1
+ contains
+  module subroutine sub1(s)
+    character(len=-1) s ! ok
+  end subroutine
+  module subroutine sub2(s)
+    !ERROR: Dummy argument 's' has type CHARACTER(KIND=1,LEN=1_8); the corresponding argument in the interface body has type CHARACTER(KIND=1,LEN=0_8)
+    character(len=1) s
+  end subroutine
+end submodule


        


More information about the flang-commits mailing list