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
#![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: Float literal inference for comparison operators
///
/// Bug: `x < 2.0` where `x: f32` generates `2.0_f64`, causing "can't compare f32 with f64".
/// Root cause: Float literal inference didn't apply to comparison operators.
///
/// Fix: Extend collect_float_literal_constraints to handle comparison ops (<, >, <=, >=, ==, !=)
/// with LHS→RHS and RHS→LHS propagation (same as arithmetic ops).
#[path = "common/test_utils.rs"]
mod test_utils;
// =============================================================================
// Basic: f32_var < literal
// =============================================================================
#[test]
fn test_f32_var_lt_literal() {
let source = r#"
pub fn check(x: f32) -> bool {
x < 2.0
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("2.0_f32"),
"x < 2.0 where x: f32 should generate 2.0_f32, got:\n{}",
output
);
assert!(
!output.contains("2.0_f64"),
"Should NOT use 2.0_f64 when LHS is f32, got:\n{}",
output
);
}
// =============================================================================
// Basic: f64_var > literal
// =============================================================================
#[test]
fn test_f64_var_gt_literal() {
let source = r#"
pub fn check(x: f64) -> bool {
x > 1.0
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("1.0_f64"),
"x > 1.0 where x: f64 should generate 1.0_f64, got:\n{}",
output
);
}
// =============================================================================
// Both literals: if x < threshold (threshold from param)
// =============================================================================
#[test]
fn test_both_operands_typed() {
let source = r#"
pub fn in_range(value: f32, min: f32, max: f32) -> bool {
min < value && value < max
}
"#;
let output = test_utils::compile_single(source);
// value is f32, so comparisons should use f32
assert!(
!output.contains("_f64"),
"All operands f32 - should not use f64, got:\n{}",
output
);
}
// =============================================================================
// Chained: min < value && value < max
// =============================================================================
#[test]
fn test_chained_comparisons() {
let source = r#"
pub fn clamp_check(val: f32, lo: f32, hi: f32) -> bool {
lo < val && val < hi
}
"#;
let output = test_utils::compile_single(source);
assert!(
!output.contains("_f64"),
"Chained comparisons with f32 should not use f64, got:\n{}",
output
);
}
// =============================================================================
// Method result: (x*x + y*y).sqrt() > 0.0
// =============================================================================
#[test]
fn test_method_result_comparison() {
let source = r#"
pub fn normalize(x: f32, y: f32) -> (f32, f32) {
let len = (x * x + y * y).sqrt()
if len > 0.0 {
return (x / len, y / len)
}
return (0.0, 0.0)
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("0.0_f32"),
"len > 0.0 where len is f32 should use 0.0_f32, got:\n{}",
output
);
assert!(
!output.contains("0.0_f64"),
"Should NOT use 0.0_f64 in f32 context, got:\n{}",
output
);
}
// =============================================================================
// Equality: f32_var == 0.0
// =============================================================================
#[test]
fn test_f32_eq_literal() {
let source = r#"
pub fn is_zero(x: f32) -> bool {
x == 0.0
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("0.0_f32"),
"x == 0.0 where x: f32 should generate 0.0_f32, got:\n{}",
output
);
}
// =============================================================================
// Inequality: f32_var != 1.0
// =============================================================================
#[test]
fn test_f32_ne_literal() {
let source = r#"
pub fn not_one(x: f32) -> bool {
x != 1.0
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("1.0_f32"),
"x != 1.0 where x: f32 should generate 1.0_f32, got:\n{}",
output
);
}
// =============================================================================
// Less-or-equal, greater-or-equal
// =============================================================================
#[test]
fn test_f32_le_ge_literal() {
let source = r#"
pub fn in_bounds(x: f32, low: f32, high: f32) -> bool {
low <= x && x <= high
}
"#;
let output = test_utils::compile_single(source);
assert!(
!output.contains("_f64"),
"All f32 - should not use f64, got:\n{}",
output
);
}
// =============================================================================
// Literal on LHS: 0.0 < x where x: f32
// =============================================================================
#[test]
fn test_literal_lhs_f32_rhs() {
let source = r#"
pub fn is_positive(x: f32) -> bool {
0.0 < x
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("0.0_f32"),
"0.0 < x where x: f32 should infer 0.0 as f32 (RHS→LHS), got:\n{}",
output
);
}
// =============================================================================
// Field access: self.x < 1.0
// =============================================================================
#[test]
fn test_field_access_comparison() {
let source = r#"
pub struct Data { pub value: f32 }
impl Data {
pub fn is_small(self) -> bool {
self.value < 1.0
}
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("1.0_f32"),
"self.value < 1.0 should generate 1.0_f32, got:\n{}",
output
);
}
// =============================================================================
// Nested field access: self.velocity.x != 0.0 (game pattern from physics_body.wj)
// =============================================================================
#[test]
fn test_nested_field_access_comparison() {
let source = r#"
pub struct Vec3 { pub x: f32, pub y: f32, pub z: f32 }
impl Vec3 {
pub fn new(x: f32, y: f32, z: f32) -> Vec3 { Vec3 { x, y, z } }
}
pub struct PhysicsBody { pub velocity: Vec3 }
impl PhysicsBody {
pub fn has_x_velocity(self) -> bool {
self.velocity.x != 0.0
}
pub fn has_z_velocity(self) -> bool {
self.velocity.z != 0.0
}
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("0.0_f32"),
"self.velocity.x != 0.0 should generate 0.0_f32 (nested FieldAccess), got:\n{}",
output
);
assert!(
!output.contains("0.0_f64"),
"Nested field access comparison should NOT use f64, got:\n{}",
output
);
}
// =============================================================================
// Nested field: self.settings.gamma != 1.0 (post_processing pattern)
// =============================================================================
#[test]
fn test_nested_settings_gamma_comparison() {
let source = r#"
pub struct ColorGradingSettings { pub gamma: f32 }
pub struct ColorGrading { pub settings: ColorGradingSettings }
impl ColorGrading {
pub fn process(self) -> bool {
if self.settings.gamma != 1.0 {
return true
}
return false
}
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("1.0_f32"),
"self.settings.gamma != 1.0 should generate 1.0_f32, got:\n{}",
output
);
assert!(
!output.contains("1.0_f64"),
"Settings gamma comparison should NOT use f64, got:\n{}",
output
);
}
// =============================================================================
// Triple nested: self.camera.position.x/y/z != 0.0 (quick_start pattern)
// =============================================================================
#[test]
fn test_triple_nested_camera_position_comparison() {
let source = r#"
pub struct Vec3 { pub x: f32, pub y: f32, pub z: f32 }
impl Vec3 {
pub fn new(x: f32, y: f32, z: f32) -> Vec3 { Vec3 { x, y, z } }
}
pub struct Camera { pub position: Vec3 }
pub struct Game { pub camera: Camera }
impl Game {
pub fn is_ready(self) -> bool {
self.camera.position.x != 0.0 || self.camera.position.y != 0.0 || self.camera.position.z != 0.0
}
}
"#;
let output = test_utils::compile_single(source);
assert!(
output.contains("0.0_f32"),
"self.camera.position.x != 0.0 should generate 0.0_f32, got:\n{}",
output
);
assert!(
!output.contains("0.0_f64"),
"Camera position comparison should NOT use f64, got:\n{}",
output
);
}