[R-pkg-devel] Altrep header, MSVC, and STRUCT_SUBTYPES macro

David Cortes d@v|d@corte@@r|ver@ @end|ng |rom gm@||@com
Wed May 15 18:54:37 CEST 2024


I'm seeing some issues using R Altrep classes when compiling a package
with the MSVC compiler on windows.

While CRAN doesn't build windows binaries with this compiler, some
packages such as Arrow and LightGBM have had some success in building
their R packages with MSVC outside of CRAN, in order to enable
functionalities that MinGW doesn't support.

Some R headers do not work out-of-the-box with MSVC, but can be made to
work by using alternative declarations of data structures which the
headers provide. For example, defining the macro 'R_LEGACY_RCOMPLEX'
switches to an alternate declaration of R's complex type, which
compiles just fine under msvc, while the regular declaration doesn't
compile.

I am now trying to use Altrep classes in a package. The code compiles
without errors under MSVC, but executing code that involves returning
Altrep objects leads to segfaults and memory corruptions, even though
it works fine under other compilers.

I see the R Altrep header has this section:
#define STRUCT_SUBTYPES
#ifdef STRUCT_SUBTYPES
# define R_SEXP(x) (x).ptr
# define R_SUBTYPE_INIT(x) { x }
  typedef struct { SEXP ptr; } R_altrep_class_t;
#else
# define R_SEXP(x) ((SEXP) (x))
# define R_SUBTYPE_INIT(x) (void *) (x)
  typedef struct R_altcls *R_altrep_class_t;
#endif

Link:
https://github.com/wch/r-source/blob/531d0fd05d3bdc0f8a0350e3ecd3e101d315ab59/src/include/R_ext/Altrep.h#L33

If I manually edit the R header to remove the definition of
'STRUCT_SUBTYPES', leading to the second definition of
'R_altrep_class_t' being used, then things work as expected when the
package is compiled with MSVC (no segfaults and no memory corruptions).

Unfortunately, the Altrep header forcibly defines the macro right there
before the #ifdef. I am guessing that the alternative defintion was
offered there in order for users to try it out with problematic
compilers, but it doesn't seem to be configurable like
'R_LEGACY_RCOMPLEX'.

Is there some way that I can get an R package to use the alternative
declaration of 'R_altrep_class_t' when including that header, or
otherwise some other solution for using Altrep classes under MSVC?



More information about the R-package-devel mailing list