Sitelet https://github.com/starkware-libs/cairo/pull/10018
Skip to content

fix(formatter): sort use self::... items by their full path compare_names("self", "self") returned Greater, violating sort reflexivity. Multiple use self::x; statements were therefore ordered only on the leading self segment and never recursed t - #10018

Merged
orizi merged 1 commit into
mainfrom
orizi/06-01-fix_formatter_sort_use_self_._items_by_their_full_path_compare_names_self_self_returned_greater_violating_sort_reflexivity._multiple_use_self_x_statements_were_therefore_ordered_only_on_the_leading_self_segment_and_never_r
Jun 1, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-01-fix_formatter_sort_use_self_._items_by_their_full_path_compare_names_self_self_returned_greater_violating_sort_reflexivity._multiple_use_self_x_statements_were_therefore_ordered_only_on_the_leading_self_segment_and_never_r

Conversation

@orizi

@orizi orizi commented Jun 1, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Fixes a bug in compare_names where "self" compared against itself did not return Ordering::Equal, causing use self::* imports to not be sorted correctly among themselves.


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

Why is this change needed?

The compare_names function handled "self" in ordering rules relative to other keywords (super, crate) but was missing an explicit case for ("self", "self"). Without it, the comparison fell through to the catch-all rules, which could produce an incorrect ordering (e.g., Ordering::Greater instead of Ordering::Equal), preventing use self::* imports from being sorted alphabetically.


What was the behavior or documentation before?

Multiple use self::* imports were not sorted correctly by the formatter. For example:

use self::e;
use self::c;
use self::a;
use self::d;
use self::b;

would not be reordered.


What is the behavior or documentation after?

use self::* imports are now sorted alphabetically:

use self::a;
use self::b;
use self::c;
use self::d;
use self::e;

Related issue or discussion (if any)

N/A


Additional context

A new test case (sort_self_use) was added with input and expected output files to cover this scenario.

compare_names("self", "self") returned Greater, violating sort
  reflexivity. Multiple `use self::x;` statements were therefore ordered
  only on the leading `self` segment and never recursed to compare the rest
  of the path, leaving them unsorted. Add a `("self", "self") => Equal`
  arm so equal leading segments fall through to the next segment.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

@orizi
orizi marked this pull request as ready for review June 1, 2026 07:35
@cursor

cursor Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Small comparator fix in the formatter with a dedicated test; no runtime, auth, or data-path impact.

Overview
Fixes use-statement sorting when multiple imports share the same self:: prefix.

compare_names now treats ("self", "self") as equal, so the formatter can compare the next path segment and alphabetize use self::… lines (e.g. e, c, a → a…e). A sort_self_use formatter test locks in the behavior.

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

@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 4 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on eytan-starkware).

@orizi
orizi added this pull request to the merge queue Jun 1, 2026
Merged via the queue into main with commit 53bb4fa Jun 1, 2026
54 checks passed
@orizi
orizi deleted the orizi/06-01-fix_formatter_sort_use_self_._items_by_their_full_path_compare_names_self_self_returned_greater_violating_sort_reflexivity._multiple_use_self_x_statements_were_therefore_ordered_only_on_the_leading_self_segment_and_never_r branch June 1, 2026 09:34
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.

3 participants