Rcpp icon indicating copy to clipboard operation
Rcpp copied to clipboard

Consider masking definition of Rf_error()?

Open kevinushey opened this issue 2 years ago • 18 comments
trafficstars

Somewhat related to a recent post on R-devel, some users will still try to use Rf_error() in C++ code using Rcpp. However, (as is documented) C++ destructors won't run when a longjmp occurs in cases like this. For example:

#include <Rcpp.h>
using namespace Rcpp;

struct A {
    A() { Rprintf("A()\n"); }
    ~A() { Rprintf("~A()\n"); }
};

// [[Rcpp::export]]
SEXP uhoh() {
    A a;
    Rf_error("Oops!");
}

/*** R
uhoh()
*/

In this example, ~A() is never printed.

We could consider masking the definition of Rf_error() in these contexts. For example, something like:

#define Rf_error(...) \
    static_assert(false, "Use of Rf_error() in C++ contexts is unwise: consider using Rcpp::stop() instead.");

There might also be a way to provide our own definition of Rf_error() that "masks" the version provided by R, but I wasn't able to find something immediately obvious.

kevinushey avatar Jan 31 '23 00:01 kevinushey

Tough. It is a clear issue, and one that is a little tricky to explain / document.

I am little burned out from the recent transition dances for Rcpp, BH, and the still-ongoing one for RcppArmadillo. But if you want to drive a test across 2600 reverse depends, and then tangle with all packages that still call Rf_error ...

Is it worth it? Dunno. Should we maybe consider implementing Rf_error() in terms of Rcpp::stop() ?

eddelbuettel avatar Jan 31 '23 01:01 eddelbuettel

I will try to revisit this issue after the release, given that I got the (long-running) RcppArmadillo transition out of the way.

eddelbuettel avatar Dec 31 '23 17:12 eddelbuettel