Skip to content

refactor(checker): unify function-entry and condition-diagnostics duplication - #145

Merged
sims1253 merged 4 commits into
mainfrom
refactor/infer-function-walks
Sep 1, 2026
Merged

sims1253 merged 4 commits into
mainfrom
refactor/infer-function-walks

Conversation

@sims1253

Copy link
Copy Markdown
Owner

Summary

Collapses the remaining large duplications inside crates/ry-checker/src/infer/. Diagnostics and inferred types are unchanged. This PR is stacked on #137 and should merge after it.

Changes

  • One function-entry path. enter_function_body replaces the two ~55-line body walks in walk_stmt: the Stmt::Assign { value: Expr::Function } arm and the Stmt::FunctionDef arm. The only line that differed is the S3 dispatch context, installed for the assignment form alone, so the helper takes the binding name plus a named_binding flag.
  • One condition-diagnostics emitter. emit_condition_diagnostics replaces the RY001/RY003/RY002 block repeated in the Stmt::If arm, the Stmt::While arm, and infer_if_expr. A ConditionContext picks the message noun and carries the RY002 case. On the While/RY002 divergence: it is intentional, not an omission. The rule table scopes RY002 to if conditions while RY001 and RY003 name if / while, and the While arm has never carried RY002 back to the first commit that introduced these rules. The helper preserves that split through ConditionContext::allows_length_rule rather than widening it. The binop scan was left alone: it checks RY032 after infer_binop, not RY100 after inferring a condition, so it is not the same shape.
  • Cached defuser set. check_lazy_default_reachability rebuilt a HashSet from DEFUSING_CALLS plus every fn-table entry on every call, once per function per fixpoint iteration. The set now lives on the Checker behind an Arc and is built on first use. collect_fns clears it, which matches the existing invalidation story: collection is the only pass that writes fn_table.fns, and every table reset is immediately followed by a collection round.
  • One ArgumentMatch per call. argument_eval_mode re-ran match_arguments per argument, making infer_call's argument loop quadratic. eval_mode_for_arg consumes a match built once. bound_argument_index_matched does the same for the assertion path, which matched up to three times per call site. argument_eval_mode and bound_argument_index remain as self-matching wrappers for the call sites outside infer/.
  • infer_data_frame calls longest_arg_length instead of repeating its combine ladder.
  • infer_rep resolves times and each through one rep_count helper instead of writing the negative-literal and non-literal early returns out twice.
  • list_binding_origin is inlined into its single caller.
  • Doc comment moved. The structure(x, class = ...) comment sat on infer_injected_call, which handles typeshed injection. It now sits on infer_structure_call, the function it describes, and infer_injected_call got a doc comment of its own.

Verification

  • cargo fmt -p ry-checker --check
  • cargo clippy -p ry-checker --all-targets -- -D warnings
  • cargo test -p ry-checker — full suite, 594 tests across 16 binaries, all green.

…helpers

Mechanical prefix of #130. Semantics are unchanged; the full crate
suite passes untouched.

- apply_narrowing loses its unused _has_else parameter and returns
  early for Narrowing::None, so a no-narrowing if skips the match-arm
  work while still returning owned scopes.
- merge_branch_bindings takes the branch scopes by reference; the
  caller no longer clones both just to keep reading them.
- infer_if_expr no longer clones the branch scopes it consumes last.
- One is_comparison predicate replaces the six-operator list written
  out at six sites.
- One concrete_json_mode mapping replaces the JsonMode-to-Mode table
  written out three times.
- extract_builtin_type_narrowing shares its inherits target lookup and
  first-argument identifier extraction; apply_narrowing shares the
  narrow-away-from-null sequence repeated across four arms.
- try_s3_binop_dispatch and try_s3_unary_dispatch share the class
  vector walk.
- equality_list_leaf_type loses its dead op parameter; unused span and
  name parameters are underscore-named at the signature instead of
  silenced in the body; a mid-file use moves to the top of misc.rs.
… emitter

Second half of #130. Semantics are unchanged; the full crate suite passes
untouched.

