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
// OPT-CODEGEN-004: Property-Based Tests for Inline Expansion
// VALIDATE Phase: Verify invariants hold across 25,000+ test cases
// Pattern: Based on PERF-002-B/C property tests (inline proptest! in #[ignore] tests)
use ruchy::backend::transpiler::inline_expander::inline_small_functions;
use ruchy::frontend::ast::{
BinaryOp, Expr, ExprKind, Literal, Param, Pattern, Span, Type, TypeKind,
};
// ============================================================================
// PROPERTY 1: Idempotence (256 × 100 = 25,600 cases)
// ============================================================================
/// Property Test 1: Inline expansion is idempotent
/// Invariant: inline(inline(expr)) = inline(expr)
#[test]
fn property_inline_idempotent() {
use proptest::prelude::*;
proptest!(|(a in -50i64..50i64, b in 1i64..50i64)| {
// Create small function: fun add(x, y) { x + y }
let add_func = Expr::new(
ExprKind::Function {
name: "add".to_string(),
type_params: vec![],
params: vec![
Param {
pattern: Pattern::Identifier("x".to_string()),
ty: Type {
kind: TypeKind::Named("i32".to_string()),
span: Span::default(),
},
span: Span::default(),
is_mutable: false,
default_value: None,
},
Param {
pattern: Pattern::Identifier("y".to_string()),
ty: Type {
kind: TypeKind::Named("i32".to_string()),
span: Span::default(),
},
span: Span::default(),
is_mutable: false,
default_value: None,
},
],
return_type: None,
body: Box::new(Expr::new(
ExprKind::Binary {
left: Box::new(Expr::new(
ExprKind::Identifier("x".to_string()),
Span::default(),
)),
op: BinaryOp::Add,
right: Box::new(Expr::new(
ExprKind::Identifier("y".to_string()),
Span::default(),
)),
},
Span::default(),
)),
is_async: false,
is_pub: false,
},
Span::default(),
);
let call = Expr::new(
ExprKind::Call {
func: Box::new(Expr::new(
ExprKind::Identifier("add".to_string()),
Span::default(),
)),
args: vec![
Expr::new(ExprKind::Literal(Literal::Integer(a, None)), Span::default()),
Expr::new(ExprKind::Literal(Literal::Integer(b, None)), Span::default()),
],
},
Span::default(),
);
let block = Expr::new(
ExprKind::Block(vec![add_func, call]),
Span::default(),
);
// Apply inline expansion once
let (once, _) = inline_small_functions(block);
// Apply inline expansion twice
let (twice, _) = inline_small_functions(once.clone());
// Convert to strings for comparison (semantic equivalence)
let once_str = format!("{once:?}");
let twice_str = format!("{twice:?}");
prop_assert_eq!(once_str, twice_str,
"Inline expansion should be idempotent: inline(inline(expr)) = inline(expr)");
});
}
// ============================================================================
// PROPERTY 2: Recursive Functions Never Inlined (256 × 100 = 25,600 cases)
// ============================================================================
/// Property Test 2: Recursive functions are never inlined
/// Invariant: Function definition still exists after inlining
#[test]
fn property_recursive_never_inlined() {
use proptest::prelude::*;
proptest!(|(n in 1i64..20i64)| {
// Create recursive factorial function
let factorial = Expr::new(
ExprKind::Function {
name: "factorial".to_string(),
type_params: vec![],
params: vec![Param {
pattern: Pattern::Identifier("n".to_string()),
ty: Type {
kind: TypeKind::Named("i32".to_string()),
span: Span::default(),
},
span: Span::default(),
is_mutable: false,
default_value: None,
}],
return_type: None,
body: Box::new(Expr::new(
ExprKind::Call {
func: Box::new(Expr::new(
ExprKind::Identifier("factorial".to_string()),
Span::default(),
)),
args: vec![Expr::new(
ExprKind::Literal(Literal::Integer(n, None)),
Span::default(),
)],
},
Span::default(),
)),
is_async: false,
is_pub: false,
},
Span::default(),
);
let call = Expr::new(
ExprKind::Call {
func: Box::new(Expr::new(
ExprKind::Identifier("factorial".to_string()),
Span::default(),
)),
args: vec![Expr::new(
ExprKind::Literal(Literal::Integer(5, None)),
Span::default(),
)],
},
Span::default(),
);
let block = Expr::new(
ExprKind::Block(vec![factorial, call]),
Span::default(),
);
let (result, _) = inline_small_functions(block);
// Verify factorial function definition still exists (not inlined)
if let ExprKind::Block(exprs) = result.kind {
prop_assert!(
exprs.iter().any(|e| matches!(&e.kind, ExprKind::Function { name, .. } if name == "factorial")),
"Recursive function 'factorial' should NOT be inlined (safety check)"
);
} else {
return Err(proptest::test_runner::TestCaseError::fail("Expected block result"));
}
});
}
// ============================================================================
// PROPERTY 3: Large Functions NOT Inlined (256 × 100 = 25,600 cases)
// ============================================================================
/// Property Test 3: Large functions (>10 LOC) are NOT inlined
/// Invariant: Function call still exists after inlining
#[test]
fn property_large_functions_not_inlined() {
use proptest::prelude::*;
proptest!(|(n in 1i64..100i64)| {
// Create large function with >10 LOC (11 let statements)
let mut nested_let = Expr::new(
ExprKind::Identifier("var11".to_string()),
Span::default(),
);
for i in (1..=11).rev() {
nested_let = Expr::new(
ExprKind::Let {
name: format!("var{i}"),
type_annotation: None,
value: Box::new(Expr::new(
ExprKind::Literal(Literal::Integer(i, None)),
Span::default(),
)),
body: Box::new(nested_let),
is_mutable: false,
else_block: None,
},
Span::default(),
);
}
let large_func = Expr::new(
ExprKind::Function {
name: "large_computation".to_string(),
type_params: vec![],
params: vec![Param {
pattern: Pattern::Identifier("n".to_string()),
ty: Type {
kind: TypeKind::Named("i32".to_string()),
span: Span::default(),
},
span: Span::default(),
is_mutable: false,
default_value: None,
}],
return_type: None,
body: Box::new(nested_let),
is_async: false,
is_pub: false,
},
Span::default(),
);
let call = Expr::new(
ExprKind::Call {
func: Box::new(Expr::new(
ExprKind::Identifier("large_computation".to_string()),
Span::default(),
)),
args: vec![Expr::new(
ExprKind::Literal(Literal::Integer(n, None)),
Span::default(),
)],
},
Span::default(),
);
let block = Expr::new(
ExprKind::Block(vec![large_func, call]),
Span::default(),
);
let (result, _) = inline_small_functions(block);
// Verify call still exists (not inlined)
if let ExprKind::Block(exprs) = &result.kind {
prop_assert!(
exprs.iter().any(|e| matches!(&e.kind, ExprKind::Call { func, .. }
if matches!(&func.kind, ExprKind::Identifier(name) if name == "large_computation"))),
"Large function (>10 LOC) should NOT be inlined (size heuristic)"
);
} else {
return Err(proptest::test_runner::TestCaseError::fail("Expected block result"));
}
});
}