Skip to content

Commit 3bc3cac

Browse files
committed
[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 #163242.
1 parent 05a3f76 commit 3bc3cac

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

flang/lib/Semantics/check-allocate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ static bool HaveCompatibleLengths(
439439
evaluate::ToInt64(type1.characterTypeSpec().length().GetExplicit())};
440440
auto v2{
441441
evaluate::ToInt64(type2.characterTypeSpec().length().GetExplicit())};
442-
return !v1 || !v2 || *v1 == *v2;
442+
return !v1 || !v2 || (*v1 >= 0 ? *v1 : 0) == (*v2 >= 0 ? *v2 : 0);
443443
} else {
444444
return true;
445445
}
@@ -452,7 +452,7 @@ static bool HaveCompatibleLengths(
452452
auto v1{
453453
evaluate::ToInt64(type1.characterTypeSpec().length().GetExplicit())};
454454
auto v2{type2.knownLength()};
455-
return !v1 || !v2 || *v1 == *v2;
455+
return !v1 || !v2 || (*v1 >= 0 ? *v1 : 0) == (*v2 >= 0 ? *v2 : 0);
456456
} else {
457457
return true;
458458
}

flang/test/Semantics/bug163242.f90

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
!RUN: %flang -fc1 -fsyntax-only %s | FileCheck --allow-empty %s
2+
!CHECK-NOT: error:
3+
character(0), allocatable :: ch
4+
allocate(character(-1) :: ch)
5+
end

0 commit comments

Comments
 (0)