Skip to content

Commit

Permalink
Fix recursive struct example again (#2185)
Browse files Browse the repository at this point in the history
  • Loading branch information
spapinistarkware committed Feb 19, 2023
1 parent b415cd0 commit 7400dc5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
19 changes: 15 additions & 4 deletions crates/cairo-lang-lowering/src/lower/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,13 @@ pub enum SealedBlockBuilder {
}
impl SealedBlockBuilder {
/// Given the extra information needed, returns the final StructuredBlock.
fn finalize(self, semantic_remapping: &SemanticRemapping) -> StructuredBlock {
fn finalize(
self,
ctx: &mut LoweringContext<'_>,
semantic_remapping: &SemanticRemapping,
) -> StructuredBlock {
match self {
SealedBlockBuilder::GotoCallsite { scope, expr } => {
SealedBlockBuilder::GotoCallsite { mut scope, expr } => {
let mut remapping = VarRemapping::default();
// Since SemanticRemapping should have unique variable ids, these asserts will pass.
for (ty, remapped_var) in semantic_remapping.implicits.iter() {
Expand All @@ -269,7 +273,14 @@ impl SealedBlockBuilder {
assert!(remapping.insert(*remapped_var, scope.semantics[*semantic]).is_none());
}
if let Some(remapped_var) = semantic_remapping.expr {
let expr = expr.expect("Block expr is unit, while sibling block isn't.");
let expr = expr.unwrap_or_else(|| {
LoweredExpr::Tuple {
exprs: vec![],
location: ctx.variables[remapped_var].location,
}
.var(ctx, &mut scope)
.unwrap()
});
assert!(remapping.insert(remapped_var, expr).is_none());
}

Expand Down Expand Up @@ -342,7 +353,7 @@ pub fn merge_sealed(
let blocks = sealed_blocks
.into_iter()
.map(|s| {
let var = s.finalize(&semantic_remapping);
let var = s.finalize(ctx, &semantic_remapping);
ctx.blocks.alloc(var)
})
.collect();
Expand Down
12 changes: 12 additions & 0 deletions tests/bug_samples/issue2172.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,15 @@ struct Node {
left: Option::<Box::<Node>>,
right: Option::<Box::<Node>>,
}

fn traverse(node: Node) {
let Node{value, left, right } = node;
match left {
Option::Some(x) => traverse(unbox(x)),
Option::None(_) => {},
}
match right {
Option::Some(x) => traverse(unbox(x)),
Option::None(_) => {},
}
}

0 comments on commit 7400dc5

Please sign in to comment.