From 3bc3cac7acbf42982dce2e75d4c281dda2dbcce6 Mon Sep 17 00:00:00 2001 From: Peter Klausler Date: Wed, 15 Oct 2025 16:12:40 -0700 Subject: [PATCH] [flang] Fix character length checking in ALLOCATE 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 823aa4e795e35..d7772da210917 100644 --- a/flang/lib/Semantics/check-allocate.cpp +++ b/flang/lib/Semantics/check-allocate.cpp @@ -439,7 +439,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; } @@ -452,7 +452,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