diff --git a/R/model.R b/R/model.R index 2e1c78862..beace3400 100644 --- a/R/model.R +++ b/R/model.R @@ -529,26 +529,24 @@ compile <- function(quiet = TRUE, cpp_options[["USER_HEADER"]] <- wsl_safe_path(absolute_path(user_header)) stanc_options[["allow-undefined"]] <- TRUE private$using_user_header_ <- TRUE - } - else if (!is.null(cpp_options[["USER_HEADER"]])) { - if(!is.null(cpp_options[["user_header"]])) { + } else if (!is.null(cpp_options[["USER_HEADER"]])) { + if (!is.null(cpp_options[["user_header"]])) { warning('User header specified both via cpp_options[["USER_HEADER"]] and cpp_options[["user_header"]].', call. = FALSE) } user_header <- cpp_options[["USER_HEADER"]] cpp_options[["USER_HEADER"]] <- wsl_safe_path(absolute_path(cpp_options[["USER_HEADER"]])) private$using_user_header_ <- TRUE - } - else if (!is.null(cpp_options[["user_header"]])) { + } else if (!is.null(cpp_options[["user_header"]])) { user_header <- cpp_options[["user_header"]] cpp_options[["user_header"]] <- wsl_safe_path(absolute_path(cpp_options[["user_header"]])) private$using_user_header_ <- TRUE } - if(!is.null(user_header)) { + if (!is.null(user_header)) { user_header <- absolute_path(user_header) # As mentioned above, just absolute, not wsl_safe_path() - if(!file.exists(user_header)) { + if (!file.exists(user_header)) { stop(paste0("User header file '", user_header, "' does not exist."), call. = FALSE) } } @@ -688,8 +686,12 @@ compile <- function(quiet = TRUE, ) ) if (is.na(run_log$status) || run_log$status != 0) { - stop("An error occured during compilation! See the message above for more information.", - call. = FALSE) + err_msg <- "An error occured during compilation! See the message above for more information." + if (grepl("auto-format flag to stanc", run_log$stderr)) { + format_msg <- "\nTo fix deprecated or removed syntax please see ?cmdstanr::format for an example." + err_msg <- paste(err_msg, format_msg) + } + stop(err_msg, call. = FALSE) } if (file.exists(exe)) { file.remove(exe) @@ -932,6 +934,28 @@ CmdStanModel$set("public", name = "check_syntax", value = check_syntax) #' #' @examples #' \dontrun{ +#' +#' # Example of fixing old syntax +#' # real x[2] --> array[2] real x; +#' file <- write_stan_file(" +#' parameters { +#' real x[2]; +#' } +#' model { +#' x ~ std_normal(); +#' } +#' ") +#' +#' # set compile=FALSE then call format to fix old syntax +#' mod <- cmdstan_model(file, compile = FALSE) +#' mod$format(canonicalize = list("deprecations")) +#' +#' # overwrite the original file instead of just printing it +#' mod$format(canonicalize = list("deprecations"), overwrite_file = TRUE) +#' mod$compile() +#' +#' +#' # Example of removing unnecessary whitespace #' file <- write_stan_file(" #' data { #' int N; diff --git a/man/model-method-format.Rd b/man/model-method-format.Rd index 57944b589..ad6b4e650 100644 --- a/man/model-method-format.Rd +++ b/man/model-method-format.Rd @@ -46,6 +46,28 @@ model directly back to the file or prints it for inspection. } \examples{ \dontrun{ + +# Example of fixing old syntax +# real x[2] --> array[2] real x; +file <- write_stan_file(" +parameters { + real x[2]; +} +model { + x ~ std_normal(); +} +") + +# set compile=FALSE then call format to fix old syntax +mod <- cmdstan_model(file, compile = FALSE) +mod$format(canonicalize = list("deprecations")) + +# overwrite the original file instead of just printing it +mod$format(canonicalize = list("deprecations"), overwrite_file = TRUE) +mod$compile() + + +# Example of removing unnecessary whitespace file <- write_stan_file(" data { int N; diff --git a/tests/testthat/resources/stan/old_array_syntax.stan b/tests/testthat/resources/stan/old_array_syntax.stan new file mode 100644 index 000000000..aa34f52e9 --- /dev/null +++ b/tests/testthat/resources/stan/old_array_syntax.stan @@ -0,0 +1,6 @@ +parameters { + real x[3]; +} +model { + x ~ normal(0, 1); +} diff --git a/tests/testthat/test-model-compile.R b/tests/testthat/test-model-compile.R index 7da135dd7..f86685a5f 100644 --- a/tests/testthat/test-model-compile.R +++ b/tests/testthat/test-model-compile.R @@ -174,6 +174,15 @@ test_that("compile errors are shown", { ) }) +test_that("compile suggests using format to fix old syntax", { + stan_file <- testing_stan_file("old_array_syntax") + expect_error( + cmdstan_model(stan_file), + "To fix deprecated or removed syntax please see ?cmdstanr::format for an example.", + fixed = TRUE + ) +}) + test_that("dir arg works for cmdstan_model and $compile()", { tmp_dir <- tempdir() tmp_dir_2 <- tempdir()