Skip to content
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

CWG2944 [expr.throw] Throw expressions should be indivisible #626

Open
Eisenwave opened this issue Oct 23, 2024 · 3 comments
Open

CWG2944 [expr.throw] Throw expressions should be indivisible #626

Eisenwave opened this issue Oct 23, 2024 · 3 comments

Comments

@Eisenwave
Copy link

Eisenwave commented Oct 23, 2024

Reference (section label): [expr.throw]

Issue description

It is unclear what happens when two throw-expressions are unsequenced, such as in:

(throw /* ... */, 0) + (throw /* ... */, 0);

As per [except.throw] paragraph 1, this results in two unsequenced transfers of control to the caller, contrary to co_await expressions, which are indivisible and non-interleavable following CWG2466 ([intro.execution] paragraph 11).

Suggested resolution

Update [intro.execution] paragraph 11 as follows:

For each function invocation, evaluation of a throw-expression, or evaluation of an await-expression F, each evaluation that does not occur within F but is evaluated on the same thread and as part of the same signal handler (if any) is either sequenced before all evaluations that occur within F or sequenced after all evaluations that occur within F;

Analogous to [expr.await] Note 2, append a note to [expr.throw] paragraph 2 as follows:

[...] from the (possibly converted) operand.

[Note: With respect to sequencing, a throw-expression is indivisible ([intro.execution]). — end note]

@frederick-vs-ja
Copy link

Consider (throw a() + b(), 0) + (throw c() + d(), 0), IIUC currently a(), b(), c(), and d() can be executed in arbitrary order. I'm not sure whether such reordering is desired, but if so, we may need to say that the transfer of control but not the whole throw-expression is indivisible.

I wonder whether the current wording even allows partial side effect on a memory location during throwing. Perhaps this also needs to be disallowed if it's not so.

@Eisenwave
Copy link
Author

Eisenwave commented Oct 23, 2024

I'm not sure whether such reordering is desired, but if so, we may need to say that the transfer of control but not the whole throw-expression is indivisible.

I have considered this, and similar to co_await being indivisible, I do think that all of throw a() + b() should be indivisible. For virtually all use cases, it would be pointless to interleave another evaluation between the construction of the exception object and throwing it.

Once the program constructs an exception, it is already on a path towards throwing it, and little or nothing can be gained from doing something else first (nor does it seem like the user would ever except that to happen).

Also, this is all purely hypothetical because throw expressions where they're indeterminately sequenced, let alone unsequenced should be vaporized in code review. No one seriously writes this.

@jensmaurer jensmaurer changed the title [expr.throw] Throw expressions should be indivisible CWG2944 [expr.throw] Throw expressions should be indivisible Oct 28, 2024
@jensmaurer
Copy link
Member

CWG2944

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants