{"record":{"id":"fa3dd9dab831812d","repo":"gleam-lang/gleam","slug":"errortype-unitmusthavesize","errorCode":null,"errorMessage":"ErrorType::UnitMustHaveSize","messagePattern":"ErrorType::UnitMustHaveSize","errorType":"validation","errorClass":"ErrorType","httpStatus":null,"severity":"error","filePath":"compiler-core/src/bit_array.rs","lineNumber":309,"sourceCode":"            return err(\n                ErrorType::TypeDoesNotAllowSize {\n                    type_: type_.label(),\n                },\n                size.location(),\n            );\n        }\n\n        _ => {}\n    }\n\n    // if unit specified, size must be specified\n    if let SegmentOptionCategories {\n        unit: Some(unit),\n        size: None,\n        ..\n    } = categories\n    {\n        return err(ErrorType::UnitMustHaveSize, unit.location());\n    }\n\n    // float only 16/32/64\n    if let SegmentOptionCategories {\n        type_: Some(Float { .. }),\n        size: Some(size),\n        ..\n    } = categories\n        && let Some(abox) = size.value()\n    {\n        match abox.as_int_literal() {\n            None => (),\n            Some(value) if value == 16.into() || value == 32.into() || value == 64.into() => (),\n            _ => return err(ErrorType::FloatWithSize, size.location()),\n        }\n    }\n\n    // Segment patterns with a zero or negative constant size must be rejected,","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/gleam-lang/gleam/blob/15b07c783065c92bb016b1b74ed0995a5259850f/compiler-core/src/bit_array.rs#L291-L327","documentation":"A segment carrying the `unit` option must also carry an explicit `size` option, since `unit(N)` is only a multiplier of an explicit size. `type_options` checks for `unit: Some(_)` with `size: None` and raises `UnitMustHaveSize` at the `unit` option's location.","triggerScenarios":"Writing `<<x:unit(8)>>` or `<<x:int-unit(8)>>` with no `size(...)` in the same segment, in a bit array expression or a pattern.","commonSituations":"Mistaking `unit` for a standalone size; translating Erlang specs where unit appeared with an implicit size; dropping a `size(...)` option during refactoring.","solutions":["Add a `size` option alongside `unit`, e.g. `<<x:unit(8)-size(2)>>` for a 16-bit segment.","Replace `unit(N)` with `size(N)` if a fixed size alone was intended.","Remove the `unit` option entirely to use the value's natural size."],"exampleFix":"// before\nlet a = <<x:unit(8)>>\n// after\nlet a = <<x:unit(8)-size(2)>>","handlingStrategy":"validation","validationCode":"// unit(N) requires an explicit size in the same segment.\nfn unit_without_size(has_unit: Bool, has_size: Bool) -> Bool {\n  has_unit && !has_size\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pair `unit(N)` with `size(N)` in the same segment.","Remember `unit` multiplies size; it is not a size itself.","When migrating from Erlang, verify each segment has an explicit size where `unit` is used."],"tags":["compiler","bit-array","option-error","gleam"],"backgroundTag":"missing-required-argument","analyzedSha":"15b07c783065c92bb016b1b74ed0995a5259850f","analyzedAt":"2026-09-14T11:14:59.388Z","contentChangedAt":"2026-09-14T11:14:59.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}