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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/// String Parameter Optimization Analyzer
///
/// Phase 2 of the String Parameter Optimization plan:
/// Analyzes function bodies to determine if borrowed string parameters can safely
/// use `&str` instead of `&String`.
///
/// PROPER APPROACH (No String Matching!):
/// Instead of hard-coding method names, we analyze method signatures:
/// 1. Look up the method in the type registry
/// 2. Check if any parameter types are `&String` or `&T where T: Borrow<String>`
/// 3. If a parameter is passed to such a method → use &String (correctness)
/// 4. Otherwise → use &str (performance)
///
/// This is:
/// - Extensible: Works with custom methods automatically
/// - Maintainable: No hard-coded string lists
/// - Correct: Based on actual type system, not heuristics
///
/// PHASE 2 MVP: Conservative implementation - returns empty set (uses &String everywhere)
/// Full implementation requires:
/// 1. Method signature lookup in type registry
/// 2. AST traversal to find method calls
/// 3. Parameter flow analysis (which params are passed where)
use crate::analyzer::Analyzer;
use crate::parser::{Expression, FunctionDecl, Statement, Type};
use std::collections::HashSet;
impl<'ast> Analyzer<'ast> {
/// Analyze all string parameters in a function and return the set that can use &str
///
/// PHASE 3: Manual Override Support
/// - Check for @str_ref decorator → force &str (developer promises it's safe)
/// - Check for @string_ref decorator → force &String (developer wants conservative)
///
/// THE PROPER WAY (Phase 2 full):
/// - Traverse function body AST
/// - For each method call, look up its signature in type registry
/// - Check if any method parameter expects &String (not &str)
/// - If parameter flows to such a method → must use &String
/// - Otherwise → can safely use &str
///
/// PHASE 2 FULL: Implement type-based analysis using signature registry
/// Analyzes function body to determine which string parameters can safely use &str
pub fn analyze_str_ref_optimizable_params(
&self,
func: &FunctionDecl,
registry: &super::SignatureRegistry,
) -> HashSet<String> {
// Extern functions use FFI types - never optimize their parameters
if func.is_extern {
return HashSet::new();
}
let mut optimizable = HashSet::new();
for param in &func.parameters {
// Only consider string parameters
let is_string = matches!(param.type_, Type::String)
|| matches!(param.type_, Type::Custom(ref name) if name == "string");
if !is_string {
continue;
}
// Check for explicit decorators
let has_str_ref = param.decorators.iter().any(|d| d.name == "str_ref");
let has_string_ref = param.decorators.iter().any(|d| d.name == "string_ref");
if has_str_ref {
// PHASE 3: Developer explicitly requested &str
// Trust the developer - they promise it's safe
optimizable.insert(param.name.clone());
} else if has_string_ref {
// PHASE 3: Developer explicitly requested &String
// Don't optimize this parameter
continue;
} else {
// No decorator - use automatic analysis
// If the function returns String and this param is returned directly,
// it needs to be owned String (not &str)
let returns_string = func
.return_type
.as_ref()
.map(|rt| {
matches!(rt, Type::String)
|| matches!(rt, Type::Custom(ref n) if n == "string")
})
.unwrap_or(false);
let param_is_returned =
returns_string && self.param_is_returned_directly(¶m.name, &func.body);
if param_is_returned {
continue;
}
let needs_string_ref =
self.param_needs_string_ref(¶m.name, &func.body, registry);
if !needs_string_ref {
optimizable.insert(param.name.clone());
}
}
}
optimizable
}
/// Check if a string parameter is returned directly (explicit return or implicit last expr)
pub(crate) fn param_is_returned_directly(&self, param_name: &str, body: &[&Statement]) -> bool {
for stmt in body {
match stmt {
Statement::Return {
value: Some(expr), ..
} => {
if let Expression::Identifier { name, .. } = &**expr {
if name == param_name {
return true;
}
}
}
Statement::Expression { expr, .. } => {
if let Expression::Identifier { name, .. } = &**expr {
if name == param_name {
return true;
}
}
}
_ => {}
}
}
false
}
/// Check if a parameter needs &String (passed to method that requires it)
/// Recursively traverses the function body to find all usages
pub(crate) fn param_needs_string_ref(
&self,
param_name: &str,
body: &[&Statement],
registry: &super::SignatureRegistry,
) -> bool {
for stmt in body {
if self.statement_uses_param_in_string_ref_context(param_name, stmt, registry) {
return true;
}
}
false
}
/// Check if a statement uses the parameter in a context requiring &String or String (owned)
pub(crate) fn statement_uses_param_in_string_ref_context(
&self,
param_name: &str,
stmt: &Statement,
registry: &super::SignatureRegistry,
) -> bool {
match stmt {
Statement::Expression { expr, .. } => {
self.expr_uses_param_in_string_ref_context(param_name, expr, registry)
}
Statement::Let { value, .. } => {
self.expr_uses_param_in_string_ref_context(param_name, value, registry)
}
// TDD FIX: Check for direct assignment to String fields
// If `self.name = name` where self.name is String, parameter must be String (owned), not &str
Statement::Assignment { target, value, .. } => {
// Check if the value is our parameter (or & to our parameter)
let value_is_param = self.expr_is_param_or_ref_to_param(param_name, value);
if value_is_param {
// Check if target is a String field
// For simplicity, if assigning parameter directly to ANY field, be conservative
// and require &String (the codegen will handle owned String if needed)
// This prevents &str → String assignment errors
if matches!(target, Expression::FieldAccess { .. }) {
return true; // Assignment to field requires owned/&String, not &str
}
}
// Recursively check both target and value
self.expr_uses_param_in_string_ref_context(param_name, target, registry)
|| self.expr_uses_param_in_string_ref_context(param_name, value, registry)
}
Statement::If {
condition,
then_block,
else_block,
..
} => {
self.expr_uses_param_in_string_ref_context(param_name, condition, registry)
|| self.block_needs_string_ref(param_name, then_block, registry)
|| else_block
.as_ref()
.map(|b| self.block_needs_string_ref(param_name, b, registry))
.unwrap_or(false)
}
Statement::While {
condition, body, ..
} => {
self.expr_uses_param_in_string_ref_context(param_name, condition, registry)
|| self.block_needs_string_ref(param_name, body, registry)
}
Statement::For { body, .. } => self.block_needs_string_ref(param_name, body, registry),
Statement::Return {
value: Some(expr), ..
} => self.expr_uses_param_in_string_ref_context(param_name, expr, registry),
Statement::Match { value, arms, .. } => {
self.expr_uses_param_in_string_ref_context(param_name, value, registry)
|| arms.iter().any(|arm| {
self.expr_uses_param_in_string_ref_context(param_name, arm.body, registry)
})
}
_ => false,
}
}
/// Check if a block needs &String (block is Vec<&Statement>)
pub(crate) fn block_needs_string_ref(
&self,
param_name: &str,
block: &Vec<&Statement>,
registry: &super::SignatureRegistry,
) -> bool {
for stmt in block {
if self.statement_uses_param_in_string_ref_context(param_name, stmt, registry) {
return true;
}
}
false
}
/// Check if an expression uses the parameter in a context requiring &String
pub(crate) fn expr_uses_param_in_string_ref_context(
&self,
param_name: &str,
expr: &Expression,
registry: &super::SignatureRegistry,
) -> bool {
match expr {
// Check method calls: param.method() or something.method(¶m)
Expression::MethodCall {
object,
method,
arguments,
..
} => {
// First check if any argument is our parameter (like items.contains(&id))
for (idx, arg) in arguments.iter().enumerate() {
let arg_expr = &arg.1;
// Check if this argument is ¶m or param
if self.expr_is_param_or_ref_to_param(param_name, arg_expr) {
// CONSERVATIVE HEURISTIC: If parameter is passed to a method on self (e.g., self.log(message)),
// and we don't have signature information, conservatively assume owned String is needed.
// This handles transitive dependencies like info(message) → log(message) → push(message).
// Known read-only methods are excluded from this heuristic.
let is_self_method = match &**object {
Expression::Identifier { name, .. } => name == "self",
// Also handle self.field (e.g., self.data.insert(...))
Expression::FieldAccess { object: inner, .. } => {
matches!(&**inner, Expression::Identifier { name, .. } if name == "self")
}
_ => false,
};
// Self-method calls (self.log(message), self.data.insert(key, val)):
// If ownership analyzer determined the param as Borrowed, then the
// downstream method that receives it will also have its string param
// analyzed. Known problematic stdlib methods (contains, push, insert)
// are handled by special cases below.
// No extra conservative block needed for self methods.
let _ = is_self_method;
if super::stdlib_method_traits::is_slice_search_method(method) && idx == 0 {
return true;
}
if super::stdlib_method_traits::is_storage_method(method) && idx == 0 {
return true;
}
if method == "insert" && idx == 1 {
return true;
}
// HashMap/BTreeMap key methods take `&K`; codegen passes `key` directly
// when the parameter already generates as `&str`/`&String` — no &String
// requirement here (that caused circular &&str bugs).
// Check if this method expects &String or String (owned) for this parameter position.
// Static/type calls (`Quest::new`) must use qualified keys — bare `new`
// hits unrelated constructors in the registry.
let method_sig = if let Expression::Identifier { name, .. } = &**object {
if name.starts_with(|c: char| c.is_ascii_uppercase()) {
registry.get_signature(&format!("{}::{}", name, method))
} else {
None
}
} else {
None
}
.or_else(|| registry.lookup_method(method));
if let Some(sig) = method_sig {
if let Some(param_type) = sig.param_type_for_arg(idx) {
if self.type_is_string_ref_not_str(param_type) {
return true;
}
if self.is_windjammer_string_param_type(param_type) {
if self.callee_string_param_uses_rust_string_ref(
sig, idx, param_type, method,
) {
return true;
}
continue;
}
if self.type_is_owned_string(param_type) {
return true;
}
}
}
}
// Recursively check argument expressions
if self.expr_uses_param_in_string_ref_context(param_name, arg_expr, registry) {
return true;
}
}
// param.method() — receiver (`self`) requirements are handled by
// ownership inference, not string-ref analysis. Do not scan other
// parameters on the callee (e.g. `inv.has(id)` must not mark `inv`
// as &String because `id` needs &String).
if let Expression::Identifier { name, .. } = &**object {
let _ = (name, method);
}
false
}
// Check function calls: function(¶m)
Expression::Call {
function,
arguments,
..
} => {
// Type::method(...) constructor calls (parsed as Call(FieldAccess)).
if let Expression::FieldAccess { object, field, .. } = &**function {
let is_constructor = field.starts_with("new") || field.starts_with("from_");
if is_constructor {
let qualified = if let Expression::Identifier { name, .. } = &**object {
Some(format!("{}::{}", name, field))
} else {
None
};
for (i, arg) in arguments.iter().enumerate() {
let arg_expr = &arg.1;
if self.expr_is_param_or_ref_to_param(param_name, arg_expr) {
if let Some(ref qname) = qualified {
if let Some(sig) = registry.get_signature(qname) {
if let Some(param_type) = sig.param_type_for_arg(i) {
if self.type_is_string_ref_not_str(param_type) {
return true;
}
if self.is_windjammer_string_param_type(param_type) {
if self.callee_string_param_uses_rust_string_ref(
sig, i, param_type, field,
) {
return true;
}
continue;
}
if self.type_is_owned_string(param_type) {
return true;
}
continue;
}
}
}
return true;
}
}
}
}
if let Expression::Identifier { name: fn_name, .. } = &**function {
// Enum variants (Some, None, Ok, Err, MyEnum::Variant) consume
// their arguments. Detect enum variants vs module-qualified fn
// calls: enum variants have an uppercase final component.
let is_enum_variant =
matches!(fn_name.as_str(), "Some" | "None" | "Ok" | "Err")
|| (fn_name.contains("::") && {
let last = fn_name.rsplit("::").next().unwrap_or("");
last.starts_with(|c: char| c.is_uppercase())
});
let is_constructor = fn_name.contains("::") && {
let last = fn_name.rsplit("::").next().unwrap_or("");
last.starts_with("new") || last.starts_with("from_")
};
if is_enum_variant {
for arg in arguments.iter() {
let arg_expr = &arg.1;
if self.expr_is_param_or_ref_to_param(param_name, arg_expr) {
return true;
}
}
}
if is_constructor {
for (i, arg) in arguments.iter().enumerate() {
let arg_expr = &arg.1;
if self.expr_is_param_or_ref_to_param(param_name, arg_expr) {
if let Some(sig) = registry.get_signature(fn_name) {
if let Some(param_type) = sig.param_type_for_arg(i) {
if self.type_is_string_ref_not_str(param_type) {
return true;
}
if self.is_windjammer_string_param_type(param_type) {
if self.callee_string_param_uses_rust_string_ref(
sig, i, param_type, fn_name,
) {
return true;
}
continue;
}
if self.type_is_owned_string(param_type) {
return true;
}
continue;
}
}
return true;
}
}
}
if let Some(sig) = registry.get_signature(fn_name) {
// Extern fns: codegen wraps string args in
// string_to_ffi(.to_string()), so &str is always safe
if !sig.is_extern {
for (i, arg) in arguments.iter().enumerate() {
let arg_expr = &arg.1;
if self.expr_is_param_or_ref_to_param(param_name, arg_expr) {
if let Some(param_type) = sig.param_type_for_arg(i) {
if self.type_is_string_ref_not_str(param_type) {
return true;
}
if self.is_windjammer_string_param_type(param_type) {
if self.callee_string_param_uses_rust_string_ref(
sig, i, param_type, fn_name,
) {
return true;
}
continue;
}
if self.type_is_owned_string(param_type) {
return true;
}
}
}
if self.expr_uses_param_in_string_ref_context(
param_name, arg_expr, registry,
) {
return true;
}
}
}
} else {
// Signature not in registry (extern fns, other Windjammer fns).
// Safe to use &str because:
// - Extern fns: codegen wraps string args in string_to_ffi(.to_string()),
// which works with both &str and &String
// - Other Windjammer fns: their borrowed string params will also be &str
// - Known problematic stdlib methods (contains, push, insert) are
// handled by special cases above
// Still recursively check sub-expressions for other patterns.
for arg in arguments.iter() {
let arg_expr = &arg.1;
if self.expr_uses_param_in_string_ref_context(
param_name, arg_expr, registry,
) {
return true;
}
}
}
}
false
}
// Check binary operations (comparisons, string concatenation, etc.)
Expression::Binary {
left, right, op, ..
} => {
// SPECIAL CASE: String concatenation `a + b` consumes the LHS (a must be String, not &str)
// If parameter is the LHS of +, it must be String (owned)
if matches!(op, crate::parser::BinaryOp::Add) {
if let Expression::Identifier { name, .. } = &**left {
if name == param_name {
return true; // LHS of + must be String (owned), not &str
}
}
}
// Recursively check both sides
self.expr_uses_param_in_string_ref_context(param_name, left, registry)
|| self.expr_uses_param_in_string_ref_context(param_name, right, registry)
}
// Check unary operations
Expression::Unary { operand, .. } => {
self.expr_uses_param_in_string_ref_context(param_name, operand, registry)
}
// Check field access
Expression::FieldAccess { object, .. } => {
self.expr_uses_param_in_string_ref_context(param_name, object, registry)
}
// Check blocks
Expression::Block { statements, .. } => {
self.param_needs_string_ref(param_name, statements, registry)
}
// Struct literal: `User { name }` into a `string` field coerces at codegen — still &str at API.
Expression::StructLiteral { name, fields, .. } => {
for (field_name, field_value) in fields {
if self.expr_is_param_or_ref_to_param(param_name, field_value) {
if self.struct_field_is_text_type(name, field_name) {
continue;
}
return true;
}
// Recursively check the field value
if self.expr_uses_param_in_string_ref_context(param_name, field_value, registry)
{
return true;
}
}
false
}
// Check tuple expressions: (name, value) where tuple might be stored
// This handles cases like relationships.push((npc, delta)) where npc must be owned String
Expression::Tuple { elements, .. } => {
for element in elements {
// Check if any element is our parameter
if self.expr_is_param_or_ref_to_param(param_name, element) {
// Conservative: If parameter is used in tuple, assume String (owned) is needed
// Tuples used in push/assign contexts require owned values
return true;
}
// Recursively check each element
if self.expr_uses_param_in_string_ref_context(param_name, element, registry) {
return true;
}
}
false
}
// Identifiers by themselves don't require &String (only when passed to methods)
Expression::Identifier { .. } => false,
// Other expressions
_ => false,
}
}
/// Check if an expression is the parameter or ¶meter
pub(crate) fn expr_is_param_or_ref_to_param(
&self,
param_name: &str,
expr: &Expression,
) -> bool {
match expr {
Expression::Identifier { name, .. } => name == param_name,
Expression::Unary {
op: crate::parser::UnaryOp::Ref,
operand,
..
} => {
if let Expression::Identifier { name, .. } = &**operand {
name == param_name
} else {
false
}
}
// TDD FIX: Detect param.clone() and param.method() patterns
// When a parameter is used in a struct literal like `Asset { name: name.clone() }`,
// we need to detect that `name` is being used even though it's wrapped in .clone()
Expression::MethodCall { object, .. } => {
// Check if the method is being called on our parameter
self.expr_is_param_or_ref_to_param(param_name, object)
}
_ => false,
}
}
/// Windjammer Phase-2 `&str` parameter (Reference(Custom("str"))).
fn is_phase2_str_ref_param_type(&self, ty: &Type) -> bool {
matches!(
ty,
Type::Reference(inner) if matches!(&**inner, Type::Custom(s) if s == "str")
)
}
/// Callee string param uses &String (Borrowed + not Phase-2 &str).
fn callee_string_param_uses_rust_string_ref(
&self,
sig: &super::FunctionSignature,
arg_idx: usize,
param_type: &Type,
method: &str,
) -> bool {
if super::stdlib_method_traits::is_storage_method(method) {
return false;
}
self.is_windjammer_string_param_type(param_type)
&& !self.is_phase2_str_ref_param_type(param_type)
&& sig
.param_ownership_for_arg(arg_idx)
.is_some_and(|o| matches!(o, super::OwnershipMode::Borrowed))
}
/// Check if a type is &String (not &str)
/// This is the key distinction for the optimization
pub(crate) fn type_is_string_ref_not_str(&self, ty: &Type) -> bool {
match ty {
Type::Reference(inner) => match &**inner {
Type::String => true,
Type::Custom(name) if name == "string" => true,
_ => false,
},
_ => false,
}
}
/// Windjammer `string` parameters (including registry stubs before &str lowering).
pub(crate) fn is_windjammer_string_param_type(&self, ty: &Type) -> bool {
matches!(ty, Type::String)
|| matches!(ty, Type::Custom(name) if name == "string")
|| matches!(
ty,
Type::Reference(inner)
if matches!(&**inner, Type::Custom(s) if s == "str")
)
}
/// Check if a type is owned String (not &str, not &String)
/// Used to detect when parameters are passed to functions expecting owned String
pub(crate) fn type_is_owned_string(&self, ty: &Type) -> bool {
matches!(ty, Type::String) || matches!(ty, Type::Custom(name) if name == "string")
}
}