{"record":{"id":"8d9b9526c44c9641","repo":"rust-lang/rust","slug":"cannot-create-a-zero-sized-constant-for-type-ty-8d9b95","errorCode":null,"errorMessage":"Cannot create a zero-sized constant for type `{ty_internal}`: Type `{ty_internal}` has {} bytes","messagePattern":"Cannot create a zero-sized constant for type `(.+?)`: Type `(.+?)` has (.+?) bytes","errorType":"exception","errorClass":"B::Error","httpStatus":null,"severity":"error","filePath":"compiler/rustc_public_bridge/src/context/impls.rs","lineNumber":481,"sourceCode":"    }\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    }\n\n    /// Create a caller location constant from a span.\n    ///\n    /// This produces a `&'static core::panic::Location<'static>` constant,\n    /// which is the implicit extra argument for `#[track_caller]` functions.\n    pub fn span_as_caller_location(&self, span: Span) -> MirConst<'tcx> {","sourceCodeStart":463,"sourceCodeEnd":499,"githubUrl":"https://github.com/rust-lang/rust/blob/7088e4b63a9516ebfbfe2ab2d999cf01a528ac14/compiler/rustc_public_bridge/src/context/impls.rs#L463-L499","documentation":"Thrown by `try_new_const_zst` (impls.rs:481) when the type's layout is computed successfully but its size is non-zero. A zero-sized constant is only valid for zero-sized types; passing a normal sized type (e.g. `i32`, a struct with fields) triggers this.","triggerScenarios":"Calling `try_new_const_zst` for a type whose `layout_of(...).size.bytes() != 0`, such as any integer/float, a non-empty struct/enum, or a pointer.","commonSituations":"Misusing `try_new_const_zst` as a generic 'default constant' constructor for arbitrary types; assuming a type is ZST when it is not after a refactor changes its fields.","solutions":["Only call `try_new_const_zst` after confirming the type's size is 0.","For non-ZST types, use the appropriate constructor (`try_new_const_uint`, `new_const_str`, scalar constructors).","Add a debug assertion / branch on `layout.size().bytes() == 0`."],"exampleFix":"// before\nlet c = ctxt.try_new_const_zst(i32_type)?; // i32 is not ZST\n\n// after\nlet c = ctxt.try_new_const_uint(0u128, i32_type)?; // sized type: use real value","handlingStrategy":"validation","validationCode":"// Only build a ZST constant when the type is genuinely zero-sized.\nlet size = ctxt.tcx.layout_of(ctxt.fully_monomorphized().as_query_input(ty))?.size.bytes();\nif size != 0 {\n    // use a sized-type constructor instead\n    return ctxt.try_new_const_uint(0, ty);\n}\nctxt.try_new_const_zst(ty)","typeGuard":"fn is_zst<'tcx>(ty: Ty<'tcx>, tcx: TyCtxt<'tcx>, env: TypingEnv<'tcx>) -> bool {\n    tcx.layout_of(env.as_query_input(ty)).map_or(false, |l| l.size.bytes() == 0)\n}","tryCatchPattern":null,"preventionTips":["Reserve try_new_const_zst strictly for unit-like / zero-sized types.","Branch on size before choosing a constant constructor."],"tags":["rustc-public-bridge","const-eval","zst","layout"],"backgroundTag":null,"analyzedSha":"7088e4b63a9516ebfbfe2ab2d999cf01a528ac14","analyzedAt":"2026-08-10T14:17:03.603Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}