Skip to content

Commit

Permalink
change missing variable in a nimbleFunction from warning to error (#1379
Browse files Browse the repository at this point in the history
)

* change missing var in nf from warning to error (issue 1365)

* put error on missing var behind a nimble option and reword msg
  • Loading branch information
paciorek authored Dec 22, 2023
1 parent 824b5bc commit 8f7b277
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
11 changes: 7 additions & 4 deletions packages/nimble/R/genCpp_sizeProcessing.R
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,13 @@ exprClasses_setSizes <- function(code, symTab, typeEnv) { ## input code is exprC
code$sizeExprs <- list()
code$toEigenize <- 'maybe'
} else {
warning(paste0("variable '",
code$name,
"' has not been created yet."),
call.=FALSE)
if(nimbleOptions('errorIfMissingNFVariable')) {
stop("variable `",
code$name,
"` is not available.",
call.=FALSE)
} else
messageIfVerbose(" [Warning] Variable `", code$name, "` is not available.")
}
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion packages/nimble/R/options.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ nimbleUserNamespace <- as.environment(list(sessionSpecificDll = NULL))
MCMCwarnUnsampledStochasticNodes = TRUE,
MCMCRJcheckHyperparam = TRUE,
MCMCenableWAIC = FALSE,
useClearCompiledInADTesting = TRUE
useClearCompiledInADTesting = TRUE,
errorIfMissingNFVariable = TRUE
)
)

Expand Down
21 changes: 21 additions & 0 deletions packages/nimble/tests/testthat/test-checkDSL.R
Original file line number Diff line number Diff line change
Expand Up @@ -461,5 +461,26 @@ test_that("nimbleFunctionVirtual works with abstract and non-abstract methods",
expect_true(grepl("hello world", res))
})

test_that("Missing variables in nf code cause error (not warning)", {

test1 <- nimbleFunction(
run = function() {
a <- 7
c <- a+b # warning about 'b' not yet created
return(c)
returnType(double(0))
})
expect_error(compileNimble(test1), "is not available")

test2 <- nimbleFunction(
run = function() {
a <- 7
return(a + b)
returnType(double(0))
})
expect_error(compileNimble(test1), "is not available")
})


options(warn = RwarnLevel)

0 comments on commit 8f7b277

Please sign in to comment.