[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.
This commit is contained in:
Peter Klausler 2025-10-16 12:21:14 -07:00 committed by GitHub
parent a6181dc84b
commit 36c9b4fd6d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 2 deletions

View File

@ -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;
}

View File

@ -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