Skip to content

Commit

Permalink
fix: dependency graph for paths with indices (#1429)
Browse files Browse the repository at this point in the history
  • Loading branch information
liangyiliang committed Jun 2, 2023
1 parent 3e589a0 commit d7e101e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
26 changes: 26 additions & 0 deletions packages/core/src/compiler/Style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1328,4 +1328,30 @@ delete x.z.p }`,
}
});
});

describe("gather dependencies", () => {
test("indexing", async () => {
const dsl = "type T";
const sub = "T t";
const sty =
canvasPreamble +
`
forall T t {
t.vals = [1, 2, 3, 4, 5, 6]
t.val = t.vals[match_id]
Circle {
r: t.val
}
}
`;

// This problem would have failed compilation when indexing is not handled correctly
// And this would fail:
const { graph } = await loadProgs({ dsl, sub, sty });
expect(graph.parents("`t`.val").sort()).toEqual(
["`t`.vals", "1:0:match_id"].sort()
);
});
});
});
9 changes: 8 additions & 1 deletion packages/core/src/compiler/Style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,14 @@ const findPathsExpr = <T>(expr: Expr<T>, context: Context): Path<T>[] => {
return expr.contents.flatMap((e) => findPathsExpr(e, context));
}
case "Path": {
return [expr];
// A `Path` (generally, `arr[index]`) expression depends on `arr` and `index` (if exists)
return [
{
...expr,
indices: [],
},
...expr.indices.flatMap((index) => findPathsExpr(index, context)),
];
}
case "UOp": {
return findPathsExpr(expr.arg, context);
Expand Down

0 comments on commit d7e101e

Please sign in to comment.