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
#![cfg(any(
not(any(
feature = "parser_tests",
feature = "analyzer_tests",
feature = "codegen_tests",
feature = "interpreter_tests",
feature = "conformance_tests",
feature = "integration_tests",
)),
feature = "analyzer_tests",
))]
//! TDD test for automatic String → &str coercion in function calls.
//!
//! Bug: When a function expects &str and receives a String (e.g., from format!()),
//! the compiler should automatically add .as_str() or & to coerce.
//! Currently it generates `draw_text(format!(...))` instead of `draw_text(&format!(...))`.
//!
//! THE WINDJAMMER WAY: The compiler handles mechanical details like borrowing.
//! Users shouldn't need to think about String vs &str coercion.
#[path = "common/test_utils.rs"]
mod test_utils;
#[test]
fn test_format_string_passed_to_str_param() {
// When format!() (producing String) is passed to a function expecting &str,
// the generated Rust should auto-borrow with &
let source = r#"
fn greet(name: string) {
println!("Hello, {}!", name)
}
fn main() {
let x = 42
greet(format!("World #{}", x))
}
"#;
let (generated, _compiles) = test_utils::compile_single_check(source);
// The generated code should have & before format! to borrow the String as &str
// Acceptable forms: &format!(...), format!(...).as_str(), &_temp variable, or &*format!(...)
assert!(
generated.contains("&format!")
|| generated.contains(".as_str()")
|| (generated.contains("format!(") && generated.contains("&_temp")),
"Expected auto-borrow of format!() when passed to &str parameter.\nGenerated:\n{}",
generated
);
}
#[test]
fn test_string_literal_to_str_param_no_extra_borrow() {
// String literals are already &str in Rust, no extra borrowing needed
let source = r#"
fn greet(name: string) {
println!("Hello, {}!", name)
}
fn main() {
greet("World")
}
"#;
let (generated, _compiles) = test_utils::compile_single_check(source);
// Should NOT double-borrow a string literal
assert!(
!generated.contains("&\"World\"") && !generated.contains("&&"),
"Should not double-borrow string literals.\nGenerated:\n{}",
generated
);
}