Skip to content

P1774R8 Portable assumptions #5671

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

Merged
merged 1 commit into from
Aug 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions source/declarations.tex
Original file line number Diff line number Diff line change
Expand Up @@ -8529,6 +8529,43 @@
\end{codeblock}
\end{example}

\rSec2[dcl.attr.assume]{Assumption attribute}

The \grammarterm{attribute-token} \tcode{assume} may be applied to a null statement;
such a statement is an \defn{assumption}.
An \grammarterm{attribute-argument-clause} shall be present and
shall have the form:
\begin{ncsimplebnf}
\terminal{(} conditional-expression \terminal{)}
\end{ncsimplebnf}
The expression is contextually converted to \tcode{bool}\iref{conv.general}.
The expression is not evaluated.
If the converted expression would evaluate to \tcode{true}
at the point where the assumption appears,
the assumption has no effect.
Otherwise, the behavior is undefined.
\begin{note}
The expression is potentially evaluated\iref{basic.def.odr}.
The use of assumptions is intended to allow implementations
to analyze the form of the expression and
deduce information used to optimize the program.
Implementations are not required to deduce
any information from any particular assumption.
\end{note}
\begin{example}
\begin{codeblock}
int divide_by_32(int x) {
[[assume(x >= 0)]];
return x/32; // The instructions produced for the division
// may omit handling of negative values.
}
int f(int y) {
[[assume(++y == 43)]]; // \tcode{y} is not incremented
return y; // statement may be replaced with \tcode{return 42;}
}
\end{codeblock}
\end{example}

\rSec2[dcl.attr.depend]{Carries dependency attribute}%
\indextext{attribute!carries dependency}

Expand Down
31 changes: 24 additions & 7 deletions source/expressions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -7359,7 +7359,8 @@

\item
an operation that would have undefined behavior
as specified in \ref{intro} through \ref{cpp};
as specified in \ref{intro} through \ref{cpp},
excluding \ref{dcl.attr.assume};
\begin{footnote}
This includes,
for example, signed integer overflow\iref{expr.pre}, certain
Expand Down Expand Up @@ -7506,12 +7507,28 @@
a \keyword{goto} statement\iref{stmt.goto}.
\end{itemize}

If $E$ satisfies the constraints of a core constant expression, but
evaluation of $E$ would evaluate an operation that has undefined behavior
as specified in \ref{library} through \ref{\lastlibchapter}, or
an invocation of the \tcode{va_start} macro\iref{cstdarg.syn},
it is unspecified whether $E$ is a core constant expression.

It is unspecified whether $E$ is a core constant expression
if $E$ satisfies the constraints of a core constant expression, but
evaluation of $E$ would evaluate
\begin{itemize}
\item
an operation that has undefined behavior
as specified in \ref{library} through \ref{\lastlibchapter},
\item
an invocation of the \tcode{va_start} macro\iref{cstdarg.syn}, or
\item
a statement with an assumption\iref{dcl.attr.assume}
whose converted \grammarterm{conditional-expression},
if evaluated where the assumption appears,
would not disqualify $E$ from being a core constant expression and
would not evaluate to \tcode{true}.
\begin{note}
$E$ is not disqualified from being a core constant expression
if the hypothetical evaluation of
the converted \grammarterm{conditional-expression}
would disqualify $E$ from being a core constant expression.
\end{note}
\end{itemize}
\begin{example}
\begin{codeblock}
int x; // not constant
Expand Down