-
Notifications
You must be signed in to change notification settings - Fork 37.7k
sync: Use decltype(auto) return type for WITH_LOCK #20495
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
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
Code review ACK 50dee5a Thanks for the excellent explanation! It's not important, but Another really good reference on #include <iostream>
#include <mutex>
#define LOCK(cs) (void(cs))
#define WITH_LOCK(cs, code) [&]() -> decltype(auto) { LOCK(cs); code; }()
int main()
{
std::mutex l;
int i = WITH_LOCK(l, {int i = 1; return i;}); // this is fine, auto(decltype(i)) is an int, so this returns by value
std::cout << "i = " << i << std::endl;
int j = WITH_LOCK(l, {int j = 1; return (j);}); // this isn't, auto(decltype((i)) is an &int, so this returns by reference to a local variable
std::cout << "j = " << j << std::endl; // segfault!! j is a dangling reference
return 0;
} This kind of bug is non-obvious from looking at the code, but it's detected by -Wreturn-local-addr, which I believe we have enabled by default:
|
Is this actually clearer or less fragile (or better in some other way?) than having the explicit |
I think the only benefit is when new code is written, it will less likely result in a compile error. So devs need to do the add- |
50dee5a
to
7a8f4b9
Compare
My intuition could be wrong, but I think that most people, when calling |
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.
Code review ACK 7a8f4b9, could squash.
@jnewbery I get a different warning:
And doesn't segfault. |
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.
utACK 7a8f4b9.
I'm +/-0 on adding the comment about decltype, since I don't think we need to document every quirk of c++ in our codebase. I don't think it does any harm though.
7a8f4b9
to
3216883
Compare
Now that we're using C++17, we can use the decltype(auto) return type (available since C++14) for functions and lambda expressions. As demonstrated in this commit, this can simplify cases where previously the compiler failed to deduce the correct return type. Just for reference, for the "assign to ref" cases fixed here, there are 3 possible solutions: - Return a pointer and immediately deref as used before this commit - Make sure the function/lambda returns declspec(auto) as used after this commit - Class& i = WITH_LOCK(..., return std::ref(...)); ----- References: 1. https://en.cppreference.com/w/cpp/language/function#Return_type_deduction 2. https://en.cppreference.com/w/cpp/language/template_argument_deduction#Other_contexts 3. https://en.cppreference.com/w/cpp/language/auto 4. https://en.cppreference.com/w/cpp/language/decltype Explanations: 1. https://stackoverflow.com/a/21369192 2. https://stackoverflow.com/a/21369170 3. Item 3 in Effective Modern C++ (Scott Meyers) via jnewbery
3216883
to
3eb94ec
Compare
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.
ACK 3eb94ec, I have reviewed the code and it looks OK, I agree it can be merged. I have verified possible warnings:
-Wreturn-stack-address
in clang-Wreturn-local-addr
in gcc
I think this change makes reading code which uses the WITH_LOCK
macro and reasoning about it easier.
utACK 3eb94ec |
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.
Code review ACK 3eb94ec
3eb94ec sync: Use decltype(auto) return type for WITH_LOCK (Carl Dong) Pull request description: > Now that we're using C++17, we can use the decltype(auto) return type > for functions and lambda expressions. > > As demonstrated in this commit, this can simplify cases where previously > the compiler failed to deduce the correct return type. > > Just for reference, for the "assign to ref" cases fixed here, there are > 3 possible solutions: > > - Return a pointer and immediately deref as used before this commit > - Make sure the function/lambda returns declspec(auto) as used after > this commit > - Class& i = WITH_LOCK(..., return std::ref(...)); > > ----- > > References: > 1. https://en.cppreference.com/w/cpp/language/function#Return_type_deduction > 2. https://en.cppreference.com/w/cpp/language/template_argument_deduction#Other_contexts > 3. https://en.cppreference.com/w/cpp/language/auto > 4. https://en.cppreference.com/w/cpp/language/decltype > > Explanations: > 1. https://stackoverflow.com/a/21369192 > 2. https://stackoverflow.com/a/21369170 Thanks to sipa and ryanofsky for helping me understand this ACKs for top commit: jnewbery: utACK 3eb94ec hebasto: ACK 3eb94ec, I have reviewed the code and it looks OK, I agree it can be merged. I have verified possible warnings: ryanofsky: Code review ACK 3eb94ec Tree-SHA512: 5f55c7722aeca8ea70e5c1a8db93e93ba0e356e8967e7f607ada38003df4b153d73c29bd2cea8d7ec1344720d37d857ea7dbfd2a88da1d92e0e9cbb9abd287df
review ACK 3eb94ec |
Thanks to sipa and ryanofsky for helping me understand this