{"id":"29ce5352ae8815ac","repo":"rust-lang/rust","slug":"unsize-is-not-const","errorCode":null,"errorMessage":"Unsize is not const","messagePattern":"Unsize is not const","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_next_trait_solver/src/solve/effect_goals.rs","lineNumber":465,"sourceCode":"    fn consider_builtin_bikeshed_guaranteed_no_drop_candidate(\n        _ecx: &mut EvalCtxt<'_, D>,\n        _goal: Goal<I, Self>,\n    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {\n        unreachable!(\"BikeshedGuaranteedNoDrop is not const\");\n    }\n\n    fn consider_builtin_try_as_dyn_candidate(\n        _ecx: &mut EvalCtxt<'_, D>,\n        goal: Goal<I, Self>,\n    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {\n        unreachable!(\"`TryAsDynCompat` is not const: {:?}\", goal)\n    }\n\n    fn consider_structural_builtin_unsize_candidates(\n        _ecx: &mut EvalCtxt<'_, D>,\n        _goal: Goal<I, Self>,\n    ) -> Result<Vec<Candidate<I>>, RerunNonErased> {\n        unreachable!(\"Unsize is not const\")\n    }\n\n    fn consider_builtin_field_candidate(\n        _ecx: &mut EvalCtxt<'_, D>,\n        _goal: Goal<<D as SolverDelegate>::Interner, Self>,\n    ) -> Result<Candidate<<D as SolverDelegate>::Interner>, NoSolutionOrRerunNonErased> {\n        unreachable!(\"Field is not const\")\n    }\n}\n\nimpl<D, I> EvalCtxt<'_, D>\nwhere\n    D: SolverDelegate<Interner = I>,\n    I: Interner,\n{\n    #[instrument(level = \"trace\", skip(self))]\n    pub(super) fn compute_host_effect_goal(\n        &mut self,","sourceCodeStart":447,"sourceCodeEnd":483,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_next_trait_solver/src/solve/effect_goals.rs#L447-L483","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nconst fn as_dyn<T: const Unsize<dyn Debug>>(t: &T) -> &dyn Debug { t }\n\n// after\nfn as_dyn<T: Unsize<dyn Debug>>(t: &T) -> &dyn Debug { t }","handlingStrategy":"validation","validationCode":"// build.rs: reject const Unsize (fat-pointer coercion) effect goals\nfn main() {\n    let src = std::fs::read_to_string(\"src/lib.rs\").unwrap_or_default();\n    for banned in [\"~const Unsize\", \"const Unsize\", \": const Unsize\"] {\n        assert!(!src.contains(banned), \"rejected: {banned} — Unsize coercion is not const\");\n    }\n    println!(\"cargo:rerun-if-changed=src/lib.rs\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["`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."],"tags":["rustc","trait-solver","const-traits","ice","unsize","coercion"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}