{"record":{"id":"e5163b069760184d","repo":"gleam-lang/gleam","slug":"invalid-constants-should-not-reach-code-generation","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/erlang.rs","lineNumber":4394,"sourceCode":"    module: &TypedModule,\n) -> imbl::HashSet<EcoString> {\n    let mut overridden_publicity = imbl::HashSet::new();\n\n    for constant in &module.definitions.constants {\n        if constant.publicity.is_importable() {\n            find_referenced_private_functions(&constant.value, &mut overridden_publicity);\n        }\n    }\n    overridden_publicity\n}\n\nfn find_referenced_private_functions(\n    constant: &TypedConstant,\n    already_found: &mut imbl::HashSet<EcoString>,\n) {\n    match constant {\n        Constant::Todo { .. } => panic!(\"todo constants should not reach code generation\"),\n        Constant::Invalid { .. } => panic!(\"invalid constants should not reach code generation\"),\n        Constant::RecordUpdate { .. } => {\n            panic!(\"record updates should not reach code generation\")\n        }\n\n        Constant::Int { .. }\n        | Constant::Float { .. }\n        | Constant::String { .. }\n        | Constant::BitArray { .. } => (),\n\n        TypedConstant::Var {\n            name, constructor, ..\n        } => {\n            if let Some(ValueConstructor { type_, .. }) = constructor.as_deref()\n                && let Type::Fn { .. } = **type_\n            {\n                let _ = already_found.insert(name.clone());\n            }\n        }","sourceCodeStart":4376,"sourceCodeEnd":4412,"githubUrl":"https://github.com/gleam-lang/gleam/blob/15b07c783065c92bb016b1b74ed0995a5259850f/compiler-core/src/erlang.rs#L4376-L4412","documentation":"An internal compiler assertion: when collecting private functions referenced by a module constant for code generation, the compiler encountered a Constant::Invalid node. Invalid constants are placeholders produced by the type checker after reporting an error, and must never be passed to the Erlang code generator.","triggerScenarios":"An invalid constant expression (one that already produced a type error) flows into find_referenced_private_functions during Erlang code generation instead of being filtered out earlier.","commonSituations":"Compiling a module that already contains a constant definition error; hitting this alongside another reported type error usually indicates a compiler bug in error recovery.","solutions":["Fix the constant-definition type errors the compiler reported earlier in the module; this panic is downstream of them.","Reduce the failing module to a minimal example and check whether the earlier error is reported correctly.","Update gleam — this is an error-recovery bug that may already be fixed.","File an issue with the minimal reproducing module at github.com/gleam-lang/gleam."],"exampleFix":"// before (module code triggering it)\npub const x = undefined_function()\n// after\npub const x = 1","handlingStrategy":"validation","validationCode":"// Ensure constants type-check before compiling\nlet outcome = project.analyse()?; // fix every reported constant error first\nif outcome.has_errors() { return Err(...); } // do not invoke codegen with errors","typeGuard":"match constant { Constant::Invalid { .. } => None, c => Some(c) }","tryCatchPattern":"// Compile as a subprocess so a compiler panic doesn't take down your tool\nlet status = Command::new(\"gleam\").args([\"build\"]).status()?;\nif !status.success() { /* handle compile failure */ }","preventionTips":["Never write constant expressions that reference undefined values or functions.","Resolve all reported type errors before attempting further builds.","Report panics with a minimal module to the gleam repo — these are compiler bugs, not user errors."],"tags":["compiler","internal","codegen","assertion"],"backgroundTag":"internal-invariant-violation","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"}