-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
return
statements in statement expressions in unevaluated contexts are ignored
#77808
Comments
@llvm/issue-subscribers-clang-frontend Author: Mital Ashok (MitalAshok)
Related: #77754
struct unrelated_type {};
int main() {
void(* p)() noexcept = []() noexcept(({ return unrelated_type{}; true; })) {};
bool(* q)() = []() -> decltype(({ return unrelated_type{}; true; })) { return true; };
unrelated_type(* r)() = []() -> decltype(({ return 0; unrelated_type{}; })) { return {}; };
} This compiles on Clang 18 (
|
So the gcc documentation does not cover this case at all: https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html In general, I think we presume that gcc's implementation is the documentation for a feature but maybe it is worth asking if this is the intent or not. |
Note statement expressions for C++ for GCC is underdocumented and even has many issues reported too. See https://gcc.gnu.org/bugzilla/show_bug.cgi?id=772 for some of them. |
So reading https://gcc.gnu.org/bugzilla/show_bug.cgi?id=772, jumping into and out of a statement expression should be rejected (though it is not currently with GCC's C++ front-end but is with GCC's C front-end). So this basically means the testcase should be rejected for all cases. |
Disallowing jumps out of statement expressions is going to break significant amounts of real world code. For example, error handling macros sometimes return from statement expressions, and Disallowing jumps into statement expressions seems like something we absolutely should do. I think it'd also be a good idea to warn on control flow out of a statement expression. |
It doesn't seem to be the case that it's rejected by GCC's C frontend: https://godbolt.org/z/8YhPv9erb |
Disallowing jumps out of a statement expressions that are in a decltype/noexcept/typeof/sizeof/etc seems like a good idea too! |
Jumps out of statement expressions are deeply weird, part I'm wary of breaking a bunch of real world code, but at the same time, I'm not super comfortable defining the behavior of jumping out of a statement expression and having to support that forever. There are so many weird edge cases to have to reason about and it makes adding new language features to C and C++ somewhat harder because it might restrict what designs we can support. Perhaps we could limit support for the "feature" to only the situations we believe are in common use and explicitly deprecate those uses (and make it an error in all other circumstances)? |
I think it might make to base the restrictions here on the restrictions as for
If we need more restrictions than that, we presumably also need those restrictions for The "outside a handler" part seems unnecessary here (I don't think there are new challenges compared to those we already need to handle in [except.pre]/3, and I think the |
Related: #77754
This compiles on Clang 18 (
-std=c++17 -x c++ -fsyntax-only
).p
andq
should definitely not compile: Either thereturn
applies to the new lambda scope (in which case, it is an incompatible type tovoid
/bool
), or it applies toint main()
(where it is still incompatible withint
). In either case, it should not be a constant expression forp
'snoexcept
specifier.r
may or may not be correct, depending on what scope thereturn
should return from.The text was updated successfully, but these errors were encountered: