Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions libcxx/docs/ReleaseNotes/24.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Potentially breaking changes
but causes programs which rely on these includes to not compile anymore. The ``_LIBCPP_KEEP_TRANSITIVE_INCLUDES_LLVM23``
macro that was provided in LLVM 23 to ease the transition has been removed in this release.

- Libc++ now diagnoses when a method of ``std::vector<T>`` is used and ``T`` is an incomplete type. The Standard requires
``T`` to be complete before any member function is used, however this worked unreliably for a few member functions. In
LLVM 24, this is diagnosed uniformly.

Announcements About Future Releases
-----------------------------------

Expand Down
1 change: 1 addition & 0 deletions libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ set(files
__utility/priority_tag.h
__utility/private_constructor_tag.h
__utility/rel_ops.h
__utility/require_complete.h
__utility/scope_guard.h
__utility/small_buffer.h
__utility/swap.h
Expand Down
42 changes: 42 additions & 0 deletions libcxx/include/__utility/require_complete.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _LIBCPP___UTILITY_REQUIRE_COMPLETE_H
#define _LIBCPP___UTILITY_REQUIRE_COMPLETE_H

#include <__config>
#include <__cstddef/size_t.h>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif

_LIBCPP_PUSH_MACROS
#include <__undef_macros>

_LIBCPP_BEGIN_NAMESPACE_STD

template <class _Tp, size_t = sizeof(_Tp)>
_LIBCPP_CONSTEXPR void __require_complete_impl(int) {}

template <class _Tp, bool _False = false>
_LIBCPP_CONSTEXPR void __require_complete_impl(long) {
static_assert(_False, "Type is required to be complete");
}

// Produce a compiler error if the given type is not complete.
template <class _Tp>
_LIBCPP_CONSTEXPR void __require_complete() {
std::__require_complete_impl<_Tp>(0);
}

_LIBCPP_END_NAMESPACE_STD

_LIBCPP_POP_MACROS

#endif // _LIBCPP___UTILITY_REQUIRE_COMPLETE_H
17 changes: 17 additions & 0 deletions libcxx/include/__vector/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include <__utility/is_pointer_in_range.h>
#include <__utility/move.h>
#include <__utility/pair.h>
#include <__utility/require_complete.h>
#include <__utility/swap.h>
#include <initializer_list>
#include <limits>
Expand Down Expand Up @@ -348,60 +349,76 @@ class vector {
// Iterators
//
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator begin() _NOEXCEPT {
std::__require_complete<_Tp>();
return __make_iter(__add_alignment_assumption(this->__layout_.__begin_ptr()));
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator begin() const _NOEXCEPT {
std::__require_complete<_Tp>();
return __make_iter(__add_alignment_assumption(this->__layout_.__begin_ptr()));
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI iterator end() _NOEXCEPT {
std::__require_complete<_Tp>();
return __make_iter(__add_alignment_assumption(__layout_.__end_ptr()));
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator end() const _NOEXCEPT {
std::__require_complete<_Tp>();
return __make_iter(__add_alignment_assumption(__layout_.__end_ptr()));
}

[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reverse_iterator rbegin() _NOEXCEPT {
std::__require_complete<_Tp>();
return reverse_iterator(end());
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator
rbegin() const _NOEXCEPT {
std::__require_complete<_Tp>();
return const_reverse_iterator(end());
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI reverse_iterator rend() _NOEXCEPT {
std::__require_complete<_Tp>();
return reverse_iterator(begin());
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator rend() const _NOEXCEPT {
std::__require_complete<_Tp>();
return const_reverse_iterator(begin());
}

[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator cbegin() const _NOEXCEPT {
std::__require_complete<_Tp>();
return begin();
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_iterator cend() const _NOEXCEPT {
std::__require_complete<_Tp>();
return end();
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator
crbegin() const _NOEXCEPT {
std::__require_complete<_Tp>();
return rbegin();
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI const_reverse_iterator crend() const _NOEXCEPT {
std::__require_complete<_Tp>();
return rend();
}

//
// [vector.capacity], capacity
//
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type size() const _NOEXCEPT {
std::__require_complete<_Tp>();
return __layout_.__size();
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type capacity() const _NOEXCEPT {
std::__require_complete<_Tp>();
return __layout_.__capacity();
}
[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI bool empty() const _NOEXCEPT {
std::__require_complete<_Tp>();
return __layout_.__empty();
}

[[__nodiscard__]] _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI size_type max_size() const _NOEXCEPT {
std::__require_complete<_Tp>();
return std::min<size_type>(__alloc_traits::max_size(__layout_.__alloc()), numeric_limits<difference_type>::max());
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void reserve(size_type __n);
Expand Down
1 change: 1 addition & 0 deletions libcxx/include/module.modulemap.in
Original file line number Diff line number Diff line change
Expand Up @@ -2229,6 +2229,7 @@ module std {
module priority_tag { header "__utility/priority_tag.h" }
module private_constructor_tag { header "__utility/private_constructor_tag.h" }
module rel_ops { header "__utility/rel_ops.h" }
module require_complete { header "__utility/require_complete.h" }
module scope_guard { header "__utility/scope_guard.h" }
module small_buffer { header "__utility/small_buffer.h" }
module swap { header "__utility/swap.h" }
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// <vector>

// This test ensures that we diagnose when an incomplete type is used in one of
// vector's methods. The Standard requires that to be the case, and we want to
// uniformly produce an error for that. Note that producing the same diagnostic
// in all cases is difficult, but we at least want to fail to fight back against
// Hyrum's law.

#include <vector>

struct Incomplete;

void f(std::vector<Incomplete>& v) {
(void)v.empty(); // expected-error@*:* {{}}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is more difficult to test than I anticipated.

This test currently happens to pass because we produce errors, but the errors are not all static asserts. In fact, most of them are other, non static_assert errors.

Furthermore, if I remove the __require_complete call from empty() (which is arguably the only one that needs something), this test still passes because some of these statements produce more than one error.

So, I'm not certain how to best test this. One test per file would technically work, but there's no way I can convince myself to do that.

(void)v.size(); // expected-error@*:* {{}}
(void)v.begin(); // expected-error@*:* {{}}
(void)v.end(); // expected-error@*:* {{}}
(void)v.cbegin(); // expected-error@*:* {{}}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another question is which methods should we test here. Some methods like push_back(T const&) clearly already imply a complete T, so I don't think there's a point in testing those. I'm not sure where we should draw the line.

(void)v.cend(); // expected-error@*:* {{}}

// etc for other APIs
}
Loading