From 36c9b4fd6de9cb5665e98b816e54e8aec5592f05 Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Thu, 16 Oct 2025 12:21:14 -0700 Subject: [PATCH] [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. --- flang/lib/Semantics/check-allocate.cpp | 4 ++-- flang/test/Semantics/bug163242.f90 | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 flang/test/Semantics/bug163242.f90 diff --git a/flang/lib/Semantics/check-allocate.cpp b/flang/lib/Semantics/check-allocate.cpp index 0779419e6272..e019bbdfa27f 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 000000000000..5e020aeb4dc0 --- /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