<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.10.0">Jekyll</generator><link href="https://lcnr.de/feed.xml" rel="self" type="application/atom+xml" /><link href="https://lcnr.de/" rel="alternate" type="text/html" /><updated>2026-10-07T16:27:32+00:00</updated><id>https://lcnr.de/feed.xml</id><title type="html">lcnr :3</title><subtitle>writing about stuff</subtitle><entry><title type="html">rustc internal: a shiny future for HIR typeck</title><link href="https://lcnr.de/blog/2026/08/24/shiny-future-typeck.html" rel="alternate" type="text/html" title="rustc internal: a shiny future for HIR typeck" /><published>2026-08-24T00:00:00+00:00</published><updated>2026-08-24T00:00:00+00:00</updated><id>https://lcnr.de/blog/2026/08/24/shiny-future-typeck</id><content type="html" xml:base="https://lcnr.de/blog/2026/08/24/shiny-future-typeck.html"><![CDATA[<blockquote>
  <p>This is not a blogpost for public consumption, it’s a note so that I don’t forgot about this myself. This stems from a 5 minute conversation with <a href="https://github.com/BoxyUwU">Boxy</a>.</p>
</blockquote>

<p>HIR typeck does not care about free regions. It cares about bound regions, i.e. whether a type is <code class="language-plaintext highlighter-rouge">for&lt;'a&gt; fn(&amp;'a u32)</code> or <code class="language-plaintext highlighter-rouge">fn(&amp;'static u32)</code> matters, but whether a type is <code class="language-plaintext highlighter-rouge">&amp;'a u32</code> vs <code class="language-plaintext highlighter-rouge">&amp;'b u32</code> does not.</p>

<p>We could mostly just use our internal <code class="language-plaintext highlighter-rouge">'erased</code> everywhere in HIR typeck and don’t bother with any region information at all there.</p>

<p>Separately, we’re currently removing higher-ranked subtyping. The fact that subtyping can impact trait selection is a nightmare and unfortunately that’s just currently the case. I’ve been looking into this with <a href="https://github.com/spastorino">Santiago Pastorino</a>, see <a href="https://github.com/rust-lang/rust/pull/160461">#160461</a>.</p>

<p>Once we’ve replaced higher-ranked subtyping with coercions we don’t need to differentiate between subtyping and equality in HIR typeck anymore and can just have a single way to relate things there.</p>

<p>This would have a significant positive perf impact. We can remove <code class="language-plaintext highlighter-rouge">Subtype</code> obligations and instead just unify all related inference variables.</p>

<p>More importantly, we can remove the <code class="language-plaintext highlighter-rouge">sub_unification_table</code>. Handling that one correctly has a pretty big impact on the complexity and performance of canonicalization for the trait solver. It forces us to check a separate unification table whenever we test whether some obligation is currently stalled.</p>]]></content><author><name></name></author><category term="blog" /><summary type="html"><![CDATA[This is not a blogpost for public consumption, it’s a note so that I don’t forgot about this myself. This stems from a 5 minute conversation with Boxy.]]></summary></entry><entry><title type="html">On always-applicable trait impls</title><link href="https://lcnr.de/blog/2026/03/06/always-applicable.html" rel="alternate" type="text/html" title="On always-applicable trait impls" /><published>2026-03-06T00:00:00+00:00</published><updated>2026-03-06T00:00:00+00:00</updated><id>https://lcnr.de/blog/2026/03/06/always-applicable</id><content type="html" xml:base="https://lcnr.de/blog/2026/03/06/always-applicable.html"><![CDATA[<p>Specialization is cool and people really want it. We also already use it in the standard library a bunch for performance reasons.</p>

<p>We only have information about <em>free regions</em> available during analysis. If we have to decide whether to specialize during codegen we do not have any region information available.</p>

<p>One way around this is to require specialization to be lifetime independent. The used implementations have to be <em>always applicable</em>.</p>

<p>This rule can either apply to impls, making sure any use of them is always applicable by construction, or to specific trait bounds. Requiring all impls used by specialization to be always applicable is not workable. We’ve <a href="https://github.com/rust-lang/rust/pull/68970">implemented this</a> for <code class="language-plaintext highlighter-rouge">#[feature(min_specialization)]</code>, an <em>intended to be sound</em> subset of full specialization for use in std.</p>

<p>The requirements on impls are too strict, so there’s an internal <code class="language-plaintext highlighter-rouge">#[rustc_unsafe_specialization_marker]</code> attribute you put on traits which disables the check for impls. We end up using this marker pretty much any time we want to specialize in the standard library.</p>

<p>If limiting trait implementations does not work, what about disallowing lifetime dependence when applying the impls?</p>

<h3 id="the-intended-behavior">The intended behavior</h3>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">trait</span> <span class="n">Trait</span> <span class="p">{}</span>
<span class="k">impl</span> <span class="n">Trait</span> <span class="k">for</span> <span class="nb">u32</span> <span class="p">{}</span>

<span class="k">impl</span><span class="o">&lt;</span><span class="nv">'a</span><span class="o">&gt;</span> <span class="n">Trait</span> <span class="k">for</span> <span class="o">&amp;</span><span class="nv">'a</span> <span class="nb">i16</span> <span class="p">{}</span>
<span class="k">impl</span> <span class="n">Trait</span> <span class="k">for</span> <span class="o">&amp;</span><span class="k">'static</span> <span class="nb">i32</span> <span class="p">{}</span>

<span class="k">impl</span><span class="o">&lt;</span><span class="nv">'a</span><span class="p">,</span> <span class="n">T</span><span class="p">:</span> <span class="n">Trait</span> <span class="o">+</span> <span class="nv">'a</span><span class="o">&gt;</span> <span class="n">Trait</span> <span class="k">for</span> <span class="o">&amp;</span><span class="nv">'a</span> <span class="n">T</span> <span class="p">{}</span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="n">Trait</span> <span class="k">for</span> <span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">T</span><span class="p">)</span> <span class="p">{}</span>

<span class="k">fn</span> <span class="n">spec_check</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span> <span class="k">-&gt;</span> <span class="nb">bool</span> <span class="p">{</span>
    <span class="c1">// fake syntax :3 specializing on `T: Trait`</span>
    <span class="k">if</span> <span class="nd">is_implemented!</span><span class="p">(</span><span class="n">T</span><span class="p">:</span> <span class="n">Trait</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">true</span> <span class="c1">// `T: Trait` holds</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="k">false</span> <span class="c1">// `T: Trait` does not hold or may be lifetime dependent</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">// no lifetimes, directly corresponds to whether `T: Trait` is implemented</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;</span><span class="nb">u32</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">true</span><span class="p">);</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;</span><span class="nb">i32</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">false</span><span class="p">);</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;</span><span class="p">(</span><span class="nb">u32</span><span class="p">,</span> <span class="nb">u32</span><span class="p">)</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">true</span><span class="p">);</span>

    <span class="c1">// not lifetime dependent, applicable for all `'a`</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;&amp;</span><span class="nb">i16</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">true</span><span class="p">);</span>

    <span class="c1">// lifetime dependent, only implemented for `&amp;'static i16`.</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;&amp;</span><span class="nb">i32</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">false</span><span class="p">);</span>

    <span class="c1">// not lifetime dependent, the outlives constraint of the</span>
    <span class="c1">// `impl&lt;'a, T: Trait + 'a&gt; Trait for &amp;'a T {}` is required by</span>
    <span class="c1">// the well-formedness of the self type.</span>
    <span class="c1">//</span>
    <span class="c1">// Implementing this is slightly annoying :&lt;</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;&amp;</span><span class="nb">u32</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">true</span><span class="p">);</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;&amp;&amp;</span><span class="nb">u32</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">true</span><span class="p">);</span>

    <span class="c1">// lifetime dependent, only implemented for tuples where</span>
    <span class="c1">// the lifetimes in both fields are the same.</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;</span><span class="p">(</span><span class="o">&amp;</span><span class="nb">u32</span><span class="p">,</span> <span class="o">&amp;</span><span class="nb">u32</span><span class="p">)</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">false</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="the-consequences">The consequences</h3>

<p>If a type parameter <code class="language-plaintext highlighter-rouge">T</code> of an impl is mentioned multiple times and instantiated with a type with free regions, the impl is now lifetime dependent. This has some annoying effects.</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">trait</span> <span class="n">Trait</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="p">{}</span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="n">Trait</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="k">for</span> <span class="n">T</span> <span class="p">{}</span>
<span class="k">fn</span> <span class="n">spec_check</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">U</span><span class="o">&gt;</span><span class="p">()</span> <span class="k">-&gt;</span> <span class="nb">bool</span> <span class="p">{</span>
    <span class="c1">// fake syntax :3 specializing on `T: Trait`</span>
    <span class="k">if</span> <span class="nd">is_implemented!</span><span class="p">(</span><span class="n">T</span><span class="p">:</span> <span class="n">Trait</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">true</span> <span class="c1">// `T: Trait` holds</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="k">false</span> <span class="c1">// `T: Trait` does not hold or may be lifetime dependent</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;</span><span class="nb">u32</span><span class="p">,</span> <span class="nb">u32</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">true</span><span class="p">);</span>
    <span class="c1">// `(&amp;'a u32, &amp;'b u32)` is not implemented while `(&amp;'a u32, &amp;'a u32)` is,</span>
    <span class="c1">// lifetime dependent.</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;&amp;</span><span class="nb">u32</span><span class="p">,</span> <span class="o">&amp;</span><span class="nb">u32</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">false</span><span class="p">);</span>
    <span class="c1">// The lifetimes in `fn(&amp;u32)` are higher-ranked, they don't</span>
    <span class="c1">// impact always applicable checking.</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;</span><span class="k">fn</span><span class="p">(</span><span class="o">&amp;</span><span class="nb">u32</span><span class="p">),</span> <span class="k">fn</span><span class="p">(</span><span class="o">&amp;</span><span class="nb">u32</span><span class="p">)</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">true</span><span class="p">);</span>
<span class="p">}</span>
</code></pre></div></div>

<p>Now, for a quick quiz:</p>
<ul>
  <li>is <code class="language-plaintext highlighter-rouge">is_implemented!(&amp;u32: Eq)</code> lifetime dependent?</li>
  <li>is <code class="language-plaintext highlighter-rouge">is_implemented!(&amp;u32: PartialEq)</code> lifetime dependent?</li>
  <li>is <code class="language-plaintext highlighter-rouge">is_implemented!(Box&lt;&amp;u32&gt;: Eq)</code> lifetime dependent?</li>
  <li>is <code class="language-plaintext highlighter-rouge">is_implemented!(Box&lt;&amp;u32&gt;: PartialEq)</code> lifetime dependent?</li>
  <li>is <code class="language-plaintext highlighter-rouge">is_implemented!(Vec&lt;&amp;u32&gt;: Eq)</code> lifetime dependent?</li>
  <li>is <code class="language-plaintext highlighter-rouge">is_implemented!(Vec&lt;&amp;u32&gt;: PartialEq)</code> lifetime dependent?</li>
</ul>

<p>If you’ve guessed that only <code class="language-plaintext highlighter-rouge">Box&lt;&amp;u32&gt;: PartialEq</code> is lifetime dependent and the others are all fine, good job!</p>

<p>If you’re unsure why, look at the definition of <code class="language-plaintext highlighter-rouge">PartialEq</code> and how it is implemented:</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">trait</span> <span class="nb">PartialEq</span><span class="o">&lt;</span><span class="n">RHS</span> <span class="o">=</span> <span class="k">Self</span><span class="o">&gt;</span> <span class="p">{}</span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">A</span><span class="p">:</span> <span class="n">PointeeSized</span><span class="p">,</span> <span class="n">B</span><span class="p">:</span> <span class="n">PointeeSized</span><span class="o">&gt;</span> <span class="nb">PartialEq</span><span class="o">&lt;&amp;</span><span class="n">B</span><span class="o">&gt;</span> <span class="k">for</span> <span class="o">&amp;</span><span class="n">A</span>
<span class="k">where</span>
    <span class="n">A</span><span class="p">:</span> <span class="nb">PartialEq</span><span class="o">&lt;</span><span class="n">B</span><span class="o">&gt;</span><span class="p">,</span>
<span class="p">{}</span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="o">?</span><span class="nb">Sized</span> <span class="o">+</span> <span class="nb">PartialEq</span><span class="p">,</span> <span class="n">A</span><span class="p">:</span> <span class="n">Allocator</span><span class="o">&gt;</span> <span class="nb">PartialEq</span> <span class="k">for</span> <span class="nb">Box</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">A</span><span class="o">&gt;</span> <span class="p">{}</span>
<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">U</span><span class="p">,</span> <span class="n">A1</span><span class="p">,</span> <span class="n">A2</span><span class="o">&gt;</span> <span class="nb">PartialEq</span><span class="o">&lt;</span><span class="nb">Vec</span><span class="o">&lt;</span><span class="n">U</span><span class="p">,</span> <span class="n">A2</span><span class="o">&gt;&gt;</span> <span class="k">for</span> <span class="nb">Vec</span><span class="o">&lt;</span><span class="n">T</span><span class="p">,</span> <span class="n">A1</span><span class="o">&gt;</span>
<span class="k">where</span>
    <span class="n">A1</span><span class="p">:</span> <span class="n">Allocator</span><span class="p">,</span>
    <span class="n">A2</span><span class="p">:</span> <span class="n">Allocator</span><span class="p">,</span>
    <span class="n">T</span><span class="p">:</span> <span class="nb">PartialEq</span><span class="o">&lt;</span><span class="n">U</span><span class="o">&gt;</span><span class="p">,</span>
<span class="p">{}</span>
</code></pre></div></div>
<p>The <code class="language-plaintext highlighter-rouge">PartialEq</code> impl for <code class="language-plaintext highlighter-rouge">Box</code> does not explicitly specify the defaulted type parameter, so it uses <code class="language-plaintext highlighter-rouge">T</code> twice. The other ones explicitly handle the case where the <code class="language-plaintext highlighter-rouge">RHS</code> is a different type. The same problem exists for other traits, like <code class="language-plaintext highlighter-rouge">Add</code>.</p>

<p>This not only affects existing traits, it also means that adding new type parameter defaults is a breaking change if that parameter default mentions another generic parameter. This is currently not the case and seems undesirable.</p>

<h3 id="we-do-not-need-the-always-applicable-rule">We do not need the always-applicable rule</h3>

<p>Lets go back to the start.</p>

<blockquote>
  <p>We only have information about <em>free regions</em> available during analysis. If we have to decide whether to specialize during codegen we do not have any region information available.</p>
</blockquote>

<p>If we decide whether to specialize during analysis we can properly check regions, allowing for lifetime dependent specialization. Let’s consider the code from the first example again:</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">trait</span> <span class="n">Trait</span> <span class="p">{}</span>
<span class="k">impl</span> <span class="n">Trait</span> <span class="k">for</span> <span class="o">&amp;</span><span class="k">'static</span> <span class="nb">u32</span> <span class="p">{}</span>

<span class="k">fn</span> <span class="n">spec_check</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span> <span class="k">-&gt;</span> <span class="nb">bool</span> <span class="p">{</span>
    <span class="c1">// fake syntax :3 specializing on `T: Trait`</span>
    <span class="k">if</span> <span class="nd">is_implemented!</span><span class="p">(</span><span class="n">T</span><span class="p">:</span> <span class="n">Trait</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">true</span> <span class="c1">// `T: Trait` holds</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="k">false</span> <span class="c1">// `T: Trait` does not hold or may be lifetime dependent</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">// We want this to succeed.</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;&amp;</span><span class="k">'static</span> <span class="nb">u32</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">true</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div></div>
<p>For this example to work, we need to know that <code class="language-plaintext highlighter-rouge">spec_check</code> relies on whether <code class="language-plaintext highlighter-rouge">T</code> implements <code class="language-plaintext highlighter-rouge">Trait</code> so that we can check whether this holds while type checking <code class="language-plaintext highlighter-rouge">fn main</code>. This can be done by adding a <code class="language-plaintext highlighter-rouge">maybe Trait</code> bounds to <code class="language-plaintext highlighter-rouge">spec_check</code>, allowing the caller to decide whether to provide <code class="language-plaintext highlighter-rouge">spec_check</code> with a proof of <code class="language-plaintext highlighter-rouge">T: Trait</code>.</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">fn</span> <span class="n">spec_check</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">maybe</span> <span class="n">Trait</span><span class="o">&gt;</span><span class="p">()</span> <span class="k">-&gt;</span> <span class="nb">bool</span> <span class="p">{</span>
    <span class="c1">// fake syntax :3 specializing on `T: Trait`</span>
    <span class="k">if</span> <span class="nd">is_implemented!</span><span class="p">(</span><span class="n">T</span><span class="p">:</span> <span class="n">Trait</span><span class="p">)</span> <span class="p">{</span>
        <span class="k">true</span> <span class="c1">// `T: Trait` holds</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="k">false</span> <span class="c1">// `T: Trait` does not hold or may be lifetime dependent</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">// Calling `spec_check` will now try to prove `T: Trait` during type checking.</span>
    <span class="c1">// If we succeed, pass in the information that it is implemented.</span>
    <span class="nd">assert_eq!</span><span class="p">(</span><span class="nn">spec_check</span><span class="p">::</span><span class="o">&lt;&amp;</span><span class="k">'static</span> <span class="nb">u32</span><span class="o">&gt;</span><span class="p">(),</span> <span class="kc">true</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="more-thoughts">More thoughts</h3>

<p>Maybe bounds need to be proven when using an item. If we’re unable to prove a maybe bound in a generic context we should emit a lint.</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">fn</span> <span class="n">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">maybe</span> <span class="nb">Clone</span><span class="o">&gt;</span><span class="p">()</span> <span class="p">{</span>
    <span class="k">if</span> <span class="nd">is_implemented!</span><span class="p">(</span><span class="n">T</span><span class="p">:</span> <span class="nb">Clone</span><span class="p">)</span> <span class="p">{</span>
        
    <span class="p">}</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="n">bar</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="nb">Clone</span><span class="o">&gt;</span><span class="p">()</span> <span class="p">{</span>
     <span class="c1">// always clone</span>
    <span class="nn">foo</span><span class="p">::</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">();</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="n">baz</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">// never clone</span>
    <span class="nn">foo</span><span class="p">::</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">();</span>
    <span class="c1">//~^ WARN: discarding maybe bound for generic type</span>
    <span class="c1">//~| NOTE: `T` may implement `Clone`</span>
    <span class="c1">//~| NOTE: this disables any specialization in `foo`</span>
    <span class="c1">//~| HELP: consider propagating the `maybe Clone` bound to the caller  </span>
<span class="p">}</span>

<span class="k">fn</span> <span class="nf">yeet</span><span class="p">()</span> <span class="p">{</span>
    <span class="nn">bar</span><span class="p">::</span><span class="o">&lt;</span><span class="nb">u32</span><span class="o">&gt;</span><span class="p">();</span>
    <span class="nn">baz</span><span class="p">::</span><span class="o">&lt;</span><span class="nb">u32</span><span class="o">&gt;</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div></div>

<p>While we can check region constraints during analysis, we cannot decide whether to prove a maybe bound depending on whether doing so would result in a region error. However, we can explicitly avoid providing the relevant bound in this case, e.g.</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">struct</span> <span class="n">CloneIfStatic</span><span class="o">&lt;</span><span class="nv">'a</span><span class="o">&gt;</span><span class="p">(</span><span class="o">&amp;</span><span class="nv">'a</span> <span class="p">());</span>
<span class="k">impl</span> <span class="nb">Clone</span> <span class="k">for</span> <span class="n">CloneIfStatic</span><span class="o">&lt;</span><span class="k">'static</span><span class="o">&gt;</span> <span class="p">{</span>
    <span class="c1">// ...</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="n">call_direct</span><span class="o">&lt;</span><span class="nv">'a</span><span class="o">&gt;</span><span class="p">()</span> <span class="p">{</span>
    <span class="nn">foo</span><span class="p">::</span><span class="o">&lt;</span><span class="n">CloneIfStatic</span><span class="o">&lt;</span><span class="nv">'a</span><span class="o">&gt;&gt;</span><span class="p">();</span> <span class="c1">//~ ERROR lifetime error, `'a` is required to outlive `'static`</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="n">foo_indir</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span> <span class="p">{</span>
    <span class="nd">#[expect(discarded_maybe_bound)]</span>
    <span class="nn">foo</span><span class="p">::</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">fn</span> <span class="n">call_indir</span><span class="o">&lt;</span><span class="nv">'a</span><span class="o">&gt;</span><span class="p">()</span> <span class="p">{</span>
    <span class="nn">foo_indir</span><span class="p">::</span><span class="o">&lt;</span><span class="n">CloneIfStatic</span><span class="o">&lt;</span><span class="nv">'a</span><span class="o">&gt;</span><span class="p">();</span> <span class="c1">// ok</span>
<span class="p">}</span>
</code></pre></div></div>

<p>We can have maybe bounds in super trait position to always allow specializing on them. A good candidate here would be <code class="language-plaintext highlighter-rouge">trait Clone: maybe Copy</code>. This would allow any item with a <code class="language-plaintext highlighter-rouge">T: Clone</code> bound to specialize on <code class="language-plaintext highlighter-rouge">T: Copy</code> in a sound way. Other candidates involve <code class="language-plaintext highlighter-rouge">trait Iterator</code> to enable its giant mess of specializations.</p>

<p>We can also allow the use of maybe bounds in types by having some type-level matching on whether a trait is implemented, unsure about the syntax, but one could imagine the following:</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">trait</span> <span class="n">Foo</span> <span class="p">{</span>
    <span class="k">type</span> <span class="n">Assoc</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">impl</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">maybe</span> <span class="nb">Copy</span><span class="o">&gt;</span> <span class="n">Foo</span> <span class="k">for</span> <span class="n">T</span> <span class="p">{</span>
    <span class="k">type</span> <span class="n">Assoc</span> <span class="o">=</span> <span class="n">type_if</span> <span class="n">T</span><span class="p">:</span> <span class="nb">Copy</span> <span class="p">{</span> <span class="n">T</span> <span class="p">}</span> <span class="k">else</span> <span class="p">{</span> <span class="nb">Rc</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="p">};</span>
<span class="p">}</span> 
</code></pre></div></div>

<p>We could also support maybe-bounds in trait objects, something like</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">// The vtable of this trait object would store an `Option&lt;&amp;'static CloneVtable&gt;`.</span>
<span class="k">fn</span> <span class="n">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">maybe</span> <span class="nb">Clone</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="o">&amp;</span><span class="n">T</span><span class="p">)</span> <span class="k">-&gt;</span> <span class="o">&amp;</span><span class="k">dyn</span> <span class="n">maybe</span> <span class="nb">Clone</span> <span class="p">{</span>
    <span class="n">x</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="nf">downcast</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="o">&amp;</span><span class="k">dyn</span> <span class="n">maybe</span> <span class="nb">Clone</span><span class="p">)</span> <span class="k">-&gt;</span> <span class="nb">Option</span><span class="o">&lt;&amp;</span><span class="k">dyn</span> <span class="nb">Clone</span><span class="o">&gt;</span> <span class="p">{</span>
    <span class="n">x</span><span class="py">.try_downcast</span><span class="p">::</span><span class="o">&lt;</span><span class="k">dyn</span> <span class="nb">Clone</span><span class="o">&gt;</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="unlike-specialization-maybe-bounds-are-trivially-sound">Unlike specialization, maybe-bounds are trivially sound</h3>

<p>Having pattern matching introduce new things in the environment is less so :3</p>

<p>One way to think of trait bounds in Rust is that every trait bound actually lowers to an implicitly provided dictionary. This is a separate topic I should also write about, for now, here’s <a href="https://rust-lang.github.io/rust-project-goals/2026/dictionary-passing-style-experiment.html">a project goal</a>.</p>

<p>Here’s the 2 minute explanation</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">fn</span> <span class="n">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="nb">Clone</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="o">&amp;</span><span class="n">T</span><span class="p">)</span> <span class="k">-&gt;</span> <span class="n">T</span>  <span class="p">{</span>
    <span class="n">x</span><span class="nf">.clone</span><span class="p">();</span>
<span class="p">}</span>

<span class="c1">// would be lowered/desugared to</span>
<span class="k">struct</span> <span class="n">CloneDict</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="p">{</span>
    <span class="n">clone_fn</span><span class="p">:</span> <span class="k">fn</span><span class="p">(</span><span class="o">&amp;</span><span class="n">T</span><span class="p">)</span> <span class="k">-&gt;</span> <span class="n">T</span><span class="p">,</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="n">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="nb">Clone</span><span class="p">,</span> <span class="k">const</span> <span class="n">DICT</span><span class="p">:</span> <span class="n">CloneDict</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="o">&amp;</span><span class="n">T</span><span class="p">)</span> <span class="k">-&gt;</span> <span class="n">T</span> <span class="p">{</span>
    <span class="p">(</span><span class="n">DICT</span><span class="py">.clone_fn</span><span class="p">)(</span><span class="n">x</span><span class="p">)</span>
<span class="p">}</span>
</code></pre></div></div>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">fn</span> <span class="n">bar</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">maybe</span> <span class="nb">Clone</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="o">&amp;</span><span class="n">T</span><span class="p">)</span> <span class="k">-&gt;</span> <span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="p">{</span>
    <span class="k">if</span> <span class="nd">is_implemented!</span><span class="p">(</span><span class="n">T</span><span class="p">:</span> <span class="nb">Clone</span><span class="p">)</span> <span class="p">{</span>
        <span class="nf">Some</span><span class="p">(</span><span class="n">x</span><span class="nf">.clone</span><span class="p">())</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="nb">None</span>
    <span class="p">}</span>
<span class="p">}</span>

<span class="c1">// would be lowered/desugared to</span>
<span class="k">fn</span> <span class="n">bar</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="nb">Clone</span><span class="p">,</span> <span class="k">const</span> <span class="n">DICT</span><span class="p">:</span> <span class="nb">Option</span><span class="o">&lt;</span><span class="n">CloneDict</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;&gt;&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="o">&amp;</span><span class="n">T</span><span class="p">)</span> <span class="k">-&gt;</span> <span class="n">T</span> <span class="p">{</span>
    <span class="k">if</span> <span class="k">let</span> <span class="nf">Some</span><span class="p">(</span><span class="n">dict</span><span class="p">)</span> <span class="o">=</span> <span class="n">DICT</span> <span class="p">{</span>
        <span class="nf">Some</span><span class="p">((</span><span class="n">dict</span><span class="py">.clone_fn</span><span class="p">)(</span><span class="n">x</span><span class="p">))</span>
    <span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
        <span class="nb">None</span>
    <span class="p">}</span>
<span class="p">}</span>
</code></pre></div></div>]]></content><author><name></name></author><category term="blog" /><summary type="html"><![CDATA[Specialization is cool and people really want it. We also already use it in the standard library a bunch for performance reasons.]]></summary></entry><entry><title type="html">Next-generation trait solver update</title><link href="https://lcnr.de/blog/2025/12/01/next-solver-update.html" rel="alternate" type="text/html" title="Next-generation trait solver update" /><published>2025-12-01T00:00:00+00:00</published><updated>2025-12-01T00:00:00+00:00</updated><id>https://lcnr.de/blog/2025/12/01/next-solver-update</id><content type="html" xml:base="https://lcnr.de/blog/2025/12/01/next-solver-update.html"><![CDATA[<p>For general info about the new trait solver, see <a href="https://blog.rust-lang.org/inside-rust/2024/12/04/trait-system-refactor-initiative/">this separate blog post</a>. This blog post is not targeted at a general audience. It’s targeted at nobody to be honest. I wrote it for myself to figure out what I need to do.</p>

<p>The new trait solver is somewhat close to stabilization, let’s look at an exhaustive list of everything that’s necessary. 
The potentially biggest - but hopefully fairly small - item is anything not mentioned below; <em>unknown unknowns</em>. That sure is an easy way to make this document exhaustive.</p>

<h3 id="nyaaa">nyaaa</h3>

<p>There are still a few behavior changes and improvements that should happen before we stabilize the trait solver. We have to figure out how to make overflow errors into a hard error during monomorphization <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/155">#155</a>. <a href="https://github.com/adwinwhite">@adwinwhite</a> implemented a partial fix in <a href="https://github.com/rust-lang/rust/pull/146096">#146096</a>. This fix is insufficient, see <a href="https://github.com/rust-lang/rust/issues/147770">#147770</a>. I personally don’t really know how I feel about this approach.</p>

<p>My intended approach would be for certain users of the trait solver to make it clear that ambiguity due to overflow should be treated as as a hard error here. I’ve discussed this with <a href="https://github.com/compiler-errors">@compiler-errors</a> a long time ago, and doing so is apparently non-trivial and somewhat annoying. That seems to also match my perspective from reading through the more recent discussions here.</p>

<h3 id="nyau">nyau</h3>

<p>While the way we handle opaque types - e.g. return position <code class="language-plaintext highlighter-rouge">impl Trait</code> - is a lot cleaner and stronger than the behavior of the old solver and now mostly subsumes the behavior supported on stable, we don’t yet support calling methods on unconstrained associated types of not-yet-defined opaque types <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/248">#248</a>. Dealing with this is <em>annoying</em>, but so is dealing with not-yet-defined opaque types in their defining scopes in general. I think the way forward here is to extend the opaque type storage to also track these associated types and then extend the method selection hacks to these as well. More on opaque types later on.</p>

<h3 id="myuw">myuw</h3>

<p>We also just fail to compile <code class="language-plaintext highlighter-rouge">ml-kem</code> due to overflow errors and hangs <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/246">#246</a>. No idea what’s causing that failure yet and not too excited to find out. <a href="https://github.com/jdonszelmann">@jdonszelmann</a> is currently looking into one of the hangs here and hopefully there aren’t too many distinct issues here.</p>

<h3 id="rawr">rawr</h3>

<p>One of the hardest problems in Rust is how to deal with aliases referencing bound regions, e.g. <code class="language-plaintext highlighter-rouge">for&lt;'a&gt; fn(&lt;T as Trait&gt;::Assoc&lt;'a&gt;)</code>.  I want to look into normalizing higher-ranked aliases during generalization. This would also fix an issue with the way we eagerly deduce closure signatures <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/191">#191</a>.</p>

<h3 id="rarw">rarw</h3>

<p>We also have performance issues. The <code class="language-plaintext highlighter-rouge">wg-grammar</code> benchmark is multiple times slower than with the old solver <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/228">#228</a>. That’s not good and shouldn’t be the case. A bunch of other crates on crater also have bad perf <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/254">#254</a>. There have also been a bunch of spurious regressions in the last <a href="https://github.com/rust-lang/crater">crater</a> run. I expect that a few of them are also caused by hangs in the trait solver.</p>

<p><a href="https://github.com/lqd">@lqd</a> opened <a href="https://github.com/rust-lang/crater/issues/812">crater#812</a> asking whether we could track the duration of each job during crater runs to uncover some significant slowdowns caused by the new solver. This would be incredibly helpful to detect more cases in which we still need to improve the performance of the new solver.</p>

<p>I’ve got 2 large performance improvements in mind I would like to try before stabilizing the new solver. I want to look into further search graph improvements and filter unnecessary information from query input to enable significantly more reuse there.</p>

<h3 id="miuw">miuw</h3>

<p>A performance optimization in the search graph I’ve added before unfortunately breaks otherwise desirable code <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/257">#257</a>. <a href="https://github.com/ShoyuVanilla">@ShoyuVanilla</a> is currently looking into removing that performance optimization and hopefully other improvements mean that this hack is not actually necessary.</p>

<h3 id="nyeww">nyeww</h3>

<p>We haven’t triaged all <a href="https://github.com/rust-lang/crater">crater</a> regressions yet. <a href="https://github.com/lqd">@lqd</a> recently went through a lot of them, but there are still a few we haven’t looked at yet. We have fixed a lot of issues since the last run so I intend to rebase <a href="https://github.com/rust-lang/rust/pull/133502">#133502</a> once most of the currently in-flight fixes have been merged. I expect that we’re then able to triage all regressions in that crater run.</p>

<h3 id="myau">myau</h3>

<p>There are also issues which don’t necessarily require changes to the implementation itself. I want to look into the blanket impl handling of the not-yet-defined opaque type inference hack again <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/229">#229</a>. There are currently some undesirable edge cases there and while we might go ahead and stabilize the behavior as is, I want to spend time trying to come up with a better alternative.</p>

<h3 id="myow">myow</h3>

<p>The way we handle non-defining uses of opaque types can introduce closures with non-identity generic arguments during analysis. This is not an issue in theory but may break code which did not consider that possibility when written. I should look through all uses of <code class="language-plaintext highlighter-rouge">ty::Closure</code> before stabilizing anything here <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/243">#243</a>.</p>

<h3 id="more-cat-noises"><em>more cat noises</em></h3>

<p>Hitting the <code class="language-plaintext highlighter-rouge">#[recursion_limit]</code> is not a fatal error in the new trait solver. This means different crates may be able to observe different behavior due to that limit, potentially resulting in unsoundness. We should take some time to look into the way the trait solver is used to check whether any of them may be problematic <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/258">#258</a>.</p>

<h3 id="klirrr"><em>Klirrr!</em></h3>

<p>Finally, we need to actually document the behavior of the trait solver, writing multiple FCP proposals and one RFC. <a href="https://github.com/BoxyUwU">@BoxyUwU</a> and I have spent some time a few months ago writing down everything we want to document as part of the stabilization procedure in <a href="https://hackmd.io/7VnJO-qnSleVaMYZKyOGkA">a HackMD file</a>. Let’s quickly summarize the most involved documents.</p>

<p>We need to write an RFC explaining why the new way we handle trait solver cycles is sound and desirable. I would like to do so in collaboration with <a href="https://github.com/nikomatsakis">@nikomatsakis</a>. This still requires some effort to figure out how to explain the behavior and ideally we also get a testable model of my underlying reasoning here.</p>

<p>I also have to explain the way we handle opaque types in the new solver. Their behavior is significantly different from how it works on stable right now. It’s significantly simpler and more consistent in some ways. It also has some fairly odd hacks to maintain backwards compatability. We need to explain all of this in a way that’s approachable to the rest of <a href="https://rust-lang.org/governance/teams/compiler/#team-types"><code class="language-plaintext highlighter-rouge">T-types</code></a>.</p>]]></content><author><name></name></author><category term="blog" /><summary type="html"><![CDATA[For general info about the new trait solver, see this separate blog post. This blog post is not targeted at a general audience. It’s targeted at nobody to be honest. I wrote it for myself to figure out what I need to do.]]></summary></entry><entry><title type="html">Implicit auto traits and trait definitions</title><link href="https://lcnr.de/blog/2025/11/28/implicit-auto-traits-assoc-types.html" rel="alternate" type="text/html" title="Implicit auto traits and trait definitions" /><published>2025-11-28T00:00:00+00:00</published><updated>2025-11-28T00:00:00+00:00</updated><id>https://lcnr.de/blog/2025/11/28/implicit-auto-traits-assoc-types</id><content type="html" xml:base="https://lcnr.de/blog/2025/11/28/implicit-auto-traits-assoc-types.html"><![CDATA[<p>Waddup everyone!</p>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">trait</span> <span class="n">Deref</span> <span class="p">{</span>
    <span class="k">type</span> <span class="n">Target</span><span class="p">:</span> <span class="o">?</span><span class="nb">Sized</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>has to get lowered to</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">trait</span> <span class="n">Deref</span><span class="p">:</span> <span class="n">Move</span> <span class="p">{</span>
    <span class="k">type</span> <span class="n">Target</span><span class="p">:</span> <span class="o">?</span><span class="nb">Sized</span> <span class="o">+</span> <span class="n">Move</span><span class="p">;</span>
