Skip to content

Commit e9ffcee

Browse files
committed
# This is a combination of 16 commits.
# This is the 1st commit message: wip: issue/88 # This is the commit message leanEthereum#2: wip: issue/88 # This is the commit message leanEthereum#3: wip: issue/88 # This is the commit message leanEthereum#4: wip: issue/88 # This is the commit message leanEthereum#5: wip: issue/88 # This is the commit message leanEthereum#6: shrink test_match # This is the commit message leanEthereum#7: wip: issue/88 # This is the commit message leanEthereum#8: remove mark_vars_as_declared # This is the commit message leanEthereum#9: wip: issue/88 # This is the commit message leanEthereum#10: wip: issue/88 # This is the commit message leanEthereum#11: wip: issue/88 # This is the commit message leanEthereum#12: reverse stack trace order # This is the commit message leanEthereum#13: add failing test case from shrinking WHIR recursion # This is the commit message leanEthereum#14: wip: issue/88: bugfix: eliminate name shadowing # This is the commit message leanEthereum#15: refactor: remove declared_vars # This is the commit message leanEthereum#16: wip: issue/88
1 parent 4828dc6 commit e9ffcee

File tree

6 files changed

+70
-78
lines changed

6 files changed

+70
-78
lines changed

crates/lean_compiler/src/a_simplify_lang.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ fn check_expr_scoping(expr: &Expression, ctx: &Context) {
326326
fn check_simple_expr_scoping(expr: &SimpleExpr, ctx: &Context) {
327327
match expr {
328328
SimpleExpr::Var(v) => {
329-
assert!(ctx.defines(&v), "Variable defined but not used: {:?}", v)
329+
assert!(ctx.defines(&v), "Variable used but not defined: {:?}", v)
330330
},
331331
SimpleExpr::Constant(_) => {},
332332
SimpleExpr::ConstMallocAccess { .. } => {},
@@ -636,8 +636,6 @@ fn simplify_lines(
636636
counter: const_malloc.counter,
637637
..ConstMalloc::default()
638638
};
639-
// TODO: what is array manager, and does it need to be updated
640-
// to make block-level scoping work?
641639
let valid_aux_vars_in_array_manager_before = array_manager.valid.clone();
642640
array_manager.valid.clear();
643641
let simplified_body = simplify_lines(
@@ -1597,6 +1595,10 @@ fn handle_inlined_functions_helper(
15971595
if let Some(func) = inlined_functions.get(&*function_name) {
15981596
let mut inlined_lines = vec![];
15991597

1598+
for var in return_data.iter() {
1599+
inlined_lines.push(Line::ForwardDeclaration { var : var.clone() });
1600+
}
1601+
16001602
let mut simplified_args = vec![];
16011603
for arg in args {
16021604
if let Expression::Value(simple_expr) = arg {

crates/lean_compiler/src/b_compile_intermediate.rs

Lines changed: 40 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,11 @@ fn compile_function(
147147
compiler.stack_size = stack_pos;
148148
compiler.args_count = function.arguments.len();
149149

150-
let mut declared_vars: BTreeSet<Var> = function.arguments.iter().cloned().collect();
151150
compile_lines(
152151
&Label::function(function.name.clone()),
153152
&function.instructions,
154153
compiler,
155154
None,
156-
&mut declared_vars,
157155
)
158156
}
159157

@@ -162,14 +160,13 @@ fn compile_lines(
162160
lines: &[SimpleLine],
163161
compiler: &mut Compiler,
164162
final_jump: Option<Label>,
165-
declared_vars: &mut BTreeSet<Var>,
166163
) -> Result<Vec<IntermediateInstruction>, String> {
167164
let mut instructions = Vec::new();
168165

169166
for (i, line) in lines.iter().enumerate() {
170167
match line {
171168
SimpleLine::ForwardDeclaration { var } => {
172-
let mut current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
169+
let current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
173170
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
174171
compiler.stack_pos += 1;
175172
}
@@ -183,9 +180,8 @@ fn compile_lines(
183180
let arg0 = IntermediateValue::from_simple_expr(arg0, compiler);
184181
let arg1 = IntermediateValue::from_simple_expr(arg1, compiler);
185182

186-
if let VarOrConstMallocAccess::Var(var) = var {
187-
declared_vars.insert(var.clone());
188-
let mut current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
183+
if let VarOrConstMallocAccess::Var(var) = var && !compiler.is_in_scope(var) {
184+
let current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
189185
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
190186
compiler.stack_pos += 1;
191187
}
@@ -219,21 +215,14 @@ fn compile_lines(
219215
let saved_stack_pos = compiler.stack_pos;
220216
let mut new_stack_pos = saved_stack_pos;
221217
for (i, arm) in arms.iter().enumerate() {
222-
let mut arm_declared_vars = declared_vars.clone();
223218
compiler.stack_pos = saved_stack_pos;
224219
let arm_instructions = compile_lines(
225220
function_name,
226221
arm,
227222
compiler,
228223
Some(end_label.clone()),
229-
&mut arm_declared_vars,
230224
)?;
231225
compiled_arms.push(arm_instructions);
232-
*declared_vars = if i == 0 {
233-
arm_declared_vars
234-
} else {
235-
declared_vars.intersection(&arm_declared_vars).cloned().collect()
236-
};
237226
new_stack_pos = new_stack_pos.max(compiler.stack_pos);
238227
}
239228
compiler.stack_pos = new_stack_pos;
@@ -268,7 +257,7 @@ fn compile_lines(
268257
updated_fp: None,
269258
});
270259

271-
let remaining = compile_lines(function_name, &lines[i + 1..], compiler, final_jump, declared_vars)?;
260+
let remaining = compile_lines(function_name, &lines[i + 1..], compiler, final_jump)?;
272261
compiler.bytecode.insert(end_label, remaining);
273262

274263
compiler.stack_frame_layout.scopes.pop();
@@ -287,8 +276,6 @@ fn compile_lines(
287276
} => {
288277
compiler.stack_frame_layout.scopes.push(ScopeLayout::default());
289278

290-
validate_vars_declared(&[condition], declared_vars)?;
291-
292279
let if_id = compiler.if_counter;
293280
compiler.if_counter += 1;
294281

@@ -363,24 +350,20 @@ fn compile_lines(
363350

364351
let saved_stack_pos = compiler.stack_pos;
365352

366-
let mut then_declared_vars = declared_vars.clone();
367353
let then_instructions = compile_lines(
368354
function_name,
369355
then_branch,
370356
compiler,
371357
Some(end_label.clone()),
372-
&mut then_declared_vars,
373358
)?;
374359

375360
let then_stack_pos = compiler.stack_pos;
376361
compiler.stack_pos = saved_stack_pos;
377-
let mut else_declared_vars = declared_vars.clone();
378362
let else_instructions = compile_lines(
379363
function_name,
380364
else_branch,
381365
compiler,
382366
Some(end_label.clone()),
383-
&mut else_declared_vars,
384367
)?;
385368

386369
compiler.bytecode.insert(if_label, then_instructions);
@@ -389,7 +372,7 @@ fn compile_lines(
389372
compiler.stack_frame_layout.scopes.pop();
390373
compiler.stack_pos = compiler.stack_pos.max(then_stack_pos);
391374

392-
let remaining = compile_lines(function_name, &lines[i + 1..], compiler, final_jump, declared_vars)?;
375+
let remaining = compile_lines(function_name, &lines[i + 1..], compiler, final_jump)?;
393376
compiler.bytecode.insert(end_label, remaining);
394377
// It is not necessary to update compiler.stack_size here because the preceding call to
395378
// compile_lines should have done so.
@@ -398,11 +381,8 @@ fn compile_lines(
398381
}
399382

400383
SimpleLine::RawAccess { res, index, shift } => {
401-
// TODO: why is validate_vars_declared here?
402-
validate_vars_declared(&[index], declared_vars)?;
403384
if let SimpleExpr::Var(var) = res && !compiler.is_in_scope(var) {
404-
declared_vars.insert(var.clone());
405-
let mut current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
385+
let current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
406386
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
407387
compiler.stack_pos += 1;
408388
}
@@ -438,13 +418,12 @@ fn compile_lines(
438418
compiler,
439419
)?);
440420

441-
// TODO: why is validate_vars_declared here?
442-
validate_vars_declared(args, declared_vars)?;
443-
declared_vars.extend(return_data.iter().cloned());
444421
for var in return_data.iter() {
445-
let mut current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
446-
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
447-
compiler.stack_pos += 1;
422+
if !compiler.is_in_scope(var) {
423+
let current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
424+
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
425+
compiler.stack_pos += 1;
426+
}
448427
}
449428

450429
let after_call = {
@@ -466,7 +445,6 @@ fn compile_lines(
466445
&lines[i + 1..],
467446
compiler,
468447
final_jump,
469-
declared_vars,
470448
)?);
471449

472450
instructions
@@ -522,10 +500,11 @@ fn compile_lines(
522500
vectorized,
523501
vectorized_len,
524502
} => {
525-
let mut current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
526-
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
527-
compiler.stack_pos += 1;
528-
declared_vars.insert(var.clone());
503+
if !compiler.is_in_scope(var) {
504+
let current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
505+
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
506+
compiler.stack_pos += 1;
507+
}
529508
instructions.push(IntermediateInstruction::RequestMemory {
530509
offset: compiler.get_offset(&var.clone().into()),
531510
size: IntermediateValue::from_simple_expr(size, compiler),
@@ -535,23 +514,23 @@ fn compile_lines(
535514
}
536515
SimpleLine::ConstMalloc { var, size, label } => {
537516
let size = size.naive_eval().unwrap().to_usize(); // TODO not very good;
538-
declared_vars.insert(var.clone());
539-
let mut current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
540-
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
541-
compiler.stack_pos += 1;
542-
current_scope_layout.const_mallocs.insert(*label, compiler.stack_pos);
543-
handle_const_malloc(declared_vars, &mut instructions, compiler, var, size, label);
544-
compiler.stack_pos += size;
517+
if !compiler.is_in_scope(var) {
518+
let current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
519+
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
520+
compiler.stack_pos += 1;
521+
}
522+
handle_const_malloc(&mut instructions, compiler, var, size, label);
545523
}
546524
SimpleLine::DecomposeBits {
547525
var,
548526
to_decompose,
549527
label,
550528
} => {
551-
declared_vars.insert(var.clone());
552-
let mut current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
553-
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
554-
compiler.stack_pos += 1;
529+
if !compiler.is_in_scope(var) {
530+
let current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
531+
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
532+
compiler.stack_pos += 1;
533+
}
555534

556535
instructions.push(IntermediateInstruction::DecomposeBits {
557536
res_offset: compiler.stack_pos,
@@ -562,24 +541,23 @@ fn compile_lines(
562541
});
563542

564543
handle_const_malloc(
565-
declared_vars,
566544
&mut instructions,
567545
compiler,
568546
var,
569547
F::bits() * to_decompose.len(),
570548
label,
571549
);
572-
compiler.stack_pos += F::bits() * to_decompose.len();
573550
}
574551
SimpleLine::DecomposeCustom {
575552
var,
576553
to_decompose,
577554
label,
578555
} => {
579-
declared_vars.insert(var.clone());
580-
let mut current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
581-
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
582-
compiler.stack_pos += 1;
556+
if !compiler.is_in_scope(var) {
557+
let current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
558+
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
559+
compiler.stack_pos += 1;
560+
}
583561

584562
instructions.push(IntermediateInstruction::DecomposeCustom {
585563
res_offset: compiler.stack_pos,
@@ -590,20 +568,19 @@ fn compile_lines(
590568
});
591569

592570
handle_const_malloc(
593-
declared_vars,
594571
&mut instructions,
595572
compiler,
596573
var,
597574
F::bits() * to_decompose.len(),
598575
label,
599576
);
600-
compiler.stack_pos += F::bits() * to_decompose.len();
601577
}
602578
SimpleLine::CounterHint { var } => {
603-
let mut current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
604-
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
605-
compiler.stack_pos += 1;
606-
declared_vars.insert(var.clone());
579+
if !compiler.is_in_scope(var) {
580+
let current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
581+
current_scope_layout.var_positions.insert(var.clone(), compiler.stack_pos);
582+
compiler.stack_pos += 1;
583+
}
607584
instructions.push(IntermediateInstruction::CounterHint {
608585
res_offset: compiler
609586
.get_offset(&var.clone().into())
@@ -640,13 +617,14 @@ fn compile_lines(
640617
}
641618

642619
fn handle_const_malloc(
643-
declared_vars: &mut BTreeSet<Var>,
644620
instructions: &mut Vec<IntermediateInstruction>,
645621
compiler: &mut Compiler,
646622
var: &Var,
647623
size: usize,
648624
label: &ConstMallocLabel,
649625
) {
626+
let current_scope_layout = compiler.stack_frame_layout.scopes.last_mut().unwrap();
627+
current_scope_layout.const_mallocs.insert(*label, compiler.stack_pos);
650628
instructions.push(IntermediateInstruction::Computation {
651629
operation: Operation::Add,
652630
arg_a: IntermediateValue::Constant(compiler.stack_pos.into()),
@@ -655,18 +633,7 @@ fn handle_const_malloc(
655633
offset: compiler.get_offset(&var.clone().into()),
656634
},
657635
});
658-
}
659-
660-
// Helper functions
661-
fn validate_vars_declared<VoC: Borrow<SimpleExpr>>(vocs: &[VoC], declared: &BTreeSet<Var>) -> Result<(), String> {
662-
for voc in vocs {
663-
if let SimpleExpr::Var(v) = voc.borrow()
664-
&& !declared.contains(v)
665-
{
666-
return Err(format!("Variable {v} not declared"));
667-
}
668-
}
669-
Ok(())
636+
compiler.stack_pos += size;
670637
}
671638

672639
fn setup_function_call(

crates/lean_compiler/src/parser/parsers/statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl Parse<Line> for StatementParser {
4040
pub struct ForwardDeclarationParser;
4141

4242
impl Parse<Line> for ForwardDeclarationParser {
43-
fn parse(pair: ParsePair<'_>, ctx: &mut ParseContext) -> ParseResult<Line> {
43+
fn parse(pair: ParsePair<'_>, _ctx: &mut ParseContext) -> ParseResult<Line> {
4444
let mut inner = pair.into_inner();
4545
let var = next_inner_pair(&mut inner, "variable name")?.as_str().to_string();
4646
Ok(Line::ForwardDeclaration { var })

crates/lean_compiler/tests/test_compiler.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,27 @@ fn test_inlined() {
381381
compile_and_run(program.to_string(), (&[], &[]), DEFAULT_NO_VEC_RUNTIME_MEMORY, false);
382382
}
383383

384+
#[test]
385+
fn test_inlined_2() {
386+
let program = r#"
387+
fn main() {
388+
b = is_one();
389+
c = b;
390+
return;
391+
}
392+
393+
fn is_one() inline -> 1 {
394+
if 1 {
395+
return 1;
396+
} else {
397+
return 0;
398+
}
399+
}
400+
"#;
401+
compile_and_run(program.to_string(), (&[], &[]), DEFAULT_NO_VEC_RUNTIME_MEMORY, false);
402+
403+
}
404+
384405
#[test]
385406
fn test_match() {
386407
let program = r#"

crates/lean_vm/src/execution/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn test_basic_memory_operations() {
1313
assert_eq!(memory.get(5).unwrap(), F::from_usize(42));
1414

1515
// Test undefined memory access
16-
assert!(matches!(memory.get(1), Err(RunnerError::UndefinedMemory)));
16+
assert!(matches!(memory.get(1), Err(RunnerError::UndefinedMemory(1))));
1717
}
1818

1919
#[test]

crates/rec_aggregation/recursion_program.lean_lang

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,8 @@ fn sample_stir_indexes_and_fold(fs_state, num_queries, merkle_leaves_in_basefiel
334334
states = malloc(1 + folded_domain_size);
335335
states[0] = leaf_hashes[i];
336336
for j in 0..folded_domain_size {
337+
var left;
338+
var right;
337339
if stir_index_bits[j] == 1 {
338340
left = merkle_path + j;
339341
right = states[j];

0 commit comments

Comments
 (0)