- enter_function_body unifies the two ~55-line function-body walks in
  walk_stmt. The Assign arm passes its binding name plus a flag for the S3
  dispatch context, the only line that differed.
- emit_condition_diagnostics replaces the RY001/RY003/RY002 block written
  three times across walk_stmt's If and While arms and infer_if_expr. A
  ConditionContext selects the message noun and carries the While arm's
  missing RY002 case unchanged: the rule table scopes RY002 to `if`
  conditions, and that arm has never had it.
- trusted_defusers is cached on the Checker instead of being rebuilt from
  DEFUSING_CALLS plus the whole fn table on every function per fixpoint
  iteration. collect_fns clears it, matching the only pass that writes
  fn_table.fns.
- infer_call builds one ArgumentMatch per call. eval_mode_for_arg and
  bound_argument_index_matched consume it, so the argument loop is no
  longer O(n^2) and the assertion path matches once instead of three
  times. argument_eval_mode stays for the schema-call site outside infer/.
- infer_data_frame calls longest_arg_length instead of repeating its
  ladder. infer_rep resolves times and each through rep_count.
- list_binding_origin is inlined into its single caller.
- The structure(x, class=...) doc comment moves from infer_injected_call,
  which handles typeshed injection, to infer_structure_call, which it
  describes.
@coderabbitai

coderabbitai Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

Next included review available in 53 minutes.

View limit details

Limit details: You’ve used the included review currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Learn how review limits work.

Review configuration:

⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Team

Run ID: b6e417ab-e353-4575-a468-ad995865da6d

📥 Commits

Reviewing files that changed from the base of the PR and between 196850f and bc916fa.

📒 Files selected for processing (9)
  • CHANGELOG.md
  • crates/ry-checker/src/collect.rs
  • crates/ry-checker/src/infer/call.rs
  • crates/ry-checker/src/infer/construct.rs
  • crates/ry-checker/src/infer/misc.rs
  • crates/ry-checker/src/infer/mod.rs
  • crates/ry-checker/src/infer/pipe.rs
  • crates/ry-checker/src/lib.rs
  • crates/ry-checker/src/tests/quoting_data_mask.rs

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@pullfrog pullfrog 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.

Important

One must-address finding inline: the new trusted_defusers cache can serve a stale, pre-propagation snapshot of the quoting flags to the pass-3 RY098 walk. Everything else in the unification verified equivalent.

Reviewed changes

  • Unified function entry. enter_function_body replaces the two ~55-line body walks in walk_stmt; verified verbatim against both removed arms — the S3 dispatch install (assignment form only, now the named_binding flag), diagnostic_parameter_type, record_scope, and the push/pop ordering all preserve the old asymmetry exactly.
  • Unified condition diagnostics. emit_condition_diagnostics + ConditionContext replace the RY001/RY003/RY002 blocks in the Stmt::If arm, the Stmt::While arm, and infer_if_expr; message nouns are byte-identical and the intentional While/RY002 split survives via allows_length_rule. No bare RY001/RY002/RY003 emit sites remain outside the helper.
  • Cached defuser set. Checker::trusted_defusers caches the RY098 defuser set behind an Arc, cleared by collect_fns. Both fn_table reset sites (check, check_with_scope) are indeed followed by a collection round — but a second invalidation seam exists inside the fixpoint; see the inline comment.
  • One ArgumentMatch per call. eval_mode_for_arg and bound_argument_index_matched consume a match built once; match_arguments guarantees param_for_arg.len() == args.len(), so the bounds-check swap is equivalent, and param_names() ordering matches params. The kept argument_eval_mode/bound_argument_index wrappers still serve their single-use callers in nse.rs, call.rs, and misc.rs.
  • Smaller folds. infer_data_frame → longest_arg_length (fold verified identical to the removed ladder, Unknown absorbing); infer_rep → rep_count (negative-literal and non-literal → Length::Unknown preserved); list_binding_origin inlined with zero remaining references; the structure(x, class = ...) doc comment now sits on infer_structure_call with its claims still matching that code, and infer_injected_call got an accurate doc of its own.

