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
#![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-loop wildcard pattern `for _ in 0..3 { ... }`
//
// Bug: The for-loop parser only handled Ident, LParen, and Ampersand tokens
// as valid loop variable patterns. Token::Underscore (wildcard `_`) was missing,
// causing `for _ in 0..3 { ... }` to fail with:
// "Expected variable name, reference pattern, or tuple pattern in for loop"
//
// Root Cause: The parse_for() function had explicit checks for &, (, and Ident
// but forgot to handle Token::Underscore. The general parse_pattern() function
// DOES handle wildcards, but wasn't being called for the Underscore case.
//
// Fix: Use parse_pattern() for all non-reference patterns in for-loops,
// which handles identifiers, wildcards, tuples, and all other pattern types.
#[path = "common/test_utils.rs"]
mod test_utils;
#[test]
fn test_for_wildcard_pattern() {
// THE BUG: `for _ in 0..3` fails to parse
let (generated, ok) = test_utils::compile_single_check(
r#"
fn main() {
for _ in 0..3 {
println("hello")
}
}
"#,
);
assert!(
ok,
"for _ in 0..3 should parse successfully.\nGenerated:\n{}",
generated
);
assert!(
generated.contains("for _ in"),
"Generated Rust should contain `for _ in`.\nGenerated:\n{}",
generated
);
}
#[test]
fn test_for_wildcard_nested() {
// Nested for-loops with wildcard outer loop (the exact failing pattern from puzzle_game.wj)
let (generated, ok) = test_utils::compile_single_check(
r#"
fn main() {
for _ in 0..3 {
for col in 0..4 {
println("{}", col)
}
}
}
"#,
);
assert!(
ok,
"Nested for with wildcard should parse.\nGenerated:\n{}",
generated
);
}
#[test]
fn test_for_tuple_destructure() {
// Tuple destructuring in for-loop: for (i, item) in items.iter().enumerate()
let (generated, ok) = test_utils::compile_single_check(
r#"
fn main() {
let items = vec![10, 20, 30]
for (i, item) in items.enumerate() {
println("index {}: {}", i, item)
}
}
"#,
);
assert!(
ok,
"for (i, item) in ... should parse.\nGenerated:\n{}",
generated
);
assert!(
generated.contains("(i, item)") || generated.contains("(_i, _item)"),
"Generated Rust should contain tuple destructuring.\nGenerated:\n{}",
generated
);
}