Skip to content

cmp: keep cycle-reference reporting specific to each side - #414

Open
adenzhou1350 wants to merge 3 commits into
google:masterfrom
adenzhou1350:codex/fix-cycle-reference-reporting
Open

adenzhou1350 wants to merge 3 commits into
google:masterfrom
adenzhou1350:codex/fix-cycle-reference-reporting

Conversation

@adenzhou1350

Copy link
Copy Markdown

Fixes #284.

When formatting a single left or right value, the reporter currently checks for cycles against ancestors from both arguments. A pointer that only occurs on the opposite side can therefore truncate the current value too early, leaving indistinguishable removed and inserted lines.

Keep paired ancestors for the combined diff, but use side-specific stack views when formatting individual values. The views preserve ordinary cycle detection without copying the ancestor stacks. Comparison/equality semantics are unchanged.

Validation on Linux/amd64, Go 1.26.9:

  • go test ./... and go test -race ./... pass, with existing golden reports unchanged.
  • The same new public regression fails in both comparison directions on unchanged production source and passes with this patch; equal-cycle control passes in both arms.
  • The original issue example, including unexported fields and AllowUnexported, now expands the left-side nested structure while retaining the right-side back-reference.
  • A separate internal test checks side selection and preservation of the parent stack. It is candidate-only because the old implementation has no split-view helper.
  • Four order-balanced fresh-process samples per arm for a differing pointer-to-struct-array display control retain 1,609 allocations/call in both arms. This is a scoped cost check, not a general performance claim.

Developed and validated with assistance from OpenAI Codex. The upstream Go/OS CI matrix remains to run.

Signed-off-by: Xucheng Zhou <aden1350@outlook.com>
Signed-off-by: Xucheng Zhou <aden1350@outlook.com>

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request refactors the cycle detection logic in go-cmp to correctly handle cross-side cycle references. It changes pointerReferences from a slice to a struct with a stack and a side indicator, and introduces a Split method to project ancestors onto each side of the comparison. This prevents formatting one side from incorrectly stopping at an ancestor that only exists on the other side. Tests are added to verify this behavior. Feedback on the changes suggests adding explicit parentheses to a complex boolean expression in report_references.go to clarify operator precedence and improve readability.

Comment thread cmp/report_references.go Outdated
Signed-off-by: Xucheng Zhou <209601943+adenzhou1350@users.noreply.github.com>
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.

Recursive output poorly reported

1 participant