Verification: cargo check -p ry-checker --all-targets clean, cargo test -p ry-checker --lib 460 passed, cargo test -p ry-checker --test corpus snapshots unchanged — no diagnostic drift on the fixtures.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using openai-compatible/glm-5.3 | 𝕏

Comment thread crates/ry-checker/src/infer/misc.rs Resolved
@sims1253
sims1253 deleted the branch main August 31, 2026 21:20
@sims1253 sims1253 closed this Aug 31, 2026
@sims1253 sims1253 reopened this Aug 31, 2026
@sims1253
sims1253 changed the base branch from refactor/infer-clone-dedup to main August 31, 2026 21:25
…on-walks

# Conflicts:
#	CHANGELOG.md
#	crates/ry-checker/src/infer/binop.rs

@pullfrog pullfrog 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.

ℹ️ No new issues from the merge — one minor doc suggestion below. The open trusted_defusers thread from the previous review is unaffected by this delta and still stands.

Reviewed changes

This run covers the delta since the prior review: the merge of origin/main (11 commits, notably #137 landing in base) into the branch — no new refactor work, and the merge introduced no conflict resolutions.

  • Rebased the diff onto post-#137 main: the stacked #137 content left this PR's diff once it landed in base, and the merge left every refactor hunk untouched — collect.rs and infer/{call,construct,misc,mod,pipe}.rs are byte-identical to the reviewed 0062328 state; lib.rs differs only by main's rewording of an unrelated comment above the severity-filter builders.
  • Absorbed main's checker-side inflow around the refactor: binop.rs dropped a dead symbol == "?" dispatch guard, nse.rs renamed an unused span parameter, and project.rs replaced prev_return_slots with a has_prev_emit flag plus a with_transitive_callers extraction — none of them touch the refactor's seams (S3 dispatch install still only honors named bindings; project pass 3 still gets a fresh with_shared_tables checker).
  • Re-ran the gates at the merge head: cargo check -p ry-checker --all-targets clean; cargo test -p ry-checker --lib 460 passed, --test corpus 5 passed, --test metamorphic 16 passed — #132's new pipe-placeholder tests now exercise the unified emit_condition_diagnostics path and stay green.

ℹ️ Nitpicks

  • After the merge dropped #137's stacked entries into base, this PR ships a perf-relevant refactor with no CHANGELOG.md entry of its own: nothing under Unreleased → Changed covers the quadratic-to-linear argument matching or the cached defuser set, while #137 documented its same-genre change ("Less work per if during checking"). A matching entry would keep the section consistent.

Pullfrog  | ⚠️ this action is pinned to a commit SHA, which freezes the cleanup step — switch to @v0 or keep the SHA fresh with Dependabot | Fix it ➔ | View workflow run | Using openai-compatible/glm-5.3 | 𝕏

The cache was built during fixpoint iteration 1 (nested function literals
fire check_lazy_default_reachability in pass 2) and survived the quoting
propagators, so the pass-3 RY098 walk could read a pre-propagation
snapshot. Both propagators and signature seeding now clear it whenever
they write quoting/defused flags. Regression test pins that a cached set
matches the post-propagation table.
@sims1253

Copy link
Copy Markdown
Owner Author

Addressed both reviews in bc916fa.

Stale trusted_defusers cache. I took the mutation-site option: both propagators drop the cache when they set params[].quoting/defused, and seed_caller_visible_signatures drops it when a seed wrote flags (seed_caller_visible_signature now reports whether it applied). A single clear at the top of run_fixpoint_inner would not guarantee a fresh pass 3: the cache is built mid-loop (iteration 1's refine_fn_return walks enter nested literals), the build is first-use-only, so later iterations reuse an iteration-1 snapshot through pass 3. Clearing at every writer makes the invariant local. The invariant comments in misc.rs and lib.rs now name the four invalidation sites.

Test outcome. Your exact divergence cannot fire through the public API today. In first_executed_identifier the trusted-defuser branch and the fallback branch both recurse into func only, so a bare g(x) never counts as a guaranteed force either way; only base::/rlang::-qualified abort/stop/warning/message force arguments, and those are not defusers. I verified this on the unfixed code: your scenario stays silent with the stale cache. The set has been inert for RY098 output since f2db1dd. So defuser_cache_reflects_propagated_quoting pins the invariant white-box instead: after the fixpoint, a cached set must contain the generic whose quoting arrived only via S3 propagation. It fails on the pre-fix code (the stale snapshot holds g.foo but not g) and passes with the fix.

Second review. Added the Unreleased → Changed entry for the once-per-call argument matching, the single function-entry walk, and the cached defuser set.

cargo fmt, cargo clippy -p ry-checker --all-targets -- -D warnings, and full cargo test -p ry-checker (461 lib tests) are green.

@pullfrog pullfrog 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.

✅ No new issues found. The trusted_defusers staleness from the earlier review is properly fixed, with regression coverage that provably fails on the pre-fix code.

Reviewed changes

This run covers the delta since the prior review: commit bc916fa, which resolves the open must-address thread about the stale trusted_defusers cache.

  • Dropped the defuser cache when quoting flags move. propagate_s3_generic_quoting and propagate_forwarded_quoting now clear trusted_defusers whenever a round changes the table, and seed_caller_visible_signatures clears it when a seed applied (seed_caller_visible_signature returns bool for this). I traced every writer of params[].quoting/defused — all four sites (collection, both propagators, signature seeding) now invalidate, and both propagators set changed on every actual flag write, so no write can slip past the clear. The remaining uses.defused = true in collect.rs writes a local ParameterUses, not the table.
  • Pinned the fix with a regression test. defuser_cache_reflects_propagated_quoting asserts the S3-propagated generic lands in the cached set and that the full check emits no RY098. Mutation-verified: removing only the S3-propagator's clear trips the cache assertion with g absent while g.foo is present — the test isolates exactly the reported bug, not a tautology.
  • Rewrote the invariant comments. The misc.rs, lib.rs, and collect.rs doc comments now name the real invalidation sites instead of the disproven "frozen once collection ends" claim.
  • Added the missing CHANGELOG entry ("Less duplicate work per call and per function entry" under Unreleased → Changed), addressing the prior run's nit.

Verification: cargo fmt -p ry-checker --check clean, cargo clippy -p ry-checker --all-targets -- -D warnings clean, cargo test -p ry-checker --lib 461 passed (460 + the new test), --test corpus 5 passed, --test metamorphic 16 passed.

Pullfrog  | ⚠️ this action is pinned to a commit SHA, which freezes the cleanup step — switch to @v0 or keep the SHA fresh with Dependabot | View workflow run | Using openai-compatible/glm-5.3 | 𝕏

@pullfrog

pullfrog Bot commented Sep 1, 2026 •

Copy link
Copy Markdown
Contributor

No changes needed. Review 5071491129 has 0 inline threads; its only actionable item is the nitpick asking for a CHANGELOG.md entry covering the perf refactor. That is already satisfied: the review ran at merge head c07f602 (whose CHANGELOG.md indeed lacked the entry), and the follow-up commit bc916fa — now the branch head and already pushed to refactor/infer-function-walks — added the requested bullet under Unreleased → Changed ("Less duplicate work per call and per function entry": argument matching once per call site, single function-entry path, RY098 defuser set built once per collection round). The same commit also drops the trusted-defusers cache when quoting flags move, addressing the trusted_defusers thread left open by the earlier review. Working tree clean, remote up to date.

Task list (4/6 completed)

Pullfrog  | ⚠️ this action is pinned to a commit SHA, which freezes the cleanup step — switch to @v0 or keep the SHA fresh with Dependabot | View workflow run | via Pullfrog | Using openai-compatible/glm-5.3 | 𝕏

@sims1253
sims1253 merged commit 0a7f38e into main Sep 1, 2026
16 checks passed
@sims1253
sims1253 deleted the refactor/infer-function-walks branch September 1, 2026 16:07
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