Skip to content

Commit 7224813

Browse files
committed
fix: Handle enum patterns against non-enum scrutinee types
When an enum pattern (like .none) is matched against a non-enum type (like i32), the pattern test now correctly returns false instead of trying to extract a discriminant from the integer, which would cause an LLVM error (ExtractValue can only be used on struct/array types).
1 parent e0f743f commit 7224813

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

crates/compiler/src/ssa.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3971,6 +3971,25 @@ impl SsaBuilder {
39713971
) -> CompilerResult<HirId> {
39723972
use zyntax_typed_ast::Type as FrontendType;
39733973

3974+
// Check if the scrutinee is actually an enum/named type that supports enum patterns
3975+
// If the scrutinee is a primitive type, enum pattern matching always fails
3976+
let is_enum_type = matches!(scrutinee_ty,
3977+
FrontendType::Named { .. } |
3978+
FrontendType::Union(_) |
3979+
FrontendType::Optional(_) |
3980+
FrontendType::Result { .. }
3981+
);
3982+
3983+
if !is_enum_type {
3984+
// For non-enum types (like integers), enum patterns always fail to match
3985+
// Return a constant false value
3986+
let false_val = self.create_value(
3987+
HirType::Bool,
3988+
HirValueKind::Constant(crate::hir::HirConstant::Bool(false))
3989+
);
3990+
return Ok(false_val);
3991+
}
3992+
39743993
// Step 1: Extract discriminant (tag) from enum
39753994
// Enums are typically represented as tagged unions: { tag: u32, payload: union { ... } }
39763995
// The tag is usually at index 0

0 commit comments

Comments
 (0)