| Issue |
242880
|
| Summary |
[flang] SELECT CASE: a case value that overflows the selector's kind is truncated, and it hides overlapping cases
|
| Labels |
miscompilation,
flang,
flang:fir-hlfir,
flang:semantics
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
When an integer case value has a wider kind than the SELECT CASE _expression_ and is outside that kind's range, flang warns (`-Wcase-overflow`) and then compiles the construct incorrectly:
1. Lowering truncates the value to the selector's kind. A point value can then match a selector value it is not equal to, and a range bound can make the range empty.
2. The overflow also turns off the check for overlapping case values, so the construct below compiles without the "conflicts with previous cases" error.
The first program below ("Wrong match") is conforming. C1152 requires a *case-value* to have the same type as the *case-expr*, not the same kind, except for CHARACTER. F2023 11.1.9.2 p1 defines a match with the intrinsic relational operators, which compare integers of different kinds in the wider kind (F2023 10.1.5.5.1 p6). So `case (1000)` can never match an INTEGER(1) value, and `case (100:1000)` matches every INTEGER(1) value from 100 up. The second program ("Overlap not diagnosed") is deliberately not conforming: `case (1)` appears twice, which C1154 forbids, so an error is expected there, and the missing error is the bug.
Wrong match:
```fortran
program p
implicit none
integer(1) :: x
x = -24_1
select case (x)
case (1000)
print *, 'matched 1000'
case default
print *, 'no match'
end select
x = 110_1
select case (x)
case (100:1000)
print *, 'in 100:1000'
case default
print *, 'not in 100:1000'
end select
end program
```
```console
$ flang t.f90 && ./a.out
t.f90:6:9: warning: CASE value (1000_4) overflows type (INTEGER(1)) of SELECT CASE _expression_ [-Wcase-overflow]
t.f90:13:13: warning: CASE value (1000_4) overflows type (INTEGER(1)) of SELECT CASE _expression_ [-Wcase-overflow]
matched 1000
not in 100:1000
```
Expected `no match` and `in 100:1000`; gfortran prints those. 1000 truncated to 8 bits is -24, which explains both results.
Overlap not diagnosed:
```fortran
subroutine s(x)
integer(1) :: x
select case (x)
case (1000)
case (1)
case (1)
end select
end subroutine
```
`flang -fsyntax-only` gives only the overflow warning and exits 0. Without the `case (1000)` line it correctly reports `CASE (1_1) conflicts with previous cases`. In `flang/lib/Semantics/check-case.cpp`, `CaseValues<T>::GetValue` sets `hasErrors_` on the overflow warning, and `Check` skips the disjointness check when `hasErrors_` is set.
The same truncation happens for UNSIGNED selectors with `-funsigned` (e.g. an UNSIGNED(1) selector and `case (300u_2)`). Reproduced on x86_64 Linux with main at 2277d3de6883239e6cb9e59ea5f6dcf87bbe2f1a, which already contains the SELECT CASE fixes 240149 and 240785, so neither of them changes this.
(This issue was filed with the help of AI)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs