# Err automatic hint::cold\_path?

**URL:** <https://internals.rust-lang.org/t/err-automatic-hint-cold-path/24404>\
**Category:** language design\
**Created:** [June 19, 2026, 3:37pm UTC](https://internals.rust-lang.org/t/err-automatic-hint-cold-path/24404 "2026-06-19T15:37:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dwarsloeper](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dwarsloeper/32/11073_2.png) [@Dwarsloeper](https://internals.rust-lang.org/u/Dwarsloeper)\
**Post date:** [June 19, 2026, 3:37pm UTC](https://internals.rust-lang.org/t/err-automatic-hint-cold-path/24404/1 "2026-06-19T15:37:08Z")

</div>

Looking at this convert function, I was surprised that it doesn’t have a `hint::cold_path`. IMHO both consuming and producing an error, doubly make the second branch a candidate. OTOH, if it didn’t have the const-hack, a `?` would offer no place to put the hint. That makes me wonder, whether just like for `panic!()` that happens automatically.

```rust
pub const fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> {
    // FIXME(const-hack): This should use `?` again, once it's `const`
    match run_utf8_validation(v) {
        Ok(_) => {
            // SAFETY: validation succeeded.
            Ok(unsafe { from_utf8_unchecked(v) })
        }
        Err(err) => Err(err),
    }
}

```

That raises the question: what about `None`. That might be more common than `Err`. But is it common enough to make an automatically inserted `hint::cold_path` counterproductive?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [June 19, 2026, 6:08pm UTC](https://internals.rust-lang.org/t/err-automatic-hint-cold-path/24404/2 "2026-06-19T18:08:34Z")

</div>

It's not obvious to me that this is the right thing for the standard library to do.

Notably, things like `u32::from_str` it's not obvious that the `Err` being cold is necessarily the right choice. And adding anything in `?` is at risk of a pretty bad compilation-time hit. (IIRC serde isn't using `?` because its generality and `From` made it materially slower to compile, even if it optimized away.)

So overall, I generally think that leaving this kind of thing to PGO is the better choice.

I definitely don't think doing it for `Option` would be good, because things like `try { x?.foo() }` being used on stuff that happens to be `None` is pretty common when it's used as "this property/hook/etc really is optional". _Maybe_ it'd work in the `Err` arm for [`Result::branch`](https://doc.rust-lang.org/std/result/enum.Result.html#method.branch)? But that'd also make `try { x? }` no longer a no-op, which has its own consequences. You could always try making a PR for it and see what happens, though since the compiler uses PGO I doubt it'll show any improvement in the compiler perf suite.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [June 23, 2026, 6:04pm UTC](https://internals.rust-lang.org/t/err-automatic-hint-cold-path/24404/3 "2026-06-23T18:04:16Z")

</div>

The other thing that people are _already_ doing here is doing this specifically for error types.

If you look in `anyhow`, for example:

> <https://github.com/dtolnay/anyhow/blob/d9dc3faf78b8647fdb5b8c5b53abb85e05e13d42/src/error.rs#L673-L683>

That's marked `#[cold]` so if you're using `anyhow` then any time `?` wraps an error into an `anyhow::Error` it's already treated as cold automatically.

Which is nice in that it's specific to that error type, since _for anyhow_ it definitely makes sense -- anything non-cold probably shouldn't be taking backtraces and such anyhow.

---

<div class="post-metadata">

**Author:** ![jhpratt](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jhpratt/32/11640_2.png) [@jhpratt](https://internals.rust-lang.org/u/jhpratt)\
**Post date:** [June 23, 2026, 8:47pm UTC](https://internals.rust-lang.org/t/err-automatic-hint-cold-path/24404/4 "2026-06-23T20:47:18Z")

</div>

For what it's worth, in `time` the macro that I use to avoid calling `From` (due to const limitations) has an explicit cold path annotation. I have measured real improvements by doing that.
