Skip to content

Clang cannot compute at compile time typeid of a variable of concrete class type with vtable #63117

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

Closed
ilya-biryukov opened this issue Jun 5, 2023 · 8 comments
Labels
c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party

Comments

@ilya-biryukov
Copy link
Contributor

ilya-biryukov commented Jun 5, 2023

On this code:

// Build with -std=c++20
#include <typeinfo>

struct base { virtual void enable_vtable(); };
struct derived : base {};

derived d;
static_assert(&typeid(d) == &typeid(derived));

Clang produces the following error:

error: static assertion expression is not an integral constant expression
note: typeid applied to object 'd' whose dynamic type is not constant

Even though typeid can be computed at compile time (since d is not a reference and has a concrete object type).
GCC and MSVC compile this code with no errors, see Godbolt.

@ilya-biryukov ilya-biryukov added c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 5, 2023
@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2023

@llvm/issue-subscribers-c-20

@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2023

@llvm/issue-subscribers-clang-frontend

@AaronBallman AaronBallman removed the c++20 label Jun 5, 2023
@AaronBallman
Copy link
Collaborator

Doesn't require C++20, you can see the same "not constant expression" behavior with an array index: https://godbolt.org/z/P54G99a53

@AaronBallman
Copy link
Collaborator

Oops, constexpr typeid, however, is a C++20 feature, so this does require C++20 after all. :-)

@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2023

@llvm/issue-subscribers-c-20

@AaronBallman
Copy link
Collaborator

http://eel.is/c++draft/expr.const#5.26 disallows use of typeid when the dynamic type is constexpr-unknown.
http://eel.is/c++draft/expr.const#8 defines this type as not being constexpr-unknown, so I believe this code should be accepted.

It's worth noting that GCC does not seem to properly note when the type is constexpr-unknown, though MSVC does: https://godbolt.org/z/x8a4c5xdq

@AaronBallman AaronBallman added the confirmed Verified by a second party label Jun 5, 2023
@cor3ntin
Copy link
Contributor

cor3ntin commented Jun 6, 2023

Related #63139

shafik added a commit to shafik/llvm-project that referenced this issue Jul 17, 2024
…nstant expressions

P2280R4 allows the use of references in pointers of unknown origins in a
constant expression context but only in specific cases that could be constant
expressions.

We track whether a variable is a constexpr unknown in a constant expression by
setting a flag in either APValue or LValue and using this flag to prevent using
unknown values in places where it is not allowed.

In `evaluateVarDeclInit` we may need to create a new `APValue` to track the
unknown referene or pointer and we track that `APValue` in the
`CallStackFrame`.

Fixes: llvm#63139
llvm#63117
cor3ntin pushed a commit that referenced this issue Jan 22, 2025
…nstant expressions (#95474)

P2280R4 allows the use of references in pointers of unknown origins in a
constant expression context but only in specific cases that could be
constant expressions.

We track whether a variable is a constexpr unknown in a constant
expression by setting a flag in either APValue or LValue and using this
flag to prevent using unknown values in places where it is not allowed.

Fixes: #63139 #63117
github-actions bot pushed a commit to arm/arm-toolchain that referenced this issue Jan 22, 2025
…ences in constant expressions (#95474)

P2280R4 allows the use of references in pointers of unknown origins in a
constant expression context but only in specific cases that could be
constant expressions.

We track whether a variable is a constexpr unknown in a constant
expression by setting a flag in either APValue or LValue and using this
flag to prevent using unknown values in places where it is not allowed.

Fixes: llvm/llvm-project#63139 llvm/llvm-project#63117
@cor3ntin
Copy link
Contributor

Fixed by #95474

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++20 clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party
Projects
None yet
Development

No branches or pull requests

4 participants