LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 50711 - Bug: undefined behavior of constexpr static_cast from int to unscoped enum without fixed type outside range compiles fine on C++17
Summary: Bug: undefined behavior of constexpr static_cast from int to unscoped enum wi...
Status: CONFIRMED
Alias: None
Product: clang
Classification: Unclassified
Component: C++17 (show other bugs)
Version: trunk
Hardware: PC Linux
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-06-14 23:29 PDT by Carlos Galvez
Modified: 2021-06-21 07:42 PDT (History)
4 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Carlos Galvez 2021-06-14 23:29:09 PDT
Hi,

The following code is undefined behavior in C++17. And undefined behavior is not allowed in constant expressions - the compiler is required to diagnose it. Adapted from cppreference:

enum foo { a = 0, b = 1 }; 
constexpr foo x = static_cast<foo>(123); // undefined behavior since C++17

However Clang happily compiles this code:

https://godbolt.org/z/bzhWMxqoE
Comment 1 Richard Smith 2021-06-17 18:28:05 PDT
Confirmed; it doesn't look like any major compiler diagnoses this.
Comment 2 Carlos Galvez 2021-06-21 07:42:58 PDT
Yep:

https://stackoverflow.com/a/66723431

There's an open bug for GCC:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95701