Repository navigation
[libc++] Uniformly require complete types in vector member functions #211110
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
Open
ldionne
wants to merge
1
commit into
llvm:main
Choose a base branch
from
ldionne:review/vector-incomplete-type-diagnose
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+95
−27
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
libcxx/test/libcxx/containers/sequences/vector/incomplete_type.compile.pass.cpp
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
libcxx/test/libcxx/containers/sequences/vector/incomplete_type.verify.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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@*:* {{}} | ||
| (void)v.size(); // expected-error@*:* {{}} | ||
| (void)v.begin(); // expected-error@*:* {{}} | ||
| (void)v.end(); // expected-error@*:* {{}} | ||
| (void)v.cbegin(); // expected-error@*:* {{}} | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Another question is which methods should we test here. Some methods like |
||
| (void)v.cend(); // expected-error@*:* {{}} | ||
|
|
||
| // etc for other APIs | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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_asserterrors.Furthermore, if I remove the
__require_completecall fromempty()(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.