<span class="p">}</span>
</code></pre></div></div>
<p>to avoid breaking the following snippets:</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">fn</span> <span class="n">nested_explicit_arg</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Deref</span><span class="o">&lt;</span><span class="n">Target</span> <span class="o">=</span> <span class="n">U</span><span class="o">&gt;</span><span class="p">,</span> <span class="n">U</span><span class="p">:</span> <span class="o">?</span><span class="nb">Sized</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="n">T</span><span class="p">)</span> <span class="p">{}</span>
<span class="k">fn</span> <span class="n">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Deref</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="n">T</span><span class="p">)</span> <span class="p">{</span>
    <span class="nf">nested_explicit_arg</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="n">nested_takes_target</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="o">?</span><span class="nb">Sized</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="nb">Option</span><span class="o">&lt;&amp;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span> <span class="p">{}</span>
<span class="k">fn</span> <span class="n">bar</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Deref</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span> <span class="p">{</span>
    <span class="nf">nested_takes_target</span><span class="p">(</span><span class="n">x</span><span class="nf">.as_deref</span><span class="p">())</span>
<span class="p">}</span>

<span class="k">trait</span> <span class="n">Trait</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="p">{}</span>
<span class="k">fn</span> <span class="n">impls_trait_with_arg</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Deref</span><span class="p">,</span> <span class="n">U</span><span class="p">:</span> <span class="n">Trait</span><span class="o">&lt;</span><span class="nn">T</span><span class="p">::</span><span class="n">Target</span><span class="o">&gt;&gt;</span><span class="p">()</span> <span class="p">{}</span>
</code></pre></div></div>

<p>This sucks. It would mean we could not implement <code class="language-plaintext highlighter-rouge">Deref</code> for <code class="language-plaintext highlighter-rouge">Box&lt;ExternType&gt;</code>. In general, adding new implicit trait bounds requires putting them on associated types to not break backwards compatibility.</p>

<p>Even more generally, removing implicit trait bounds in trait definitions is a breaking change. While free functions can freely remove implicit trait bounds, we must not do so in trait definitions.</p>

<p>We have to really look for a way to weaken requirements of traits in a backwards compatible way if we want to introduce new implicit auto traits. Doing so is especially challenging for associated types.</p>

<h2 id="elaborating-on-use">Elaborating on use</h2>

<p>Instead of adding the implicit default to the <em>trait definition</em>, we could try to add these defaults to uses of the trait. We can then change the lowering in a future edition to no longer implicitly add the default auto trait. I don’t whether we can get this to work. It would result in the following lowering for the code above</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">trait</span> <span class="n">Deref</span> <span class="p">{</span>
    <span class="k">type</span> <span class="n">Target</span><span class="p">:</span> <span class="o">?</span><span class="nb">Sized</span><span class="p">;</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="n">nested_explicit_arg</span><span class="o">&lt;</span>
    <span class="n">T</span><span class="p">:</span> <span class="n">Deref</span><span class="o">&lt;</span><span class="n">Target</span> <span class="o">=</span> <span class="n">U</span><span class="o">&gt;</span> <span class="o">+</span> <span class="n">Move</span><span class="p">,</span>
    <span class="n">U</span><span class="p">:</span> <span class="o">?</span><span class="nb">Sized</span> <span class="o">+</span> <span class="n">Move</span>
<span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="n">T</span><span class="p">)</span> <span class="p">{}</span>
<span class="k">fn</span> <span class="n">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Deref</span><span class="o">&lt;</span><span class="n">Target</span><span class="p">:</span> <span class="n">Move</span><span class="o">&gt;</span> <span class="o">+</span> <span class="n">Move</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="n">T</span><span class="p">)</span> <span class="p">{</span>
    <span class="nf">nested_explicit_arg</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="p">}</span>

