Skip to content

Commit cc1f872

Browse files
authored
Merge 2024-03 CWG Motion 6
P2809R3 Trivial infinite loops are not Undefined Behavior
2 parents 51ddb91 + 29fcc1c commit cc1f872

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

source/basic.tex

+9-4
Original file line numberDiff line numberDiff line change
@@ -6478,13 +6478,18 @@
64786478
following:
64796479
\begin{itemize}
64806480
\item terminate,
6481+
\item invoke the function \tcode{std::this_thread::yield}\iref{thread.thread.this},
64816482
\item make a call to a library I/O function,
6482-
\item perform an access through a volatile glvalue, or
6483-
\item perform a synchronization operation or an atomic operation.
6483+
\item perform an access through a volatile glvalue,
6484+
\item perform a synchronization operation or an atomic operation, or
6485+
\item continue execution of a trivial infinite loop\iref{stmt.iter.general}.
64846486
\end{itemize}
64856487
\begin{note}
6486-
This is intended to allow compiler transformations such as removal of
6487-
empty loops, even when termination cannot be proven.
6488+
This is intended to allow compiler transformations
6489+
such as removal, merging, and reordering of empty loops,
6490+
even when termination cannot be proven.
6491+
An affordance is made for trivial infinite loops,
6492+
which cannot be removed nor reordered.
64886493
\end{note}
64896494

64906495
\pnum

source/statements.tex

+30
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,36 @@
546546
Thus after the \keyword{while} statement, \tcode{i} is no longer in scope.
547547
\end{example}
548548

549+
\pnum
550+
A \defnadj{trivially empty}{iteration statement} is
551+
an iteration statement matching one of the following forms:
552+
\begin{itemize}
553+
\item \tcode{while (} \grammarterm{expression} \tcode{) ;}
554+
\item \tcode{while (} \grammarterm{expression} \tcode{) \{ \}}
555+
\item \tcode{do ; while (} \grammarterm{expression} \tcode{) ;}
556+
\item \tcode{do \{ \} while (} \grammarterm{expression} \tcode{) ;}
557+
\item \tcode{for (} \grammarterm{init-statement} \opt{\grammarterm{expression}} \tcode{; ) ;}
558+
\item \tcode{for (} \grammarterm{init-statement} \opt{\grammarterm{expression}} \tcode{; ) \{ \}}
559+
\end{itemize}
560+
The \defnadj{controlling}{expression} of a trivially empty iteration statement
561+
is the \grammarterm{expression} of
562+
a \tcode{while}, \tcode{do}, or \tcode{for} statement
563+
(or \tcode{true}, if the \tcode{for} statement has no \grammarterm{expression}).
564+
A \defnadj{trivial infinite}{loop} is a trivially empty iteration statement
565+
for which the converted controlling expression is a constant expression,
566+
when interpreted as a \grammarterm{constant-expression}\iref{expr.const}, and
567+
evaluates to \tcode{true}.
568+
The \grammarterm{statement} of a trivial infinite loop is replaced with
569+
a call to the function \tcode{std::this_thread::yield}\iref{thread.thread.this};
570+
it is implementation-defined whether this replacement occurs
571+
on freestanding implementations.
572+
\begin{note}
573+
In a freestanding environment,
574+
concurrent forward progress is not guaranteed;
575+
such systems therefore require explicit cooperation.
576+
A call to yield can add implicit cooperation where none is otherwise intended.
577+
\end{note}
578+
549579
\rSec2[stmt.while]{The \keyword{while} statement}%
550580
\indextext{statement!\idxcode{while}}
551581

0 commit comments

Comments
 (0)