Skip to content

Commit dfa2f32

Browse files
klauslergithub-actions[bot]
authored andcommitted
Automerge: [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 llvm/llvm-project#163242.
2 parents 4c36059 + 36c9b4f commit dfa2f32

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
@@ -441,7 +441,7 @@ static bool HaveCompatibleLengths(
441441
evaluate::ToInt64(type1.characterTypeSpec().length().GetExplicit())};
442442
auto v2{
443443
evaluate::ToInt64(type2.characterTypeSpec().length().GetExplicit())};
444-
return !v1 || !v2 || *v1 == *v2;
444+
return !v1 || !v2 || (*v1 >= 0 ? *v1 : 0) == (*v2 >= 0 ? *v2 : 0);
445445
} else {
446446
return true;
447447
}
@@ -454,7 +454,7 @@ static bool HaveCompatibleLengths(
454454
auto v1{
455455
evaluate::ToInt64(type1.characterTypeSpec().length().GetExplicit())};
456456
auto v2{type2.knownLength()};
457-
return !v1 || !v2 || *v1 == *v2;
457+
return !v1 || !v2 || (*v1 >= 0 ? *v1 : 0) == (*v2 >= 0 ? *v2 : 0);
458458
} else {
459459
return true;
460460
}

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)