{"id":"a6ca8443811d2345","repo":"rust-lang/rust","slug":"tryasdyncompat-is-not-const","errorCode":null,"errorMessage":"`TryAsDynCompat` is not const: {:?}","messagePattern":"`TryAsDynCompat` is not const: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_next_trait_solver/src/solve/effect_goals.rs","lineNumber":458,"sourceCode":"    fn consider_builtin_transmute_candidate(\n        _ecx: &mut EvalCtxt<'_, D>,\n        _goal: Goal<I, Self>,\n    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {\n        unreachable!(\"TransmuteFrom is not const\")\n    }\n\n    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>","sourceCodeStart":440,"sourceCodeEnd":476,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_next_trait_solver/src/solve/effect_goals.rs#L440-L476","documentation":"Internal `unreachable!` in the next-gen trait solver's const-effect candidate assembly for `TryAsDynCompat` (the `core::ops::TryAsDynCompat` trait behind dyn-compat trait upcasting). Unlike the sibling stubs, this panic also formats the offending `goal` via `{:?}`, so the ICE message includes the exact predicate that was routed. `TryAsDynCompat` has no const implementation, so reaching this candidate is a solver bug.","triggerScenarios":"The host-effect solver is asked to prove `T: const TryAsDynCompat<U>` / `~const TryAsDynCompat<U>`, typically via dyn-trait upcasting (`dyn Trait` → `dyn Supertrait`) performed inside a `const fn` or under `#[const_trait]` propagation, with `-Znext-solver` enabled.","commonSituations":"Nightly code using trait upcasting coercion (`dyn Sub` → `dyn Super`) inside const context; `#[const_trait]` hierarchies that imply `TryAsDynCompat`; `-Znext-solver` regression after dyn-compat routing was added to effect-goal assembly.","solutions":["Read the `{:?}` goal in the ICE to identify the exact predicate, then drop its const/`~const TryAsDynCompat` bound.","Move the dyn upcast out of the const context into runtime code.","Report upstream with the ICE message (it already embeds the goal) and a minimal reproducer under `-Znext-solver`.","Bisect nightlies — dyn-compat const routing is new and unstable."],"exampleFix":"// before\nconst fn upcast<T: ?Sized + const TryAsDynCompat<dyn Debug>>(t: &T) -> &dyn Debug { t }\n\n// after\nfn upcast<T: ?Sized + TryAsDynCompat<dyn Debug>>(t: &T) -> &dyn Debug { t }","handlingStrategy":"validation","validationCode":"// build.rs: reject const TryAsDynCompat (dyn upcast) usage\nfn main() {\n    let src = std::fs::read_to_string(\"src/lib.rs\").unwrap_or_default();\n    for banned in [\"~const TryAsDynCompat\", \"const TryAsDynCompat\", \"TryAsDynCompat\"] {\n        assert!(!src.contains(banned), \"rejected: {banned} — dyn-compat upcast is not const\");\n    }\n    println!(\"cargo:rerun-if-changed=src/lib.rs\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["`TryAsDynCompat` (trait-object upcast / dyn-compatibility check) has no const candidate (effect_goals.rs:458); never perform dyn upcasts inside `const fn`.","Do trait-object upcasting only at runtime, where the vtable is available.","In const code, dispatch via `match` on an enum-of-types or a concrete type rather than `dyn Trait` upcasting.","If the ICE surfaces without an explicit upcast, the solver reached the const candidate unexpectedly — minimize and report."],"tags":["rustc","trait-solver","const-traits","ice","dyn-compat","try-as-dyn"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}