Skip to content

[clang] Allow constexpr cast from void* in more cases #89484

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

Merged
merged 3 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions clang/lib/AST/ExprConstant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9237,9 +9237,10 @@ bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&
!Result.IsNullPtr;
bool VoidPtrCastMaybeOK =
HasValidResult &&
Info.Ctx.hasSameUnqualifiedType(Result.Designator.getType(Info.Ctx),
E->getType()->getPointeeType());
Result.IsNullPtr ||
(HasValidResult &&
Info.Ctx.hasSimilarType(Result.Designator.getType(Info.Ctx),
E->getType()->getPointeeType()));
// 1. We'll allow it in std::allocator::allocate, and anything which that
// calls.
// 2. HACK 2022-03-28: Work around an issue with libstdc++'s
Expand Down
8 changes: 8 additions & 0 deletions clang/test/CXX/drs/dr28xx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@
// expected-no-diagnostics
#endif

namespace cwg2819 { // cwg2819: 19 review 2023-12-01
#if __cpp_constexpr >= 202306L
constexpr void* p = nullptr;
constexpr int* q = static_cast<int*>(p);
static_assert(q == nullptr);
#endif
}

namespace cwg2847 { // cwg2847: 19

#if __cplusplus >= 202002L
Expand Down
7 changes: 7 additions & 0 deletions clang/test/CXX/expr/expr.const/p5-26.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,10 @@ void err() {
// cxx23-note {{cast from 'void *' is not allowed in a constant expression in C++ standards before C++2c}} \
// cxx26-note {{cast from 'void *' is not allowed in a constant expression because the pointed object type 'T' is not similar to the target type 'S'}}
}

int* p;
constexpr int** pp = &p;
constexpr void* vp = pp;
constexpr auto cvp = static_cast<const int* volatile*>(vp);
// cxx23-error@-1 {{constant expression}}
// cxx23-note@-2 {{cast from 'void *' is not allowed in a constant expression}}
2 changes: 1 addition & 1 deletion clang/www/cxx_dr_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -16722,7 +16722,7 @@ <h2 id="cxxdr">C++ defect report implementation status</h2>
<td><a href="https://cplusplus.github.io/CWG/issues/2819.html">2819</a></td>
<td>review</td>
<td>Cast from null pointer value in a constant expression</td>
<td align="center">Not resolved</td>
<td title="Clang 19 implements 2023-12-01 resolution" align="center">Not Resolved*</td>
</tr>
<tr class="open" id="2820">
<td><a href="https://cplusplus.github.io/CWG/issues/2820.html">2820</a></td>
Expand Down