<span class="k">fn</span> <span class="n">nested_takes_target</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="o">?</span><span class="nb">Sized</span> <span class="o">+</span> <span class="n">Move</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="nb">Option</span><span class="o">&lt;&amp;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span> <span class="p">{}</span>
<span class="k">fn</span> <span class="n">bar</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Deref</span><span class="o">&lt;</span><span class="n">Target</span><span class="p">:</span> <span class="n">Move</span><span class="o">&gt;</span> <span class="o">+</span> <span class="n">Move</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span> <span class="p">{</span>
    <span class="nf">nested_takes_target</span><span class="p">(</span><span class="n">x</span><span class="nf">.as_deref</span><span class="p">())</span>
<span class="p">}</span>

<span class="k">trait</span> <span class="n">Trait</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span> <span class="p">{}</span>
<span class="k">fn</span> <span class="n">impls_trait_with_arg</span><span class="o">&lt;</span>
    <span class="n">T</span><span class="p">:</span> <span class="n">Deref</span><span class="o">&lt;</span><span class="n">Target</span><span class="p">:</span> <span class="n">Move</span><span class="o">&gt;</span> <span class="o">+</span> <span class="n">Move</span><span class="p">,</span>
    <span class="n">U</span><span class="p">:</span> <span class="n">Trait</span><span class="o">&lt;</span><span class="nn">T</span><span class="p">::</span><span class="n">Target</span><span class="o">&gt;</span> <span class="o">+</span> <span class="n">Move</span><span class="p">,</span>
