rust-lang/rust · critical

Unsize is not const

Error message

Unsize is not const

What it means

Internal `unreachable!` in the next-gen trait solver's const-effect candidate assembly for the built-in `Unsize` trait (`core::marker::Unsize`), which backs unsizing coercions (`&[T; N]` → `&[T]`, `&T` → `&dyn Trait`, etc.). `consider_structural_builtin_unsize_candidates` is a stub because unsizing is not const, so reaching it is a solver routing bug and ICEs the compiler.

Source

Thrown at compiler/rustc_next_trait_solver/src/solve/effect_goals.rs:465

    fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(
        _ecx: &mut EvalCtxt<'_, D>,
        _goal: Goal<I, Self>,
    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
        unreachable!("BikeshedGuaranteedNoDrop is not const");
    }

    fn consider_builtin_try_as_dyn_candidate(
        _ecx: &mut EvalCtxt<'_, D>,
        goal: Goal<I, Self>,
    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
        unreachable!("`TryAsDynCompat` is not const: {:?}", goal)
    }

    fn consider_structural_builtin_unsize_candidates(
        _ecx: &mut EvalCtxt<'_, D>,
        _goal: Goal<I, Self>,
    ) -> Result<Vec<Candidate<I>>, RerunNonErased> {
        unreachable!("Unsize is not const")
    }

    fn consider_builtin_field_candidate(
        _ecx: &mut EvalCtxt<'_, D>,
        _goal: Goal<<D as SolverDelegate>::Interner, Self>,
    ) -> Result<Candidate<<D as SolverDelegate>::Interner>, NoSolutionOrRerunNonErased> {
        unreachable!("Field is not const")
    }
}

impl<D, I> EvalCtxt<'_, D>
where
    D: SolverDelegate<Interner = I>,
    I: Interner,
{
    #[instrument(level = "trace", skip(self))]
    pub(super) fn compute_host_effect_goal(
        &mut self,

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Do not perform unsizing coercions in const context — they are not const-evaluable; build the unsized value at runtime or as a `static`.
  2. Drop any `const`/`~const Unsize` bound and any `#[const_trait]` supertrait that implies it.
  3. Report the ICE upstream with a minimal reproducer and `-Znext-solver` flags.
  4. Bisect nightlies; unsizing const routing is under active refinement.

Example fix

// before
const fn as_dyn<T: const Unsize<dyn Debug>>(t: &T) -> &dyn Debug { t }

// after
fn as_dyn<T: Unsize<dyn Debug>>(t: &T) -> &dyn Debug { t }
Defensive patterns

Strategy: validation

Validate before calling

// build.rs: reject const Unsize (fat-pointer coercion) effect goals
fn main() {
    let src = std::fs::read_to_string("src/lib.rs").unwrap_or_default();
    for banned in ["~const Unsize", "const Unsize", ": const Unsize"] {
        assert!(!src.contains(banned), "rejected: {banned} — Unsize coercion is not const");
    }
    println!("cargo:rerun-if-changed=src/lib.rs");
}

Prevention

When it happens

Trigger: The host-effect solver probes a `T: const Unsize<U>` / `~const Unsize<U>` goal — e.g. an unsizing coercion performed inside a `const fn`, a fat-pointer/dyn-trait construction in const context, or a `#[const_trait]` whose implied bounds pull in `Unsize`. Note this method returns `Vec<Candidate>` (not a single candidate), so it is called once per unsizing probe.

Common situations: Constructing `&dyn Trait`/`Box<dyn Trait>`/slice references inside nightly `const fn` or `const` initializers; `-Znext-solver` on code with unsizing coercions; nightly regression after unsizing effect-goal routing landed.

Related errors


AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03). Data as JSON: /data/errors/29ce5352ae8815ac.json. Report an issue: GitHub.