{"record":{"id":"e424e7a14c9ea02a","repo":"gleam-lang/gleam","slug":"invalid-constants-should-not-reach-code-generation-e424e7","errorCode":null,"errorMessage":"invalid constants should not reach code generation","messagePattern":"invalid constants should not reach code generation","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-core/src/javascript/expression.rs","lineNumber":2865,"sourceCode":"                | BinOp::RemainderInt => {\n                    unreachable!(\"invalid constant operator made it to code generation\")\n                }\n\n                BinOp::Concatenate => {\n                    let left = self.constant_expression(arena, context, left);\n                    let right = self.constant_expression(arena, context, right);\n                    docvec![arena, left, SPACE_PLUS_SPACE_DOCUMENT, right]\n                }\n            },\n\n            Constant::RecordUpdate { .. } => {\n                panic!(\"record updates should not reach code generation\")\n            }\n            Constant::Todo { .. } => {\n                panic!(\"todo constants should not reach code generation\")\n            }\n            Constant::Invalid { .. } => {\n                panic!(\"invalid constants should not reach code generation\")\n            }\n        }\n    }\n\n    fn constant_bit_array(\n        &mut self,\n        arena: &'doc DocumentArena<'a, 'doc>,\n        segments: &'a [TypedConstantBitArraySegment],\n        context: Context,\n    ) -> Document<'a, 'doc> {\n        self.tracker.bit_array_literal_used = true;\n        let segments_array = array(\n            arena,\n            segments.iter().filter_map(|segment| {\n                let value = match context {\n                    Context::Constant => self.constant_expression(arena, context, &segment.value),\n                    Context::Guard => self.guard_constant_expression(arena, &segment.value),\n                };","sourceCodeStart":2847,"sourceCodeEnd":2883,"githubUrl":"https://github.com/gleam-lang/gleam/blob/49f8762da595eb3c8f576656a4200a142ca188c9/compiler-core/src/javascript/expression.rs#L2847-L2883","documentation":"A defensive unreachable!() guard in the JavaScript code generator for constant expressions. When compiling a constant BinOp, only operators valid for constant folding should ever appear; RemainderInt on constants is rejected earlier (constant folding does not support integer remainder), so reaching this arm means a constant expression was not properly evaluated or filtered before code generation. It indicates an internal compiler bug — invalid constant expressions should be caught during constant evaluation, not during code emission.","triggerScenarios":"A module whose constant expression failed to parse/typecheck (leaving an `Invalid` node) is fed into the JavaScript code generation pass — typically only possible when invoking compiler internals without aborting on earlier errors.","commonSituations":"Compiler-as-library usage that skips error checks; compiler regression in error recovery that failed to record the error and stop.","solutions":["Fix the invalid constant syntax in the source module (run `gleam check` to locate it).","In tooling that uses compiler internals, stop the pipeline when errors are reported before codegen.","If no error was surfaced but this panic fires, file a Gleam compiler bug."],"exampleFix":"// before (internal pipeline)\nlet ir = lower(module);\nlet js = javascript::compile(ir); // panics on Invalid constant\n// after\nif module.error_count() > 0 { return Err(...); }\nlet js = javascript::compile(ir);","handlingStrategy":"validation","validationCode":"// abort before codegen when any constant failed to parse\nif constants.iter().any(|c| matches!(c, Constant::Invalid { .. })) {\n    return Err(Error::parse_failed());\n}","typeGuard":"fn constant_is_valid(c: &Constant) -> bool {\n    !matches!(c, Constant::Invalid { .. })\n}","tryCatchPattern":"match std::panic::catch_unwind(|| codegen_constant(&c)) {\n    Ok(doc) => doc,\n    Err(_) => report_internal_compiler_bug(),\n}","preventionTips":["Fix reported parse/type errors before attempting codegen.","In compiler-embedding tooling, check error counts after each phase.","Keep the compiler updated so error-recovery bugs are patched."],"tags":["compiler","panic","code-generation","error-recovery"],"backgroundTag":"internal-invariant-violation","analyzedSha":"49f8762da595eb3c8f576656a4200a142ca188c9","analyzedAt":"2026-09-14T11:14:59.388Z","contentChangedAt":"2026-09-14T11:14:59.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}