blob: d9ca349d4494706848f9eed35fc576ccd6fa3445 [file] [log] [blame] [edit]
<!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-de23e50b.svg">
<link rel="shortcut icon" href="favicon-8114d1fc.png">
<link rel="stylesheet" href="css/variables-8adf115d.css">
<link rel="stylesheet" href="css/general-2459343d.css">
<link rel="stylesheet" href="css/chrome-ae938929.css">
<link rel="stylesheet" href="css/print-9e4910d8.css" media="print">
<!-- Fonts -->
<link rel="stylesheet" href="fonts/fonts-9644e21d.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" id="mdbook-highlight-css" href="highlight-493f70e1.css">
<link rel="stylesheet" id="mdbook-tomorrow-night-css" href="tomorrow-night-4c0ae647.css">
<link rel="stylesheet" id="mdbook-ayu-highlight-css" href="ayu-highlight-3fdfc3ac.css">
<!-- Custom theme stylesheets -->
<!-- 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-d266744b.js";
</script>
<!-- Start loading toc.js asap -->
<script src="toc-9a328f65.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="mdbook-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="mdbook-sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
let sidebar = null;
const sidebar_toggle = document.getElementById("mdbook-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="mdbook-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="mdbook-sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<div id="mdbook-page-wrapper" class="page-wrapper">
<div class="page">
<div id="mdbook-menu-bar-hover-placeholder"></div>
<div id="mdbook-menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="mdbook-sidebar-toggle" class="icon-button" for="mdbook-sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="mdbook-sidebar">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M0 96C0 78.3 14.3 64 32 64H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32C14.3 128 0 113.7 0 96zM0 256c0-17.7 14.3-32 32-32H416c17.7 0 32 14.3 32 32s-14.3 32-32 32H32c-17.7 0-32-14.3-32-32zM448 416c0 17.7-14.3 32-32 32H32c-17.7 0-32-14.3-32-32s14.3-32 32-32H416c17.7 0 32 14.3 32 32z"/></svg></span>
</label>
<button id="mdbook-theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="mdbook-theme-list">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M371.3 367.1c27.3-3.9 51.9-19.4 67.2-42.9L600.2 74.1c12.6-19.5 9.4-45.3-7.6-61.2S549.7-4.4 531.1 9.6L294.4 187.2c-24 18-38.2 46.1-38.4 76.1L371.3 367.1zm-19.6 25.4l-116-104.4C175.9 290.3 128 339.6 128 400c0 3.9 .2 7.8 .6 11.6c1.8 17.5-10.2 36.4-27.8 36.4H96c-17.7 0-32 14.3-32 32s14.3 32 32 32H240c61.9 0 112-50.1 112-112c0-2.5-.1-5-.2-7.5z"/></svg></span>
</button>
<ul id="mdbook-theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-default_theme">Auto</button></li>
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="mdbook-theme-ayu">Ayu</button></li>
</ul>
<button id="mdbook-search-toggle" class="icon-button" type="button" title="Search (`/`)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="/ s" aria-controls="mdbook-searchbar">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M416 208c0 45.9-14.9 88.3-40 122.7L502.6 457.4c12.5 12.5 12.5 32.8 0 45.3s-32.8 12.5-45.3 0L330.7 376c-34.4 25.2-76.8 40-122.7 40C93.1 416 0 322.9 0 208S93.1 0 208 0S416 93.1 416 208zM208 352c79.5 0 144-64.5 144-144s-64.5-144-144-144S64 128.5 64 208s64.5 144 144 144z"/></svg></span>
</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">
<span class=fa-svg id="print-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M128 0C92.7 0 64 28.7 64 64v96h64V64H354.7L384 93.3V160h64V93.3c0-17-6.7-33.3-18.7-45.3L400 18.7C388 6.7 371.7 0 354.7 0H128zM384 352v32 64H128V384 368 352H384zm64 32h32c17.7 0 32-14.3 32-32V256c0-35.3-28.7-64-64-64H64c-35.3 0-64 28.7-64 64v96c0 17.7 14.3 32 32 32H64v64c0 35.3 28.7 64 64 64H384c35.3 0 64-28.7 64-64V384zm-16-88c-13.3 0-24-10.7-24-24s10.7-24 24-24s24 10.7 24 24s-10.7 24-24 24z"/></svg></span>
</a>
<a href="https://github.com/rust-lang/rustc-dev-guide" title="Git repository" aria-label="Git repository">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg></span>
</a>
<a href="https://github.com/rust-lang/rustc-dev-guide/edit/main/src/coroutine-closures.md" title="Suggest an edit" aria-label="Suggest an edit" rel="edit">
<span class=fa-svg id="git-edit-button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M421.7 220.3l-11.3 11.3-22.6 22.6-205 205c-6.6 6.6-14.8 11.5-23.8 14.1L30.8 511c-8.4 2.5-17.5 .2-23.7-6.1S-1.5 489.7 1 481.2L38.7 353.1c2.6-9 7.5-17.2 14.1-23.8l205-205 22.6-22.6 11.3-11.3 33.9 33.9 62.1 62.1 33.9 33.9zM96 353.9l-9.3 9.3c-.9 .9-1.6 2.1-2 3.4l-25.3 86 86-25.3c1.3-.4 2.5-1.1 3.4-2l9.3-9.3H112c-8.8 0-16-7.2-16-16V353.9zM453.3 19.3l39.4 39.4c25 25 25 65.5 0 90.5l-14.5 14.5-22.6 22.6-11.3 11.3-33.9-33.9-62.1-62.1L314.3 67.7l11.3-11.3 22.6-22.6 14.5-14.5c25-25 65.5-25 90.5 0z"/></svg></span>
</a>
</div>
</div>
<div id="mdbook-search-wrapper" class="hidden">
<form id="mdbook-searchbar-outer" class="searchbar-outer">
<div class="search-wrapper">
<input type="search" id="mdbook-searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="mdbook-searchresults-outer" aria-describedby="searchresults-header">
<div class="spinner-wrapper">
<span class=fa-svg id="fa-spin"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M304 48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zm0 416c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM48 304c26.5 0 48-21.5 48-48s-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48zm464-48c0-26.5-21.5-48-48-48s-48 21.5-48 48s21.5 48 48 48s48-21.5 48-48zM142.9 437c18.7-18.7 18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zm0-294.2c18.7-18.7 18.7-49.1 0-67.9S93.7 56.2 75 75s-18.7 49.1 0 67.9s49.1 18.7 67.9 0zM369.1 437c18.7 18.7 49.1 18.7 67.9 0s18.7-49.1 0-67.9s-49.1-18.7-67.9 0s-18.7 49.1 0 67.9z"/></svg></span>
</div>
</div>
</form>
<div id="mdbook-searchresults-outer" class="searchresults-outer hidden">
<div id="mdbook-searchresults-header" class="searchresults-header"></div>
<ul id="mdbook-searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('mdbook-sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('mdbook-sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#mdbook-sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="mdbook-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><code class="language-rust ignore">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!</code></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 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>
<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 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>
<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 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>
<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 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>
<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">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg></span>
</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">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
</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">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M41.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l160 160c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L109.3 256 246.6 118.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-160 160z"/></svg></span>
</a>
<a rel="next prefetch" href="part-5-intro.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M278.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-160 160c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L210.7 256 73.4 118.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l160 160z"/></svg></span>
</a>
</nav>
</div>
<template id=fa-eye><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M288 32c-80.8 0-145.5 36.8-192.6 80.6C48.6 156 17.3 208 2.5 243.7c-3.3 7.9-3.3 16.7 0 24.6C17.3 304 48.6 356 95.4 399.4C142.5 443.2 207.2 480 288 480s145.5-36.8 192.6-80.6c46.8-43.5 78.1-95.4 93-131.1c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C433.5 68.8 368.8 32 288 32zM432 256c0 79.5-64.5 144-144 144s-144-64.5-144-144s64.5-144 144-144s144 64.5 144 144zM288 192c0 35.3-28.7 64-64 64c-11.5 0-22.3-3-31.6-8.4c-.2 2.8-.4 5.5-.4 8.4c0 53 43 96 96 96s96-43 96-96s-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6z"/></svg></span></template>
<template id=fa-eye-slash><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M38.8 5.1C28.4-3.1 13.3-1.2 5.1 9.2S-1.2 34.7 9.2 42.9l592 464c10.4 8.2 25.5 6.3 33.7-4.1s6.3-25.5-4.1-33.7L525.6 386.7c39.6-40.6 66.4-86.1 79.9-118.4c3.3-7.9 3.3-16.7 0-24.6c-14.9-35.7-46.2-87.7-93-131.1C465.5 68.8 400.8 32 320 32c-68.2 0-125 26.3-169.3 60.8L38.8 5.1zM223.1 149.5C248.6 126.2 282.7 112 320 112c79.5 0 144 64.5 144 144c0 24.9-6.3 48.3-17.4 68.7L408 294.5c5.2-11.8 8-24.8 8-38.5c0-53-43-96-96-96c-2.8 0-5.6 .1-8.4 .4c5.3 9.3 8.4 20.1 8.4 31.6c0 10.2-2.4 19.8-6.6 28.3l-90.3-70.8zm223.1 298L373 389.9c-16.4 6.5-34.3 10.1-53 10.1c-79.5 0-144-64.5-144-144c0-6.9 .5-13.6 1.4-20.2L83.1 161.5C60.3 191.2 44 220.8 34.5 243.7c-3.3 7.9-3.3 16.7 0 24.6c14.9 35.7 46.2 87.7 93 131.1C174.5 443.2 239.2 480 320 480c47.8 0 89.9-12.9 126.2-32.5z"/></svg></span></template>
<template id=fa-copy><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M502.6 70.63l-61.25-61.25C435.4 3.371 427.2 0 418.7 0H255.1c-35.35 0-64 28.66-64 64l.0195 256C192 355.4 220.7 384 256 384h192c35.2 0 64-28.8 64-64V93.25C512 84.77 508.6 76.63 502.6 70.63zM464 320c0 8.836-7.164 16-16 16H255.1c-8.838 0-16-7.164-16-16L239.1 64.13c0-8.836 7.164-16 16-16h128L384 96c0 17.67 14.33 32 32 32h47.1V320zM272 448c0 8.836-7.164 16-16 16H63.1c-8.838 0-16-7.164-16-16L47.98 192.1c0-8.836 7.164-16 16-16H160V128H63.99c-35.35 0-64 28.65-64 64l.0098 256C.002 483.3 28.66 512 64 512h192c35.2 0 64-28.8 64-64v-32h-47.1L272 448z"/></svg></span></template>
<template id=fa-play><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M73 39c-14.8-9.1-33.4-9.4-48.5-.9S0 62.6 0 80V432c0 17.4 9.4 33.4 24.5 41.9s33.7 8.1 48.5-.9L361 297c14.3-8.7 23-24.2 23-41s-8.7-32.2-23-41L73 39z"/></svg></span></template>
<template id=fa-clock-rotate-left><span class=fa-svg><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!--! Font Awesome Free 6.2.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2022 Fonticons, Inc. --><path d="M75 75L41 41C25.9 25.9 0 36.6 0 57.9V168c0 13.3 10.7 24 24 24H134.1c21.4 0 32.1-25.9 17-41l-30.8-30.8C155 85.5 203 64 256 64c106 0 192 86 192 192s-86 192-192 192c-40.8 0-78.6-12.7-109.7-34.4c-14.5-10.1-34.4-6.6-44.6 7.9s-6.6 34.4 7.9 44.6C151.2 495 201.7 512 256 512c141.4 0 256-114.6 256-256S397.4 0 256 0C185.3 0 121.3 28.7 75 75zm181 53c-13.3 0-24 10.7-24 24V256c0 6.4 2.5 12.5 7 17l72 72c9.4 9.4 24.6 9.4 33.9 0s9.4-24.6 0-33.9l-65-65V152c0-13.3-10.7-24-24-24z"/></svg></span></template>
<script>
window.playground_copyable = true;
</script>
<script src="elasticlunr-ef4e11c1.min.js"></script>
<script src="mark-09e88c2c.min.js"></script>
<script src="searcher-c2a407aa.js"></script>
<script src="clipboard-1626706a.min.js"></script>
<script src="highlight-abc7f01d.js"></script>
<script src="book-a0b12cfe.js"></script>
<!-- Custom JS scripts -->
<script src="mermaid-cc85ecea.min.js"></script>
<script src="mermaid-init-4533fb11.js"></script>
</div>
</body>
</html>