Skip to content

(refactor): unify lowering/optimization golden tests into a single before/after phase test runner - #10284

Merged
orizi merged 1 commit into
mainfrom
orizi/08-02-_refactor_unify_lowering_optimization_golden_tests_into_a_single_before_after_phase_test_runner
Aug 4, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/08-02-_refactor_unify_lowering_optimization_golden_tests_into_a_single_before_after_phase_test_runner

Conversation

@orizi

@orizi orizi commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds a single unified golden-file test runner for lowering/optimization tests, crates/cairo-lang-lowering/src/test_runner.rs (#[cfg(test)]), parameterized by plain Rust arguments:

  • stage: LoweringStage — the stage the before body is fetched from (db.lowered_body),
  • before_phases: &[OptimizationPhase] — applied to produce before,
  • after_phases: &[OptimizationPhase] — applied to a clone to produce after,

with two variants for the special cases: ..._with_db (custom DB setup, used by early_unsafe_panic for Flag::UnsafePanic) and ..._with_extra_outputs (used by reboxing to keep its candidates output tag).

14 of the 15 optimizations/*_test.rs files plus inline/test.rs are reduced to thin bindings passing their exact previous configuration. Net: 19 files, +382/−567.

Type of change

  • Refactor (test-code only, no behavior change)

Why is this change needed?

Every per-optimization test file duplicated the same ~40-line runner: fetch lowered body at some stage, apply a hardcoded phase slice, clone, apply the optimization, format before/after. The copies had drifted (three different diagnostics-fetch orderings, one stateful runner macro with a useless db field, copy-pasted fn/module names) and adding a new optimization test meant copying the boilerplate again.

What was the behavior before / after?

Test behavior is preserved: the golden-file diff across the whole migration is 2 files / 8 linesgas_redeposit's test_runner_name tag rename (its runner struct became a fn) and 4 purely additive empty diagnostics tags in one reboxing case. Zero before/after/candidates body lines changed; the other golden files are byte-identical. Case counts are unchanged (170 optimization cases + 23 inline cases).

Additional context

  • scrub_units_test.rs deliberately stays bespoke: scrub_units has no OptimizationPhase variant (it is a hardcoded step inside lowered_body), and adding a production variant only for a test would pollute the strategy vocabulary. Documented in the runner's module doc, along with the other out-of-scope lowering tests (single-output shape, no before/after pair).
  • Known pre-existing issues left untouched to keep this diff reviewable (follow-ups planned): const_folding_test.rs's fn is misnamed test_match_optimizer, and three files share the scrub_units test_file_test! module arg.
  • Failure policy is uniform: non-empty semantic diagnostics → early return with only that tag; lowering failure → placeholder bodies + lowering_diagnostics. No existing golden case exercises these paths.

🤖 Generated with Claude Code

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

orizi commented Aug 2, 2026

Copy link
Copy Markdown
Collaborator Author

@orizi
orizi force-pushed the orizi/08-02-_refactor_unify_lowering_optimization_golden_tests_into_a_single_before_after_phase_test_runner branch from 202cb20 to 781bbb8 Compare August 2, 2026 14:45
@orizi
orizi marked this pull request as ready for review August 2, 2026 14:47
@orizi
orizi enabled auto-merge August 2, 2026 14:47
@cursor

cursor Bot commented Aug 2, 2026

Copy link
Copy Markdown

PR Summary

Low Risk
Test-only refactor with preserved golden outputs; no production lowering or optimization logic changes.

Overview
Introduces cairo-lang-lowering::test_runner (run_lowering_phases_test and _with_db / _with_extra_outputs) so lowering golden tests share one before/after pipeline: lower to a LoweringStage, apply before_phases, then after_phases, and emit the standard tags via formatted_lowered.

14 optimizations/*_test.rs files plus inline/test.rs are reduced to thin wrappers that pass the same stage/phase lists as before (including moving CSE, dedup blocks, and gas redeposit off ad-hoc function calls onto OptimizationPhase). Gas redeposit drops the stateful TestFileRunner macro in favor of a plain function; reboxing keeps its candidates tag through extra_outputs; early unsafe panic still pre-configures the DB via _with_db. scrub_units_test.rs stays custom (no OptimizationPhase), with formatted_lowered alignment only.

Docs in golden-framework.md now tell authors not to hand-roll before/after runners. Golden diffs are minimal (runner name rename + empty diagnostic tags in one reboxing case).

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

@orizi
orizi force-pushed the orizi/08-02-_refactor_unify_lowering_optimization_golden_tests_into_a_single_before_after_phase_test_runner branch 3 times, most recently from 35e00fe to 512169c Compare August 4, 2026 09:03

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@eytan-starkware reviewed 1 file and all commit messages, and made 1 comment.
Reviewable status: 1 of 20 files reviewed, 1 unresolved discussion (waiting on orizi and TomerStarkware).


.claude/skills/writing-tests/references/golden-framework.md line 213 at r2 (raw file):

Variants: `run_lowering_phases_test_with_db` (pre-configured database, e.g. setting a flag) and
`run_lowering_phases_test_with_extra_outputs` (extra output tags derived from the two bodies).

Variant text is not very informative on what they do. Is it elsewhere in the skill?

@TomerStarkware TomerStarkware left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

:lgtm:

@TomerStarkware reviewed 20 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi).

@orizi
orizi force-pushed the orizi/08-02-_refactor_unify_lowering_optimization_golden_tests_into_a_single_before_after_phase_test_runner branch from 512169c to ba42234 Compare August 4, 2026 10:37

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@orizi made 1 comment.
Reviewable status: 19 of 20 files reviewed, 1 unresolved discussion (waiting on eytan-starkware and TomerStarkware).


.claude/skills/writing-tests/references/golden-framework.md line 213 at r2 (raw file):

Previously, eytan-starkware wrote…

Variant text is not very informative on what they do. Is it elsewhere in the skill?

Two variants take one extra parameter each, for tests the plain entry point cannot express:

  • run_lowering_phases_test_with_db(db, inputs, stage, before_phases, after_phases) — runs on the
    LoweringDatabaseForTesting you pass in instead of a default one, for tests that must configure
    the database before the test function is lowered. early_unsafe_panic_test.rs uses it to set
    Flag::UnsafePanic.
  • run_lowering_phases_test_with_extra_outputs(inputs, stage, before_phases, after_phases, extra_outputs)extra_outputs is a |before, after| -> Vec<(String, String)> closure whose
    entries are appended as output tags after the standard four; it is not called if the function
    failed to lower. reboxing_test.rs uses it to emit a candidates tag computed from before.

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@eytan-starkware reviewed 1 file and resolved 1 discussion.
Reviewable status: all files reviewed (commit messages unreviewed), all discussions resolved (waiting on orizi).

@eytan-starkware eytan-starkware left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

:lgtm:

@eytan-starkware reviewed 19 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on orizi).

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@orizi reviewed all commit messages.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on orizi).

@orizi
orizi added this pull request to the merge queue Aug 4, 2026
Merged via the queue into main with commit b16220a Aug 4, 2026
55 checks passed
@orizi
orizi deleted the orizi/08-02-_refactor_unify_lowering_optimization_golden_tests_into_a_single_before_after_phase_test_runner branch August 4, 2026 11:05
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.

4 participants