{"id":"bcec1327feed70ef","repo":"rust-lang/rust","slug":"unexpected-associated-type-in-field","errorCode":null,"errorMessage":"unexpected associated type {:?} in `Field`","messagePattern":"unexpected associated type (.+?) in `Field`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"compiler/rustc_next_trait_solver/src/solve/normalizes_to.rs","lineNumber":1073,"sourceCode":"    }\n\n    fn consider_builtin_field_candidate(\n        ecx: &mut EvalCtxt<'_, D>,\n        goal: Goal<I, Self>,\n    ) -> Result<Candidate<I>, NoSolutionOrRerunNonErased> {\n        let self_ty = goal.predicate.self_ty();\n        let ty::Adt(def, args) = self_ty.kind() else {\n            return Err(NoSolution.into());\n        };\n        let Some(FieldInfo { base, ty, .. }) = def.field_representing_type_info(ecx.cx(), args)\n        else {\n            return Err(NoSolution.into());\n        };\n        let def_id = goal.predicate.alias.expect_projection_ty_def_id();\n        let ty = match ecx.cx().as_projection_lang_item(def_id) {\n            Some(SolverProjectionLangItem::FieldBase) => base,\n            Some(SolverProjectionLangItem::FieldType) => ty,\n            _ => panic!(\"unexpected associated type {:?} in `Field`\", goal.predicate),\n        };\n        ecx.probe_builtin_trait_candidate(BuiltinImplSource::Misc).enter(|ecx| {\n            ecx.instantiate_normalizes_to_term(goal, ty.into())?;\n            ecx.evaluate_added_goals_and_make_canonical_response(Certainty::Yes)\n        })\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!(\"try_as_dyn helper trait doesn't have assoc types\")\n    }\n}\n\nimpl<D, I> EvalCtxt<'_, D>\nwhere\n    D: SolverDelegate<Interner = I>,","sourceCodeStart":1055,"sourceCodeEnd":1091,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_next_trait_solver/src/solve/normalizes_to.rs#L1055-L1091","documentation":"The `Field` lang-item trait (the experimental named-struct-field-projection / offset_of feature) exposes exactly two associated-type projections, `FieldBase` and `FieldType`. `consider_builtin_field_candidate` (normalizes_to.rs:1073) handles those two via `SolverProjectionLangItem::{FieldBase,FieldType}`; the `_ => panic!` arm fires when the projection's def_id resolves to some other lang item or an unexpected associated type on `Field`. That means a `NormalizesTo` goal was built for an associated type this builtin candidate does not know how to project — an internal-invariant violation.","triggerScenarios":"A projection goal `<T as Field>::Assoc` reaches `consider_builtin_field_candidate` where `as_projection_lang_item(def_id)` returns neither `FieldBase` nor `FieldType` — e.g. a hand-written lang-item trait with extra assoc types, or a mismatch where the def_id maps to a different lang item.","commonSituations":"Developers experimenting with `core::ops::Field` / field-projection on nightly under `-Znext-solver`, particularly after changes to the `Field` lang item or when mixing the feature with custom assoc types on the same trait.","solutions":["Report the ICE at https://github.com/rust-lang/rust/issues with the `<T as Field>::...` projection and the panic line.","Disable `-Znext-solver` and the `field`/`offset_of_enum` nightly feature flags to avoid the path.","Update nightly (`rustup update nightly`).","Minimize: remove custom associated types on the type involved in the `Field` projection and re-test."],"exampleFix":"// before — custom assoc on a type used with field projection\nimpl<...> Field for S { type FieldBase = ...; type FieldType = ...; type Extra = ...; }\n// after — keep only the two well-known associated types\nimpl<...> Field for S { type FieldBase = ...; type FieldType = ...; }","handlingStrategy":"type-guard","validationCode":"// The `Field` trait (struct field projection / offset_of feature) only provides\n// types matching real struct fields. Validate that any associated type you name\n// corresponds to an actual field before referencing it.\nstruct Foo { a: i32, b: String }\n// Correct:  <Foo as Field<name::a>>::Type  -> i32\n// Wrong:    <Foo as Field<name::c>>::Type   -> triggers error\nfn check_field_type<T>() -> bool { /* compile-time only */ false }","typeGuard":"// Only reference Field associated types that are backed by a real named field.\n// Use a macro to restrict projections to known field names:\nmacro_rules! field_type {\n    ($struct:ty, $field:ident) => {\n        <$struct as core::ops::Field<core::mem::offset_of!($struct, $field)>>::Type\n    };\n}\n// This fails at compile time if `$field` does not exist on `$struct`,\n// surfacing a clear E-field-not-found rather than the solver panic.","tryCatchPattern":null,"preventionTips":["Only reference associated types on the `Field` trait that correspond to actual struct fields declared in the type definition.","Gate field-projection code behind `#![feature(struct_field_projection)]` awareness — the feature is unstable and solver paths may panic on misuse.","Run `cargo check` with the current nightly before adding new Field projections; the error surfaces only when the solver normalises the goal."],"tags":["rustc","trait-solver","next-solver","ice","field-projection","lang-item"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}