[Rd] Is structure(NA, class = c("def", "condition")) a valid 'condition' object?

Ivan Krylov |kry|ov @end|ng |rom d|@root@org
Sun Oct 26 15:31:55 CET 2025


On Tue, 7 Oct 2025 16:42:12 -0700
Henrik Bengtsson <henrik.bengtsson using gmail.com> wrote:

> I think structure(NA, class = c("def", "condition")) is a valid
> 'condition' object. Am I wrong?

For what it's worth, there are a few places in base R where error and
warning conditions are assumed to contain $call and $message elements:

cnd2 <- structure(NA, class = c("cnd2", "error", "condition"))
conditionMessage.cnd2 <- \(...) "my message"
conditionCall.cnd2 <- \(...) NULL

# These work well:
cnd2
# <cnd2: my message>
stop(cnd2)
# Error: my message

# These fail:
tools::assertError(stop(cnd2), 'cnd2', verbose=TRUE)
# Error in error[[1L]]$message : $ operator is invalid for atomic
# vectors
tools::assertWarning(
 warning(
  structure(cnd2, class = c('cnd2', 'warning', 'condition'))
 ),
 'cnd2', verbose=TRUE
)
# Error in warning[[1L]]$message : $ operator is invalid for atomic
# vectors
(\(x = stop(cnd2)) ls.str())()
# x : Error in o$message : $ operator is invalid for atomic vectors

A few more similar code paths can be found in 'methods', 'compiler',
and R CMD check using static analysis (and a lot of false positives in
'stats' model objects), but I'm not sure whether it's possible to reach
them with a custom error condition.

Is this worth preparing a patch for?

-- 
Best regards,
Ivan



More information about the R-devel mailing list