Skip to content

Commit 17678d5

Browse files
authored
Merge pull request #852 from stan-dev/auto-format-suggestion
suggest format method after error due to old syntax
2 parents 22ee5fc + 480a775 commit 17678d5

4 files changed

Lines changed: 70 additions & 9 deletions

File tree

R/model.R

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -529,26 +529,24 @@ compile <- function(quiet = TRUE,
529529
cpp_options[["USER_HEADER"]] <- wsl_safe_path(absolute_path(user_header))
530530
stanc_options[["allow-undefined"]] <- TRUE
531531
private$using_user_header_ <- TRUE
532-
}
533-
else if (!is.null(cpp_options[["USER_HEADER"]])) {
534-
if(!is.null(cpp_options[["user_header"]])) {
532+
} else if (!is.null(cpp_options[["USER_HEADER"]])) {
533+
if (!is.null(cpp_options[["user_header"]])) {
535534
warning('User header specified both via cpp_options[["USER_HEADER"]] and cpp_options[["user_header"]].', call. = FALSE)
536535
}
537536

538537
user_header <- cpp_options[["USER_HEADER"]]
539538
cpp_options[["USER_HEADER"]] <- wsl_safe_path(absolute_path(cpp_options[["USER_HEADER"]]))
540539
private$using_user_header_ <- TRUE
541-
}
542-
else if (!is.null(cpp_options[["user_header"]])) {
540+
} else if (!is.null(cpp_options[["user_header"]])) {
543541
user_header <- cpp_options[["user_header"]]
544542
cpp_options[["user_header"]] <- wsl_safe_path(absolute_path(cpp_options[["user_header"]]))
545543
private$using_user_header_ <- TRUE
546544
}
547545

548546

549-
if(!is.null(user_header)) {
547+
if (!is.null(user_header)) {
550548
user_header <- absolute_path(user_header) # As mentioned above, just absolute, not wsl_safe_path()
551-
if(!file.exists(user_header)) {
549+
if (!file.exists(user_header)) {
552550
stop(paste0("User header file '", user_header, "' does not exist."), call. = FALSE)
553551
}
554552
}
@@ -688,8 +686,12 @@ compile <- function(quiet = TRUE,
688686
)
689687
)
690688
if (is.na(run_log$status) || run_log$status != 0) {
691-
stop("An error occured during compilation! See the message above for more information.",
692-
call. = FALSE)
689+
err_msg <- "An error occured during compilation! See the message above for more information."
690+
if (grepl("auto-format flag to stanc", run_log$stderr)) {
691+
format_msg <- "\nTo fix deprecated or removed syntax please see ?cmdstanr::format for an example."
692+
err_msg <- paste(err_msg, format_msg)
693+
}
694+
stop(err_msg, call. = FALSE)
693695
}
694696
if (file.exists(exe)) {
695697
file.remove(exe)
@@ -932,6 +934,28 @@ CmdStanModel$set("public", name = "check_syntax", value = check_syntax)
932934
#'
933935
#' @examples
934936
#' \dontrun{
937+
#'
938+
#' # Example of fixing old syntax
939+
#' # real x[2] --> array[2] real x;
940+
#' file <- write_stan_file("
941+
#' parameters {
942+
#' real x[2];
943+
#' }
944+
#' model {
945+
#' x ~ std_normal();
946+
#' }
947+
#' ")
948+
#'
949+
#' # set compile=FALSE then call format to fix old syntax
950+
#' mod <- cmdstan_model(file, compile = FALSE)
951+
#' mod$format(canonicalize = list("deprecations"))
952+
#'
953+
#' # overwrite the original file instead of just printing it
954+
#' mod$format(canonicalize = list("deprecations"), overwrite_file = TRUE)
955+
#' mod$compile()
956+
#'
957+
#'
958+
#' # Example of removing unnecessary whitespace
935959
#' file <- write_stan_file("
936960
#' data {
937961
#' int N;

man/model-method-format.Rd

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters {
2+
real x[3];
3+
}
4+
model {
5+
x ~ normal(0, 1);
6+
}

tests/testthat/test-model-compile.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ test_that("compile errors are shown", {
174174
)
175175
})
176176

177+
test_that("compile suggests using format to fix old syntax", {
178+
stan_file <- testing_stan_file("old_array_syntax")
179+
expect_error(
180+
cmdstan_model(stan_file),
181+
"To fix deprecated or removed syntax please see ?cmdstanr::format for an example.",
182+
fixed = TRUE
183+
)
184+
})
185+
177186
test_that("dir arg works for cmdstan_model and $compile()", {
178187
tmp_dir <- tempdir()
179188
tmp_dir_2 <- tempdir()

0 commit comments

Comments
 (0)