{"record":{"id":"45988f544fd88b3e","repo":"loco-rs/loco","slug":"type-mismatch-in-refguard","errorCode":null,"errorMessage":"Type mismatch in RefGuard","messagePattern":"Type mismatch in RefGuard","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/app.rs","lineNumber":243,"sourceCode":"\n// A wrapper around DashMap's Ref type that erases the exact type\n// but provides deref to the target type\npub struct RefGuard<'a, T: 'static + Send + Sync> {\n    inner: dashmap::mapref::one::Ref<'a, TypeId, Box<dyn Any + Send + Sync>>,\n    _phantom: std::marker::PhantomData<&'a T>,\n}\n\nimpl<T: 'static + Send + Sync> std::ops::Deref for RefGuard<'_, T> {\n    type Target = T;\n\n    fn deref(&self) -> &Self::Target {\n        // This is safe because we only create a RefGuard for a specific type\n        // after looking it up by its TypeId\n        #[allow(clippy::coerce_container_to_any)]\n        self.inner\n            .value()\n            .downcast_ref::<T>()\n            .expect(\"Type mismatch in RefGuard\")\n    }\n}\n\n/// Represents the application context for a web server.\n///\n/// This struct encapsulates various components and configurations required by\n/// the web server to operate. It is typically used to store and manage shared\n/// resources and settings that are accessible throughout the application's\n/// lifetime.\n#[derive(Clone, FromRef)]\n#[allow(clippy::module_name_repetitions)]\n#[non_exhaustive]\npub struct AppContext {\n    /// The environment in which the application is running.\n    pub environment: Environment,\n    #[cfg(feature = \"with-db\")]\n    /// A database connection used by the application.\n    pub db: DatabaseConnection,","sourceCodeStart":225,"sourceCodeEnd":261,"githubUrl":"https://github.com/loco-rs/loco/blob/23639d1e360dbc618073642b507d6f8664adbaff/src/app.rs#L225-L261","documentation":"A defensive `expect` panic in Loco's shared-store `RefGuard` access. The guard is only supposed to be created for a type after looking it up by `TypeId`, so a downcast failure means the internal type-map invariant was violated — the stored value's concrete type doesn't match `T`.","triggerScenarios":"Accessing `ctx.shared_store` with a type parameter `T` that was never stored under that `TypeId`, or a bug in the RefGuard construction path storing a mismatched concrete type.","commonSituations":"Requesting a shared-store value with a slightly different type than inserted (e.g. `Arc<Config>` vs `Config`); framework-level bugs after refactors of the shared_store container.","solutions":["Check that the exact type you pass when reading the shared store matches the one inserted (including wrappers like `Arc<...>`)","Insert the value through the typed API before creating a RefGuard for it","If it reproduces with correct usage, file a loco-rs issue — this indicates an internal invariant violation"],"exampleFix":"// before\nlet cfg = ctx.shared_store.get::<Config>();\n// after\nlet cfg = ctx.shared_store.get::<Arc<Config>>(); // type actually stored","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"fn try_get<T: Any + Send + Sync>(store: &SharedStore) -> Option<RefGuard<T>> {\n    store.try_get::<T>()\n}","tryCatchPattern":null,"preventionTips":["Read and write the shared store with the exact same type, including wrappers like Arc<T>","Prefer the typed accessor APIs over raw TypeId lookups","Document the types stored in shared_store for the whole app"],"tags":["panic","invariant","shared-store","downcast"],"backgroundTag":"type-mismatch","analyzedSha":"23639d1e360dbc618073642b507d6f8664adbaff","analyzedAt":"2026-09-12T01:47:20.769Z","contentChangedAt":"2026-09-12T01:47:20.769Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}