Skip to content

Commit a65cdbc

Browse files
jensmaurertkoeppe
authored andcommitted
P2809R3 Trivial infinite loops are not Undefined Behavior
1 parent 51ddb91 commit a65cdbc

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

source/basic.tex

Lines changed: 9 additions & 4 deletions
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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,9 +543,40 @@
543543
int i;
544544
}
545545
\end{codeblock}
546+
546547
Thus after the \keyword{while} statement, \tcode{i} is no longer in scope.
547548
\end{example}
548549

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

0 commit comments

Comments
 (0)