<span class="o">&gt;</span><span class="p">()</span> <span class="p">{}</span>
</code></pre></div></div>
<p>It’s unclear how this could handle</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">trait</span> <span class="n">Recur</span> <span class="p">{</span>
    <span class="k">type</span> <span class="n">Assoc</span><span class="p">:</span> <span class="n">Recur</span><span class="p">;</span>	
<span class="p">}</span>

<span class="k">fn</span> <span class="n">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Recur</span><span class="o">&gt;</span><span class="p">()</span> <span class="p">{}</span>
</code></pre></div></div>
<p>Trying to lower this would result in an infinite expansion:</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">fn</span> <span class="n">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Recur</span><span class="o">&gt;</span><span class="p">()</span>
<span class="k">where</span>
    <span class="n">T</span><span class="p">:</span> <span class="n">Move</span><span class="p">,</span>
    <span class="nn">T</span><span class="p">::</span><span class="n">Assoc</span><span class="p">:</span> <span class="n">Move</span><span class="p">,</span>
    <span class="o">&lt;</span><span class="nn">T</span><span class="p">::</span><span class="n">Assoc</span> <span class="k">as</span> <span class="n">Recur</span><span class="o">&gt;</span><span class="p">::</span><span class="n">Assoc</span><span class="p">:</span> <span class="n">Move</span><span class="p">,</span>
    <span class="o">&lt;&lt;</span><span class="nn">T</span><span class="p">::</span><span class="n">Assoc</span> <span class="k">as</span> <span class="n">Recur</span><span class="o">&gt;</span><span class="p">::</span><span class="n">Assoc</span> <span class="k">as</span> <span class="n">Recur</span><span class="o">&gt;</span><span class="p">::</span><span class="n">Assoc</span><span class="p">:</span> <span class="n">Move</span><span class="p">,</span>
    <span class="o">...</span>
