Skip to content

fix(checker): keep a deep callback nest's return type, so four nested sapply() calls report RY040 again - #912

Merged
sims1253 merged 1 commit into
mainfrom
fix/infer-closure-trailing-expr
Nov 13, 2026
Merged

sims1253 merged 1 commit into
mainfrom
fix/infer-closure-trailing-expr

Conversation

@sims1253

Copy link
Copy Markdown
Owner
 infer_higher_order_call (non-discarding)
-  walk_callback_for_diagnostics(..)                 # walks the literal, drops its type
-  infer_ho_result -> callback_return_type(literal)  # walks it again, discarding,
-                                                    # capped at MAX_CLOSURE_DEPTH (3)
+  walked = walk_callback_for_diagnostics(..)        # the one walk also returns the type
+  infer_ho_result -> walked ?? callback_return_type(named callback)

 walk_body_returns(body)                             # new, shared by signature, callback
   walk_stmt(each statement)                         # and user-fn return inference
+  walk_stmt returns a bare expression's value       # trailing expression inferred once
-  trailing_return_type(body) re-infers that tail

Four nested sapply() callbacks lost their result type, so v <- sapply(1L, function(a) ... sapply(1L, function(d) "x")); v + 1L missed the RY040 that GNU R's "non-numeric argument to binary operator" calls for. Each callback level was walked once for diagnostics and again, discarding, for its return type. #895 had to cap that second walk at three levels to keep the work linear. Now the diagnostic walk returns the literal's type itself, so every level is walked once and the type survives at any depth. A body's trailing expression now comes from its statement walk instead of being inferred a second time.

Removing the cap alone is not enough. Each level's diagnostic walk would start a fresh uncapped discarding chain, and that is quadratic: lapply took 11, 49 and 119 block walks at depths 2, 4 and 6. The cap stays for discarding walks such as signature building and the user-function fixpoint.

Mechanism: walk_callback_for_diagnostics and infer_ho_result in crates/ry-checker/src/higher_order.rs, plus walk_body_returns in infer/mod.rs. Witness that should change: the issue's example (and the same lines inside a function body) now reports RY040. True positive that stays: lapply(x, function(e) "a")[[i]] + 1 still reports RY040. Adjacent idiom that stays quiet: closure_depth_cap_falls_back_to_opaque (four trailing-return closures stay opaque).

Proof: closure_nesting_work::deep_callback_nest_keeps_return_type failed before the fix (got []) and passes after. nested_closure_work_grows_linearly_with_depth passes, with a new if-tailed sapply shape. The new oracle fixture deep_callback_nest_result.R (must-flag) is backed by Rscript --vanilla, which errors with non-numeric argument to binary operator. There is a new case in testdata/cases/type_inference.R. Release binaries, main ff77e45 against this branch: the example goes from silent to RY040. 24-deep sapply/lapply nests check in 0.02 s. JSON diagnostics with --warn all are byte-identical across all 73 ecosystem-cache packages. ecosystem/run.sh --check passes for both manifests, so the ledgers are unchanged. Local runs: cargo test -p ry-checker --lib (1064 passed), --test corpus, --test oracle (default gate) and cargo fmt --all -- --check. Not run locally: workspace tests, clippy and the full oracle matrix (CI runs them), and a dump-facts comparison.

Closes #896

Reviewed with sol-loop: 1 round with gpt-6.1-sol on high. Sol verdict: approved
Claude Opus 5.5 on behalf of Max

@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

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

Next included review available in 8 minutes.

Check out review usage here.

View limit details

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

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 2d56701d-78fe-416f-9281-506e5f832f77
Commits

Reviewing files that changed from the base of the PR and between d634c2a and 992742c.

