Skip to main content

oxilean_parse/pattern/
patterncompiler_check_methods.rs

1//! # PatternCompiler - check_methods Methods
2//!
3//! This module contains method implementations for `PatternCompiler`.
4//!
5//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
6
7use crate::Pattern;
8
9use super::patterncompiler_type::PatternCompiler;
10
11impl PatternCompiler {
12    /// Check if a pattern set is exhaustive.
13    ///
14    /// A pattern set is exhaustive when it contains at least one irrefutable
15    /// pattern (wildcard or variable binding).  When only constructor patterns
16    /// are present, exhaustiveness cannot be verified without the full type
17    /// definition, so the check is optimistically accepted.
18    pub fn check_exhaustive(&self, patterns: &[Pattern]) -> Result<(), String> {
19        if patterns.is_empty() {
20            return Err("No patterns provided".to_string());
21        }
22        for pattern in patterns {
23            if matches!(pattern, Pattern::Wild | Pattern::Var(_)) {
24                return Ok(());
25            }
26        }
27        let has_ctor = patterns
28            .iter()
29            .any(|p| matches!(p, Pattern::Ctor(_, _) | Pattern::Lit(_)));
30        if has_ctor {
31            Ok(())
32        } else {
33            Err("Non-exhaustive patterns".to_string())
34        }
35    }
36}