From 77a476de8a8016e55a65d3de8438dca013fb5079 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 23 Mar 2023 18:41:56 +0200 Subject: [PATCH 1/4] Add version check when exposing external functions --- R/model.R | 1 + R/utils.R | 4 ++++ tests/testthat/test-model-expose-functions.R | 22 ++++++++++++++++++++ 3 files changed, 27 insertions(+) diff --git a/R/model.R b/R/model.R index 1de220b69..ff5b637e4 100644 --- a/R/model.R +++ b/R/model.R @@ -582,6 +582,7 @@ compile <- function(quiet = TRUE, } stancflags_standalone <- c("--standalone-functions", stancflags_val, stancflags_combined) self$functions$hpp_code <- get_standalone_hpp(temp_stan_file, stancflags_standalone) + self$functions$external <- !is.null(user_header) if (compile_standalone) { expose_functions(self$functions, !quiet) } diff --git a/R/utils.R b/R/utils.R index c32f50ea7..8317e293f 100644 --- a/R/utils.R +++ b/R/utils.R @@ -878,6 +878,10 @@ expose_functions <- function(function_env, global = FALSE, verbose = FALSE) { "WSL CmdStan and will not be compiled", call. = FALSE) } + if (function_env$external && cmdstan_version() < "2.32") { + stop("Exporting standalone functions with external C++ is not available before CmdStan 2.32", + call. = FALSE) + } require_suggested_package("Rcpp") require_suggested_package("RcppEigen") require_suggested_package("decor") diff --git a/tests/testthat/test-model-expose-functions.R b/tests/testthat/test-model-expose-functions.R index dfc2f74c4..c5808aad0 100644 --- a/tests/testthat/test-model-expose-functions.R +++ b/tests/testthat/test-model-expose-functions.R @@ -147,3 +147,25 @@ test_that("Overloaded functions give meaningful errors", { expect_error(funmod$expose_functions(), "Overloaded functions are currently not able to be exposed to R! The following overloaded functions were found: fun1, fun3") }) + +test_that("Exposing external functions errors before v2.32", { + tmpfile <- tempfile(fileext = ".hpp") + hpp <- + " + #include + namespace standalone_external_model_namespace { + int rtn_int(int x, std::ostream *pstream__) { return x; } + }" + cat(hpp, file = tmpfile, sep = "\n") + stanfile <- file.path(tempdir(), "standalone_external.stan") + cat("functions { int rtn_int(int x); }\n", file = stanfile) + expect_error({ + cmdstan_model( + stan_file = stanfile, + user_header = tmpfile, + compile_standalone = TRUE + ) + }, + "Exporting standalone functions with external C++ is not available before CmdStan 2.32", + fixed = TRUE) +}) From c112f37f0dbc4c1c2f56e9701fb1c6b297ba6f57 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 23 Mar 2023 18:55:05 +0200 Subject: [PATCH 2/4] Use fixed version in test --- tests/testthat/test-model-expose-functions.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/testthat/test-model-expose-functions.R b/tests/testthat/test-model-expose-functions.R index c5808aad0..27ebd05d3 100644 --- a/tests/testthat/test-model-expose-functions.R +++ b/tests/testthat/test-model-expose-functions.R @@ -149,6 +149,16 @@ test_that("Overloaded functions give meaningful errors", { }) test_that("Exposing external functions errors before v2.32", { + if (getRversion() < '3.5.0') { + dir <- tempdir() + } else { + dir <- tempdir(check = TRUE) + } + install_cmdstan(dir = dir, cores = 2, quiet = FALSE, + overwrite = TRUE, version = "2.31.0", + wsl = os_is_wsl()) + set_cmdstan_path(dir) + tmpfile <- tempfile(fileext = ".hpp") hpp <- " From 552901464700f03fe9817ec6b07861888f2b96f1 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 23 Mar 2023 20:29:05 +0200 Subject: [PATCH 3/4] Fix test failure --- tests/testthat/test-model-expose-functions.R | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/testthat/test-model-expose-functions.R b/tests/testthat/test-model-expose-functions.R index 27ebd05d3..a88ea320b 100644 --- a/tests/testthat/test-model-expose-functions.R +++ b/tests/testthat/test-model-expose-functions.R @@ -157,7 +157,6 @@ test_that("Exposing external functions errors before v2.32", { install_cmdstan(dir = dir, cores = 2, quiet = FALSE, overwrite = TRUE, version = "2.31.0", wsl = os_is_wsl()) - set_cmdstan_path(dir) tmpfile <- tempfile(fileext = ".hpp") hpp <- From 36427554c2f09eeedc8df4b9b4668bd251937dd5 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Thu, 23 Mar 2023 22:12:42 +0200 Subject: [PATCH 4/4] Missed wsl skip --- tests/testthat/test-model-expose-functions.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/test-model-expose-functions.R b/tests/testthat/test-model-expose-functions.R index a88ea320b..3be219e0f 100644 --- a/tests/testthat/test-model-expose-functions.R +++ b/tests/testthat/test-model-expose-functions.R @@ -149,6 +149,8 @@ test_that("Overloaded functions give meaningful errors", { }) test_that("Exposing external functions errors before v2.32", { + skip_if(os_is_wsl()) + if (getRversion() < '3.5.0') { dir <- tempdir() } else {