307 lines
18 KiB
HTML
307 lines
18 KiB
HTML
<!DOCTYPE HTML>
|
|
<html lang="en" class="light sidebar-visible" dir="ltr">
|
|
<head>
|
|
<!-- Book generated using mdBook -->
|
|
<meta charset="UTF-8">
|
|
<title>Opaque types - 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 -->
|
|
|
|
|
|
<!-- Provide site root and default themes to javascript -->
|
|
<script>
|
|
const path_to_root = "../";
|
|
const default_light_theme = "light";
|
|
const default_dark_theme = "navy";
|
|
</script>
|
|
<!-- Start loading toc.js asap -->
|
|
<script src="../toc.js"></script>
|
|
</head>
|
|
<body>
|
|
<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 = sidebar === 'visible';
|
|
html.classList.remove('sidebar-visible');
|
|
html.classList.add("sidebar-" + sidebar);
|
|
</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. (Shortkey: s)" 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/solve/opaque-types.md" title="Suggest an edit" aria-label="Suggest an 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">
|
|
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
|
</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="opaque-types-in-the-new-solver"><a class="header" href="#opaque-types-in-the-new-solver">Opaque types in the new solver</a></h1>
|
|
<p>The way <a href="../opaque-types-type-alias-impl-trait.html">opaque types</a> are handled in the new solver differs from the old implementation.
|
|
This should be a self-contained explanation of the behavior in the new solver.</p>
|
|
<h2 id="opaques-are-alias-types"><a class="header" href="#opaques-are-alias-types">opaques are alias types</a></h2>
|
|
<p>Opaque types are treated the same as other aliases, most notabily associated types,
|
|
whenever possible. There should be as few divergences in behavior as possible.</p>
|
|
<p>This is desirable, as they are very similar to other alias types, in that they can be
|
|
normalized to their hidden type and also have the same requirements for completeness.
|
|
Treating them this way also reduces the complexity of the type system by sharing code.
|
|
Having to deal with opaque types separately results in more complex rules and new kinds
|
|
of interactions. As we need to treat them like other aliases in the implicit-negative
|
|
mode, having significant differences between modes also adds complexity.</p>
|
|
<p><em>open question: is there an alternative approach here, maybe by treating them more like rigid
|
|
types with more limited places to instantiate them? they would still have to be ordinary
|
|
aliases during coherence</em></p>
|
|
<h3 id="normalizes-to-for-opaques"><a class="header" href="#normalizes-to-for-opaques"><code>normalizes-to</code> for opaques</a></h3>
|
|
<p><a href="https://github.com/rust-lang/rust/blob/384d26fc7e3bdd7687cc17b2662b091f6017ec2a/compiler/rustc_trait_selection/src/solve/normalizes_to/opaque_types.rs#L13">source</a></p>
|
|
<p><code>normalizes-to</code> is used to define the one-step normalization behavior for aliases in the new
|
|
solver: <code><<T as IdInner>::Assoc as IdOuter>::Assoc</code> first normalizes to <code><T as IdInner>::Assoc</code>
|
|
which then normalizes to <code>T</code>. It takes both the <code>AliasTy</code> which is getting normalized and the
|
|
expected <code>Term</code>. To use <code>normalizes-to</code> for actual normalization, the expected term can simply
|
|
be an unconstrained inference variable.</p>
|
|
<p>For opaque types in the defining scope and in the implicit-negative coherence mode, this is
|
|
always done in two steps. Outside of the defining scope <code>normalizes-to</code> for opaques always
|
|
returns <code>Err(NoSolution)</code>.</p>
|
|
<p>We start by trying to assign the expected type as a hidden type.</p>
|
|
<p>In the implicit-negative coherence mode, this currently always results in ambiguity without
|
|
interacting with the opaque types storage. We could instead add allow 'defining' all opaque types,
|
|
discarding their inferred types at the end, changing the behavior of an opaque type is used
|
|
multiple times during coherence: <a href="https://github.com/rust-lang/rust/blob/master/tests/ui/type-alias-impl-trait/coherence_different_hidden_ty.rs">example</a></p>
|
|
<p>Inside of the defining scope we start by checking whether the type and const arguments of the
|
|
opaque are all placeholders: <a href="https://github.com/rust-lang/rust/blob/384d26fc7e3bdd7687cc17b2662b091f6017ec2a/compiler/rustc_trait_selection/src/solve/normalizes_to/opaque_types.rs#L33">source</a>. If this check is ambiguous,
|
|
return ambiguity, if it fails, return <code>Err(NoSolution)</code>. This check ignores regions which are
|
|
only checked at the end of borrowck. If it succeeds, continue.</p>
|
|
<p>We then check whether we're able to <em>semantically</em> unify the generic arguments of the opaque
|
|
with the arguments of any opaque type already in the opaque types storage. If so, we unify the
|
|
previously stored type with the expected type of this <code>normalizes-to</code> call: <a href="https://github.com/rust-lang/rust/blob/384d26fc7e3bdd7687cc17b2662b091f6017ec2a/compiler/rustc_trait_selection/src/solve/normalizes_to/opaque_types.rs#L51-L59">source</a><sup class="footnote-reference" id="fr-1-1"><a href="#footnote-1">1</a></sup>.</p>
|
|
<p>If not, we insert the expected type in the opaque types storage: <a href="https://github.com/rust-lang/rust/blob/384d26fc7e3bdd7687cc17b2662b091f6017ec2a/compiler/rustc_trait_selection/src/solve/normalizes_to/opaque_types.rs#L68">source</a><sup class="footnote-reference" id="fr-2-1"><a href="#footnote-2">2</a></sup>.
|
|
Finally, we check whether the item bounds of the opaque hold for the expected type:
|
|
<a href="https://github.com/rust-lang/rust/blob/384d26fc7e3bdd7687cc17b2662b091f6017ec2a/compiler/rustc_trait_selection/src/solve/normalizes_to/opaque_types.rs#L69-L74">source</a>.</p>
|
|
<h3 id="using-alias-bounds-of-normalizable-aliases"><a class="header" href="#using-alias-bounds-of-normalizable-aliases">using alias-bounds of normalizable aliases</a></h3>
|
|
<p>https://github.com/rust-lang/trait-system-refactor-initiative/issues/77</p>
|
|
<p>Using an <code>AliasBound</code> candidate for normalizable aliases is generally not possible as an
|
|
associated type can have stronger bounds then the resulting type when normalizing via a
|
|
<code>ParamEnv</code> candidate.</p>
|
|
<p>These candidates would change our exact normalization strategy to be user-facing. It is otherwise
|
|
pretty much unobservable whether we eagerly normalize. Where we normalize is something we likely
|
|
want to change that after removing support for the old solver, so that would be undesirable.</p>
|
|
<h2 id="opaque-types-can-be-defined-anywhere"><a class="header" href="#opaque-types-can-be-defined-anywhere">opaque types can be defined anywhere</a></h2>
|
|
<p>Opaque types in their defining-scope can be defined anywhere, whether when simply relating types
|
|
or in the trait solver. This removes order dependence and incompleteness. Without this the result
|
|
of a goal can differ due to subtle reasons, e.g. whether we try to evaluate a goal using the
|
|
opaque before the first defining use of the opaque.</p>
|
|
<h2 id="higher-ranked-opaque-types-in-their-defining-scope"><a class="header" href="#higher-ranked-opaque-types-in-their-defining-scope">higher ranked opaque types in their defining scope</a></h2>
|
|
<p>These are not supported and trying to define them right now should always error.</p>
|
|
<p>FIXME: Because looking up opaque types in the opaque type storage can now unify regions,
|
|
we have to eagerly check that the opaque types does not reference placeholders. We otherwise
|
|
end up leaking placeholders.</p>
|
|
<h2 id="member-constraints"><a class="header" href="#member-constraints">member constraints</a></h2>
|
|
<p>The handling of member constraints does not change in the new solver. See the
|
|
<a href="../borrow_check/region_inference/member_constraints.html">relevant existing chapter</a> for that.</p>
|
|
<h2 id="calling-methods-on-opaque-types"><a class="header" href="#calling-methods-on-opaque-types">calling methods on opaque types</a></h2>
|
|
<p>FIXME: We need to continue to support calling methods on still unconstrained
|
|
opaque types in their defining scope. It's unclear how to best do this.</p>
|
|
<pre><pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
|
|
</span><span class="boring">fn main() {
|
|
</span>use std::future::Future;
|
|
use futures::FutureExt;
|
|
|
|
fn go(i: usize) -> impl Future<Output = ()> + Send + 'static {
|
|
async move {
|
|
if i != 0 {
|
|
// This returns `impl Future<Output = ()>` in its defining scope,
|
|
// we don't know the concrete type of that opaque at this point.
|
|
// Currently treats the opaque as a known type and succeeds, but
|
|
// from the perspective of "easiest to soundly implement", it would
|
|
// be good for this to be ambiguous.
|
|
go(i - 1).boxed().await;
|
|
}
|
|
}
|
|
}
|
|
<span class="boring">}</span></code></pre></pre>
|
|
<hr>
|
|
<ol class="footnote-definition"><li id="footnote-1">
|
|
<p>FIXME: this should ideally only result in a unique candidate given that we require the args to be placeholders and regions are always inference vars <a href="#fr-1-1">↩</a></p>
|
|
</li>
|
|
<li id="footnote-2">
|
|
<p>FIXME: why do we check whether the expected type is rigid for this. <a href="#fr-2-1">↩</a></p>
|
|
</li>
|
|
</ol>
|
|
</main>
|
|
|
|
<nav class="nav-wrapper" aria-label="Page navigation">
|
|
<!-- Mobile navigation buttons -->
|
|
<a rel="prev" href="../solve/proof-trees.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="../solve/significant-changes.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="../solve/proof-trees.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="../solve/significant-changes.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>
|
|
|
|
|
|
</div>
|
|
</body>
|
|
</html>
|