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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
//! Formal parameter list emission (excluding implicit `self` receiver).
use std::collections::HashSet;
use crate::analyzer::*;
use crate::codegen::rust::type_analysis;
use crate::parser::*;
use super::CodeGenerator;
impl<'ast> CodeGenerator<'ast> {
pub(in crate::codegen::rust) fn compute_unused_formal_parameter_names(
&self,
func: &FunctionDecl<'ast>,
) -> HashSet<String> {
// TDD FIX: Pre-compute which parameters are actually used in the function body.
// Unused parameters get prefixed with `_` to suppress "unused variable" warnings.
// THE WINDJAMMER WAY: The compiler handles this automatically — developers don't
// need to manually prefix unused parameters with `_`.
let body_refs: Vec<&Statement> = func.body.to_vec();
func.parameters
.iter()
.filter(|p| p.name != "self")
.filter(|p| !Self::variable_used_in_statements(&body_refs, &p.name))
.map(|p| p.name.clone())
.collect()
}
pub(in crate::codegen::rust) fn refresh_unused_let_bindings_for_function_body(
&mut self,
body: &[&'ast Statement<'ast>],
) {
// TDD FIX: Pre-compute unused let bindings and for-loop variables.
// Like unused params, these get prefixed with `_` in the generated Rust.
self.unused_let_bindings.clear();
Self::find_unused_bindings(body, &mut self.unused_let_bindings);
}
pub(in crate::codegen::rust) fn collect_additional_formal_parameter_strings(
&mut self,
analyzed: &AnalyzedFunction<'ast>,
func: &FunctionDecl<'ast>,
needs_lifetime: bool,
unused_params: &HashSet<String>,
) -> Vec<String> {
func.parameters
.iter()
.enumerate()
.map(|(param_idx, param)| {
// SMART STRING INFERENCE: Use the inferred type from analyzer (string → &str vs String)
let inferred_type = analyzed
.inferred_param_types
.get(param_idx)
.unwrap_or(¶m.type_);
// PHASE 9 OPTIMIZATION: Check if this parameter should use Cow<'_, T>
if self.cow_optimizations.contains(¶m.name) {
let base_type = self.type_to_rust(inferred_type);
// For String types, use Cow<'_, str>
let cow_type = if base_type == "String" {
"Cow<'_, str>".to_string()
} else {
format!("Cow<'_, {}>", base_type)
};
return format!("{}: {}", param.name, cow_type);
}
// Handle explicit ownership hints (self, &self, &mut self)
let type_str = match ¶m.ownership {
OwnershipHint::Owned => {
if param.name == "self" {
let body_modifies = self.function_modifies_self(&analyzed.decl);
let consumes_self = super::self_analysis::function_consumes_self(&analyzed.decl);
let eff_ownership =
self.get_effective_self_ownership(&func.name, analyzed);
let self_str = if let Some(ownership_mode) = eff_ownership {
match ownership_mode {
OwnershipMode::Borrowed | OwnershipMode::MutBorrowed
if !self.in_trait_impl
&& (self.method_returns_impl_struct(func) || consumes_self) =>
{
if body_modifies { "mut self" } else { "self" }
}
OwnershipMode::MutBorrowed => "&mut self",
OwnershipMode::Borrowed => {
if !self.in_trait_impl && body_modifies {
"&mut self"
} else {
"&self"
}
}
OwnershipMode::Owned => {
if self.in_trait_impl {
"self"
} else {
self.owned_self_receiver(&analyzed.decl)
}
}
}
} else {
if self.in_trait_impl {
"self"
} else {
self.owned_self_receiver(&analyzed.decl)
}
};
// Sync borrowed-params sets with actual generated receiver.
match self_str {
"&self" => {
self.inferred_borrowed_params.insert("self".to_string());
self.inferred_mut_borrowed_params.remove("self");
}
"&mut self" => {
self.inferred_mut_borrowed_params.insert("self".to_string());
self.inferred_borrowed_params.remove("self");
}
_ => {
self.inferred_borrowed_params.remove("self");
self.inferred_mut_borrowed_params.remove("self");
}
}
self.record_self_receiver_upgrade(
&func.name,
eff_ownership,
self_str,
);
return self_str.to_string();
}
// Owned parameters are always mutable in Windjammer
return format!("mut {}: {}", param.name, self.type_to_rust(inferred_type));
}
OwnershipHint::Ref => {
if param.name == "self" {
let body_modifies = self.function_modifies_self(&analyzed.decl);
if let Some(ownership_mode) =
self.get_effective_self_ownership(&func.name, analyzed)
{
match ownership_mode {
OwnershipMode::MutBorrowed => return "&mut self".to_string(),
OwnershipMode::Borrowed => {
if !self.in_trait_impl && body_modifies {
return "&mut self".to_string();
}
return "&self".to_string();
}
OwnershipMode::Owned => {
return "self".to_string();
}
}
}
if !self.in_trait_impl && body_modifies {
return "&mut self".to_string();
}
return "&self".to_string();
}
// Don't add & if the type is already a Reference
if matches!(
inferred_type,
Type::Reference(_) | Type::MutableReference(_)
) {
self.type_to_rust(inferred_type)
} else {
// TDD FIX: Borrowed → &T (including &String for strings)
// Correctness > idioms: &String works with Vec<String> methods
format!("&{}", self.type_to_rust(inferred_type))
}
}
OwnershipHint::Mut => {
if param.name == "self" {
let body_modifies = self.function_modifies_self(&analyzed.decl);
if let Some(ownership_mode) =
self.get_effective_self_ownership(&func.name, analyzed)
{
return match ownership_mode {
OwnershipMode::Borrowed => {
if !self.in_trait_impl && body_modifies {
"&mut self".to_string()
} else {
"&self".to_string()
}
}
OwnershipMode::MutBorrowed => "&mut self".to_string(),
OwnershipMode::Owned => {
if self.in_trait_impl {
"self".to_string()
} else {
self.owned_self_receiver(&analyzed.decl).to_string()
}
}
};
}
return "&mut self".to_string();
}
// Don't add &mut if the type is already a MutableReference
if matches!(inferred_type, Type::MutableReference(_)) {
self.type_to_rust(inferred_type)
} else {
format!("&mut {}", self.type_to_rust(inferred_type))
}
}
OwnershipHint::Inferred => {
if param.name == "self" {
let body_modifies = self.function_modifies_self(&analyzed.decl);
let returns_self = self.method_returns_impl_struct(&analyzed.decl);
let consumes_self = super::self_analysis::function_consumes_self(&analyzed.decl);
let self_str = if let Some(ownership_mode) =
self.get_effective_self_ownership(&func.name, analyzed)
{
match ownership_mode {
OwnershipMode::Borrowed | OwnershipMode::MutBorrowed
if !self.in_trait_impl && (returns_self || consumes_self) =>
{
if body_modifies { "mut self" } else { "self" }
}
OwnershipMode::MutBorrowed => "&mut self",
OwnershipMode::Borrowed => {
if !self.in_trait_impl && body_modifies {
"&mut self"
} else {
"&self"
}
}
OwnershipMode::Owned => {
if self.in_trait_impl {
"self"
} else {
self.owned_self_receiver(&analyzed.decl)
}
}
}
} else if body_modifies && returns_self {
"mut self"
} else if consumes_self {
"self"
} else if body_modifies {
"&mut self"
} else if returns_self {
"self"
} else {
"&self"
};
// Sync borrowed-params sets with actual generated receiver.
match self_str {
"&self" => {
self.inferred_borrowed_params.insert("self".to_string());
self.inferred_mut_borrowed_params.remove("self");
}
"&mut self" => {
self.inferred_mut_borrowed_params.insert("self".to_string());
self.inferred_borrowed_params.remove("self");
}
_ => {
self.inferred_borrowed_params.remove("self");
self.inferred_mut_borrowed_params.remove("self");
}
}
let eff = self.get_effective_self_ownership(&func.name, analyzed);
self.record_self_receiver_upgrade(
&func.name,
eff,
self_str,
);
return self_str.to_string();
}
// Check if type already has ownership baked in (like &str from string inference)
if matches!(
inferred_type,
Type::Reference(_) | Type::MutableReference(_)
) {
// Already has & or &mut - just convert
self.type_to_rust(inferred_type)
} else {
// Apply ownership mode from analyzer
// TDD FIX: Default to Owned, not Borrowed
// THE WINDJAMMER WAY: Parameters are owned by default unless analyzer
// detects they should be borrowed (e.g., only read, passed to & functions)
let ownership_mode = analyzed
.inferred_ownership
.get(¶m.name)
.unwrap_or(&OwnershipMode::Owned);
if std::env::var("WJ_DEBUG_CODEGEN").is_ok() {
eprintln!(
" [CODEGEN] param={} fn={} ownership={:?} all_keys={:?}",
param.name, func.name, ownership_mode,
analyzed.inferred_ownership.keys().collect::<Vec<_>>()
);
}
// E0053 FIX: Trait impl parameters MUST match the trait
// definition's parameter types exactly. Look up the trait's
// method signature and use its ownership for each parameter.
let ownership_mode = if self.in_trait_impl {
let trait_param_ownership = self
.current_trait_impl_name
.as_ref()
.and_then(|trait_name| {
let methods = self
.analyzed_trait_methods
.get(trait_name.as_str())
.or_else(|| {
trait_name
.rfind("::")
.map(|i| &trait_name[i + 2..])
.and_then(|key| {
self.analyzed_trait_methods.get(key)
})
});
methods.and_then(|m| {
m.get(func.name.as_str()).and_then(|trait_fn| {
// Find matching param by name in the trait
// method's inferred ownership
trait_fn.inferred_ownership.get(¶m.name)
})
})
});
trait_param_ownership.unwrap_or(&OwnershipMode::Owned)
} else {
ownership_mode
};
match ownership_mode {
OwnershipMode::Owned => self.type_to_rust(inferred_type),
OwnershipMode::Borrowed => {
if type_analysis::is_copy_type(inferred_type) {
// Copy types pass by value even when borrowed
self.type_to_rust(inferred_type)
} else {
// PHASE 2: Check if this string parameter can use &str optimization
let is_string = matches!(inferred_type, Type::String)
|| matches!(inferred_type, Type::Custom(ref name) if name == "string");
if is_string
&& analyzed.str_ref_optimizable_params.contains(¶m.name)
{
"&str".to_string()
} else {
format!("&{}", self.type_to_rust(inferred_type))
}
}
}
OwnershipMode::MutBorrowed => {
format!("&mut {}", self.type_to_rust(inferred_type))
}
}
}
}
};
// WINDJAMMER LIFETIME INFERENCE: Add 'a lifetime to reference parameters
// when the function needs explicit lifetime annotations.
let type_str = if needs_lifetime && param.name != "self" {
if let Some(stripped) = type_str.strip_prefix("&mut ") {
format!("&'a mut {}", stripped)
} else if let Some(stripped) = type_str.strip_prefix("&") {
format!("&'a {}", stripped)
} else {
type_str
}
} else {
type_str
};
// TDD FIX: Auto-infer `mut` for owned parameters
// THE WINDJAMMER WAY: Users don't track mutability - the compiler does.
// If a parameter has mutating method calls or field mutations,
// the binding needs `mut` even if not explicitly written.
let auto_needs_mut = param.name != "self"
&& !param.is_mutable
&& matches!(type_str.as_str(), s if !s.starts_with("&"))
&& self.variable_needs_mut(¶m.name);
let mut_prefix = if param.is_mutable || auto_needs_mut {
"mut "
} else {
""
};
// TDD FIX: Prefix unused parameter names with `_` to suppress warnings
let display_name = if unused_params.contains(¶m.name) {
format!("_{}", param.name)
} else {
param.name.clone()
};
// Check if this is a pattern parameter
if let Some(pattern) = ¶m.pattern {
// Generate pattern: type syntax
format!(
"{}{}: {}",
mut_prefix,
self.generate_pattern(pattern),
type_str
)
} else {
// Simple name: type syntax
format!("{}{}: {}", mut_prefix, display_name, type_str)
}
})
.collect()
}
}