<span class="p">{}</span>
</code></pre></div></div>
<p>Alternatively, we could only elaborate the where-bound for one layer until <code class="language-plaintext highlighter-rouge">T: Recur&lt;Assoc: Recur&gt;</code>, without any bound for <code class="language-plaintext highlighter-rouge">&lt;T::Assoc as Recur&gt;::Assoc: Move</code>. This would cause the following code to error however:</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">fn</span> <span class="n">foo</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Recur</span><span class="o">&gt;</span><span class="p">()</span> <span class="p">{}</span>
<span class="k">fn</span> <span class="n">bar</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Recur</span><span class="o">&gt;</span><span class="p">()</span> <span class="p">{</span>
    <span class="c1">// `bar` has an elaborated where-bound that `T::Assoc: Move` holds,</span>
    <span class="c1">// would fail to prove the elaborated where-bound of `foo` requiring</span>
    <span class="c1">// `&lt;T::Assoc as Recur&gt;::Assoc: Move` however. </span>
    <span class="nn">foo</span><span class="p">::</span><span class="o">&lt;</span><span class="nn">T</span><span class="p">::</span><span class="n">Assoc</span><span class="o">&gt;</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div></div>
<p>That sort of breakage seems like a no-go to me.</p>

<p>While elaborating on use seems challenging in general, <a href="https://github.com/davidtwco">@davidtwco</a> and <a href="https://github.com/nikomatsakis">@nikomatsakis</a> have considered only doing it for selected traits, e.g. <code class="language-plaintext highlighter-rouge">Deref</code>, avoiding the infinite expansion issue. This then allows nice use of <code class="language-plaintext highlighter-rouge">Deref</code> for pointers to extern types in future editions.</p>

<h2 id="rules-more-like-guidelines">Rules? More like guidelines</h2>

<p>Rust’s type system is cool and all, however, we could just give up and not bother.</p>

<p>What if instead of elaborating traits to make sure default auto-traits don’t break anything, we support deferring that check to monomorphization time for rigid aliases - and maybe generic parameters - while emitting a future compat lint.</p>

<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">fn</span> <span class="n">nested_takes_target</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="o">?</span><span class="nb">Sized</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="nb">Option</span><span class="o">&lt;&amp;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span> <span class="p">{}</span>
<span class="k">fn</span> <span class="n">bar</span><span class="o">&lt;</span><span class="n">T</span><span class="p">:</span> <span class="n">Deref</span><span class="o">&gt;</span><span class="p">(</span><span class="n">x</span><span class="p">:</span> <span class="nb">Option</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">)</span> <span class="p">{</span>
    <span class="nf">nested_takes_target</span><span class="p">(</span><span class="n">x</span><span class="nf">.as_deref</span><span class="p">())</span>
    <span class="c1">//~^ WARN `T::Target: Move` does not hold but should</span>
    <span class="c1">//~| NOTE required by `nested_takes_target`</span>
    <span class="c1">//~| NOTE this is guaranteed to result in an error when monomorphizing this item with a type which is not `Move`</span>
    <span class="c1">//~| NOTE this is currently accepted but is getting phased out</span>
<span class="p">}</span>
</code></pre></div></div>

<h3 id="is-this-actually-possible">Is this actually possible?</h3>

<p>Adding support for this kind of hack is non-trivial, but should be possible. This is sketch of how this could work and I have not actually tried to get a working implementation here.</p>

<p>When encountering a rigid alias for which an implicit-default trait does not hold, we add a candidate which holds with this goal as a deferred requirement in <a href="https://github.com/rust-lang/rust/blob/69408a877e11c759a9d94f152b8333c929133d9b/compiler/rustc_type_ir/src/solve/mod.rs#L253-L257">its external constraints</a>.</p>

<p>This way the trait system is now able to handle <code class="language-plaintext highlighter-rouge">Option&lt;T::Assoc&gt;: Move</code>. This goal will now hold while returning the deferred requirement that <code class="language-plaintext highlighter-rouge">T::Assoc: Move</code> holds. We then require the <code class="language-plaintext highlighter-rouge">InferCtxt</code> which got these deferred requirements to do something with them.</p>

<p>What to do with these requirements depends on the query we’re in. In general, we’d erase any lifetimes and then put them in the corresponding MIR body/check that they are already required by this MIR body. When monomorphizing a MIR body during codegen/CTFE and so on we check these requirements.</p>

<h3 id="lifetime-dependence">Lifetime-dependence</h3>

<p>This cannot handle lifetimes. I don’t believe this to be a major issue.<code class="language-plaintext highlighter-rouge">Move</code> implementations should generally not be lifetime dependent. We could either require them to not be by adding a check to user-written impls. This feels like the best option to me.</p>

<p>Alternatively, we could prove that these deferred goals hold for all lifetimes. This is challenging as it would require us to “uniquify” lifetimes which causes hangs for deeply recursive, but otherwise self-similar types.</p>

<p>Another somewhat hacky alternative is to track whether we’ve relied on a potentially lifetime dependent impl while proving the deferred goal, and if so, emit a hard error. As long as this check never incorrectly triggers for builtin impls it’d only cause breakage in cases where somebody already replaced the builtin auto-trait impl with a different user-written impl. This feels like an acceptable risk to me.</p>

<p>We could just accept some theoretical unsoundness here and mention that concern in the future compat lint. This should only be done if the future compat lint triggers fairly rarely. I cannot judge this right now.</p>

<h3 id="its-possible-cool-how-can-users-fix-the-fcw">It’s possible, cool! How can users fix the FCW</h3>

<h4 id="free-functions-and-methods">Free functions and methods</h4>

<p>For free functions they propagate the requirement to the where-clauses of their function. This should never be a breaking change.</p>

<p>It may move an error from monomorphization time to analysis if the function already had a caller which does not implement the default auto trait, however that should not be considered a breaking change.</p>

<h4 id="trait-implementations-and-definitions">Trait implementations and definitions</h4>

<p>Adding implicit super trait bounds limits implementations of that trait. Users can always instead add a bound on the impl. We can implicitly support this by implicitly adding these auto traits during where-clause lowering instead, which allows us to remove the implicit bound over an edition. We do require these bounds on traits to support using them in trait methods which rely on the auto-trait.</p>

<p>If all methods of a trait implementation require the new builtin auto-trait, and the requirement can be added as a where-clause to the impl itself,  fixing this is not a breaking change, same as for free functions.</p>

<p>If only some of the trait methods rely on the auto-trait or if the requirement is for a generic parameter of a trait function, it’s harder to fix. Moving these requirements to the trait definition is limiting and a breaking change unfortunately. It breaks code which currently uses the trait for types which don’t have that auto-trait requirement.</p>

<h2 id="anyways">Anyways</h2>

<p>Changing auto trait errors to be FCWs instead of hard errors and only hard erroring if they don’t hold during monomophization is possible. It could empower us to better add new implicit trait bounds.</p>

<p>It will still not be easy and I don’t have the time to think this through. I luckily don’t have to. Have fun <a href="https://github.com/yoshuawuyts">@yoshuawuyts</a>. Please send any language design ideas or questions to them.</p>]]></content><author><name></name></author><category term="blog" /><summary type="html"><![CDATA[Waddup everyone!]]></summary></entry><entry><title type="html">∀ does not imply ∃</title><link href="https://lcnr.de/blog/2025/08/19/forall-does-not-impl-exists.html" rel="alternate" type="text/html" title="∀ does not imply ∃" /><published>2025-08-19T00:00:00+00:00</published><updated>2025-08-19T00:00:00+00:00</updated><id>https://lcnr.de/blog/2025/08/19/forall-does-not-impl-exists</id><content type="html" xml:base="https://lcnr.de/blog/2025/08/19/forall-does-not-impl-exists.html"><![CDATA[<p>duh :&gt;</p>]]></content><author><name></name></author><category term="blog" /><summary type="html"><![CDATA[duh :&gt;]]></summary></entry><entry><title type="html">Adding implicit auto-trait bounds is hard</title><link href="https://lcnr.de/blog/2025/07/28/implicit-auto-trait-bounds.html" rel="alternate" type="text/html" title="Adding implicit auto-trait bounds is hard" /><published>2025-07-28T00:00:00+00:00</published><updated>2025-07-28T00:00:00+00:00</updated><id>https://lcnr.de/blog/2025/07/28/implicit-auto-trait-bounds</id><content type="html" xml:base="https://lcnr.de/blog/2025/07/28/implicit-auto-trait-bounds.html"><![CDATA[<p>Waddup gamers, there are a lot of desirable extensions for Rust which boil down to adding a new implicit/default trait bound.</p>
<ul>
  <li>some sort of <code class="language-plaintext highlighter-rouge">Leak</code> bound for improved linear type support/to avoid dropping stuff that is <a href="https://rust-lang.github.io/async-fundamentals-initiative/roadmap/async_drop.html"><code class="language-plaintext highlighter-rouge">AsyncDrop</code></a></li>
  <li>something something <code class="language-plaintext highlighter-rouge">Moveable</code> to supercede <code class="language-plaintext highlighter-rouge">Pin</code></li>
  <li>the merged experimental <a href="https://github.com/rust-lang/rust/pull/137944"><code class="language-plaintext highlighter-rouge">feature(sized_hierarchy)</code></a> which adds <code class="language-plaintext highlighter-rouge">MetaSized</code> as a super trait of <code class="language-plaintext highlighter-rouge">Sized</code></li>
</ul>

<h2 id="the-performance-cost-of-adding-an-implicit-trait-bound-which-does-not-stop-at-indirection">The performance cost of adding an implicit trait bound which does not stop at indirection</h2>

<p>Adding an implicit auto trait bound whose impls do not stop at pointer-indirection has a prohibitively high performance cost.</p>