Files ignored due to path filters: 1

  • crates/ry-checker/tests/snapshots/corpus__cases_type_inference.snap is excluded by !**/*.snap
Files selected for processing: 7

  • CHANGELOG.md
  • crates/ry-checker/src/collect.rs
  • crates/ry-checker/src/higher_order.rs
  • crates/ry-checker/src/infer/mod.rs
  • crates/ry-checker/src/tests/closure_nesting_work.rs
  • crates/ry-checker/testdata/cases/type_inference.R
  • crates/ry-checker/testdata/oracle/deep_callback_nest_result.R
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

A callback literal's diagnostic walk now also collects its return type,
and higher-order inference uses it instead of a second, discarding walk
of the same literal. That second walk was capped at MAX_CLOSURE_DEPTH, so
four nested sapply() callbacks lost their type and v + 1L missed RY040.
Without the cap it would make every level walk all deeper levels again.

walk_stmt now returns a bare expression statement's value, so a body's
trailing expression is inferred once. walk_body_returns shares that walk
between signature, callback and user-function return inference.

Closes #896
@sims1253
sims1253 force-pushed the fix/infer-closure-trailing-expr branch from 22e1914 to 992742c Compare November 13, 2026 00:40

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

Reviewed changes — the full diff on fix/infer-closure-trailing-expr (8 files), tracing the new return-type flow end to end against the old two-walk design.

  • Single-walk callback typing (higher_order.rs): walk_callback_for_diagnostics now returns Option<Option<RType>> and infer_ho_result prefers that type over a second, discarding callback_return_type walk. The fallback is correctly preserved for pass 2 / discarding contexts and for named (Expr::Ident) callbacks, and fold specs still short-circuit to unknown after the diagnostic walk.
  • Shared body walk (infer/mod.rs, collect.rs): walk_stmt returns a bare trailing expression's value, and the new walk_body_returns derives the implicit return from that single walk (trailing_return_type still handles trailing if / closure-factory / empty bodies). Signature building, callback diagnostics, and refine_fn_return all share it, which is what makes the work linear.
  • Equivalence verified arm by arm: the unreachable filters, is_return_call covering both return``/invisible(types already collected by the walk), dead-code tails, and the%<-%destructure tail all land where the oldtrailing_return_typedid. The literal depth0equals the ambientself.closure_depthat every reachable invocation (the field is only mutated insidewalk_literal_returns, which forces discarding = true), so only the deliberate cap relaxation differs. Skipping invalidate_ops_environment()` on the callback scope is sound for callbacks, which execute synchronously at the lexical call site — and matches the assumption the diagnostics walk has always made.
  • Tests and docs: the new linearity shape (sapply with if tails), the deep_callback_nest_keeps_return_type unit test, the corpus case + snapshot, the R-verified must-flag oracle fixture, and the CHANGELOG entry all pin the fix and its adjacent idioms; block_walks counting is unchanged by the refactor.

CI on this head is green across test, Oracle (R), ecosystem, the perf gate, and Known-gap budget, which backs the byte-identical-diagnostics and no-ledger-drift claims. (Local cargo test was not possible in this runner: toolchain rustc 1.99.1 vs the workspace's required 1.100, which ry-core genuinely uses.)

Pullfrog  | View workflow run | Using glm-5.3 | 𝕏

@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 delta is a pure rebase, not a code change.

Reviewed changes — the incremental delta since the prior pullfrog review at 22e1914.

  • Rebase onto current main only: the new head 992742c sits on d634c2a (the merge of #910) instead of ff77e452. The tree diff between the two heads is exactly #910's content (infer/construct.rs, infer/types.rs, the union-return cases in testdata/cases/type_inference.R, the new union_return_arg_length.R oracle fixture) — none of this PR's own files (higher_order.rs, infer/mod.rs, collect.rs, closure_nesting_work.rs) differ between the two heads.
  • Consequences verified: the corpus case's RY040 moved from line 1504 to 1526 with the file shift, and the snapshot was regenerated accordingly; the new #910 material interleaves cleanly with the added case. CI (test, Oracle (R), ecosystem, perf gate) is green on the new head.

Pullfrog  | View workflow run | Using glm-5.3 | 𝕏

@sims1253
sims1253 merged commit 87ba24a into main Nov 13, 2026
19 of 20 checks passed
@sims1253
sims1253 deleted the fix/infer-closure-trailing-expr branch November 13, 2026 02:26
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.

Infer a callback's trailing expression once, so deep callback nests keep their return type

1 participant