-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc++][test] Fix invalid low-level const conversion in limited_allocator #118189
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
Conversation
2f2f79a
to
1d5f220
Compare
@llvm/pr-subscribers-libcxx Author: Peng Liu (winner245) ChangesThis PR fixes an invalid low-level const conversion bug in The bug did not crash our tests likely because our current usage of the template has not invoked the specific member function. However, we can trigger an immediate compilation error through explicit template instantiation or by using the operators == or !=. Full diff: https://github.com/llvm/llvm-project/pull/118189.diff 1 Files Affected:
diff --git a/libcxx/test/support/test_allocator.h b/libcxx/test/support/test_allocator.h
index dcd15332ca304f..49dad642184da3 100644
--- a/libcxx/test/support/test_allocator.h
+++ b/libcxx/test/support/test_allocator.h
@@ -467,7 +467,10 @@ class limited_allocator {
TEST_CONSTEXPR_CXX20 pointer allocate(size_type n) { return handle_->template allocate<T>(n); }
TEST_CONSTEXPR_CXX20 void deallocate(pointer p, size_type n) { handle_->template deallocate<T>(p, n); }
TEST_CONSTEXPR size_type max_size() const { return N; }
- TEST_CONSTEXPR BuffT* getHandle() const { return handle_.get(); }
+
+ // In C++11, constexpr non-static member functions are implicitly const, but this is no longer the case since C++14.
+ TEST_CONSTEXPR_CXX14 BuffT* getHandle() { return handle_.get(); }
+ TEST_CONSTEXPR const BuffT* getHandle() const { return handle_.get(); }
};
template <class T, class U, std::size_t N>
|
libcxx/test/support/test_allocator.h
Outdated
@@ -467,7 +467,10 @@ class limited_allocator { | |||
TEST_CONSTEXPR_CXX20 pointer allocate(size_type n) { return handle_->template allocate<T>(n); } | |||
TEST_CONSTEXPR_CXX20 void deallocate(pointer p, size_type n) { handle_->template deallocate<T>(p, n); } | |||
TEST_CONSTEXPR size_type max_size() const { return N; } | |||
TEST_CONSTEXPR BuffT* getHandle() const { return handle_.get(); } | |||
|
|||
// In C++11, constexpr non-static member functions are implicitly const, but this is no longer the case since C++14. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How did you come across this issue?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was working on a draft PR #118141 adding exception tests for other vector operations, which heavily used the limited_allocator
class and somehow triggered the usage of operator==
.
libcxx/test/support/test_allocator.h
Outdated
TEST_CONSTEXPR_CXX14 BuffT* getHandle() { return handle_.get(); } | ||
TEST_CONSTEXPR const BuffT* getHandle() const { return handle_.get(); } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO we should instead do this:
TEST_CONSTEXPR_CXX14 BuffT* getHandle() { return handle_.get(); } | |
TEST_CONSTEXPR const BuffT* getHandle() const { return handle_.get(); } | |
TEST_CONSTEXPR const BuffT* getHandle() const { return handle_.get(); } |
Let's just provide the const
version, since the non-const version is not needed. This method is only used by operator==
below.
The comment can also be removed if we only have the const
version. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agree. We only need a const
version.
479e0e4
to
313bfb3
Compare
313bfb3
to
a51a2db
Compare
(I ran clang-format separately and rebased your patch) |
This PR fixes an invalid low-level const conversion bug in
limited_allocator
. As this class is a widely used utility for testing allocator-aware containers, it is important to fix it. It is a very simple fix by just adding a const qualification.The bug did not crash our tests likely because our current usage of the template has not invoked the specific member function. However, we can trigger an immediate compilation error through explicit template instantiation or by using the operators == or !=.