[flang] -funsigned: SELECT CASE reports a false conflict when a case value exceeds the signed maximum
With -funsigned, SELECT CASE on an UNSIGNED selector reports a conflict between disjoint case ranges when one of the values is above the signed maximum of the kind:
subroutine s(u)
unsigned(1), intent(in) :: u
select case (u)
case (0u_1:99u_1)
print *, 'low'
case (200u_1:)
print *, 'high'
end select
end subroutine
$ flang -funsigned -c t.f90
error: Semantic errors in t.f90
t.f90:6:3: error: CASE (200U_1:) conflicts with previous cases
case (200u_1:)
^^^^^^^^^^^^^^
t.f90:4:3: Conflicting CASE (0U_1:99U_1)
case (0u_1:99u_1)
^^^^^^^^^^^^^^^^^
The ranges 0..99 and 200..255 do not overlap, so the subroutine should compile. Every UNSIGNED kind is affected: with unsigned :: u, case (0u:99u) and case (3000000000u:) are reported the same way.
The check in flang/lib/Semantics/check-case.cpp sorts the cases with *x.upper < *y.lower, and evaluate::Integer::operator< compares as signed (CompareSigned), so 200u_1 orders as -56 and the ranges look like they overlap. Lowering is not affected: when only case (200u_1:) is present, a selector value of 50u_1 correctly goes to case default.
Reproduced on x86_64 Linux with main at 1087db09497370a892a0586ffa1e5827ab9b1c4f and with flang 21.1.5.
(This issue was filed with the help of AI)
- [ ]
@llvm/issue-subscribers-flang-frontend
Author: Eugene Epshteyn (eugeneepshteyn)
subroutine s(u)
unsigned(1), intent(in) :: u
select case (u)
case (0u_1:99u_1)
print *, 'low'
case (200u_1:)
print *, 'high'
end select
end subroutine
$ flang -funsigned -c t.f90
error: Semantic errors in t.f90
t.f90:6:3: error: CASE (200U_1:) conflicts with previous cases
case (200u_1:)
^^^^^^^^^^^^^^
t.f90:4:3: Conflicting CASE (0U_1:99U_1)
case (0u_1:99u_1)
^^^^^^^^^^^^^^^^^
The ranges 0..99 and 200..255 do not overlap, so the subroutine should compile. Every UNSIGNED kind is affected: with unsigned :: u, case (0u:99u) and case (3000000000u:) are reported the same way.
The check in flang/lib/Semantics/check-case.cpp sorts the cases with *x.upper < *y.lower, and evaluate::Integer::operator< compares as signed (CompareSigned), so 200u_1 orders as -56 and the ranges look like they overlap. Lowering is not affected: when only case (200u_1:) is present, a selector value of 50u_1 correctly goes to case default.
Reproduced on x86_64 Linux with main at 1087db09497370a892a0586ffa1e5827ab9b1c4f and with flang 21.1.5.
(This issue was filed with the help of AI)
Hi!
This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
- Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
- Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
- Fix the issue locally. Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.
@llvm/issue-subscribers-good-first-issue
Author: Eugene Epshteyn (eugeneepshteyn)
subroutine s(u)
unsigned(1), intent(in) :: u
select case (u)
case (0u_1:99u_1)
print *, 'low'
case (200u_1:)
print *, 'high'
end select
end subroutine
$ flang -funsigned -c t.f90
error: Semantic errors in t.f90
t.f90:6:3: error: CASE (200U_1:) conflicts with previous cases
case (200u_1:)
^^^^^^^^^^^^^^
t.f90:4:3: Conflicting CASE (0U_1:99U_1)
case (0u_1:99u_1)
^^^^^^^^^^^^^^^^^
The ranges 0..99 and 200..255 do not overlap, so the subroutine should compile. Every UNSIGNED kind is affected: with unsigned :: u, case (0u:99u) and case (3000000000u:) are reported the same way.
The check in flang/lib/Semantics/check-case.cpp sorts the cases with *x.upper < *y.lower, and evaluate::Integer::operator< compares as signed (CompareSigned), so 200u_1 orders as -56 and the ranges look like they overlap. Lowering is not affected: when only case (200u_1:) is present, a selector value of 50u_1 correctly goes to case default.
Reproduced on x86_64 Linux with main at 1087db09497370a892a0586ffa1e5827ab9b1c4f and with flang 21.1.5.
(This issue was filed with the help of AI)
Can I work on this issue?
@anshithayeruva , it's yours. Please pay attention to the "good introductory issue" rules posted in a comment above, including this one:
Be aware that LLVM's AI policy forbids the use of AI tools to fix good first issues since it squanders a learning opportunity. Using LLMs to search and learn is permitted, since the goal is learning.
@eugeneepshteyn Thank you. I'll follow the contribution guidelines, including the AI policy, and work on the implementation independently.