[flang-commits] [flang] 36c9b4f - [flang] Fix character length checking in ALLOCATE (#163657)

via flang-commits flang-commits at lists.llvm.org
Thu Oct 16 12:21:17 PDT 2025


Author: Peter Klausler
Date: 2025-10-16T12:21:14-07:00
New Revision: 36c9b4fd6de9cb5665e98b816e54e8aec5592f05

URL: https://github.com/llvm/llvm-project/commit/36c9b4fd6de9cb5665e98b816e54e8aec5592f05
DIFF: https://github.com/llvm/llvm-project/commit/36c9b4fd6de9cb5665e98b816e54e8aec5592f05.diff

LOG: [flang] Fix character length checking in ALLOCATE (#163657)

The known character length compatibility check for ALLOCATE statements
needs to allow for negative lengths, which are effectively zero.

Fixes https://github.com/llvm/llvm-project/issues/163242.

Added: 
    flang/test/Semantics/bug163242.f90

Modified: 
    flang/lib/Semantics/check-allocate.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/check-allocate.cpp b/flang/lib/Semantics/check-allocate.cpp
index 0779419e62723..e019bbdfa27f6 100644
--- a/flang/lib/Semantics/check-allocate.cpp
+++ b/flang/lib/Semantics/check-allocate.cpp
@@ -441,7 +441,7 @@ static bool HaveCompatibleLengths(
         evaluate::ToInt64(type1.characterTypeSpec().length().GetExplicit())};
     auto v2{
         evaluate::ToInt64(type2.characterTypeSpec().length().GetExplicit())};
-    return !v1 || !v2 || *v1 == *v2;
+    return !v1 || !v2 || (*v1 >= 0 ? *v1 : 0) == (*v2 >= 0 ? *v2 : 0);
   } else {
     return true;
   }
@@ -454,7 +454,7 @@ static bool HaveCompatibleLengths(
     auto v1{
         evaluate::ToInt64(type1.characterTypeSpec().length().GetExplicit())};
     auto v2{type2.knownLength()};
-    return !v1 || !v2 || *v1 == *v2;
+    return !v1 || !v2 || (*v1 >= 0 ? *v1 : 0) == (*v2 >= 0 ? *v2 : 0);
   } else {
     return true;
   }

diff  --git a/flang/test/Semantics/bug163242.f90 b/flang/test/Semantics/bug163242.f90
new file mode 100644
index 0000000000000..5e020aeb4dc0d
--- /dev/null
+++ b/flang/test/Semantics/bug163242.f90
@@ -0,0 +1,5 @@
+!RUN: %flang -fc1 -fsyntax-only %s | FileCheck --allow-empty %s
+!CHECK-NOT: error:
+character(0), allocatable :: ch
+allocate(character(-1) :: ch)
+end


        


More information about the flang-commits mailing list