{"id":"db651aa25639c8d1","repo":"rust-lang/rust","slug":"unexpected-nested-goal-in-relate-p","errorCode":null,"errorMessage":"unexpected nested goal in `relate`: {p:?}","messagePattern":"unexpected nested goal in `relate`: (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs","lineNumber":1254,"sourceCode":"\n    #[instrument(level = \"trace\", skip(self, param_env), ret)]\n    pub(super) fn relate<T: Relate<I>>(\n        &mut self,\n        param_env: I::ParamEnv,\n        lhs: T,\n        variance: ty::Variance,\n        rhs: T,\n    ) -> Result<(), NoSolutionOrRerunNonErased> {\n        let goals = self.delegate.relate(param_env, lhs, variance, rhs, self.origin_span)?;\n        for &goal in goals.iter() {\n            let source = match goal.predicate.kind().skip_binder() {\n                ty::PredicateKind::Subtype { .. }\n                | ty::PredicateKind::Clause(ty::ClauseKind::Projection(..)) => {\n                    GoalSource::TypeRelating\n                }\n                // FIXME(-Znext-solver=coinductive): should these WF goals also be unproductive?\n                ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(_)) => GoalSource::Misc,\n                p => unreachable!(\"unexpected nested goal in `relate`: {p:?}\"),\n            };\n            self.add_goal(source, goal)?;\n        }\n        Ok(())\n    }\n\n    /// Equates two values returning the nested goals without adding them\n    /// to the nested goals of the `EvalCtxt`.\n    ///\n    /// If possible, try using `eq` instead which automatically handles nested\n    /// goals correctly.\n    #[instrument(level = \"trace\", skip(self, param_env), ret)]\n    pub(super) fn eq_and_get_goals<T: Relate<I>>(\n        &self,\n        param_env: I::ParamEnv,\n        lhs: T,\n        rhs: T,\n    ) -> Result<Vec<Goal<I, I::Predicate>>, NoSolution> {","sourceCodeStart":1236,"sourceCodeEnd":1272,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs#L1236-L1272","documentation":"EvalCtxt::relate_and_add_goals calls the delegate's relate(), which returns nested goals; only Subtype, Projection (ClauseKind::Projection), and WellFormed goals are valid here. Any other PredicateKind triggers unreachable! because the type-relation pipeline is not designed to ingest arbitrary goals (e.g. Trait, RegionOutlives, ConstEquate).","triggerScenarios":"Reached when SolverDelegate::relate (invoked at compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs:1245) yields a nested goal whose kind is not Subtype, Clause(Projection), or Clause(WellFormed). Typically caused by a delegate implementation (rustc vs chalk vs external) emitting an unexpected goal kind.","commonSituations":"ICE encountered during type relation (subtyping/equating/coercion) under -Znext-solver. Almost always a bug in the delegate glue that bridges the infcx to the new solver, not user code.","solutions":["File a rustc ICE report with the {p:?} predicate printed by the panic.","Inspect the delegate's relate implementation for the backend in use and confirm it only emits Subtype/Projection/WellFormed nested goals.","Work around by disabling -Znext-solver.","Bisect changes to the delegate's TypeRelating/Generalizer code that may have started emitting extra goal kinds."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Relate must not produce nested goals. Audit user code that calls traits whose\n// associated items impose additional bounds during a relate step.\nfn related_traits_introduce_nested_goals(item: &TraitItem) -> bool {\n    item.bounds.iter().any(|b| matches!(b, Bound::Trait(_)))\n}\n// run in a `xshell`-driven lint over the crate before `cargo build`","typeGuard":"trait RelateFlat: Sized {}\nimpl<T> RelateFlat for T where T: Copy {}\n// narrow relate operations to types that cannot carry nested goals","tryCatchPattern":"use std::panic::{catch_unwind, AssertUnwindSafe};\nlet r = catch_unwind(AssertUnwindSafe(|| relate(a, b, variance)));\nmatch r { Ok(()) => (), Err(_) => log::warn!(\"nested relate goal; deferring to legacy solver\") }","preventionTips":["Avoid associated-type bounds (`T: Trait<Assoc: Bound>`) inside generic `where` clauses used by relate-heavy code paths.","Split complex trait relations into multiple, simpler relations so no single relate step spawns a nested obligation.","Reduce usage of higher-ranked trait bounds (`for<'a> T: Trait<'a>`) — these are the most common nested-goal source."],"tags":["rust","rustc","trait-solver","ice","next-solver","type-relation","delegate"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}