Skip to content

Commit e4ff5d4

Browse files
authored
Merge pull request #831 from stan-dev/informative-error-precompiled
Give informative error when exposing stan functions with precompiled model
2 parents f028607 + 04e580c commit e4ff5d4

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

R/model.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,7 @@ compile <- function(quiet = TRUE,
519519
private$precompile_cpp_options_ <- NULL
520520
private$precompile_stanc_options_ <- NULL
521521
private$precompile_include_paths_ <- NULL
522+
self$functions$existing_exe <- TRUE
522523
self$exe_file(exe)
523524
return(invisible(self))
524525
} else {
@@ -586,6 +587,7 @@ compile <- function(quiet = TRUE,
586587
stancflags_standalone <- c("--standalone-functions", stancflags_val, stancflags_combined)
587588
self$functions$hpp_code <- get_standalone_hpp(temp_stan_file, stancflags_standalone)
588589
self$functions$external <- !is.null(user_header)
590+
self$functions$existing_exe <- FALSE
589591
if (compile_standalone) {
590592
expose_stan_functions(self$functions, !quiet)
591593
}

R/utils.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,6 +914,10 @@ expose_stan_functions <- function(function_env, global = FALSE, verbose = FALSE)
914914
"WSL CmdStan and will not be compiled",
915915
call. = FALSE)
916916
}
917+
if (function_env$existing_exe) {
918+
stop("Exporting standalone functions is not possible with a pre-compiled Stan model!",
919+
call. = FALSE)
920+
}
917921
if (function_env$external && cmdstan_version() < "2.32") {
918922
stop("Exporting standalone functions with external C++ is not available before CmdStan 2.32",
919923
call. = FALSE)

tests/testthat/test-model-expose-functions.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,24 @@ test_that("Exposing external functions errors before v2.32", {
175175

176176
reset_cmdstan_version()
177177
})
178+
179+
test_that("Exposing functions with precompiled model gives meaningful error", {
180+
skip_if(os_is_wsl())
181+
182+
stan_file <- write_stan_file("
183+
functions {
184+
real a_plus_b(real a, real b) { return a + b; }
185+
}
186+
parameters { real x; }
187+
model { x ~ std_normal(); }
188+
")
189+
mod1 <- cmdstan_model(stan_file, compile_standalone = TRUE)
190+
expect_equal(7.5, mod1$functions$a_plus_b(5, 2.5))
191+
192+
mod2 <- cmdstan_model(stan_file)
193+
expect_error(
194+
mod2$expose_functions(),
195+
"Exporting standalone functions is not possible with a pre-compiled Stan model!",
196+
fixed = TRUE
197+
)
198+
})

0 commit comments

Comments
 (0)