Skip to content

fix(linters): safely resolve reflect.DeepEqual function aliases - #67636

Merged
pelikhan merged 1 commit into
mainfrom
pelikhan-reflect-deepequal-alias-detection
Oct 11, 2026
Merged

pelikhan merged 1 commit into
mainfrom
pelikhan-reflect-deepequal-alias-detection

Conversation

@pelikhan

Copy link
Copy Markdown
Collaborator

Summary

Closes #66436.

Current main already follows function-value initializers and asserts the issue's original alias fixture. That implementation can incorrectly flag aliases after reassignment. This change completes the detection safely:

  • Resolve actual types.Func objects and track aliases using type-object identity, including alias chains, parentheses, renamed imports, and dot imports.
  • Conservatively reject aliases with reassignment (including short declarations, range loops, and closures), address escapes, missing initializers, or package scope. No flow-sensitive claims are made for calls before later writes.
  • Expand positive and negative regression fixtures, including unrelated functions, shadowing, method values, and alias-specific nolint suppression.
  • Document alias-resolution boundaries. CI enforcement status, coverage gating, and diagnostic text remain unchanged.

Validation

  • go test ./pkg/linters/reflect-deepequal-usage/... ./pkg/linters/internal/astutil/... ./pkg/linters/jsonmarshalignoredeerror/... ./pkg/linters -count=1 passed.
  • Build, Go formatting, standard and custom Go lint, schema freshness, and workflow drift checks passed.
  • make agent-report-progress ran impacted tests successfully; its lint job initially encountered another session's golangci-lint lock. Retried the remaining gate with make agent-report-progress-no-test and a session-local TMPDIR; passed without rerunning test-unit.
  • make fmt completed Go formatting but its repository-wide JavaScript formatter failed. Reverted only its unrelated formatting side effects; scoped validation passed and no JavaScript changes are included.

Reject mutable and escaped aliases using type-object identity, and cover alias chains, import forms, reassignment, and unrelated functions. Keep reflectdeepequalusage CI enforcement unchanged.

Co-authored-by: Copilot App <[email protected]>
@pelikhan
pelikhan marked this pull request as ready for review October 11, 2026 14:57
Copilot AI balanced review requested due to automatic review settings October 11, 2026 14:57
@pelikhan
pelikhan merged commit 0ce2c18 into main Oct 11, 2026
53 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

🔎 PR Code Quality Reviewer is reviewing code quality for this pull request...

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

✅ Design Decision Gate 🏗️ completed the design decision gate check. See the comment below for the result and any generated ADR draft.

🏗️ ADR gate enforced by Design Decision Gate 🏗️

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

✅ Ponytail Reviewer completed successfully!

Lean already. Ship.

Generated by Ponytail Reviewer for #67636

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

✅ Test Quality Sentinel completed test quality analysis.

No test files were added or modified in this PR. Test Quality Sentinel skipped.

🧪 Test quality analysis by Test Quality Sentinel

@github-actions

github-actions Bot commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

🧠 Matt Pocock Skills Reviewer has completed the skills-based review. ✅

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer

Copilot AI 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.

🟢 Approval recommended

The implementation is conservative, type-aware, and supported by comprehensive positive and negative regression fixtures.

0 open findings

What changed in this PR

Safely extends package-call resolution to detect immutable local aliases of reflect.DeepEqual without false positives from mutable aliases.

Changes:

  • Resolves function identities through aliases, renamed imports, and dot imports.
  • Rejects reassigned, escaped, uninitialized, and package-level aliases.
  • Adds regression coverage and documents resolution boundaries.
File Description
pkg/​linters/​internal/​astutil/​astutil.go Adds conservative, type-aware alias resolution.
pkg/​linters/​reflect-deepequal-usage/​testdata/​src/​a/​a.go Tests valid local alias forms.
pkg/​linters/​reflect-deepequal-usage/​testdata/​src/​a/​import_alias.go Tests renamed imports.
pkg/​linters/​reflect-deepequal-usage/​testdata/​src/​a/​dot_import.go Tests dot imports.
pkg/​linters/​reflect-deepequal-usage/​testdata/​src/​b/​b.go Tests unsupported and mutable aliases.
pkg/​linters/​README.md Documents alias-resolution boundaries.

🧠 Review effort: Balanced


💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

@github-actions

Copy link
Copy Markdown
Contributor

🏗️ Design Decision Gate — ADR Required

This PR makes significant changes to core business logic (197 new lines in business-logic directories, threshold 100) but does not have a linked Architecture Decision Record (ADR).

📄 Draft ADR committed: docs/adr/67636-resolve-function-aliases-by-type-object-identity.md — review and complete it before merging.

🔒 This PR cannot merge until an ADR is linked in the PR body.

