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
use super::super::module_resolver::ModuleResolver;
use super::super::SemanticAnalyzer;
use crate::ast::{Definition, File, FnDef, Statement, StructDef, Type};
use crate::error::CompilerError;
impl<R: ModuleResolver> SemanticAnalyzer<R> {
/// Pass 4: Validate trait implementations
/// Check that structs implement all required fields from their traits,
/// and that impl Trait for Struct blocks provide all required methods.
pub(in crate::semantic) fn validate_trait_implementations(&mut self, file: &File) {
for statement in &file.statements {
if let Statement::Definition(def) = statement {
match &**def {
Definition::Struct(struct_def) => {
self.validate_struct_trait_implementation(struct_def);
}
Definition::Impl(impl_def) => {
if let Some(trait_ident) = &impl_def.trait_name {
self.validate_impl_trait_methods(
&impl_def.functions,
&trait_ident.name,
&impl_def.trait_args,
&impl_def.name.name,
impl_def.span,
);
}
}
Definition::Trait(_)
| Definition::Enum(_)
| Definition::Module(_)
| Definition::Function(_) => {}
}
}
}
}
/// Check that an `impl Trait for Struct` block provides all methods declared in the trait.
///
/// Generic-traits PR: when the impl is `impl Foo<X, Y> for Z`, the
/// `trait_args` slot carries the concrete arg types and the
/// trait's required-method signatures get their generic params
/// substituted before comparison. Without this, `impl Eq<I32>
/// for Foo` would always report a `TraitMethodSignatureMismatch`
/// because the trait declares `fn eq(self, other: T)` and the
/// impl declares `fn eq(self, other: I32)`.
fn validate_impl_trait_methods(
&mut self,
impl_functions: &[FnDef],
trait_name: &str,
trait_args: &[Type],
_struct_name: &str,
impl_span: crate::location::Span,
) {
// Collect all required methods from the trait (including composed traits)
let required_methods = self.collect_all_trait_methods(trait_name);
// Build trait-param to concrete-arg substitution map. Empty
// when the trait isn't generic or no args were supplied.
let trait_generic_params: Vec<String> = self
.symbols
.get_trait(trait_name)
.map(|info| info.generics.iter().map(|g| g.name.name.clone()).collect())
.unwrap_or_default();
let subs: std::collections::HashMap<String, Type> = trait_generic_params
.iter()
.zip(trait_args.iter())
.map(|(name, arg)| (name.clone(), arg.clone()))
.collect();
for (method_name, required_params, required_return) in required_methods {
let required_params: Vec<crate::ast::FnParam> = required_params
.into_iter()
.map(|mut p| {
if let Some(t) = &mut p.ty {
Self::substitute_type_params(t, &subs);
}
p
})
.collect();
let required_return = required_return.map(|mut t| {
Self::substitute_type_params(&mut t, &subs);
t
});
// Find this method in the impl block
match impl_functions.iter().find(|f| f.name.name == method_name) {
None => {
self.errors.push(CompilerError::MissingTraitMethod {
method: method_name.clone(),
trait_name: trait_name.to_string(),
span: impl_span,
});
}
Some(impl_fn) => {
// Check: param count (excluding self), conventions, and return type
let required_non_self: Vec<_> = required_params
.iter()
.filter(|p| p.name.name != "self")
.collect();
let impl_non_self: Vec<_> = impl_fn
.params
.iter()
.filter(|p| p.name.name != "self")
.collect();
let param_count_mismatch = impl_non_self.len() != required_non_self.len();
let convention_mismatch = !param_count_mismatch
&& required_non_self
.iter()
.zip(impl_non_self.iter())
.any(|(req, imp)| req.convention != imp.convention);
// also compare parameter *types*.
// Previously only arity and conventions were checked, so
// an impl could return `fn foo(x: Int)` for a trait
// method declared `fn foo(x: String)` without error.
let param_type_mismatch = !param_count_mismatch
&& required_non_self
.iter()
.zip(impl_non_self.iter())
.any(|(req, imp)| match (&req.ty, &imp.ty) {
(Some(req_ty), Some(imp_ty)) => !Self::types_match(req_ty, imp_ty),
(None, None) => false,
_ => true,
});
// Also check self convention if both have self
let self_convention_mismatch = {
let req_self = required_params.iter().find(|p| p.name.name == "self");
let imp_self = impl_fn.params.iter().find(|p| p.name.name == "self");
match (req_self, imp_self) {
(Some(r), Some(i)) => r.convention != i.convention,
_ => false,
}
};
let return_type_mismatch = match (&required_return, &impl_fn.return_type) {
(Some(req_ret), Some(impl_ret)) => !Self::types_match(req_ret, impl_ret),
(None, None) => false,
_ => true,
};
if param_count_mismatch
|| convention_mismatch
|| self_convention_mismatch
|| return_type_mismatch
|| param_type_mismatch
{
let expected = required_return
.as_ref()
.map_or_else(|| "()".to_string(), Self::type_to_string);
let actual = impl_fn
.return_type
.as_ref()
.map_or_else(|| "()".to_string(), Self::type_to_string);
self.errors
.push(CompilerError::TraitMethodSignatureMismatch {
method: method_name.clone(),
trait_name: trait_name.to_string(),
expected,
actual,
span: impl_fn.span,
});
}
}
}
}
}
/// Collect the methods declared directly in a trait (not inherited ones).
///
/// Each `impl Trait for Struct` provides only the methods declared
/// directly in that trait. Methods inherited from composed traits are
/// covered by separate impl blocks for those base traits; this is a
/// deliberate design choice documented in the language reference.
fn collect_all_trait_methods(
&self,
trait_name: &str,
) -> Vec<(String, Vec<crate::ast::FnParam>, Option<Type>)> {
self.symbols
.traits
.get(trait_name)
.map_or_else(Vec::new, |trait_info| {
trait_info
.methods
.iter()
.map(|m| (m.name.name.clone(), m.params.clone(), m.return_type.clone()))
.collect()
})
}
/// Validate that a struct implements all required fields from its traits
pub(in crate::semantic) fn validate_struct_trait_implementation(
&mut self,
struct_def: &StructDef,
) {
// For each implemented trait, check required fields via impl blocks
// (trait field validation is handled through impl Trait for Struct)
// Walk through trait_impls for this struct
let struct_name = struct_def.name.name.clone();
let trait_impls: Vec<String> = self
.symbols
.trait_impls
.get(&struct_name)
.cloned()
.unwrap_or_default()
.into_iter()
.map(|t| t.trait_name)
.collect();
for trait_name in &trait_impls {
// Get all required fields from this trait (including composed traits)
let required_fields = self.symbols.get_all_trait_fields(trait_name);
// Check each required field
for (field_name, required_type) in required_fields {
// Look for the field in the struct
match struct_def.fields.iter().find(|f| f.name.name == field_name) {
Some(struct_field) => {
// Field exists, check type matches
if !Self::types_match(&struct_field.ty, &required_type) {
self.errors.push(CompilerError::TraitFieldTypeMismatch {
field: field_name.clone(),
trait_name: trait_name.clone(),
expected: Self::type_to_string(&required_type),
actual: Self::type_to_string(&struct_field.ty),
span: struct_field.span,
});
}
}
None => {
// Field is missing
self.errors.push(CompilerError::MissingTraitField {
field: field_name.clone(),
trait_name: trait_name.clone(),
span: struct_def.span,
});
}
}
}
}
}
}