<p>Computing <code class="language-plaintext highlighter-rouge">Sized</code> is generally quite cheap, even for very complex types as even the most gnarly types tend to be fairly shallow. Their fields end up quite quickly relying on <code class="language-plaintext highlighter-rouge">Vec</code>, <code class="language-plaintext highlighter-rouge">Box</code> or other kinds of references. Checking whether <a href="https://github.com/rust-lang/rust/blob/c360e219f5a56631baa46065d28e9852ca7d4ce3/compiler/rustc_middle/src/ty/context.rs#L1373-L1375">the following type</a> is <code class="language-plaintext highlighter-rouge">Sized</code> is trivial. Checking whether <code class="language-plaintext highlighter-rouge">GlobalCtxt</code> is <code class="language-plaintext highlighter-rouge">Sized</code> is still comparatively fast, even though it’s an outlier. Even with its large number of fields the amount of types hidden behind indirection is significantly greater.</p>

<p>Proving a <em>comparatively rarely checked</em> auto trait for <code class="language-plaintext highlighter-rouge">TyCtxt</code> added a 7 second performance regression to our bootstrap builds, causing us to <a href="https://github.com/rust-lang/rust/pull/138092">add explicit non-recursive impls</a>.</p>

<p>As a more informative datapoint, <a href="https://github.com/Bryanskiy">@Bryanskiy</a> actually went ahead and added support for additional default auto-traits to rustc. This is now disabled by default. Initial testing by them caused building the compiler itself <a href="https://github.com/rust-lang/rust/pull/120706#issuecomment-1956382036">to take 33 minutes instead of 7</a>.</p>

<p>We’ve recently also landed support for <a href="https://github.com/rust-lang/rust/issues/144404"><code class="language-plaintext highlighter-rouge">MetaSized</code></a>. This is one of the “good traits” as it stops at indirection and has the same behavior as <code class="language-plaintext highlighter-rouge">Sized</code>. It resulted in an up-to 10% performance impact, even though it mostly piggy-packed on the fact that it’s implied by stuff being <code class="language-plaintext highlighter-rouge">Sized</code> and the fact that checking <code class="language-plaintext highlighter-rouge">Sized</code>-ness is comparatively easy: <a href="https://github.com/rust-lang/rust/pull/137944#issuecomment-2981732648">comment</a>.</p>

<p><code class="language-plaintext highlighter-rouge">Sized</code> is able to avoid looking at most fields:</p>
<ul>
  <li>we don’t care about indirection, so we don’t need to recurse into e.g. <code class="language-plaintext highlighter-rouge">Vec</code> and <code class="language-plaintext highlighter-rouge">Box</code></li>
  <li>we only need to look at the last fields, as all other fields are required to be <code class="language-plaintext highlighter-rouge">Sized</code> for well-formedness. Checking whether a struct implements an auto trait needs to check all fields.</li>
</ul>

<h3 id="how-to-test-the-performance-impact-yourself">How to test the performance impact yourself</h3>

<p>We’ve actually merged support for <a href="https://github.com/rust-lang/rust/issues/138781">additional implicit trait bounds</a>. To test the performance impact, we need to add the following to <code class="language-plaintext highlighter-rouge">core</code> and enable <code class="language-plaintext highlighter-rouge">-Zexperimental-default-bounds</code>.</p>
<div class="language-rust highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nd">#![feature(lang_items,</span> <span class="nd">auto_traits)]</span>

<span class="nd">#[lang</span> <span class="nd">=</span> <span class="s">"default_trait1"</span><span class="nd">]</span>
<span class="n">auto</span> <span class="k">trait</span> <span class="n">Leak</span> <span class="p">{}</span>
</code></pre></div></div>

<p>Adding this to a leaf crate is insufficient as we need this trait to be a where-bound of all methods and items of the standard library to get the full perf impact.</p>

<h3 id="we-will-not-be-able-to-avoid-these-performance-regressions-by-improving-the-implementation">We will not be able to avoid these performance regressions by improving the implementation</h3>

<p>Even right now this is largely a linear performance hit. We are simply doing more work to prove the additional trait bounds. It is not avoidable by e.g. waiting for <a href="https://github.com/rust-lang/rust/issues/107374"><code class="language-plaintext highlighter-rouge">-Znext-solver</code></a>. Enabling <code class="language-plaintext highlighter-rouge">MetaSized</code> resulted in the same kind of performance regressions there as with the old solver.</p>

<p>Annoyingly, we are only able to cache the “head” of cycles, so if we’ve got <code class="language-plaintext highlighter-rouge">List&lt;T&gt;: Send</code>, which depends on <code class="language-plaintext highlighter-rouge">Option&lt;Box&lt;List&lt;T&gt;&gt;: Send</code>, which depends on <code class="language-plaintext highlighter-rouge">List&lt;T&gt;: Send</code> again, we are not able to cache the result of <code class="language-plaintext highlighter-rouge">Option&lt;Box&lt;List&lt;T&gt;&gt;: Send</code> as it’s not a cycle head. We only cache <code class="language-plaintext highlighter-rouge">List&lt;T&gt;: Send</code> and then have to separately compute the cycle if we start by proving <code class="language-plaintext highlighter-rouge">Option&lt;Box&lt;List&lt;T&gt;&gt;: Send</code>. This is likely a significant part of the performance cost of proving auto trait bounds for large types. Caching cycle participants is not possible for two reasons which may both be solveable, but require a lot of effort:</p>
<ul>
  <li>incremental query dependency tracking <a href="https://rust-lang.zulipchat.com/#narrow/channel/364551-t-types.2Ftrait-system-refactor/topic/global.20cache/near/324056794">zulip</a></li>
  <li>the result of evaluating cycles depends on their root, but caching must not be observable: <a href="https://github.com/rust-lang/rust/blob/cb6785f73df1aa3f558796a22a4ab9652cf38e26/tests/ui/traits/next-solver/cycles/coinduction/incompleteness-unstable-result.rs">UI test</a></li>
</ul>

<h2 id="there-are-other-issues">There are other issues</h2>

<p>There are a few other hard/blocking issues we need to resolve if we want to add additional implicit trait bounds.</p>

<p>If the implicit traits don’t stop at indirection, some crates are forced to increase their <code class="language-plaintext highlighter-rouge">#[recursion_limit]</code> to avoid errors.</p>

<p>Having to prove additional trait bounds triggers <a href="https://github.com/rust-lang/rust/issues/110338">incorrect coroutine lifetime errors</a> in far more cases, which is a breaking change.</p>

<p>These don’t seem as fundamental as the performance hit however. I expect that we can avoid these given enough time and effort.</p>

<h2 id="edit-2025-09-19-actually-adding-move"><em>edit 2025-09-19: actually adding <code class="language-plaintext highlighter-rouge">Move</code></em></h2>

<p>I’ve been chatting with <a href="https://github.com/yoshuawuyts">yoshuawuyts</a> we’ve tested the performance impact of a <code class="language-plaintext highlighter-rouge">Move</code> trait in <a href="https://github.com/rust-lang/rust/pull/146201">#146201</a>. Some notes from the experiment:</p>

<p><code class="language-plaintext highlighter-rouge">Move</code> is quite nice as it is similar to <code class="language-plaintext highlighter-rouge">Sized</code> and also does not recur through pointers or references. The performance impact ended up being kinda acceptable with a max of 50% and there are still some avenues to significantly reduce this impact. I have no idea, but expect it’s going to be 5-10%.</p>

<p>Adding new default traits needs to add them as implied supertrait bounds. This encounters candidate preference issues we need to hack around by preferring impl and alias-bound candidates over where-bounds in most cases. Properly supporting this is blocked on <a href="https://github.com/rust-lang/rust/issues/107374"><code class="language-plaintext highlighter-rouge">-Znext-solver</code></a> which should land soon-ish (less than a year). We’ve got the same issue for <code class="language-plaintext highlighter-rouge">MetaSized</code> but there it’s a bit easier as there are no user-written impls of <code class="language-plaintext highlighter-rouge">MetaSized</code>. cc <a href="https://github.com/rust-lang/rust/pull/144064">#144064</a>.</p>

<p>Updating all the blanket impls in <code class="language-plaintext highlighter-rouge">std</code> is really annoying. This is not a technical problem, just a general gripe with default trait bounds.</p>]]></content><author><name></name></author><category term="blog" /><summary type="html"><![CDATA[Waddup gamers, there are a lot of desirable extensions for Rust which boil down to adding a new implicit/default trait bound. some sort of Leak bound for improved linear type support/to avoid dropping stuff that is AsyncDrop something something Moveable to supercede Pin the merged experimental feature(sized_hierarchy) which adds MetaSized as a super trait of Sized]]></summary></entry><entry><title type="html">rustc trait system refactor update</title><link href="https://lcnr.de/update/2023-12-08" rel="alternate" type="text/html" title="rustc trait system refactor update" /><published>2023-12-08T00:00:00+00:00</published><updated>2023-12-08T00:00:00+00:00</updated><id>https://lcnr.de/update/update</id><content type="html" xml:base="https://lcnr.de/update/2023-12-08"><![CDATA[<p>While overflow handling continues to be a major source of pain, there has been some quite significant progress this week. Will try to summarize my current understanding over the next week and will hopefully end up with an acceptable status quo, even if imperfect.</p>

