Skip to content

Propagate errors from recursive calls in sort - #1

Closed
diracdeltafunk wants to merge 1 commit into
developmentfrom
sort-propagate-errors
Closed

diracdeltafunk wants to merge 1 commit into
developmentfrom
sort-propagate-errors

Conversation

@diracdeltafunk

@diracdeltafunk diracdeltafunk commented Oct 30, 2026 •

Copy link
Copy Markdown
Collaborator

Description

sort and rsort drop an error raised by a ? method whenever the failing comparison happens inside one of quicksort's recursive calls. subsort (in d/actors3.d) returns the error from each comparison it makes itself, but ignores the values returned by its recursive calls, so the caller gets a partially sorted list and no error. This change returns those errors too. basicsort already restores its saved state when subsort returns an error, so nothing else is needed.

T = new Type of List
T ? T := (a, b) -> if set {a#0, b#0} === set {1, 2} then error "incomparable" else a#0 ? b#0
sort apply({4,3,2,1,0}, i -> new T from {i})

Before: {T{2}, T{1}, T{0}, T{3}, T{4}}, with no error.
After: error: incomparable.

Testing

Added a case to tests/normal/sort.m2: a ? method that fails on one pair of adjacent elements. Quicksort compares adjacent elements at some level of the recursion whatever pivots it picks, so the test doesn't depend on pivot choice. Both new assertions fail on 1.26.06 and pass with this change.

Built on macOS (arm64, CMake) and ran tests/normal, with no new failures. (command.m2 also fails on 1.26.06; gb-hilbert.m2 and gbf4.m2 call restart, so they can't run under the ctest command and were skipped.)

Release notes

sort and rsort no longer ignore errors raised by the comparison method.

AI disclosure

  • 0 — No AI tools were used or consulted.
  • 1 — AI was used only for lookups, explanations, or advice; it wrote no code or text in this pull request.
  • 2 — AI made small edits, which I reviewed.
  • 3 — AI drafted substantial parts, which I reviewed.
  • 4 — AI produced most or all of the content, which I reviewed thoroughly.
  • 5 — AI produced the content with minimal human review.

Tools and models used: Claude Code (Claude Opus 5.5). The bug was first flagged in an automated audit by Codex.
How AI was used, and on which parts: Claude reproduced the bug, wrote the fix, the test, and this description.
How I verified the result: TODO (Ben): fill in after review

License

  • My changes use the existing license of the affected files or package.
  • I release my new material under the GNU GPL, version 2 or later.
  • Other (public domain, another license, or not applicable); details below.

Checklist

  • This pull request targets the development branch.
  • I have reviewed my own changes.
  • Documentation and tests are updated as needed.

🤖 Generated with Claude Code

subsort returned errors from the comparisons it made itself but ignored
the values of its recursive calls, so an error from a ? method was
dropped whenever it happened below the top level, and sort and rsort
returned a partially sorted list instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@diracdeltafunk

Copy link
Copy Markdown
Collaborator Author

Moved upstream: Macaulay2#4767

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.

1 participant