-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc++] mark std::expected
as nodiscard
#130820
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
base: main
Are you sure you want to change the base?
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-libcxx Author: Mohamed Emad (hulxv) ChangesFix #130656 Full diff: https://github.com/llvm/llvm-project/pull/130820.diff 2 Files Affected:
diff --git a/libcxx/include/__cxx03/__expected/expected.h b/libcxx/include/__cxx03/__expected/expected.h
index 1d54bb9f6edeb..ce9676c68c2dc 100644
--- a/libcxx/include/__cxx03/__expected/expected.h
+++ b/libcxx/include/__cxx03/__expected/expected.h
@@ -58,7 +58,7 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _Tp, class _Err>
-class expected;
+class [[nodiscrad]] expected;
template <class _Tp>
struct __is_std_expected : false_type {};
diff --git a/libcxx/include/__expected/expected.h b/libcxx/include/__expected/expected.h
index 03bbd1623ed5c..8dd32668bcb33 100644
--- a/libcxx/include/__expected/expected.h
+++ b/libcxx/include/__expected/expected.h
@@ -60,7 +60,7 @@ _LIBCPP_PUSH_MACROS
_LIBCPP_BEGIN_NAMESPACE_STD
template <class _Tp, class _Err>
-class expected;
+class [[nodiscard]] expected;
template <class _Tp>
struct __is_std_expected : false_type {};
|
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.
Thanks for working on this!
This needs tests to verify that the nodiscard
works as expected.
libcxx/test/std/containers/associative/multimap/empty.verify.cpp
contains an example test.
Thanks for guidance! I will add it now |
libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp
Outdated
Show resolved
Hide resolved
libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp
Outdated
Show resolved
Hide resolved
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.
We should also add [[nodiscard]]
to this partial specialization:
llvm-project/libcxx/include/__expected/expected.h
Lines 1355 to 1358 in bd748b3
template <class _Tp, class _Err> | |
requires is_void_v<_Tp> | |
class expected<_Tp, _Err> : private __expected_void_base<_Err> { | |
static_assert(__valid_std_unexpected<_Err>::value, |
Superseded
Per CI failures, we should add void
cast to function calls in these files to suppress warnings:
libcxx/utilities/expected/expected.expected/and_then.mandates.verify.cpp
libcxx/utilities/expected/expected.expected/or_else.mandates.verify.cpp
libcxx/utilities/expected/expected.void/and_then.mandates.verify.cpp
libcxx/utilities/expected/expected.void/or_else.mandates.verify.cpp
libcxx/test/libcxx/utilities/expected/expected.expected/nodiscard.verify.cpp
Outdated
Show resolved
Hide resolved
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'm conflicted about making this change -- I suspect this could end up being really verbose. Before we actually land this, can you please ping @llvm/libcxx-vendors to give folks a chance to run this change internally first? I'd like to try that out to get some signal.
Chromium isn't on C++23 yet, so we're not using std::expected and I'm not really familiar with the class, but I did notice that our own "backport" is marked |
57dc193
to
a5f9e46
Compare
If we are doing this, should we add a reason for why |
AFAIK we never considered to add a reason. That feature was added in C++20, while I'm not against adding a reason. FYI @philnik777 proposed to add reasons to |
I came from a Rust background and we have let res: Result<T,E> = result_return_result(); // no warning
result_return_result(); // WARNING!!!
result_return_result().unwrap(); // no warning. ".unwrap()" works as a handler and returns the expected value So, I don't find it a verbose thing and it's normal from my point of view |
It seems like it's pretty much always a bug to not check an |
I also feel like implicitly ignoring an expected is always a bug. It would be nice to see a reason added to it. |
A data point that may be of interest - MSVC's STL marked |
While I like the idea in general, I really dislike that there is no way for the user to say "it's fine to discard the |
Well, in C++26, users will be able to do std::ignore = some_func_returning_expected(); to signify that it is OK to discard an Whether this is an acceptable solution to force on users is up for debate, though. |
✅ With the latest revision this PR passed the C/C++ code formatter. |
@hulxv can you please fix the CI before requesting reviews again? If you need help with the CI issues feel free to reach out on Discord. I tend not to look at patches when there are a lot of CI issues. |
Is there any paper adding ‘[[candiscard]]’ floating around ? I’d much prefer to making it possible for the function owner to make the function possible to discard the returned value, before we adding the blanket nodiscard for everyone. Imagine if we make printf nodiscard for the reason of not checking the error code? It will simply end up with lots of #pragma gcc diagnostic |
Yes, there is P2992R1 proposing the |
Thank is not the same though. that |
3c13814
to
23a7f6f
Compare
23a7f6f
to
55da0b3
Compare
Fix #130656