{"record":{"id":"c710bb6f79261d19","repo":"rust-lang/rust","slug":"cannot-create-a-zero-sized-constant-for-type-ty","errorCode":null,"errorMessage":"Cannot create a zero-sized constant for type `{ty_internal}`: {err}","messagePattern":"Cannot create a zero-sized constant for type `(.+?)`: (.+?)","errorType":"exception","errorClass":"B::Error","httpStatus":null,"severity":"error","filePath":"compiler/rustc_public_bridge/src/context/impls.rs","lineNumber":475,"sourceCode":"\n    /// Evaluate constant as a target usize.\n    pub fn eval_target_usize(&self, cnst: MirConst<'tcx>) -> Result<u64, B::Error> {\n        use crate::context::TypingEnvHelpers;\n        cnst.try_eval_target_usize(self.tcx, self.fully_monomorphized())\n            .ok_or_else(|| B::Error::new(format!(\"Const `{cnst:?}` cannot be encoded as u64\")))\n    }\n\n    pub fn eval_target_usize_ty(&self, cnst: ty::Const<'tcx>) -> Result<u64, B::Error> {\n        cnst.try_to_target_usize(self.tcx)\n            .ok_or_else(|| B::Error::new(format!(\"Const `{cnst:?}` cannot be encoded as u64\")))\n    }\n\n    pub fn try_new_const_zst(&self, ty_internal: Ty<'tcx>) -> Result<MirConst<'tcx>, B::Error> {\n        let size = self\n            .tcx\n            .layout_of(self.fully_monomorphized().as_query_input(ty_internal))\n            .map_err(|err| {\n                B::Error::new(format!(\n                    \"Cannot create a zero-sized constant for type `{ty_internal}`: {err}\"\n                ))\n            })?\n            .size;\n        if size.bytes() != 0 {\n            return Err(B::Error::new(format!(\n                \"Cannot create a zero-sized constant for type `{ty_internal}`: \\\n                Type `{ty_internal}` has {} bytes\",\n                size.bytes()\n            )));\n        }\n\n        Ok(MirConst::Ty(ty_internal, self.const_zero_sized(ty_internal)))\n    }\n\n    pub fn const_zero_sized(&self, ty_internal: Ty<'tcx>) -> ty::Const<'tcx> {\n        ty::Const::zero_sized(self.tcx, ty_internal)\n    }","sourceCodeStart":457,"sourceCodeEnd":493,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_public_bridge/src/context/impls.rs#L457-L493","documentation":"Thrown by `try_new_const_zst` (impls.rs:475) when computing the layout of `ty_internal` fails. `layout_of` returns an error for types whose layout cannot be determined — unsized types, types with normalization errors, size overflow, or cyclic layout.","triggerScenarios":"Calling `try_new_const_zst` on an unsized type (`str`, `dyn Trait`, slices), a type with an unknown/opaque layout, or a type whose layout computation overflows or hits a cycle.","commonSituations":"Constructing zero-sized constants from inferred or generic types whose layout is not yet resolvable; passing foreign-opaque types into const construction.","solutions":["Resolve/normalize the type and confirm it is sized before constructing a ZST constant.","If the type genuinely has no static layout, construct the constant through a different path (e.g. an explicit allocation).","Catch the layout error and report the offending type for diagnosis."],"exampleFix":"// before\nlet c = ctxt.try_new_const_zst(ty)?;\n\n// after\n// ensure the type has a computable layout first\nlet layout = ctxt.tcx.layout_of(ctxt.fully_monomorphized().as_query_input(ty))\n    .map_err(|e| format!(\"layout error for {ty}: {e}\"))?;\nlet c = ctxt.try_new_const_zst(ty)?;","handlingStrategy":"try-catch","validationCode":"// Pre-check layout computability before constructing the ZST constant.\nif ctxt.tcx.layout_of(ctxt.fully_monomorphized().as_query_input(ty)).is_err() {\n    return Err(format!(\"type {ty} has no computable layout\"));\n}","typeGuard":"fn has_computable_layout<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, env: TypingEnv<'tcx>) -> bool {\n    tcx.layout_of(env.as_query_input(ty)).is_ok()\n}","tryCatchPattern":"let c = match ctxt.try_new_const_zst(ty) {\n    Ok(c) => c,\n    Err(e) => {\n        // layout failed; log type and fall back or propagate\n        return Err(e);\n    }\n};","preventionTips":["Verify a type is sized with a known layout before ZST const construction.","Normalize generic types in fully_monomorphized env first."],"tags":["rustc-public-bridge","const-eval","layout","zst"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}