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] Array access during constant evaluation is inconsistently rejected #122300

Open
cookiestarfish opened this issue Jan 9, 2025 · 2 comments
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party

Comments

@cookiestarfish
Copy link

cookiestarfish commented Jan 9, 2025

I hope it is obvious from the example, clang should either reject both element accesses or accept both during constant evaluation.

struct Base
{};

struct Derived : Base
{
    int value;
};

static_assert([]() {
    Derived d[4] {};
    d[0] = Derived{.value=10};
    d[1] = Derived{.value=20};
    d[2] = Derived{.value=30};
    d[3] = Derived{.value=40};

    Base* b = d;
    
    // does not compile
    // return static_cast<Derived*>(b)[2].value;
    
    // using a temporary compiles.
    Derived* temp = static_cast<Derived*>(b);
    return temp[2].value;
}() == 30);
@llvmbot llvmbot added the clang Clang issues not falling into any other category label Jan 9, 2025
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" consteval C++20 consteval and removed clang Clang issues not falling into any other category labels Jan 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 9, 2025

@llvm/issue-subscribers-clang-frontend

Author: marco (cookiestarfish)

I hope it is obvious from the example, clang should either reject both element accesses or accept both during constant evaluation. ``` struct Base {};

struct Derived : Base
{
int value;
};

static_assert( {
Derived d[4] {};
d[0] = Derived{.value=10};
d[1] = Derived{.value=20};
d[2] = Derived{.value=30};
d[3] = Derived{.value=40};

Base* b = d;

// does not compile
// return static_cast&lt;Derived*&gt;(b)[2].value;

// using a temporary compiles.
Derived* temp = static_cast&lt;Derived*&gt;(b);
return temp[2].value;

}() == 30);

</details>

@cor3ntin cor3ntin added confirmed Verified by a second party and removed consteval C++20 consteval labels Jan 9, 2025
@cor3ntin
Copy link
Contributor

cor3ntin commented Jan 9, 2025

Confirmed - https://godbolt.org/z/MEeW9neKr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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