Skip to content

Make the call of a dispatched method deparsable (#783) - #2

Closed
jjjermiah wants to merge 2 commits into
mainfrom
fix/783-deparse-condition-call
Closed

jjjermiah wants to merge 2 commits into
mainfrom
fix/783-deparse-condition-call

Conversation

@jjjermiah

@jjjermiah jjjermiah commented Oct 14, 2026 •

Copy link
Copy Markdown
Owner

Fixes RConsortium#783.

Errors raised in an S7 method record a call that holds the dispatched promises. When a dispatch argument is an S7 object, deparse() on released R forces the promise and fails:

library(S7)
Foo <- new_class("Foo")
g <- new_generic("g", "x")
method(g, Foo) <- function(x) stop("boom")

cnd <- tryCatch(g(Foo()), error = identity)
deparse(conditionCall(cnd))
#> Error in deparse(conditionCall(cnd)) :
#>   'S4SXP': should not happen - please report

Only an already-forced S7 value (an OBJSXP without the S4 bit) breaks deparse(). So method_call_() now leaves every other argument in the recorded call exactly as before, and routes only those values through .... The call deparses on any R version, and existing tracebacks are unchanged:

deparse(conditionCall(cnd))
#> [1] "`method(g, Foo)`(...)"

g2 <- new_generic("g2", "x", function(x, y, ...) S7_dispatch())
method(g2, Foo) <- function(x, y, ...) stop("boom")
# g2(Foo(), 4) records: `method(g2, Foo)`(y = 4, ...)
# g(1) records:         `method(g, class_double)`(x = 1, ...)   (as on main)

How it works:

  • The routed arguments keep their tags and promises. R only exposes a DOTSXP as the ... of a frame, so the C code calls a small dispatch_dots <- function(...) environment() with them, plus the generic's own ..., and takes its ....
  • The method is still evaluated in the generic's frame, so parent.frame() doesn't change. The generic's own ... is swapped for the new one during the call and restored with R_ExecWithCleanup(), including on error.
  • Tagged promises are kept, so substitute(), missing(), nargs() and match.call() behave as before. New tests pin these, and they also pass on main.
  • When no argument is an S7 value, dispatch takes the same path as main, with no extra call and no ... swap.

Trade-offs:

  • Non-dispatch arguments are left in the call as they are, unforced. One that holds an S7 object (g3(Foo(), Foo(), z = Foo()) with z not used for dispatch) still breaks deparse(). Checking would mean forcing the promise, which main never does.
  • Routing costs about 0.2µs per dispatch (S7 argument 1.85µs on main, 2.05µs here). Dispatch with no S7 argument benchmarks the same as main.

Tested on R 4.3.3: the full suite passes (2018 expectations, previously 1996), the existing traceback snapshots are untouched, and R CMD check --no-manual is OK. Not yet run on newer R or with the R-devel non-API checks.

The method call recorded by S7_dispatch() held the dispatch arguments as
forced promises. deparse(conditionCall(cnd)) and rlang::as_label() force
those promises and then fail on released versions of R with "'S4SXP':
should not happen", because the promise value is an S7 object.

Collect the arguments into a `...` (a DOTSXP, so tags and promises are
kept and substitute() still sees the original expressions) and call the
method as `method(generic, Class)`(...). The method is still evaluated in
the generic's frame, with the generic's own `...` restored afterwards.

Tracebacks no longer show argument values.
@coderabbitai

coderabbitai Bot commented Oct 14, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • 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

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

…tium#783)

Routing every argument through `...` made tracebacks lose argument
values. Only a forced S7 value (an OBJSXP without the S4 bit) breaks
deparse(), so route just those, and keep every other argument in the
recorded call as on main. If nothing needs routing, dispatch is
unchanged and skips the dispatch_dots() call.

Non-dispatch arguments are not forced to check, so one holding an S7
object still breaks deparse().
@jjjermiah

Copy link
Copy Markdown
Owner Author

Closing this fork draft. RConsortium#785 merged the fix for the R 4.2/4.3 method-call deparse bug, and upstream issue RConsortium#783 is closed. Modern R already deparses the call. Keeping a second, divergent fork implementation open would add no path to shipping the fix.

@jjjermiah jjjermiah closed this Nov 1, 2026
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.

bug: deparse(conditionCall(cnd)) fails for errors raised in S7 methods

1 participant