Skip to content

Commit

Permalink
Merge pull request #831 from stan-dev/informative-error-precompiled
Browse files Browse the repository at this point in the history
Give informative error when exposing stan functions with precompiled model
  • Loading branch information
andrjohns authored Aug 23, 2023
2 parents f028607 + 04e580c commit e4ff5d4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ compile <- function(quiet = TRUE,
private$precompile_cpp_options_ <- NULL
private$precompile_stanc_options_ <- NULL
private$precompile_include_paths_ <- NULL
self$functions$existing_exe <- TRUE
self$exe_file(exe)
return(invisible(self))
} else {
Expand Down Expand Up @@ -586,6 +587,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)
self$functions$existing_exe <- FALSE
if (compile_standalone) {
expose_stan_functions(self$functions, !quiet)
}
Expand Down
4 changes: 4 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -914,6 +914,10 @@ expose_stan_functions <- function(function_env, global = FALSE, verbose = FALSE)
"WSL CmdStan and will not be compiled",
call. = FALSE)
}
if (function_env$existing_exe) {
stop("Exporting standalone functions is not possible with a pre-compiled Stan model!",
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)
Expand Down
21 changes: 21 additions & 0 deletions tests/testthat/test-model-expose-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,24 @@ test_that("Exposing external functions errors before v2.32", {

reset_cmdstan_version()
})

test_that("Exposing functions with precompiled model gives meaningful error", {
skip_if(os_is_wsl())

stan_file <- write_stan_file("
functions {
real a_plus_b(real a, real b) { return a + b; }
}
parameters { real x; }
model { x ~ std_normal(); }
")
mod1 <- cmdstan_model(stan_file, compile_standalone = TRUE)
expect_equal(7.5, mod1$functions$a_plus_b(5, 2.5))

mod2 <- cmdstan_model(stan_file)
expect_error(
mod2$expose_functions(),
"Exporting standalone functions is not possible with a pre-compiled Stan model!",
fixed = TRUE
)
})

0 comments on commit e4ff5d4

Please sign in to comment.