1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//! Match exhaustiveness, enum-instantiation field checks, and the
//! optional-condition auto-binding helper used by `if`.
use super::super::module_resolver::ModuleResolver;
use super::super::sem_type::SemType;
use super::super::SemanticAnalyzer;
use crate::ast::{Definition, Expr, File, Statement};
use crate::error::CompilerError;
use crate::location::Span;
use std::collections::HashSet;
impl<R: ModuleResolver> SemanticAnalyzer<R> {
/// If `condition` is a reference or field access whose type is optional
/// (`T?`), install a local binding whose name matches the trailing
/// segment with the unwrapped type `T` and return the binding name
/// (plus the prior entry, if any, so the caller can restore it after
/// the then-branch). Otherwise returns (None, None).
pub(super) fn bind_optional_auto_binding(
&mut self,
condition: &Expr,
file: &File,
) -> (Option<String>, Option<(String, bool)>) {
let cond_sem = self.infer_type_sem(condition, file);
let SemType::Optional(inner) = &cond_sem else {
return (None, None);
};
let unwrapped_owned = inner.display();
let unwrapped = unwrapped_owned.as_str();
let name_opt = match condition {
Expr::Reference { path, .. } => path.last().map(|id| id.name.clone()),
Expr::FieldAccess { field, .. } => Some(field.name.clone()),
Expr::Literal { .. }
| Expr::Invocation { .. }
| Expr::EnumInstantiation { .. }
| Expr::InferredEnumInstantiation { .. }
| Expr::Array { .. }
| Expr::Tuple { .. }
| Expr::BinaryOp { .. }
| Expr::UnaryOp { .. }
| Expr::ForExpr { .. }
| Expr::IfExpr { .. }
| Expr::MatchExpr { .. }
| Expr::Group { .. }
| Expr::DictLiteral { .. }
| Expr::DictAccess { .. }
| Expr::ClosureExpr { .. }
| Expr::LetExpr { .. }
| Expr::MethodCall { .. }
| Expr::Block { .. } => None,
};
let Some(name) = name_opt else {
return (None, None);
};
let prev = self.local_let_bindings.get(&name).cloned();
self.local_let_bindings
.insert(name.clone(), (unwrapped.to_string(), false));
(Some(name), prev)
}
/// Validate match expression exhaustiveness
pub(super) fn validate_match(
&mut self,
scrutinee: &Expr,
arms: &[crate::ast::MatchArm],
span: Span,
file: &File,
) {
// Infer scrutinee type - must be an enum
let scrutinee_type = self.infer_type_sem(scrutinee, file).display();
// Skip when type is unknown (field access, method calls — IR lowering handles these)
if scrutinee_type == "Unknown" {
return;
}
// Check if scrutinee is an enum (look it up in symbol table)
if !self.symbols.is_enum(&scrutinee_type) {
self.errors.push(CompilerError::MatchNotEnum {
actual: scrutinee_type,
span,
});
return;
}
// Get enum variants from symbol table
let variants = match self.symbols.get_enum_variants(&scrutinee_type) {
Some(v) => v.clone(),
None => return, // Should not happen if is_enum returned true
};
// Collect all variant names from match arms
let mut covered_variants = HashSet::new();
let mut has_wildcard = false;
for arm in arms {
match &arm.pattern {
crate::ast::Pattern::Variant { name, bindings } => {
// Check for duplicate arms
if !covered_variants.insert(name.name.clone()) {
self.errors.push(CompilerError::DuplicateMatchArm {
variant: name.name.clone(),
span: arm.span,
});
continue;
}
// Validate variant exists and arity matches
self.validate_match_arm(
&scrutinee_type,
&name.name,
bindings.len(),
arm.span,
&variants,
);
}
crate::ast::Pattern::Wildcard => {
// Wildcard covers all remaining variants
has_wildcard = true;
}
}
}
// Check exhaustiveness - all variants must be covered (unless there's a wildcard)
if !has_wildcard {
let missing_variants: Vec<String> = variants
.keys()
.filter(|v| !covered_variants.contains(*v))
.cloned()
.collect();
if !missing_variants.is_empty() {
self.errors.push(CompilerError::NonExhaustiveMatch {
missing: missing_variants.join(", "),
span,
});
}
}
}
/// Validate enum instantiation with named parameters
pub(super) fn validate_enum_instantiation(
&mut self,
enum_name: &crate::ast::Ident,
variant_name: &crate::ast::Ident,
data: &[(crate::ast::Ident, Expr)],
span: Span,
file: &File,
) {
// Check if the enum exists
if !self.symbols.is_enum(&enum_name.name) {
self.errors.push(CompilerError::UndefinedType {
name: enum_name.name.clone(),
span: enum_name.span,
});
return;
}
// Get the enum definition to access variant field information
let variant_fields =
self.get_enum_variant_fields(&enum_name.name, &variant_name.name, file);
match variant_fields {
Some(fields) => {
// Check if variant has no fields but data was provided
if fields.is_empty() && !data.is_empty() {
self.errors.push(CompilerError::EnumVariantWithoutData {
variant: variant_name.name.clone(),
enum_name: enum_name.name.clone(),
span,
});
return;
}
// Check if variant has fields but no data was provided
if !fields.is_empty() && data.is_empty() {
self.errors.push(CompilerError::EnumVariantRequiresData {
variant: variant_name.name.clone(),
enum_name: enum_name.name.clone(),
span,
});
return;
}
// Check that all required fields are provided
let provided_fields: HashSet<&str> =
data.iter().map(|(name, _)| name.name.as_str()).collect();
let required_fields: HashSet<&str> =
fields.iter().map(|f| f.name.name.as_str()).collect();
// Check for missing fields
for field in &required_fields {
if !provided_fields.contains(field) {
self.errors.push(CompilerError::MissingField {
field: field.to_string(),
type_name: format!("{}.{}", enum_name.name, variant_name.name),
span,
});
}
}
// Check for unknown fields
for (provided_field, _) in data {
if !required_fields.contains(provided_field.name.as_str()) {
self.errors.push(CompilerError::UnknownField {
field: provided_field.name.clone(),
type_name: format!("{}.{}", enum_name.name, variant_name.name),
span: provided_field.span,
});
}
}
}
None => {
// Variant doesn't exist
self.errors.push(CompilerError::UnknownEnumVariant {
variant: variant_name.name.clone(),
enum_name: enum_name.name.clone(),
span: variant_name.span,
});
}
}
}
/// Get the field definitions for a specific enum variant
/// Returns None if the enum or variant doesn't exist
pub(super) fn get_enum_variant_fields(
&self,
enum_name: &str,
variant_name: &str,
current_file: &File,
) -> Option<Vec<crate::ast::FieldDef>> {
// First, search in the current file
for statement in ¤t_file.statements {
if let Statement::Definition(def) = statement {
if let Definition::Enum(enum_def) = &**def {
if enum_def.name.name == enum_name {
// Find the variant
for variant in &enum_def.variants {
if variant.name.name == variant_name {
return Some(variant.fields.clone());
}
}
return None; // Variant not found
}
}
}
}
// If not found in current file, search through module cache
for (file, _) in self.module_cache.values() {
for statement in &file.statements {
if let Statement::Definition(def) = statement {
if let Definition::Enum(enum_def) = &**def {
if enum_def.name.name == enum_name {
// Find the variant
for variant in &enum_def.variants {
if variant.name.name == variant_name {
return Some(variant.fields.clone());
}
}
return None; // Variant not found
}
}
}
}
}
None // Enum not found
}
/// Validate a single match arm
pub(super) fn validate_match_arm(
&mut self,
enum_name: &str,
variant_name: &str,
binding_count: usize,
span: Span,
variants: &std::collections::HashMap<String, (usize, Span)>,
) {
// Check if variant exists
match variants.get(variant_name) {
Some((expected_arity, _)) => {
// Check arity matches
if *expected_arity != binding_count {
self.errors.push(CompilerError::VariantArityMismatch {
variant: variant_name.to_string(),
expected: *expected_arity,
actual: binding_count,
span,
});
}
}
None => {
// Variant doesn't exist in enum
self.errors.push(CompilerError::UnknownEnumVariant {
variant: variant_name.to_string(),
enum_name: enum_name.to_string(),
span,
});
}
}
}
}