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
//! Setup and per-function analyzer state loaded before emitting a regular function.
use crate::analyzer::*;
use crate::parser::*;
use super::CodeGenerator;
impl<'ast> CodeGenerator<'ast> {
/// Push `#[test]` for `test_*` functions in `*_test.wj` files when no `@test` / `@property_test`.
pub(in crate::codegen::rust) fn push_auto_test_attribute_if_needed(
&self,
func: &FunctionDecl<'ast>,
output: &mut String,
) {
let filename_str = self.current_wj_file.to_string_lossy();
let is_test_file = filename_str.ends_with("_test.wj") || filename_str.contains("_test.wj");
let is_test_function = func.name.starts_with("test_");
let has_test_decorator = func.decorators.iter().any(|d| d.name == "test");
let has_property_test = func.decorators.iter().any(|d| d.name == "property_test");
if is_test_file && is_test_function && !has_test_decorator && !has_property_test {
output.push_str("#[test]\n");
}
}
/// Configure `CodeGenerator` fields from `AnalyzedFunction` before signature/body emission.
pub(in crate::codegen::rust) fn prepare_codegen_environment_for_regular_function(
&mut self,
analyzed: &AnalyzedFunction<'ast>,
) {
let func = &analyzed.decl;
// LOCAL VARIABLE TRACKING: Push new scope for this function
self.local_variable_scopes
.push(std::collections::HashSet::new());
// AUTO-CLONE: Load auto-clone analysis for this function
self.auto_clone_analysis = Some(analyzed.auto_clone_analysis.clone());
self.auto_clone_counter = 0;
// PHASE 2 OPTIMIZATION: Load clone optimizations for this function
// Variables in this set can safely avoid .clone() calls
self.clone_optimizations.clear();
for opt in &analyzed.clone_optimizations {
self.clone_optimizations.insert(opt.variable.clone());
}
self.current_function_params = func.parameters.clone();
// Combine inline bounds (<T: Foo>) and where clause for trait resolution
let mut all_bounds: Vec<(String, Vec<String>)> = func
.type_params
.iter()
.filter(|tp| !tp.bounds.is_empty())
.map(|tp| (tp.name.clone(), tp.bounds.clone()))
.collect();
for (name, bounds) in &func.where_clause {
if let Some(existing) = all_bounds.iter_mut().find(|(n, _)| n == name) {
existing.1.extend(bounds.iter().cloned());
} else {
all_bounds.push((name.clone(), bounds.clone()));
}
}
self.current_function_type_bounds = all_bounds;
// Clear local variable types for new function scope
self.local_var_types.clear();
self.borrowed_iterator_vars.clear();
// Track function return type for string literal conversion
self.current_function_return_type = func.return_type.clone();
// Track method return types for usize inference in comparisons
// When in an impl block, record the return type so expression_produces_usize
// can resolve method calls like animation.frame_count() → usize
if self.in_impl_block {
if let Some(ref ret_type) = func.return_type {
self.method_return_types
.insert(func.name.to_string(), ret_type.clone());
}
// NEW ARCHITECTURE: Register method signature for type-based parameter resolution
// This replaces ALL hard-coded method name heuristics
if let Some(impl_type) = &self.current_struct_name {
// Build parameter types and ownership from ANALYZED function
// Use the actual inferred ownership from the analyzer, not defaults!
let mut param_types = Vec::new();
let mut param_ownership = Vec::new();
// Parameter types for call-site coercion must match analyzer + Rust codegen.
// Phase-2 optimized `string` parameters become `Reference(str)` in
// `AnalyzedFunction::inferred_param_types`, but AST param.type_ stays plain
// `string`. Registering AST types breaks MethodCallAnalyzer's user-signatures path
// (wrong `param_is_str_ref`, missing `&` on String fields, spurious `.to_string()`).
for (idx, param) in func.parameters.iter().enumerate() {
if param.name != "self" {
let p_type = analyzed
.inferred_param_types
.get(idx)
.cloned()
.unwrap_or_else(|| param.type_.clone());
param_types.push(p_type);
// Use ACTUAL analyzed ownership from inferred_ownership
let ownership = analyzed
.inferred_ownership
.get(¶m.name)
.copied()
.unwrap_or(crate::analyzer::OwnershipMode::Borrowed);
param_ownership.push(ownership);
}
}
// Check if method has self receiver
let has_self_receiver = func.parameters.iter().any(|p| p.name == "self");
let signature = crate::codegen::rust::generator::MethodSignature::new(
impl_type.clone(),
func.name.clone(),
param_types,
param_ownership,
func.return_type.clone(),
has_self_receiver,
);
self.register_method_signature(signature);
}
}
// Track function body for data flow analysis
self.current_function_body = func.body.clone();
// FOR-LOOP AUTO-BORROW: Pre-scan function body to find local variables
// that are iterated in for-loops and also used after the loop.
// These need `&` auto-inserted to prevent consuming the collection.
self.precompute_for_loop_borrows(&func.body);
// Track parameters inferred as borrowed/mut-borrowed for codegen decisions
self.inferred_borrowed_params.clear();
self.inferred_mut_borrowed_params.clear();
self.str_ref_optimized_params.clear();
for (param_name, ownership) in &analyzed.inferred_ownership {
match ownership {
crate::analyzer::OwnershipMode::Borrowed => {
self.inferred_borrowed_params.insert(param_name.clone());
}
crate::analyzer::OwnershipMode::MutBorrowed => {
self.inferred_mut_borrowed_params.insert(param_name.clone());
}
_ => {}
}
}
// Track Phase 2 string-optimized parameters (string type params that become &str)
for param_name in &analyzed.str_ref_optimizable_params {
self.str_ref_optimized_params.insert(param_name.clone());
}
// Any parameter the analyzer lowered to `Reference(str)` generates as `&str` in Rust.
// Call-site borrow helpers must treat these as already referenced (map.get(key) not get(&key)).
for (idx, param) in func.parameters.iter().enumerate() {
if let Some(Type::Reference(inner)) = analyzed.inferred_param_types.get(idx) {
if matches!(&**inner, Type::Custom(s) if s == "str") {
self.str_ref_optimized_params.insert(param.name.clone());
}
}
}
// Track explicit &String/&string params that become &str via type_to_rust
// (Type::Reference(String) → "&str"). These aren't Phase 2 optimized but still
// need .to_string() conversions in the body (e.g., Some(s) → Some(s.to_string())).
for param in &func.parameters {
if matches!(¶m.type_, Type::Reference(inner)
if matches!(&**inner, Type::String)
|| matches!(&**inner, Type::Custom(ref n) if n == "string" || n == "String"))
{
self.str_ref_optimized_params.insert(param.name.clone());
}
}
// METHOD PARAM OWNERSHIP: Register this method's parameter ownership modes
// for use at call sites (auto-borrow arguments).
{
let ownership_vec: Vec<(String, crate::analyzer::OwnershipMode)> = analyzed
.inferred_ownership
.iter()
.filter(|(name, _)| name.as_str() != "self")
.map(|(name, mode)| (name.clone(), *mode))
.collect();
if !ownership_vec.is_empty() {
self.method_param_ownership
.insert(func.name.to_string(), ownership_vec);
}
}
// WINDJAMMER FIX: Track usize-typed parameters for auto-cast logic
// DON'T clear here - we need to accumulate variables from let statements during generation!
// Only clear at the very beginning of function generation, before body processing.
// TDD FIX (Bug #3): Moved clear to happen BEFORE pre-passes, so marking during
// statement generation can accumulate variables.
// Clear ONCE at function start (before any analysis)
self.usize_variables.clear();
// When a parameter is declared as `usize`, add it to usize_variables
// so expression_produces_usize() correctly identifies it
for (param_idx, param) in func.parameters.iter().enumerate() {
// Use inferred type if available, otherwise use declared type
let param_type = analyzed
.inferred_param_types
.get(param_idx)
.unwrap_or(¶m.type_);
// Check if this parameter is usize
if matches!(param_type, Type::Custom(name) if name == "usize") {
self.usize_variables.insert(param.name.clone());
}
}
// PHASE 8 OPTIMIZATION: Load SmallVec optimizations for this function
// DISABLED: SmallVec optimizations conflict with return types
// TODO: Re-enable with smarter conversion at return sites
self.smallvec_optimizations.clear();
// for opt in &analyzed.smallvec_optimizations {
// self.smallvec_optimizations
// .insert(opt.variable.clone(), opt.clone());
// self.needs_smallvec_import = true; // Mark that we need the smallvec crate
// }
// PHASE 9 OPTIMIZATION: Load Cow optimizations for this function
self.cow_optimizations.clear();
for opt in &analyzed.cow_optimizations {
self.cow_optimizations.insert(opt.variable.clone());
self.needs_cow_import = true; // Mark that we need Cow from std::borrow
}
// PHASE 3 OPTIMIZATION: Load struct mapping optimizations
// Track which structs can use optimized construction strategies
self.struct_mapping_hints.clear();
for opt in &analyzed.struct_mapping_optimizations {
self.struct_mapping_hints
.insert(opt.target_struct.clone(), opt.strategy.clone());
}
// PHASE 4 OPTIMIZATION: Load string operation optimizations
// Track capacity hints for string operations
self.string_capacity_hints.clear();
// PHASE 5 OPTIMIZATION: Load assignment operation optimizations
// Track which variables can use compound assignment operators
self.assignment_optimizations.clear();
for opt in &analyzed.assignment_optimizations {
self.assignment_optimizations
.insert(opt.variable.clone(), opt.operation.clone());
}
for opt in &analyzed.string_optimizations {
if let Some(capacity) = opt.estimated_capacity {
self.string_capacity_hints.insert(opt.location, capacity);
}
}
// PHASE 6 OPTIMIZATION: Load defer drop optimizations
// Track variables that should have their drops deferred to background thread
self.defer_drop_optimizations = analyzed.defer_drop_optimizations.clone();
}
}