llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Clang rejects narrowing conversions in case labels

Open miyuki opened this issue 9 years ago • 1 comments

Bugzilla Link 35287
Resolution INVALID
Resolved on Nov 28, 2017 17:53
Version trunk
OS Linux
CC @zygoloid

Extended Description

Consider the following test case:

$ cat test.cc int main() { switch (0u) { case -1: break; } }

Clang rejects this code when compiling in C++11 and later modes with the following error: $ clang++ -std=c++11 -fsyntax-only test.cc test.cc:3:10: error: case value evaluates to -1, which cannot be narrowed to type 'unsigned int' [-Wc++11-narrowing] case -1: break; ^

GCC and EDG (ICC 18) accept this code: https://godbolt.org/g/hVWQgW ISTM that Clang is wrong (nothing in [stmt.switch] suggests that narrowing conversions are not allowed in case labels).

miyuki avatar Nov 13 '17 11:11 miyuki

For posterity: [stmt.switch]p2 says: "the constant-expression shall be a converted constant expression (8.6) of the adjusted type of the switch condition"

And [expr.const]p5 says: "A converted constant expression of type T is an expression, implicitly converted to type T, where the converted expression is a constant expression and the implicit conversion sequence contains only [...] integral conversions (7.8) other than narrowing conversions (11.6.4)"

The other compilers are simply wrong. FWIW, ICC / EDG gets this wrong intentionally, for GCC compatibility. EDG's strict mode rejects it.