-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[libc++] Make sure that __desugars_to isn't tripped up by reference_wrapper, const and ref qualifiers #132092
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
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3641b46
[libc++] Make sure that __desugars_to isn't tripped up by reference_w…
ldionne 24f7084
Decouple test from std::equal_to
ldionne 01752d2
Fix modules issues
ldionne 6b152f8
Frozen C++03 headers
ldionne 5a6ea9f
Simplify some uses of __desugars_to with remove_cvref
ldionne 3796483
GCC fixes
ldionne 50a089c
Gcc
ldionne bfd2a23
Drop volatile
ldionne 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
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
42 changes: 42 additions & 0 deletions
42
libcxx/test/libcxx/type_traits/desugars_to.compile.pass.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,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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: FROZEN-CXX03-HEADERS-FIXME | ||
|
||
// This test requires variable templates | ||
// UNSUPPORTED: gcc && c++11 | ||
|
||
#include <__type_traits/desugars_to.h> | ||
|
||
struct Tag {}; | ||
struct Operation {}; | ||
|
||
namespace std { | ||
template <> | ||
bool const __desugars_to_v<Tag, Operation> = true; | ||
} | ||
|
||
void tests() { | ||
// Make sure that __desugars_to is false by default | ||
{ | ||
struct Foo {}; | ||
static_assert(!std::__desugars_to_v<Tag, Foo>, ""); | ||
} | ||
|
||
// Make sure that __desugars_to bypasses const and ref qualifiers on the operation | ||
{ | ||
static_assert(std::__desugars_to_v<Tag, Operation>, ""); // no quals | ||
static_assert(std::__desugars_to_v<Tag, Operation const>, ""); | ||
|
||
static_assert(std::__desugars_to_v<Tag, Operation&>, ""); | ||
static_assert(std::__desugars_to_v<Tag, Operation const&>, ""); | ||
|
||
static_assert(std::__desugars_to_v<Tag, Operation&&>, ""); | ||
static_assert(std::__desugars_to_v<Tag, Operation const&&>, ""); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
libcxx/test/libcxx/utilities/function.objects/refwrap/desugars_to.compile.pass.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,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: FROZEN-CXX03-HEADERS-FIXME | ||
|
||
// This test requires variable templates | ||
// UNSUPPORTED: gcc && c++11 | ||
|
||
// <functional> | ||
|
||
// reference_wrapper | ||
|
||
// Ensure that std::reference_wrapper does not inhibit optimizations based on the | ||
// std::__desugars_to internal helper. | ||
|
||
#include <functional> | ||
#include <__type_traits/desugars_to.h> | ||
|
||
struct Operation {}; | ||
struct Tag {}; | ||
|
||
namespace std { | ||
template <> | ||
bool const __desugars_to_v<Tag, Operation> = true; | ||
} | ||
|
||
static_assert(std::__desugars_to_v<Tag, Operation>, "something is wrong with the test"); | ||
|
||
// make sure we pass through reference_wrapper | ||
static_assert(std::__desugars_to_v<Tag, std::reference_wrapper<Operation> >, ""); | ||
static_assert(std::__desugars_to_v<Tag, std::reference_wrapper<Operation const> >, ""); |
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.
Do we have tests where we test this desugaring with cvref types in our algorithms?
Here if
a
andb
are avolatile ref
to the same objectc
the statement above may not hold true and can change every time it's tested.My concern is specifically about
volatile
.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.
There is a subtlety here. This patch only disregards the cv-ref qualifiers on the operation itself, which would be
std::less
in this case. It does not disregard the cv-ref qualifiers on the arguments that are passed to the function.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.
Still I'm concerned that a
volatile
qualified operator is intended not to be optimized. I expect them to be very rare so not desugaring them sounds not a big loss. IMO if we want to desugar them I'd like some tests that desugaring them has no unexpected issues in cases where avolatile
operator depends on read and writes not being optimized away.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 suspect that this is entirely theoretical at the moment, since I don't think we use volatile-qualified predicates anywhere. At least we don't desugar any such predicates that are not stateless, which is where
volatile
could conceivably make a difference.I feel that
__desugars_to
operates at the level of "is this operation mathematically the same as that other operation", not "is this operation exactly the same as that other operation". This distinction is actually important because otherwise we might not want to also strip ref qualifiers from predicates. For the same reason, I don't think we can ever specialize__desugars_to
on a predicate that is not stateless, making thevolatile
qualification issue somewhat moot.Do you have something concrete in mind like an example of an algorithm that could be passed a
volatile
predicate and where that could be interpreted as "don't optimize this"?In the meantime, I think stripping
volatile
is mostly for consistency and of low value, so I don't mind removing it, but I'd like to either understand a good reason for not doing it, or do it for consistency.