blob: 5c191bf36961bdaac4362571126959f215be3234 [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en" class="light sidebar-visible" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>Async closures/&quot;coroutine-closures&quot; - Rust Compiler Development Guide</title>
<!-- Custom HTML head -->
<meta name="description" content="A guide to developing the Rust compiler (rustc)">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="favicon.svg">
<link rel="shortcut icon" href="favicon.png">
<link rel="stylesheet" href="css/variables.css">
<link rel="stylesheet" href="css/general.css">
<link rel="stylesheet" href="css/chrome.css">
<link rel="stylesheet" href="css/print.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="highlight-css" href="highlight.css">
<link rel="stylesheet" id="tomorrow-night-css" href="tomorrow-night.css">
<link rel="stylesheet" id="ayu-highlight-css" href="ayu-highlight.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="pagetoc.css">
<!-- Provide site root and default themes to javascript -->
<script>
const path_to_root = "";
const default_light_theme = "light";
const default_dark_theme = "navy";
window.path_to_searchindex_js = "searchindex.js";
</script>
<!-- Start loading toc.js asap -->
<script src="toc.js"></script>
</head>
<body>
<div id="mdbook-help-container">
<div id="mdbook-help-popup">
<h2 class="mdbook-help-title">Keyboard shortcuts</h2>
<div>
<p>Press <kbd></kbd> or <kbd></kbd> to navigate between chapters</p>
<p>Press <kbd>S</kbd> or <kbd>/</kbd> to search in the book</p>
<p>Press <kbd>?</kbd> to show this help</p>
<p>Press <kbd>Esc</kbd> to hide this help</p>
</div>
</div>
</div>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
let theme = localStorage.getItem('mdbook-theme');
let sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
let theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
const html = document.documentElement;
html.classList.remove('light')
html.classList.add(theme);
html.classList.add("js");
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
sidebar_toggle.checked = false;
}
if (sidebar === 'visible') {
sidebar_toggle.checked = true;
} else {
html.classList.remove('sidebar-visible');
}
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<!-- populated by js -->
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
<noscript>
<iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
</noscript>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Rust Compiler Development Guide</h1>
<div class="right-buttons">
<a href="print.html" title="Print this book" aria-label="Print this book">
<i id="print-button" class="fa fa-print"></i>
</a>
<a href="https://github.com/rust-lang/rustc-dev-guide" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
<a href="https://github.com/rust-lang/rustc-dev-guide/edit/master/src/coroutine-closures.md" title="Suggest an edit" aria-label="Suggest an edit" rel="edit">
<i id="git-edit-button" class="fa fa-edit"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<div class="search-wrapper">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
<div class="spinner-wrapper">
<i class="fa fa-spinner fa-spin"></i>
</div>
</div>
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="async-closurescoroutine-closures"><a class="header" href="#async-closurescoroutine-closures">Async closures/"coroutine-closures"</a></h1>
<p>Please read <a href="https://rust-lang.github.io/rfcs/3668-async-closures.html">RFC 3668</a> to understand the general motivation of the feature. This is a very technical and somewhat "vertical" chapter; ideally we'd split this and sprinkle it across all the relevant chapters, but for the purposes of understanding async closures <em>holistically</em>, I've put this together all here in one chapter.</p>
<h2 id="coroutine-closures----a-technical-deep-dive"><a class="header" href="#coroutine-closures----a-technical-deep-dive">Coroutine-closures -- a technical deep dive</a></h2>
<p>Coroutine-closures are a generalization of async closures, being special syntax for closure expressions which return a coroutine, notably one that is allowed to capture from the closure's upvars.</p>
<p>For now, the only usable kind of coroutine-closure is the async closure, and supporting async closures is the extent of this PR. We may eventually support <code>gen || {}</code>, etc., and most of the problems and curiosities described in this document apply to all coroutine-closures in general.</p>
<p>As a consequence of the code being somewhat general, this document may flip between calling them "async closures" and "coroutine-closures". The future that is returned by the async closure will generally be called the "coroutine" or the "child coroutine".</p>
<h3 id="hir"><a class="header" href="#hir">HIR</a></h3>
<p>Async closures (and in the future, other coroutine flavors such as <code>gen</code>) are represented in HIR as a <code>hir::Closure</code>.
The closure-kind of the <code>hir::Closure</code> is <code>ClosureKind::CoroutineClosure(_)</code><sup class="footnote-reference" id="fr-k1-1"><a href="#footnote-k1">1</a></sup>, which wraps an async block, which is also represented in HIR as a <code>hir::Closure</code>.
The closure-kind of the async block is <code>ClosureKind::Closure(CoroutineKind::Desugared(_, CoroutineSource::Closure))</code><sup class="footnote-reference" id="fr-k2-1"><a href="#footnote-k2">2</a></sup>.</p>
<p>Like <code>async fn</code>, when lowering an async closure's body, we need to unconditionally move all of the closures arguments into the body so they are captured. This is handled by <code>lower_coroutine_body_with_moved_arguments</code><sup class="footnote-reference" id="fr-l1-1"><a href="#footnote-l1">3</a></sup>. The only notable quirk with this function is that the async block we end up generating as a capture kind of <code>CaptureBy::ByRef</code><sup class="footnote-reference" id="fr-l2-1"><a href="#footnote-l2">4</a></sup>. We later force all of the <em>closure args</em> to be captured by-value<sup class="footnote-reference" id="fr-l3-1"><a href="#footnote-l3">5</a></sup>, but we don't want the <em>whole</em> async block to act as if it were an <code>async move</code>, since that would defeat the purpose of the self-borrowing of an async closure.</p>
<h3 id="rustc_middlety-representation"><a class="header" href="#rustc_middlety-representation"><code>rustc_middle::ty</code> Representation</a></h3>
<p>For the purposes of keeping the implementation mostly future-compatible (i.e. with gen <code>|| {}</code> and <code>async gen || {}</code>), most of this section calls async closures "coroutine-closures".</p>
<p>The main thing that this PR introduces is a new <code>TyKind</code> called <code>CoroutineClosure</code><sup class="footnote-reference" id="fr-t1-1"><a href="#footnote-t1">6</a></sup> and corresponding variants on other relevant enums in typeck and borrowck (<code>UpvarArgs</code>, <code>DefiningTy</code>, <code>AggregateKind</code>).</p>
<p>We introduce a new <code>TyKind</code> instead of generalizing the existing <code>TyKind::Closure</code> due to major representational differences in the type. The major differences between <code>CoroutineClosure</code>s can be explored by first inspecting the <code>CoroutineClosureArgsParts</code>, which is the "unpacked" representation of the coroutine-closure's generics.</p>
<h4 id="similarities-to-closures"><a class="header" href="#similarities-to-closures">Similarities to closures</a></h4>
<p>Like a closure, we have <code>parent_args</code>, a <code>closure_kind_ty</code>, and a <code>tupled_upvars_ty</code>. These represent the same thing as their closure counterparts; namely: the generics inherited from the body that the closure is defined in, the maximum "calling capability" of the closure (i.e. must it be consumed to be called, like <code>FnOnce</code>, or can it be called by-ref), and the captured upvars of the closure itself.</p>
<h4 id="the-signature"><a class="header" href="#the-signature">The signature</a></h4>
<p>A traditional closure has a <code>fn_sig_as_fn_ptr_ty</code> which it uses to represent the signature of the closure. In contrast, we store the signature of a coroutine closure in a somewhat "exploded" way, since coroutine-closures have <em>two</em> signatures depending on what <code>AsyncFn*</code> trait you call it with (see below sections).</p>
<p>Conceptually, the coroutine-closure may be thought as containing several different signature types depending on whether it is being called by-ref or by-move.</p>
<p>To conveniently recreate both of these signatures, the <code>signature_parts_ty</code> stores all of the relevant parts of the coroutine returned by this coroutine-closure. This signature parts type will have the general shape of <code>fn(tupled_inputs, resume_ty) -&gt; (return_ty, yield_ty)</code>, where <code>resume_ty</code>, <code>return_ty</code>, and <code>yield_ty</code> are the respective types for the <em>coroutine</em> returned by the coroutine-closure<sup class="footnote-reference" id="fr-c1-1"><a href="#footnote-c1">7</a></sup>.</p>
<p>The compiler mainly deals with the <code>CoroutineClosureSignature</code> type<sup class="footnote-reference" id="fr-c2-1"><a href="#footnote-c2">8</a></sup>, which is created by extracting the relevant types out of the <code>fn()</code> ptr type described above, and which exposes methods that can be used to construct the <em>coroutine</em> that the coroutine-closure ultimately returns.</p>
<h4 id="the-data-we-need-to-carry-along-to-construct-a-coroutine-return-type"><a class="header" href="#the-data-we-need-to-carry-along-to-construct-a-coroutine-return-type">The data we need to carry along to construct a <code>Coroutine</code> return type</a></h4>
<p>Along with the data stored in the signature, to construct a <code>TyKind::Coroutine</code> to return, we also need to store the "witness" of the coroutine.</p>
<p>So what about the upvars of the <code>Coroutine</code> that is returned? Well, for <code>AsyncFnOnce</code> (i.e. call-by-move), this is simply the same upvars that the coroutine returns. But for <code>AsyncFnMut</code>/<code>AsyncFn</code>, the coroutine that is returned from the coroutine-closure borrows data from the coroutine-closure with a given "environment" lifetime<sup class="footnote-reference" id="fr-c3-1"><a href="#footnote-c3">9</a></sup>. This corresponds to the <code>&amp;self</code> lifetime<sup class="footnote-reference" id="fr-c4-1"><a href="#footnote-c4">10</a></sup> on the <code>AsyncFnMut</code>/<code>AsyncFn</code> call signature, and the GAT lifetime of the <code>ByRef</code><sup class="footnote-reference" id="fr-c5-1"><a href="#footnote-c5">11</a></sup>.</p>
<h4 id="actually-getting-the-coroutine-return-types"><a class="header" href="#actually-getting-the-coroutine-return-types">Actually getting the coroutine return type(s)</a></h4>
<p>To most easily construct the <code>Coroutine</code> that a coroutine-closure returns, you can use the <code>to_coroutine_given_kind_and_upvars</code><sup class="footnote-reference" id="fr-helper-1"><a href="#footnote-helper">12</a></sup> helper on <code>CoroutineClosureSignature</code>, which can be acquired from the <code>CoroutineClosureArgs</code>.</p>
<p>Most of the args to that function will be components that you can get out of the <code>CoroutineArgs</code>, except for the <code>goal_kind: ClosureKind</code> which controls which flavor of coroutine to return based off of the <code>ClosureKind</code> passed in -- i.e. it will prepare the by-ref coroutine if <code>ClosureKind::Fn | ClosureKind::FnMut</code>, and the by-move coroutine if <code>ClosureKind::FnOnce</code>.</p>
<h3 id="trait-hierarchy"><a class="header" href="#trait-hierarchy">Trait Hierarchy</a></h3>
<p>We introduce a parallel hierarchy of <code>Fn*</code> traits that are implemented for . The motivation for the introduction was covered in a blog post: <a href="https://hackmd.io/@compiler-errors/async-closures">Async Closures</a>.</p>
<p>All currently-stable callable types (i.e., closures, function items, function pointers, and <code>dyn Fn*</code> trait objects) automatically implement <code>AsyncFn*() -&gt; T</code> if they implement <code>Fn*() -&gt; Fut</code> for some output type <code>Fut</code>, and <code>Fut</code> implements <code>Future&lt;Output = T&gt;</code><sup class="footnote-reference" id="fr-tr1-1"><a href="#footnote-tr1">13</a></sup>.</p>
<p>Async closures implement <code>AsyncFn*</code> as their bodies permit; i.e. if they end up using upvars in a way that is compatible (i.e. if they consume or mutate their upvars, it may affect whether they implement <code>AsyncFn</code> and <code>AsyncFnMut</code>...)</p>
<h4 id="lending"><a class="header" href="#lending">Lending</a></h4>
<p>We may in the future move <code>AsyncFn*</code> onto a more general set of <code>LendingFn*</code> traits; however, there are some concrete technical implementation details that limit our ability to use <code>LendingFn</code> ergonomically in the compiler today. These have to do with:</p>
<ul>
<li>Closure signature inference.</li>
<li>Limitations around higher-ranked trait bounds.</li>
<li>Shortcomings with error messages.</li>
</ul>
<p>These limitations, plus the fact that the underlying trait should have no effect on the user experience of async closures and async <code>Fn</code> trait bounds, leads us to <code>AsyncFn*</code> for now. To ensure we can eventually move to these more general traits, the precise <code>AsyncFn*</code> trait definitions (including the associated types) are left as an implementation detail.</p>
<h4 id="when-do-async-closures-implement-the-regular-fn-traits"><a class="header" href="#when-do-async-closures-implement-the-regular-fn-traits">When do async closures implement the regular <code>Fn*</code> traits?</a></h4>
<p>We mention above that "regular" callable types can implement <code>AsyncFn*</code>, but the reverse question exists of "can async closures implement <code>Fn*</code> too"? The short answer is "when it's valid", i.e. when the coroutine that would have been returned from <code>AsyncFn</code>/<code>AsyncFnMut</code> does not actually have any upvars that are "lent" from the parent coroutine-closure.</p>
<p>See the "follow-up: when do..." section below for an elaborated answer. The full answer describes a pretty interesting and hopefully thorough heuristic that is used to ensure that most async closures "just work".</p>
<h3 id="tale-of-two-bodies"><a class="header" href="#tale-of-two-bodies">Tale of two bodies...</a></h3>
<p>When async closures are called with <code>AsyncFn</code>/<code>AsyncFnMut</code>, they return a coroutine that borrows from the closure. However, when they are called via <code>AsyncFnOnce</code>, we consume that closure, and cannot return a coroutine that borrows from data that is now dropped.</p>
<p>To work around this limitation, we synthesize a separate by-move MIR body for calling <code>AsyncFnOnce::call_once</code> on a coroutine-closure that can be called by-ref.</p>
<p>This body operates identically to the "normal" coroutine returned from calling the coroutine-closure, except for the fact that it has a different set of upvars, since we must <em>move</em> the captures from the parent coroutine-closure into the child coroutine.</p>
<h4 id="synthesizing-the-by-move-body"><a class="header" href="#synthesizing-the-by-move-body">Synthesizing the by-move body</a></h4>
<p>When we want to access the by-move body of the coroutine returned by a coroutine-closure, we can do so via the <code>coroutine_by_move_body_def_id</code><sup class="footnote-reference" id="fr-b1-1"><a href="#footnote-b1">14</a></sup> query.</p>
<p>This query synthesizes a new MIR body by copying the MIR body of the coroutine and inserting additional derefs and field projections<sup class="footnote-reference" id="fr-b2-1"><a href="#footnote-b2">15</a></sup> to preserve the semantics of the body.</p>
<p>Since we've synthesized a new def id, this query is also responsible for feeding a ton of other relevant queries for the MIR body. This query is <code>ensure()</code>d<sup class="footnote-reference" id="fr-b3-1"><a href="#footnote-b3">16</a></sup> during the <code>mir_promoted</code> query, since it operates on the <em>built</em> mir of the coroutine.</p>
<h3 id="closure-signature-inference"><a class="header" href="#closure-signature-inference">Closure signature inference</a></h3>
<p>The closure signature inference algorithm for async closures is a bit more complicated than the inference algorithm for "traditional" closures. Like closures, we iterate through all of the clauses that may be relevant (for the expectation type passed in)<sup class="footnote-reference" id="fr-deduce1-1"><a href="#footnote-deduce1">17</a></sup>.</p>
<p>To extract a signature, we consider two situations:</p>
<ul>
<li>Projection predicates with <code>AsyncFnOnce::Output</code>, which we will use to extract the inputs and output type for the closure. This corresponds to the situation that there was a <code>F: AsyncFn*() -&gt; T</code> bound<sup class="footnote-reference" id="fr-deduce2-1"><a href="#footnote-deduce2">18</a></sup>.</li>
<li>Projection predicates with <code>FnOnce::Output</code>, which we will use to extract the inputs. For the output, we also try to deduce an output by looking for relevant <code>Future::Output</code> projection predicates. This corresponds to the situation that there was an <code>F: Fn*() -&gt; T, T: Future&lt;Output = U&gt;</code> bound.<sup class="footnote-reference" id="fr-deduce3-1"><a href="#footnote-deduce3">19</a></sup>
<ul>
<li>If there is no <code>Future</code> bound, we simply use a fresh infer var for the output. This corresponds to the case where one can pass an async closure to a combinator function like <code>Option::map</code>.<sup class="footnote-reference" id="fr-deduce4-1"><a href="#footnote-deduce4">20</a></sup></li>
</ul>
</li>
</ul>
<p>We support the latter case simply to make it easier for users to simply drop-in <code>async || {}</code> syntax, even when they're calling an API that was designed before first-class <code>AsyncFn*</code> traits were available.</p>
<h4 id="calling-a-closure-before-its-kind-has-been-inferred"><a class="header" href="#calling-a-closure-before-its-kind-has-been-inferred">Calling a closure before its kind has been inferred</a></h4>
<p>We defer<sup class="footnote-reference" id="fr-call1-1"><a href="#footnote-call1">21</a></sup> the computation of a coroutine-closure's "kind" (i.e. its maximum calling mode: <code>AsyncFnOnce</code>/<code>AsyncFnMut</code>/<code>AsyncFn</code>) until the end of typeck. However, since we want to be able to call that coroutine-closure before the end of typeck, we need to come up with the return type of the coroutine-closure before that.</p>
<p>Unlike regular closures, whose return type does not change depending on what <code>Fn*</code> trait we call it with, coroutine-closures <em>do</em> end up returning different coroutine types depending on the flavor of <code>AsyncFn*</code> trait used to call it.</p>
<p>Specifically, while the def-id of the returned coroutine does not change, the upvars<sup class="footnote-reference" id="fr-call2-1"><a href="#footnote-call2">22</a></sup> (which are either borrowed or moved from the parent coroutine-closure) and the coroutine-kind<sup class="footnote-reference" id="fr-call3-1"><a href="#footnote-call3">23</a></sup> are dependent on the calling mode.</p>
<p>We introduce a <code>AsyncFnKindHelper</code> trait which allows us to defer the question of "does this coroutine-closure support this calling mode"<sup class="footnote-reference" id="fr-helper1-1"><a href="#footnote-helper1">24</a></sup> via a trait goal, and "what are the tupled upvars of this calling mode"<sup class="footnote-reference" id="fr-helper2-1"><a href="#footnote-helper2">25</a></sup> via an associated type, which can be computed by appending the input types of the coroutine-closure to either the upvars or the "by ref" upvars computed during upvar analysis.</p>
<h4 id="ok-so-why"><a class="header" href="#ok-so-why">Ok, so why?</a></h4>
<p>This seems a bit roundabout and complex, and I admit that it is. But let's think of the "do nothing" alternative -- we could instead mark all <code>AsyncFn*</code> goals as ambiguous until upvar analysis, at which point we would know exactly what to put into the upvars of the coroutine we return. However, this is actually <em>very</em> detrimental to inference in the program, since it means that programs like this would not be valid:</p>
<pre><pre class="playground"><code class="language-rust!"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>let c = async || -&gt; String { .. };
let s = c().await;
// ^^^ If we can't project `&lt;{c} as AsyncFn&gt;::call()` to a coroutine, then the `IntoFuture::into_future` call inside of the `.await` stalls, and the type of `s` is left unconstrained as an infer var.
s.as_bytes();
// ^^^ That means we can't call any methods on the awaited return of a coroutine-closure, like... at all!
<span class="boring">}</span></code></pre></pre>
<p>So <em>instead</em>, we use this alias (in this case, a projection: <code>AsyncFnKindHelper::Upvars&lt;'env, ...&gt;</code>) to delay the computation of the <em>tupled upvars</em> and give us something to put in its place, while still allowing us to return a <code>TyKind::Coroutine</code> (which is a rigid type) and we may successfully confirm the built-in traits we need (in our case, <code>Future</code>), since the <code>Future</code> implementation doesn't depend on the upvars at all.</p>
<h3 id="upvar-analysis"><a class="header" href="#upvar-analysis">Upvar analysis</a></h3>
<p>By and large, the upvar analysis for coroutine-closures and their child coroutines proceeds like normal upvar analysis. However, there are several interesting bits that happen to account for async closures' special natures:</p>
<h4 id="forcing-all-inputs-to-be-captured"><a class="header" href="#forcing-all-inputs-to-be-captured">Forcing all inputs to be captured</a></h4>
<p>Like async fn, all input arguments are captured. We explicitly force<sup class="footnote-reference" id="fr-f1-1"><a href="#footnote-f1">26</a></sup> all of these inputs to be captured by move so that the future coroutine returned by async closures does not depend on whether the input is <em>used</em> by the body or not, which would impart an interesting semver hazard.</p>
<h4 id="computing-the-by-ref-captures"><a class="header" href="#computing-the-by-ref-captures">Computing the by-ref captures</a></h4>
<p>For a coroutine-closure that supports <code>AsyncFn</code>/<code>AsyncFnMut</code>, we must also compute the relationship between the captures of the coroutine-closure and its child coroutine. Specifically, the coroutine-closure may <code>move</code> a upvar into its captures, but the coroutine may only borrow that upvar.</p>
<p>We compute the "<code>coroutine_captures_by_ref_ty</code>" by looking at all of the child coroutine's captures and comparing them to the corresponding capture of the parent coroutine-closure<sup class="footnote-reference" id="fr-br1-1"><a href="#footnote-br1">27</a></sup>. This <code>coroutine_captures_by_ref_ty</code> ends up being represented as a <code>for&lt;'env&gt; fn() -&gt; captures...</code> type, with the additional binder lifetime representing the "<code>&amp;self</code>" lifetime of calling <code>AsyncFn::async_call</code> or <code>AsyncFnMut::async_call_mut</code>. We instantiate that binder later when actually calling the methods.</p>
<p>Note that not every by-ref capture from the parent coroutine-closure results in a "lending" borrow. See the <strong>Follow-up: When do async closures implement the regular <code>Fn*</code> traits?</strong> section below for more details, since this intimately influences whether or not the coroutine-closure is allowed to implement the <code>Fn*</code> family of traits.</p>
<h4 id="by-move-body--fnonce-quirk"><a class="header" href="#by-move-body--fnonce-quirk">By-move body + <code>FnOnce</code> quirk</a></h4>
<p>There are several situations where the closure upvar analysis ends up inferring upvars for the coroutine-closure's child coroutine that are too relaxed, and end up resulting in borrow-checker errors. This is best illustrated via examples. For example, given:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>fn force_fnonce&lt;T: async FnOnce()&gt;(t: T) -&gt; T { t }
let x = String::new();
let c = force_fnonce(async move || {
println!("{x}");
});
<span class="boring">}</span></code></pre></pre>
<p><code>x</code> will be moved into the coroutine-closure, but the coroutine that is returned would only borrow <code>&amp;x</code>. However, since <code>force_fnonce</code> forces the coroutine-closure to <code>AsyncFnOnce</code>, which is not <em>lending</em>, we must force the capture to happen by-move<sup class="footnote-reference" id="fr-bm1-1"><a href="#footnote-bm1">28</a></sup>.</p>
<p>Similarly:</p>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>let x = String::new();
let y = String::new();
let c = async move || {
drop(y);
println!("{x}");
};
<span class="boring">}</span></code></pre></pre>
<p><code>x</code> will be moved into the coroutine-closure, but the coroutine that is returned would only borrow <code>&amp;x</code>. However, since we also capture <code>y</code> and drop it, the coroutine-closure is forced to be <code>AsyncFnOnce</code>. We must also force the capture of <code>x</code> to happen by-move. To determine this situation in particular, since unlike the last example the coroutine-kind's closure-kind has not yet been constrained, we must analyze the body of the coroutine-closure to see if how all of the upvars are used, to determine if they've been used in a way that is "consuming" -- i.e. that would force it to <code>FnOnce</code><sup class="footnote-reference" id="fr-bm2-1"><a href="#footnote-bm2">29</a></sup>.</p>
<h4 id="follow-up-when-do-async-closures-implement-the-regular-fn-traits"><a class="header" href="#follow-up-when-do-async-closures-implement-the-regular-fn-traits">Follow-up: When do async closures implement the regular <code>Fn*</code> traits?</a></h4>
<p>Well, first of all, all async closures implement <code>FnOnce</code> since they can always be called <em>at least once</em>.</p>
<p>For <code>Fn</code>/<code>FnMut</code>, the detailed answer involves answering a related question: is the coroutine-closure lending? Because if it is, then it cannot implement the non-lending <code>Fn</code>/<code>FnMut</code> traits.</p>
<p>Determining when the coroutine-closure must <em>lend</em> its upvars is implemented in the <code>should_reborrow_from_env_of_parent_coroutine_closure</code> helper function<sup class="footnote-reference" id="fr-u1-1"><a href="#footnote-u1">30</a></sup>. Specifically, this needs to happen in two places:</p>
<ol>
<li>Are we borrowing data owned by the parent closure? We can determine if that is the case by checking if the parent capture is by-move, EXCEPT if we apply a deref projection, which means we're reborrowing a reference that we captured by-move.</li>
</ol>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>let x = &amp;1i32; // Let's call this lifetime `'1`.
let c = async move || {
println!("{:?}", *x);
// Even though the inner coroutine borrows by ref, we're only capturing `*x`,
// not `x`, so the inner closure is allowed to reborrow the data for `'1`.
};
<span class="boring">}</span></code></pre></pre>
<ol start="2">
<li>If a coroutine is mutably borrowing from a parent capture, then that mutable borrow cannot live for longer than either the parent <em>or</em> the borrow that we have on the original upvar. Therefore we always need to borrow the child capture with the lifetime of the parent coroutine-closure's env.</li>
</ol>
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
</span><span class="boring">fn main() {
</span>let mut x = 1i32;
let c = async || {
x = 1;
// The parent borrows `x` for some `&amp;'1 mut i32`.
// However, when we call `c()`, we implicitly autoref for the signature of
// `AsyncFnMut::async_call_mut`. Let's call that lifetime `'call`. Since
// the maximum that `&amp;'call mut &amp;'1 mut i32` can be reborrowed is `&amp;'call mut i32`,
// the inner coroutine should capture w/ the lifetime of the coroutine-closure.
};
<span class="boring">}</span></code></pre></pre>
<p>If either of these cases apply, then we should capture the borrow with the lifetime of the parent coroutine-closure's env. Luckily, if this function is not correct, then the program is not unsound, since we still borrowck and validate the choices made from this function -- the only side-effect is that the user may receive unnecessary borrowck errors.</p>
<h3 id="instance-resolution"><a class="header" href="#instance-resolution">Instance resolution</a></h3>
<p>If a coroutine-closure has a closure-kind of <code>FnOnce</code>, then its <code>AsyncFnOnce::call_once</code> and <code>FnOnce::call_once</code> implementations resolve to the coroutine-closure's body<sup class="footnote-reference" id="fr-res1-1"><a href="#footnote-res1">31</a></sup>, and the <code>Future::poll</code> of the coroutine that gets returned resolves to the body of the child closure.</p>
<p>If a coroutine-closure has a closure-kind of <code>FnMut</code>/<code>Fn</code>, then the same applies to <code>AsyncFn</code> and the corresponding <code>Future</code> implementation of the coroutine that gets returned.<sup class="footnote-reference" id="fr-res1-2"><a href="#footnote-res1">31</a></sup> However, we use a MIR shim to generate the implementation of <code>AsyncFnOnce::call_once</code>/<code>FnOnce::call_once</code><sup class="footnote-reference" id="fr-res2-1"><a href="#footnote-res2">32</a></sup>, and <code>Fn::call</code>/<code>FnMut::call_mut</code> instances if they exist<sup class="footnote-reference" id="fr-res3-1"><a href="#footnote-res3">33</a></sup>.</p>
<p>This is represented by the <code>ConstructCoroutineInClosureShim</code><sup class="footnote-reference" id="fr-i1-1"><a href="#footnote-i1">34</a></sup>. The <code>receiver_by_ref</code> bool will be true if this is the instance of <code>Fn::call</code>/<code>FnMut::call_mut</code>.<sup class="footnote-reference" id="fr-i2-1"><a href="#footnote-i2">35</a></sup> The coroutine that all of these instances returns corresponds to the by-move body we will have synthesized by this point.<sup class="footnote-reference" id="fr-i3-1"><a href="#footnote-i3">36</a></sup></p>
<h3 id="borrow-checking"><a class="header" href="#borrow-checking">Borrow-checking</a></h3>
<p>It turns out that borrow-checking async closures is pretty straightforward. After adding a new <code>DefiningTy::CoroutineClosure</code><sup class="footnote-reference" id="fr-bck1-1"><a href="#footnote-bck1">37</a></sup> variant, and teaching borrowck how to generate the signature of the coroutine-closure<sup class="footnote-reference" id="fr-bck2-1"><a href="#footnote-bck2">38</a></sup>, borrowck proceeds totally fine.</p>
<p>One thing to note is that we don't borrow-check the synthetic body we make for by-move coroutines, since by construction (and the validity of the by-ref coroutine body it was derived from) it must be valid.</p>
<hr>
<ol class="footnote-definition"><li id="footnote-k1">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_ast_lowering/src/expr.rs#L1147">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_ast_lowering/src/expr.rs#L1147</a> <a href="#fr-k1-1"></a></p>
</li>
<li id="footnote-k2">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_ast_lowering/src/expr.rs#L1117">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_ast_lowering/src/expr.rs#L1117</a> <a href="#fr-k2-1"></a></p>
</li>
<li id="footnote-l1">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_ast_lowering/src/item.rs#L1096-L1100">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_ast_lowering/src/item.rs#L1096-L1100</a> <a href="#fr-l1-1"></a></p>
</li>
<li id="footnote-l2">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_ast_lowering/src/item.rs#L1276-L1279">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_ast_lowering/src/item.rs#L1276-L1279</a> <a href="#fr-l2-1"></a></p>
</li>
<li id="footnote-l3">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_hir_typeck/src/upvar.rs#L250-L256">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_hir_typeck/src/upvar.rs#L250-L256</a> <a href="#fr-l3-1"></a></p>
</li>
<li id="footnote-t1">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_type_ir/src/ty_kind.rs#L163-L168">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_type_ir/src/ty_kind.rs#L163-L168</a> <a href="#fr-t1-1"></a></p>
</li>
<li id="footnote-c1">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_type_ir/src/ty_kind/closure.rs#L221-L229">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_type_ir/src/ty_kind/closure.rs#L221-L229</a> <a href="#fr-c1-1"></a></p>
</li>
<li id="footnote-c2">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_type_ir/src/ty_kind/closure.rs#L362">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_type_ir/src/ty_kind/closure.rs#L362</a> <a href="#fr-c2-1"></a></p>
</li>
<li id="footnote-c3">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_type_ir/src/ty_kind/closure.rs#L447-L455">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_type_ir/src/ty_kind/closure.rs#L447-L455</a> <a href="#fr-c3-1"></a></p>
</li>
<li id="footnote-c4">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/library/core/src/ops/async_function.rs#L36">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/library/core/src/ops/async_function.rs#L36</a> <a href="#fr-c4-1"></a></p>
</li>
<li id="footnote-c5">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/library/core/src/ops/async_function.rs#L30">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/library/core/src/ops/async_function.rs#L30</a> <a href="#fr-c5-1"></a></p>
</li>
<li id="footnote-helper">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_type_ir/src/ty_kind/closure.rs#L419">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_type_ir/src/ty_kind/closure.rs#L419</a> <a href="#fr-helper-1"></a></p>
</li>
<li id="footnote-tr1">
<p><a href="https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs#L404-L409">https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_next_trait_solver/src/solve/assembly/structural_traits.rs#L404-L409</a> <a href="#fr-tr1-1"></a></p>
</li>
<li id="footnote-b1">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_mir_transform/src/coroutine/by_move_body.rs#L1-L70">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_mir_transform/src/coroutine/by_move_body.rs#L1-L70</a> <a href="#fr-b1-1"></a></p>
</li>
<li id="footnote-b2">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_mir_transform/src/coroutine/by_move_body.rs#L131-L195">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_mir_transform/src/coroutine/by_move_body.rs#L131-L195</a> <a href="#fr-b2-1"></a></p>
</li>
<li id="footnote-b3">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_mir_transform/src/lib.rs#L339-L342">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_mir_transform/src/lib.rs#L339-L342</a> <a href="#fr-b3-1"></a></p>
</li>
<li id="footnote-deduce1">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_hir_typeck/src/closure.rs#L345-L362">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_hir_typeck/src/closure.rs#L345-L362</a> <a href="#fr-deduce1-1"></a></p>
</li>
<li id="footnote-deduce2">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_hir_typeck/src/closure.rs#L486-L487">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_hir_typeck/src/closure.rs#L486-L487</a> <a href="#fr-deduce2-1"></a></p>
</li>
<li id="footnote-deduce3">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_hir_typeck/src/closure.rs#L517-L534">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_hir_typeck/src/closure.rs#L517-L534</a> <a href="#fr-deduce3-1"></a></p>
</li>
<li id="footnote-deduce4">
<p><a href="https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_hir_typeck/src/closure.rs#L575-L590">https://github.com/rust-lang/rust/blob/5ca0e9fa9b2f92b463a0a2b0b34315e09c0b7236/compiler/rustc_hir_typeck/src/closure.rs#L575-L590</a> <a href="#fr-deduce4-1"></a></p>
</li>
<li id="footnote-call1">
<p><a href="https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_hir_typeck/src/callee.rs#L169-L210">https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_hir_typeck/src/callee.rs#L169-L210</a> <a href="#fr-call1-1"></a></p>
</li>
<li id="footnote-call2">
<p><a href="https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_type_ir/src/ty_kind/closure.rs#L574-L576">https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_type_ir/src/ty_kind/closure.rs#L574-L576</a> <a href="#fr-call2-1"></a></p>
</li>
<li id="footnote-call3">
<p><a href="https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_type_ir/src/ty_kind/closure.rs#L554-L563">https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_type_ir/src/ty_kind/closure.rs#L554-L563</a> <a href="#fr-call3-1"></a></p>
</li>
<li id="footnote-helper1">
<p><a href="https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/library/core/src/ops/async_function.rs#L135-L144">https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/library/core/src/ops/async_function.rs#L135-L144</a> <a href="#fr-helper1-1"></a></p>
</li>
<li id="footnote-helper2">
<p><a href="https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/library/core/src/ops/async_function.rs#L146-L154">https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/library/core/src/ops/async_function.rs#L146-L154</a> <a href="#fr-helper2-1"></a></p>
</li>
<li id="footnote-f1">
<p><a href="https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_hir_typeck/src/upvar.rs#L250-L259">https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_hir_typeck/src/upvar.rs#L250-L259</a> <a href="#fr-f1-1"></a></p>
</li>
<li id="footnote-br1">
<p><a href="https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_hir_typeck/src/upvar.rs#L375-L471">https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_hir_typeck/src/upvar.rs#L375-L471</a> <a href="#fr-br1-1"></a></p>
</li>
<li id="footnote-bm1">
<p><a href="https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_hir_typeck/src/upvar.rs#L211-L248">https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_hir_typeck/src/upvar.rs#L211-L248</a> <a href="#fr-bm1-1"></a></p>
</li>
<li id="footnote-bm2">
<p><a href="https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_hir_typeck/src/upvar.rs#L532-L539">https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_hir_typeck/src/upvar.rs#L532-L539</a> <a href="#fr-bm2-1"></a></p>
</li>
<li id="footnote-u1">
<p><a href="https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_hir_typeck/src/upvar.rs#L1818-L1860">https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_hir_typeck/src/upvar.rs#L1818-L1860</a> <a href="#fr-u1-1"></a></p>
</li>
<li id="footnote-res1">
<p><a href="https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_ty_utils/src/instance.rs#L351">https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_ty_utils/src/instance.rs#L351</a> <a href="#fr-res1-1"></a> <a href="#fr-res1-2">↩2</a></p>
</li>
<li id="footnote-res2">
<p><a href="https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_ty_utils/src/instance.rs#L341-L349">https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_ty_utils/src/instance.rs#L341-L349</a> <a href="#fr-res2-1"></a></p>
</li>
<li id="footnote-res3">
<p><a href="https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_ty_utils/src/instance.rs#L312-L326">https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_ty_utils/src/instance.rs#L312-L326</a> <a href="#fr-res3-1"></a></p>
</li>
<li id="footnote-i1">
<p><a href="https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_middle/src/ty/instance.rs#L129-L134">https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_middle/src/ty/instance.rs#L129-L134</a> <a href="#fr-i1-1"></a></p>
</li>
<li id="footnote-i2">
<p><a href="https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_middle/src/ty/instance.rs#L136-L141">https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_middle/src/ty/instance.rs#L136-L141</a> <a href="#fr-i2-1"></a></p>
</li>
<li id="footnote-i3">
<p><a href="https://github.com/rust-lang/rust/blob/07cbbdd69363da97075650e9be24b78af0bcdd23/compiler/rustc_middle/src/ty/instance.rs#L841">https://github.com/rust-lang/rust/blob/07cbbdd69363da97075650e9be24b78af0bcdd23/compiler/rustc_middle/src/ty/instance.rs#L841</a> <a href="#fr-i3-1"></a></p>
</li>
<li id="footnote-bck1">
<p><a href="https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_borrowck/src/universal_regions.rs#L110-L115">https://github.com/rust-lang/rust/blob/705cfe0e966399e061d64dd3661bfbc57553ed87/compiler/rustc_borrowck/src/universal_regions.rs#L110-L115</a> <a href="#fr-bck1-1"></a></p>
</li>
<li id="footnote-bck2">
<p><a href="https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_borrowck/src/universal_regions.rs#L743-L790">https://github.com/rust-lang/rust/blob/7c7bb7dc017545db732f5cffec684bbaeae0a9a0/compiler/rustc_borrowck/src/universal_regions.rs#L743-L790</a> <a href="#fr-bck2-1"></a></p>
</li>
</ol>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="closure.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="part-5-intro.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="closure.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="part-5-intro.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_copyable = true;
</script>
<script src="elasticlunr.min.js"></script>
<script src="mark.min.js"></script>
<script src="searcher.js"></script>
<script src="clipboard.min.js"></script>
<script src="highlight.js"></script>
<script src="book.js"></script>
<!-- Custom JS scripts -->
<script src="mermaid.min.js"></script>
<script src="mermaid-init.js"></script>
<script src="pagetoc.js"></script>
</div>
</body>
</html>