🔎 Decision inferred from the diff
  • Decision: resolve linter function aliases via *types.Func object identity in astutil.PackageCall/packageFunction, and conservatively refuse to resolve aliases that are reassigned, have their address taken, lack an initializer, or live at package scope.
  • Driver: syntactic name-based alias matching mis-resolves renamed/dot imports and method values and reports diagnostics for aliases that are later reassigned — false positives are costly in a CI-gating analyzer.
  • Alternatives: keep syntactic matching; add flow-sensitive SSA (buildssa) resolution; drop alias support entirely.
  • Consequences: correct under renaming/shadowing and no reassignment false positives, at the cost of missed true positives before a later write, no package-level alias support, and a per-alias whole-pass mutability scan.
📋 What to do next
  1. Review the draft ADR committed to your branch — it was generated from the PR diff
  2. Complete the missing sections — add context the AI couldn't infer, refine the decision rationale, and list real alternatives you considered
  3. Commit the finalized ADR to docs/adr/ on your branch
  4. Reference the ADR in this PR body by adding a line such as:

    ADR: ADR-67636: Resolve Linter Function Aliases by Type-Object Identity With Conservative Mutability Rejection

Once an ADR is linked in the PR body, this gate will re-run and verify the implementation matches the decision.

📋 Michael Nygard ADR Format Reference

An ADR must contain these four sections to be considered complete:

  • Context — What is the problem? What forces are at play?
  • Decision — What did you decide? Why?
  • Alternatives Considered — What else could have been done?
  • Consequences — What are the trade-offs (positive and negative)?

All ADRs are stored in docs/adr/ as Markdown files numbered by PR number.

🏗️ ADR gate enforced by Design Decision Gate 🏗️ · pi · opus50 · 43.5 AIC · ⌖ 53.9 AIC · ⊞ 1.8K · ◷
Comment /review to run again

@github-actions github-actions Bot 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.

Skills-Based Review 🧠

Applied /diagnosing-bugs and /codebase-design.

The core alias-resolution fix is solid: it resolves types.Func objects (not just types.Object names), tracks mutation via assignment/short-decl/range/address-escape, and correctly restricts resolution to local (non-package-scope) variables. I traced through all 13 new negative fixtures in b.go (reassignment, conditional reassignment, later reassignment before a later write, uninitialized alias, closure reassignment, address escape, range reassignment, alias chains through a mutated base, shadowing, package-scope alias, method values, unrelated parameter) and the 4 positive fixtures (declared alias, parenthesized alias chain, multiple aliases, closure capture) plus the new dot_import.go and import_alias.go cases — all match the described conservative semantics.

📋 Key Themes & Highlights

Key Themes

  • Correctness: root cause of the original bug (alias detection breaking on reassignment) is properly addressed via type-identity tracking and conservative mutation detection, not a narrow symptom patch.
  • Test coverage: regression fixtures are unusually thorough for an AST-matching change — this is exactly the kind of coverage /diagnosing-bugs and /tdd look for.
  • Minor performance note (left as inline comment): packageFunctionAlias walks every file in the package per alias lookup, and alias chains compound that walk per indirection level. Not a correctness issue, just worth considering before this pattern is reused in more linters.

Positive Highlights

  • ✅ Switched from string/name-based matching to types.Func/types.Object identity, eliminating a class of false positives/negatives from shadowing or renamed imports.
  • ✅ Explicit, well-named negative tests for each unsafe case (reassignment, address escape, uninitialized var, package scope) rather than relying on absence of a positive match.
  • ✅ Documentation (README.md, doc comments) updated to match the new, more conservative semantics.

@copilot please address the review comments above.

🧠 Reviewed using Matt Pocock's skills by Matt Pocock Skills Reviewer · copilot · sonnet50 · 60.8 AIC · ⌖ 14.6 AIC · ⊞ 10.3K
Comment /matt to run again

seen[obj] = true
var initializer ast.Expr
mutable := false
for _, file := range pass.Files {

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.

[/codebase-design] packageFunctionAlias re-walks every file in the package on each call, and resolving an alias chain recurses back through packageFunction into packageFunctionAlias, repeating the full per-package AST walk at each indirection level. This is O(aliases × package size) rather than a single pass, and this helper is shared by multiple linters (jsonmarshalignoredeerror, reflectdeepequalusage), so the cost multiplies across call sites.

💡 Possible mitigation

Consider memoizing per-object alias resolution results (e.g. keyed on pass + types.Object), or scoping the walk to the enclosing function/file instead of all pass.Files, so repeated alias lookups in a hot package don't re-walk the whole file set.

Not a correctness issue — the regression fixtures (reassignment, range, address escape, closures, package scope) look thorough and the detection logic is sound. Just worth noting before this pattern gets copied into more linters.

@copilot please address this.

@github-actions

Copy link
Copy Markdown
Contributor

🎉 This pull request is included in a new release.

Release: v0.91.8

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.

reflectdeepequalusage: function-value alias bypasses detection, gap documented by its own test fixture

2 participants