Skip to content

Commit 8b2cd0e

Browse files
committed
P2809R3 Trivial infinite loops are not Undefined Behavior
1 parent 9ec133c commit 8b2cd0e

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
@@ -6448,13 +6448,18 @@
64486448
following:
64496449
\begin{itemize}
64506450
\item terminate,
6451+
\item invoke the function \tcode{std::this_thread::yield}\iref{thread.thread.this},
64516452
\item make a call to a library I/O function,
6452-
\item perform an access through a volatile glvalue, or
6453-
\item perform a synchronization operation or an atomic operation.
6453+
\item perform an access through a volatile glvalue,
6454+
\item perform a synchronization operation or an atomic operation, or
6455+
\item continue execution of a trivial infinite loop\iref{stmt.iter.general}.
64546456
\end{itemize}
64556457
\begin{note}
6456-
This is intended to allow compiler transformations such as removal of
6457-
empty loops, even when termination cannot be proven.
6458+
This is intended to allow compiler transformations
6459+
such as removal, merging, and reordering of empty loops,
6460+
even when termination cannot be proven.
6461+
An affordance is made for trivial infinite loops,
6462+
which cannot be removed nor reordered.
64586463
\end{note}
64596464

64606465
\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)