rust-lang/rust · critical
try_as_dyn helper trait doesn't have assoc types
Error message
try_as_dyn helper trait doesn't have assoc types
What it means
`try_as_dyn` is an internal helper trait used by the new reflection mode (to treat a concrete type as `dyn Trait`); it declares no associated types. The `unreachable!` at normalizes_to.rs:1085 in `consider_builtin_try_as_dyn_candidate` fires only if a `NormalizesTo` goal is assembled against this trait. Since there is nothing to normalize, dispatching a projection goal here means the solver constructed a malformed goal — a compiler bug.
Source
Thrown at compiler/rustc_next_trait_solver/src/solve/normalizes_to.rs:1085
return Err(NoSolution.into());
};
let def_id = goal.predicate.alias.expect_projection_ty_def_id();
let ty = match ecx.cx().as_projection_lang_item(def_id) {
Some(SolverProjectionLangItem::FieldBase) => base,
Some(SolverProjectionLangItem::FieldType) => ty,
_ => panic!("unexpected associated type {:?} in `Field`", goal.predicate),
};
ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {
ecx.instantiate_normalizes_to_term(goal, ty.into())?;
ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)
})
}
fn consider_builtin_try_as_dyn_candidate(
_ecx: &mut EvalCtxt<'_, D>,
_goal: Goal<I, Self>,
) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {
unreachable!("try_as_dyn helper trait doesn't have assoc types")
}
}
impl<D, I> EvalCtxt<'_, D>
where
D: SolverDelegate<Interner = I>,
I: Interner,
{
fn translate_args(
&mut self,
goal: Goal<I, ty::NormalizesTo<I>>,
impl_def_id: I::ImplId,
impl_args: I::GenericArgs,
impl_trait_ref: rustc_type_ir::TraitRef<I>,
target_container_def_id: I::DefId,
) -> Result<I::GenericArgs, NoSolutionOrRerunNonErased> {
let cx = self.cx();
Ok(if target_container_def_id == impl_trait_ref.def_id.into() {View on GitHub (pinned to 22057b88b0)
Solutions
- File/search an ICE at https://github.com/rust-lang/rust/issues with the panic text `try_as_dyn helper trait doesn't have assoc types`.
- Drop `-Znext-solver` (and `-Znext-solver=reflection` in particular) from RUSTFLAGS / `.cargo/config.toml`.
- `rustup update nightly`.
- `cargo bisect-rustc` to pin the regression and attach it to the report.
Example fix
// before — reflection/try_as_dyn experiment enabled // .cargo/config.toml: rustflags = ["-Znext-solver=reflection"] // after // .cargo/config.toml: rustflags = []
Defensive patterns
Strategy: type-guard
Validate before calling
// `try_as_dyn` is an internal helper trait used by the solver to coerce
// concrete types into dyn-trait objects. It has no associated types by design.
// Do not attempt `<T as TryAsDyn<_>>::Assoc`.
fn uses_dyn_correctly<T: ?Sized + Trait>(x: &T) -> &dyn Trait { x } Type guard
// Avoid any type expression that projects an associated type off a dyn-coercion
// helper. If you need a dyn-trait's type, project off the real user trait:
// GOOD: <T as MyTrait>::Output
// BAD: <T as TryAsDyn<dyn MyTrait>>::Output (helper has no assoc types)
trait MyTrait { type Output; }
// If a macro generates these, audit the macro hygiene so it does not
// accidentally reference the internal helper name. Prevention
- Never reference `try_as_dyn` or any solver-internal helper trait in user code; it is an implementation detail with no associated types.
- Project associated types off the user-facing trait, not off dyn-coercion intermediaries.
- Audit proc-macro / derive output for accidental references to internal trait names if this ICE appears.
When it happens
Trigger: A `NormalizesTo` goal whose trait_ref is the `try_as_dyn` helper trait is routed into its builtin candidate, under `-Znext-solver` (most likely reflection mode).
Common situations: Nightly users running `-Znext-solver` with reflection (`-Znext-solver=reflection` / try-as-dyn experiments), or after a rustc update that rewires try_as_dyn candidate assembly.
Related errors
- unexpected type `{self_ty:?}`
- `TryAsDynCompat` is not const: {:?}
- we never retry stalled queries if the parent was erased
- this never happens at the root, we're never in erased mode h
- unexpected orig_value: {ty:?}
AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03).
Data as JSON: /data/errors/6f525a10365c5040.json.
Report an issue: GitHub.