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
- Do not perform unsizing coercions in const context — they are not const-evaluable; build the unsized value at runtime or as a `static`.
- Drop any `const`/`~const Unsize` bound and any `#[const_trait]` supertrait that implies it.
- Report the ICE upstream with a minimal reproducer and `-Znext-solver` flags.
- 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
- `Unsize` (the coercion behind `&[T;N] as &[T]` and `&T as &dyn Trait`) has no const candidate (effect_goals.rs:465); avoid unsizing coercions inside `const fn` / `static` initializers.
- In const code, work with fixed-size arrays `[T; N]` and references to concrete types, not `&[T]` slices or `&dyn Trait` produced by coercion.
- If you need a slice in const context, construct it from an array reference only where the compiler supports it without the Unsize effect goal; otherwise compute at runtime.
- Bare ICE without an explicit coercion = solver routing bug; capture the `-Ztrait-solver=next` reproducer.
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
- unexpected infer {a_ty:?} {b_ty:?}
- FusedIterator is not const
- AsyncIterator is not const
- Coroutine is not const
- DiscriminantKind is not const
AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03).
Data as JSON: /data/errors/29ce5352ae8815ac.json.
Report an issue: GitHub.