Skip to content

Replace the heuristic lookup of Cairo variables with a semantic walk in FunctionDebugInfo#9992

Open
integraledelebesgue wants to merge 2 commits into
debugger-annotations-v2/var-multimapfrom
debugger-annotations-v2/semantic-mapping
Open

Replace the heuristic lookup of Cairo variables with a semantic walk in FunctionDebugInfo#9992
integraledelebesgue wants to merge 2 commits into
debugger-annotations-v2/var-multimapfrom
debugger-annotations-v2/semantic-mapping

Conversation

@integraledelebesgue

Copy link
Copy Markdown
Collaborator

Summary

This PR reworks the Sierra -> Cairo variable reconstruction logic.
Instead of a heuristic lookup, we perform a top-down walk through the function's semantic model and construct the mapping by hand, using a dedicated logic for every type of statement and expression.
This allows to handle all problematic cases of Cairo variables, like re-binds or loops.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

⚠️ Note:
To keep maintainer workload sustainable, we generally do not accept PRs that
are only minor wording, grammar, formatting, or style changes.
Such PRs may be closed without detailed review.


Why is this change needed?

It allows the Cairo debugger to properly handle variables in general and support loops.


What was the behavior or documentation before?


What is the behavior or documentation after?


Related issue or discussion (if any)


Additional context

@integraledelebesgue

integraledelebesgue commented May 27, 2026

Copy link
Copy Markdown
Collaborator Author

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

@integraledelebesgue integraledelebesgue force-pushed the debugger-annotations-v2/var-multimap branch from 755d61f to bda4cf2 Compare May 28, 2026 09:16
@integraledelebesgue integraledelebesgue force-pushed the debugger-annotations-v2/semantic-mapping branch 2 times, most recently from 2e1fbb0 to 481558b Compare June 2, 2026 17:00
@integraledelebesgue integraledelebesgue marked this pull request as ready for review June 2, 2026 17:00
@cursor

cursor Bot commented Jun 2, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes debugger-facing debug metadata generation across compiler, Starknet, and test builds; behavior should improve but regressions in variable display are possible in edge cases (generics, loops, closures).

Overview
Replaces statement-location heuristics with a semantic-model walk when building Sierra→Cairo variable mappings for FunctionDebugInfo (cairo-debugger annotations).

extract_serializable_debug_info no longer takes StatementsLocations or a ProgramRegistry; compiler, Starknet, and test-plugin call sites pass only the Sierra program. Mapping is built by resolving the Sierra function to FunctionWithBodyId, indexing Sierra vars by stable syntax pointers from variables_locations, inferring var types from libfunc signatures, and walking the body with BindingCollector (lets, assignments, if/match/for/while/loop, compound assignment, pattern binds, and rebinds via an alias map).

Large blocks of syntax-tree identifier lookup and lookup-item resolution are removed in favor of this structured traversal.

Reviewed by Cursor Bugbot for commit a0d63ce. Bugbot is set up for automated code reviews on this repo. Configure here.

Comment thread crates/cairo-lang-sierra-generator/src/debug_info/function_debug_info.rs Outdated
@integraledelebesgue integraledelebesgue force-pushed the debugger-annotations-v2/var-multimap branch from bda4cf2 to abd9ac1 Compare June 8, 2026 10:13
@integraledelebesgue integraledelebesgue force-pushed the debugger-annotations-v2/semantic-mapping branch 2 times, most recently from 4d24288 to f684aea Compare June 8, 2026 10:17
@integraledelebesgue integraledelebesgue force-pushed the debugger-annotations-v2/var-multimap branch from abd9ac1 to 142b89f Compare June 8, 2026 10:53
@integraledelebesgue integraledelebesgue force-pushed the debugger-annotations-v2/semantic-mapping branch from f684aea to 610632e Compare June 8, 2026 10:54
}
let rhs_ty = self.db.expr_semantic(self.function_with_body_id, let_stmt.expr).ty();
let rhs_var = self.resolve_rhs_sierra_var(let_stmt.expr, rhs_ty);
self.bind_pattern(let_stmt.pattern, rhs_var);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let-else else mutates aliases early

Medium Severity

For Statement::Let with a let-else clause, the walker visits the else expression before it resolves the RHS Sierra variable and binds the pattern. Because resolve_rhs_sierra_var uses the live alias_map, any rebindings performed while visiting the else branch can change how the main let RHS is resolved, producing incorrect Sierra-to-Cairo mappings on the success path.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 610632e. Configure here.

@piotmag769 piotmag769 force-pushed the debugger-annotations-v2/var-multimap branch from 142b89f to 0890266 Compare July 3, 2026 16:59
@piotmag769 piotmag769 force-pushed the debugger-annotations-v2/semantic-mapping branch from 610632e to a0d63ce Compare July 3, 2026 16:59

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

There are 3 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit a0d63ce. Configure here.

self.bind_pattern(pattern_id, scrutinee_sierra.clone());
}
self.visit_expr(arm.expression);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Control flow merges alias state

Medium Severity

BindingCollector keeps one global alias_map while visiting every if branch and every match arm in order. Later branches overwrite earlier bindings, so code after control flow (and later arms) resolves Cairo variables using the last visited branch’s Sierra slots, not the branch that would run at runtime.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a0d63ce. Configure here.

self.bind_pattern(for_expr.pattern, None);
self.visit_expr(for_expr.body);
}
Expr::Loop(loop_expr) => self.visit_expr(loop_expr.body),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Loop body walked once

Medium Severity

Expr::Loop and Expr::While call visit_expr on the body a single time, so each reassignment inside the loop produces at most one rebind event. Sierra variables allocated on later iterations at the same source locations are never added to bindings, leaving many loop-live slots without Cairo names.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit a0d63ce. Configure here.

@piotmag769 piotmag769 force-pushed the debugger-annotations-v2/var-multimap branch from 0890266 to caca9d7 Compare July 5, 2026 20:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants