{"record":{"id":"238e5f7edec58a09","repo":"BoundaryML/baml","slug":"diagnostic-message-style-code-is-a-valid-unicode-variation","errorCode":null,"errorMessage":"diagnostic message style code is a valid Unicode variation selector","messagePattern":"diagnostic message style code is a valid Unicode variation selector","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"baml_language/crates/baml_compiler_diagnostics/src/render.rs","lineNumber":688,"sourceCode":"    };\n    let mut attributes = 0;\n    if style.attributes.contains(HighlightAttributes::BOLD) {\n        attributes |= 1;\n    }\n    if style.attributes.contains(HighlightAttributes::DIM) {\n        attributes |= 2;\n    }\n    if style.attributes.contains(HighlightAttributes::ITALIC) {\n        attributes |= 4;\n    }\n    if style\n        .attributes\n        .contains(HighlightAttributes::STRIKETHROUGH)\n    {\n        attributes |= 8;\n    }\n    char::from_u32(MESSAGE_STYLE_CODE_BASE + foreground + MESSAGE_FOREGROUND_COUNT * attributes)\n        .expect(\"diagnostic message style code is a valid Unicode variation selector\")\n}\n\nfn message_style(code: char) -> Option<OwoStyle> {\n    let value = u32::from(code).checked_sub(MESSAGE_STYLE_CODE_BASE)?;\n    if value >= MESSAGE_FOREGROUND_COUNT * MESSAGE_ATTRIBUTE_COUNT {\n        return None;\n    }\n    let foreground = match value % MESSAGE_FOREGROUND_COUNT {\n        0 => None,\n        1 => Some(HighlightColor::Green),\n        2 => Some(HighlightColor::Yellow),\n        3 => Some(HighlightColor::Magenta),\n        4 => Some(HighlightColor::Cyan),\n        5 => Some(HighlightColor::BrightYellow),\n        6 => Some(HighlightColor::BrightBlue),\n        7 => Some(HighlightColor::BrightMagenta),\n        8 => Some(HighlightColor::BrightCyan),\n        _ => unreachable!(),","sourceCodeStart":670,"sourceCodeEnd":706,"githubUrl":"https://github.com/BoundaryML/baml/blob/bd85ce9dee1463ff04d27efd20531013a4ff46c1/baml_language/crates/baml_compiler_diagnostics/src/render.rs#L670-L706","documentation":"message_style_code packs a foreground color and highlight attributes into a Unicode variation selector: char::from_u32(MESSAGE_STYLE_CODE_BASE + foreground + MESSAGE_FOREGROUND_COUNT * attributes). The expect asserts the computed scalar is a valid char. Given the constants (small foreground/attribute counts), the result always stays below the Unicode scalar maximum, so it can only fail if the constants change.","triggerScenarios":"Only if MESSAGE_STYLE_CODE_BASE, MESSAGE_FOREGROUND_COUNT, or the attribute bitmask is edited so the sum exceeds 0x10FFFF (or lands in a surrogate range) — then from_u32 returns None and this panics when marked_message styles a message.","commonSituations":"A maintainer raises the number of supported foreground colors or adds attribute bits (e.g. a ninth attribute) without re-basing the variation-selector encoding.","solutions":["Keep MESSAGE_FOREGROUND_COUNT * MESSAGE_ATTRIBUTE_COUNT small enough that BASE + max index is a valid scalar; add a compile-time or unit-test assertion.","If adding attributes/colors, recompute the encoding bounds and update the decoder (message_style) consistently.","Replace expect with a const-checked computation or debug_assert plus graceful fallback."],"exampleFix":"// before\nchar::from_u32(MESSAGE_STYLE_CODE_BASE + foreground + MESSAGE_FOREGROUND_COUNT * attributes)\n    .expect(\"diagnostic message style code is a valid Unicode variation selector\")\n// after\nconst _: () = assert!(\n    MESSAGE_STYLE_CODE_BASE + MESSAGE_FOREGROUND_COUNT * MESSAGE_ATTRIBUTE_COUNT <= 0x10FFFF,\n    \"style code encoding exceeds Unicode scalar range\"\n);","handlingStrategy":"validation","validationCode":"const _: () = assert!(\n    MESSAGE_STYLE_CODE_BASE + MESSAGE_FOREGROUND_COUNT * MESSAGE_ATTRIBUTE_COUNT <= 0x10FFFF,\n    \"style encoding exceeds Unicode scalar range\"\n);","typeGuard":null,"tryCatchPattern":"// Prefer compile-time assertions; a runtime fallback: if from_u32 returns None, render unstyled.","preventionTips":["Never raise MESSAGE_FOREGROUND_COUNT or attribute bits without recomputing encoding bounds.","Add a unit test that round-trips every style code through message_style.","Keep attribute count a power of two and document the bit layout."],"tags":["rust","unicode","encoding","invariant"],"backgroundTag":"value-out-of-range","analyzedSha":"bd85ce9dee1463ff04d27efd20531013a4ff46c1","analyzedAt":"2026-09-12T03:38:25.718Z","contentChangedAt":"2026-09-12T03:38:25.718Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}