Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clang accepts the undefined behavior operation in a constant expression #56528

Closed
xmh0511 opened this issue Jul 14, 2022 · 4 comments
Closed
Labels
c++20 clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer clang:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate

Comments

@xmh0511
Copy link

xmh0511 commented Jul 14, 2022

enum A {a = 0};
constexpr A e = static_cast<A>(1024);

According to [expr.static.cast p10

If the enumeration type does not have a fixed underlying type, the value is unchanged if the original value is within the range of the enumeration values ([dcl.enum]), and otherwise, the behavior is undefined.

[dcl.enum] p8 defines the range of the enumeration values, which says

Otherwise, the values of the enumeration are the values representable by a hypothetical integer type with minimal width M such that all enumerators can be represented.

In this case, it is sufficient to represent the value 0 of the unique enumerator if M is 1. Apparently, the value 1024 cannot be representable in the hypothetical integer type with minimal width M. So, the full-expression of the initialization is not a constant expression since the violation of [expr.const] p5

an operation that would have undefined behavior as specified in [intro] through [cpp];

However, Clang accepts this example without any diagnosis.

@tbaederr tbaederr added c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer and removed new issue labels Jul 14, 2022
@llvmbot
Copy link
Member

llvmbot commented Jul 14, 2022

@llvm/issue-subscribers-clang-frontend

@llvmbot
Copy link
Member

llvmbot commented Jul 14, 2022

@llvm/issue-subscribers-c-20

@shafik
Copy link
Collaborator

shafik commented Jul 14, 2022

Looks like duplicate of #50055

@AaronBallman
Copy link
Collaborator

Agreed, closing this as a duplicate of #50055.

@AaronBallman AaronBallman closed this as not planned Won't fix, can't repro, duplicate, stale Jul 14, 2022
@EugeneZelenko EugeneZelenko added the duplicate Resolved as duplicate label Jul 14, 2022
@usx95 usx95 moved this to Done in C++ 20 in Clang Aug 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++20 clang:diagnostics New/improved warning or error message in Clang, but not in clang-tidy or static analyzer clang:frontend Language frontend issues, e.g. anything involving "Sema" duplicate Resolved as duplicate
Projects
Status: Done
Development

No branches or pull requests

6 participants