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
#![allow(missing_docs)]
//! DEFECT-PARSER-007: Inline Comments in Struct Field Definitions
//!
//! **Problem**: Inline comments after struct field declarations cause "Expected field name" error
//! **Root Cause**: `parse_struct_fields()` doesn't skip comment tokens between fields
//! **Book Examples Affected**: ch19-00-structs-oop.md (example 7)
//!
//! **Reproduction**:
//! ```ruchy
//! struct BankAccount {
//! pub owner: String, // Public field
//! balance: f64 // Private field
//! }
//! ```
//!
//! Run with: cargo test --test `defect_parser_007_struct_inline_comments`
use ruchy::frontend::parser::Parser;
/// Helper: Parse Ruchy code and verify it succeeds
fn parse_ruchy(source: &str) -> Result<(), String> {
let mut parser = Parser::new(source);
parser.parse().map_err(|e| format!("Parse error: {e:?}"))?;
Ok(())
}
// ============================================================================
// RED PHASE: Failing Tests (Reproduce Defect)
// ============================================================================
#[test]
fn test_defect_parser_007_inline_comment_after_field() {
// SHOULD FAIL: Inline comment after struct field declaration
let code = r"
struct BankAccount {
pub owner: String, // Public field
balance: f64
}
";
let result = parse_ruchy(code);
assert!(
result.is_ok(),
"Inline comments after struct fields should work: {result:?}"
);
}
#[test]
fn test_defect_parser_007_multiple_inline_comments() {
// Book example from ch19-00-structs-oop.md
let code = r"
struct BankAccount {
pub owner: String, // Public field
balance: f64, // Private field (default)
pub(crate) id: i32 // Crate-visible field
}
";
let result = parse_ruchy(code);
assert!(
result.is_ok(),
"Multiple inline comments in struct should work: {result:?}"
);
}
#[test]
fn test_defect_parser_007_block_comment_after_field() {
// Block comments should also work
let code = r"
struct Point {
x: f64, /* X coordinate */
y: f64 /* Y coordinate */
}
";
let result = parse_ruchy(code);
assert!(
result.is_ok(),
"Block comments after struct fields should work: {result:?}"
);
}
#[test]
fn test_defect_parser_007_mixed_comments() {
// Mix of line and block comments
let code = r"
struct Config {
host: String, // Server hostname
port: i32, /* Port number */
timeout: i32 // Connection timeout
}
";
let result = parse_ruchy(code);
assert!(
result.is_ok(),
"Mixed comments in struct should work: {result:?}"
);
}
#[test]
fn test_defect_parser_007_no_comments_still_works() {
// Regression test - ensure fix doesn't break existing behavior
let code = r"
struct Counter {
count: i32,
max: i32
}
";
let result = parse_ruchy(code);
assert!(
result.is_ok(),
"Struct without comments should still work: {result:?}"
);
}
#[test]
fn test_defect_parser_007_comment_before_field() {
// Comments before fields (should already work)
let code = r"
struct User {
// Username field
name: String,
// User ID
id: i32
}
";
let result = parse_ruchy(code);
assert!(
result.is_ok(),
"Comments before struct fields should work: {result:?}"
);
}
// ============================================================================
// GREEN PHASE: Implementation (To Be Added)
// ============================================================================
// Implementation changes will be made in:
// - src/frontend/parser/expressions_helpers/structs.rs (parse_struct_fields function)
// ============================================================================
// REFACTOR PHASE: Quality Validation (After Fix)
// ============================================================================
// After fix:
// - Run cargo test --test defect_parser_007_struct_inline_comments
// - Verify all 6 tests pass
// - Run book extraction: deno run --allow-read --allow-write --allow-run ../ruchy-book/scripts/extract-examples.ts
// - Verify ch19-ex7 passes (98% → 99%)