{"record":{"id":"dae9f07feafff6eb","repo":"gleam-lang/gleam","slug":"errortype-constantsizenotpositive","errorCode":null,"errorMessage":"ErrorType::ConstantSizeNotPositive","messagePattern":"ErrorType::ConstantSizeNotPositive","errorType":"validation","errorClass":"ErrorType","httpStatus":null,"severity":"error","filePath":"compiler-core/src/bit_array.rs","lineNumber":334,"sourceCode":"        ..\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,\n    // we know they will never match!\n    // A negative size is still allowed in expressions as it will just result\n    // in an empty segment.\n    if let (Some(size @ Size { value, .. }), TypeOptionsMode::Pattern) = (categories.size, mode) {\n        match value.as_int_literal() {\n            Some(n) if n <= BigInt::ZERO => {\n                return err(ErrorType::ConstantSizeNotPositive, size.location());\n            }\n            Some(_) | None => (),\n        }\n    }\n\n    Ok(categories.segment_type())\n}\n\npub trait GetLiteralValue {\n    fn as_int_literal(&self) -> Option<BigInt>;\n}\n\nimpl GetLiteralValue for ast::TypedPattern {\n    fn as_int_literal(&self) -> Option<BigInt> {\n        match self {\n            ast::Pattern::Int { int_value, .. }\n            | ast::Pattern::BitArraySize(ast::BitArraySize::Int { int_value, .. }) => {\n                Some(int_value.clone())","sourceCodeStart":316,"sourceCodeEnd":352,"githubUrl":"https://github.com/gleam-lang/gleam/blob/15b07c783065c92bb016b1b74ed0995a5259850f/compiler-core/src/bit_array.rs#L316-L352","documentation":"In bit array patterns, a constant size expression evaluating to zero or a negative number makes the segment impossible to match, so the compiler rejects it with `ConstantSizeNotPositive` (checked via `as_int_literal` against `BigInt::ZERO`). This runs only in `TypeOptionsMode::Pattern`; in expressions a negative size is allowed and merely yields an empty segment.","triggerScenarios":"Pattern matching with `<<_:size(0)>>`, `<<x:size(-8)>>`, or any constant non-positive size literal inside a bit array pattern.","commonSituations":"Copy-pasting an expression's size spec into a pattern; sizes computed from negative constants; assuming zero-width patterns are legal as they sometimes are in expressions.","solutions":["Use a positive constant size in the pattern, e.g. `<<_:size(8)>>`.","To match an empty bit array, write the empty pattern `<<>>` instead of `size(0)`.","If the size is variable, use a variable-sized rest segment such as `<<x:bytes>>` or a non-constant size expression."],"exampleFix":"// before\nlet assert <<x:size(-8)>> = data\n// after\nlet assert <<x:size(8)>> = data","handlingStrategy":"validation","validationCode":"// Pattern sizes must be strictly positive constants.\nfn valid_pattern_size(n: Int) -> Bool {\n  n > 0\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use size(0) or negative sizes in patterns; use `<<>>` to match empty arrays.","Remember negative sizes are only valid in expressions, not patterns.","Ensure size constants are positive before using them in pattern matches."],"tags":["compiler","bit-array","pattern-error","gleam"],"backgroundTag":"value-out-of-range","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"}