<p>We finally fixed <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/1">trait-system-refactor-initiative#1</a> in <a href="https://github.com/rust-lang/rust/pull/118725">#118725</a>. While this does not affect coherence, it has been a major blocker when trying to compile crates with <code class="language-plaintext highlighter-rouge">-Ztrait-solver=next</code>. After fixing the generalizer in <a href="https://github.com/rust-lang/rust/pull/117088">#117088</a> this PR was now fairly pleasant to implement. It surfaced a bug in writeback with the new solver, causing an ICE. I opened a fix for that in <a href="https://github.com/rust-lang/rust/pull/118751">#118751</a>.</p>

<p><a href="https://github.com/compiler-errors">@compiler-errors</a> opened <a href="https://github.com/rust-lang/rust/pull/118649">#118649</a> to change the <code class="language-plaintext highlighter-rouge">coinductive_overlap_in_coherence</code> lint to a hard error. This will change the existing behavior to match the new solver, simplifying its stabilization.</p>

<p>Also check out the <a href="https://github.com/rust-lang/rust/pulls?q=is%3Apr+is%3Amerged+label%3AWG-trait-system-refactor+-label%3Arollup+closed%3A%3C2023-12-09+-closed%3A%3C2023-12-01+">full list of PRs merged this week</a>.</p>]]></content><author><name></name></author><category term="update" /><summary type="html"><![CDATA[While overflow handling continues to be a major source of pain, there has been some quite significant progress this week. Will try to summarize my current understanding over the next week and will hopefully end up with an acceptable status quo, even if imperfect.]]></summary></entry><entry><title type="html">rustc trait system refactor update</title><link href="https://lcnr.de/update/2023-12-01" rel="alternate" type="text/html" title="rustc trait system refactor update" /><published>2023-12-01T00:00:00+00:00</published><updated>2023-12-01T00:00:00+00:00</updated><id>https://lcnr.de/update/update</id><content type="html" xml:base="https://lcnr.de/update/2023-12-01"><![CDATA[<p>I forgot to write an update last friday (again). As stated in the last update, we’re getting close towards posting a blog asking for testing.</p>

<p>We’ve implemented most of the improvements necessary to match the existing diagnostics. This happened over multiple PRs. I first opened <a href="https://github.com/rust-lang/rust/pull/118089">#118089</a> and <a href="https://github.com/rust-lang/rust/pull/118210">#118210</a>. With this the intercrate ambiguity cause computation now matches the old solver. <a href="https://github.com/compiler-errors">@compiler-errors</a> then opened <a href="https://github.com/rust-lang/rust/pull/118346">#118346</a> to normalize types as much as possible before printing them in diagnostics. As the new solver does not eagerly normalize, we now have to explicitly normalize here.</p>

<p>I’ve spend a bunch of time experimenting with the non-fatal overflow handling. While I have a far better understanding of the design constraints now, I still don’t have a nice solution. I intend to fully documented all the conflicting requirements I’ve encountered and then chat about them with some other Types Team members during the next week. We currently <a href="https://github.com/rust-lang/rust/blob/64d7e0d0b61c460fbc882ae37c0f236756dd9c39/compiler/rustc_trait_selection/src/solve/eval_ctxt/canonical.rs#L97-L110">discards constraints in case the current goal encountered overflow</a>. This <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/70">breaks existing code</a> while also preventing a lot of otherwise difficult to avoid hangs.</p>

<p>There have also been some smaller changes; check out the <a href="https://github.com/rust-lang/rust/pulls?q=is%3Apr+is%3Amerged+label%3AWG-trait-system-refactor+-label%3Arollup+-closed%3A%3C2023-11-18+closed%3A%3C2023-12-01+">full list of merged PRs</a>.</p>]]></content><author><name></name></author><category term="update" /><summary type="html"><![CDATA[I forgot to write an update last friday (again). As stated in the last update, we’re getting close towards posting a blog asking for testing.]]></summary></entry><entry><title type="html">rustc trait system refactor update</title><link href="https://lcnr.de/update/2023-11-17" rel="alternate" type="text/html" title="rustc trait system refactor update" /><published>2023-11-17T00:00:00+00:00</published><updated>2023-11-17T00:00:00+00:00</updated><id>https://lcnr.de/update/update</id><content type="html" xml:base="https://lcnr.de/update/2023-11-17"><![CDATA[<p>I did not spend too much time working on the solver as I waited for <a href="https://github.com/rust-lang/rust/pull/117278">#117278</a> to get merged and worked on other things. I finally cleaned up some of our region terminology, implementing <a href="https://github.com/rust-lang/types-team/issues/95">types-team#95</a>,</p>

<p>I am still waiting for the T-types FCP in <a href="https://github.com/rust-lang/rust/pull/117088">#117088</a>. Once that PR has landed and we’ve fixed some - now fairly straightforward - diagnostics regressions, I intend to open a blogpost asking for testing of the new solver.</p>

<p>I plan to go through our non-fatal overflow handling again before the stabilization to minimize the risk of forward compatibility issues. An example of something we previously didn’t consider is the hang when compiling <a href="https://github.com/Dragon-Hatcher/type-system-chess">type-system-chess</a> mentioned last week.  There exists at least one known regression caused by the current approach: <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/70">trait-system-refactor-initiative#70</a>. I don’t expect my work here to result in any significant user-facing changes though.</p>

<p>There has also been a kind of embarrassing bug in the <code class="language-plaintext highlighter-rouge">ProofTreeVisitor</code> API, which has been discovered and fixed by <a href="https://github.com/gavinleroy">@gavinleroy</a> in <a href="https://github.com/rust-lang/rust/pull/117878">#117878</a>. The full list of merged changes can be found <a href="https://github.com/rust-lang/rust/pulls?q=is%3Apr+is%3Amerged+label%3AWG-trait-system-refactor+-label%3Arollup+closed%3A%3C2023-11-18+-closed%3A%3C2023-11-10+">here</a>.</p>]]></content><author><name></name></author><category term="update" /><summary type="html"><![CDATA[I did not spend too much time working on the solver as I waited for #117278 to get merged and worked on other things. I finally cleaned up some of our region terminology, implementing types-team#95,]]></summary></entry><entry><title type="html">rustc trait system refactor update</title><link href="https://lcnr.de/update/2023-11-10" rel="alternate" type="text/html" title="rustc trait system refactor update" /><published>2023-11-10T00:00:00+00:00</published><updated>2023-11-10T00:00:00+00:00</updated><id>https://lcnr.de/update/update</id><content type="html" xml:base="https://lcnr.de/update/2023-11-10"><![CDATA[<p>I unfortunately forgot to write an update last week. I’ve finished the rework of the way we handle <code class="language-plaintext highlighter-rouge">alias-relate</code> goals. The PR is now ready and waiting for review: <a href="https://github.com/rust-lang/rust/pull/117278">#117278</a>. This rework seems quite promising and I am happy with the results.</p>

<p>While working on this I encountered a subtle issue where invariants we’d like to hold conflict and opened <a href="https://github.com/rust-lang/trait-system-refactor-initiative/issues/75">trait-system-refactor-initiative#75</a> for it. I still have to improve my understanding of the exact constraints at play here and improve the wording of that issue. When talking about invariants it’s suboptimal to avoid details. We discussed that problem in a types team meeting on monday (<a href="https://rust-lang.zulipchat.com/#narrow/stream/326132-t-types.2Fmeetings/topic/2023-11-06.20rotating.20sync.20meeting">zulip</a>). While I have some ideas on how to handle this, this definitely requires some further consideration.</p>

<p>I also changed proof tree generation to also use the global cache in <a href="https://github.com/rust-lang/rust/pull/117394">#117394</a>. The trait solver relies on the global cache to avoid hangs, so not using it while computing a goal is less than ideal.</p>

<p>I also took another look at <a href="https://github.com/Dragon-Hatcher/type-system-chess">type-system-chess</a>. It previously compiled with the new solver but it now hangs. This change was caused by the removal of the provisional cache, which I previously expected to not be relevant. I still want to minimize the pattern causing the hang and have a lot of thoughts and ideas here.</p>

<p>There haven’t been many other changes to the new solver, see <a href="https://github.com/rust-lang/rust/pulls?q=is%3Apr+is%3Amerged+label%3AWG-trait-system-refactor+-label%3Arollup+-closed%3A%3C2023-10-27+closed%3A%3C2023-11-10+">the full list of relevant PRs</a>.</p>]]></content><author><name></name></author><category term="update" /><summary type="html"><![CDATA[I unfortunately forgot to write an update last week. I’ve finished the rework of the way we handle alias-relate goals. The PR is now ready and waiting for review: #117278. This rework seems quite promising and I am happy with the results.]]></summary></entry></feed>