Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Warning but no error if compile_standalone but no functions found #956

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,11 @@ expose_stan_functions <- function(function_env, global = FALSE, verbose = FALSE)
stop("Exporting standalone functions with external C++ is not available before CmdStan 2.32",
call. = FALSE)
}
if (!is.null(function_env$hpp_code) &&
!any(grepl("[[stan::function]]", function_env$hpp_code, fixed = TRUE))) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andrjohns Is this an ok way to check whether there are any user defined functions?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, looks right to me

warning("No standalone functions found to compile and expose to R!", call. = FALSE)
return(invisible(NULL))
}
require_suggested_package("Rcpp")
if (function_env$compiled) {
if (!global) {
Expand Down
26 changes: 26 additions & 0 deletions tests/testthat/test-model-expose-functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,32 @@ test_that("Functions can be compiled with model", {
)
})

test_that("compile_standalone warns but doesn't error if no functions", {
stan_no_funs_block <- write_stan_file("
parameters {
real x;
}
model {
x ~ std_normal();
}
")
expect_warning(
mod1 <- cmdstan_model(stan_no_funs_block, compile = TRUE, compile_standalone = TRUE, force_recompile = TRUE),
"No standalone functions found to compile and expose to R"
)
checkmate::expect_r6(mod1, "CmdStanModel")

stan_empty_funs_block <- write_stan_file("
functions {
}
")
expect_warning(
mod2 <- cmdstan_model(stan_empty_funs_block, compile = TRUE, compile_standalone = TRUE, force_recompile = TRUE),
"No standalone functions found to compile and expose to R"
)
checkmate::expect_r6(mod2, "CmdStanModel")
})

test_that("rng functions can be exposed", {
skip_if(os_is_wsl())
function_decl <- "functions { real wrap_normal_rng(real mu, real sigma) { return normal_rng(mu, sigma); } }"
Expand Down
Loading