gleam-lang/gleam · error
invalid constants should not reach code generation
Error message
invalid constants should not reach code generation
What it means
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.
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.
Example fix
// before (module code triggering it) pub const x = undefined_function() // after pub const x = 1
Defensive patterns
Strategy: validation
Validate before calling
// Ensure constants type-check before compiling
let outcome = project.analyse()?; // fix every reported constant error first
if outcome.has_errors() { return Err(...); } // do not invoke codegen with errors Type guard
match constant { Constant::Invalid { .. } => None, c => Some(c) } Try / catch
// Compile as a subprocess so a compiler panic doesn't take down your tool
let status = Command::new("gleam").args(["build"]).status()?;
if !status.success() { /* handle compile failure */ } Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.
Related errors
- record updates should not reach code generation
- invalid patterns should not reach code generation
- invalid size in pattern size segment
- uninferred constructor made it to codegen
- invalid pattern inside aliased bit array pattern segment
AI-assisted analysis of gleam-lang/gleam@15b07c7830 (2026-09-14).
Data as JSON: /api/errors/e5163b069760184d.
Report an issue: GitHub.
Appendix: source
Thrown at compiler-core/src/erlang.rs:4394
module: &TypedModule,
) -> imbl::HashSet<EcoString> {
let mut overridden_publicity = imbl::HashSet::new();
for constant in &module.definitions.constants {
if constant.publicity.is_importable() {
find_referenced_private_functions(&constant.value, &mut overridden_publicity);
}
}
overridden_publicity
}
fn find_referenced_private_functions(
constant: &TypedConstant,
already_found: &mut imbl::HashSet<EcoString>,
) {
match constant {
Constant::Todo { .. } => panic!("todo constants should not reach code generation"),
Constant::Invalid { .. } => panic!("invalid constants should not reach code generation"),
Constant::RecordUpdate { .. } => {
panic!("record updates should not reach code generation")
}
Constant::Int { .. }
| Constant::Float { .. }
| Constant::String { .. }
| Constant::BitArray { .. } => (),
TypedConstant::Var {
name, constructor, ..
} => {
if let Some(ValueConstructor { type_, .. }) = constructor.as_deref()
&& let Type::Fn { .. } = **type_
{
let _ = already_found.insert(name.clone());
}
}View on GitHub (pinned to 15b07c7830)