Skip to content
Snippets Groups Projects
Commit 206dff96 authored by Jasper Clemens Gräflich's avatar Jasper Clemens Gräflich
Browse files

Add subsubsection on generalized TPB

parent d080649c
No related branches found
No related tags found
No related merge requests found
Pipeline #10764 passed
......@@ -291,7 +291,7 @@
organization = {IEEE}
}
@misc{enwiki:1071814133,
@misc{enwiki:1071814133,
author = {{Wikipedia contributors}},
title = {Horn clause --- {Wikipedia}{,} The Free Encyclopedia},
year = {2022},
......@@ -316,4 +316,12 @@
booktitle = {Proceedings of the ACM SIGPLAN 2002 Conference on Programming language design and implementation},
pages = {282--293},
year = {2002}
}
@misc{rfc2025nestedmethodcalls,
author = {{Matsakis, Niko}},
title = {{RFC: Enable nested method calls}},
year = {{2017}},
howpublished = {\url{https://github.com/rust-lang/rfcs/pull/2025}},
note = {[Online; accessed 10-August-2022]}
}
\ No newline at end of file
......@@ -474,11 +474,26 @@ let vlen = v.len();
v.push(vlen);
\end{lstlisting}
This pattern—calling a method with an exclusive reference but also using shared references in the arguments—is very common and the workaround is unwieldy. Therefore \acs{RFC} 2025 \todo{Add reference for RFC 2025} introduces the concept of a \ac{TPB}. In it, the lifetime of an exclusive reference is split up into two phases. During the \emph{reservation phase}, it is treated like a shared reference, meaning more shared references can be created and reads are possible. The \emph{activation} happens as soon as the reference is first used to perform a mutation. From this point on, it is treated as a full exclusive reference.
This pattern—calling a method with an exclusive reference but also using shared references in the arguments—is very common and the workaround is unwieldy. Therefore \acs{RFC} 2025 \cite{rfc2025nestedmethodcalls} introduces the concept of a \ac{TPB}. In it, the lifetime of an exclusive reference is split up into two phases. During the \emph{reservation phase}, it is treated like a shared reference, meaning more shared references can be created and reads are possible. The \emph{activation} happens as soon as the reference is first used to perform a mutation. From this point on, it is treated as a full exclusive reference.
Right now, two-phase borrowing works only for method calls where the first argument is \inline{&mut self}, and it is not resolved if generalized \ac{TPB} should even be supported. \todo{Point to Issue \#49434 for generalized TPB.}
\subsubsection{Generalized \ac{TPB}}
\todo[inline]{Excurse on multi-phase borrowing}
Right now, a \ac{TPB} may only happen in a few specific places, namely when calling a method with \inline{&mut self} as first argument and with assignment operators (\inline{+=}, \inline{*=}, …), and the other references can only be shared. There are several ways in which the requirements can be relaxed and which are discussed in the RFC and Issue \#49434\footnote{\url{https://github.com/rust-lang/rust/issues/49434}}.
Currently, the only place an exclusive reference is allowed is in the \inline{&mut self} position. This means that it is not possible to use an exclusive reference twice, and a reborrow is also not possible because the references have no names. One could relax this and allow exclusive references everywhere, and also mix shared and exclusive references. This poses the difficulty that a shared reference (or one during reservation) can observe change. Resolving this makes the model more complicated.
In a similar vein, \ac{TPB} could be expanded to all function calls or even in inline code, as shown in \autoref{lst:inline-tpb}. Here, creating a shared reference is not allowed because an exclusive one already exists and is live. With generalized \ac{TPB} it would be okay, though, since \inline{xm} is only in reservation phase at that point.
\begin{lstlisting}[language=Rust, caption={Example of inline \ac{TPB}}, label={lst:inline-tpb}]
let mut x = 1;
let xm = &mut x;
let xr = &x; // Error
*xm = 2;
\end{lstlisting}
Even further, \emph{discontinuous borrows} would make it possible that a borrow is only active at exactly the points at which it is used and deactivated in between. Also, if the mutation capabilities of the reference is not used anymore, it could be \emph{downgraded} to a shared reference: For example, a function could take an exclusive reference to a value but then returns only a shared one.
All of these changes would entail a big step up in complexity and surface area of the language without actually having much practical use since relevant situations are rare. They are discussed in detail in \cite{rfc2025nestedmethodcalls}.
\subsection{Loans and Regions}
......
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment