Skip to content

Commit 289563c

Browse files
authored
Trap use of parentheses in indexing in model code (#1556)
* Fix misspelling in an error msg. * Trap use of () in model indexing code. * Add test for use of parens in model indexing.
1 parent d4c2e06 commit 289563c

3 files changed

Lines changed: 14 additions & 3 deletions

File tree

packages/nimble/R/genCpp_sizeProcessing.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,8 +2801,11 @@ sizeIndexingBracket <- function(code, symTab, typeEnv) {
28012801
}
28022802
next
28032803
} else { ## not dropping a dimension, so the index is non-scalar
2804-
if(isExprClass) ## If it is an expression that is not `:` or blank, then a simple block is not allowed
2805-
if((code$args[[i+1]]$name != ':') && (code$args[[i+1]]$name != "")) simpleBlockOK <- FALSE
2804+
if(isExprClass) { ## If it is an expression that is not `:` or blank, then a simple block is not allowed
2805+
if(code$args[[i+1]]$name == '(')
2806+
stop("detected unexpected use of `(` in model code in `", safeDeparse(code$expr), "`. Parentheses cannot be used in indexing in NIMBLE models")
2807+
if((code$args[[i+1]]$name != ':') && (code$args[[i+1]]$name != "")) simpleBlockOK <- FALSE
2808+
}
28062809
}
28072810
needMap <- TRUE ## If the "next" in if(dropThisDim) {} is always hit, then needMap will never be set to TRUE
28082811

packages/nimble/R/nimbleProject.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ nimbleProjectClass <- setRefClass('nimbleProjectClass',
899899
ans <- nfCppDef$buildCallable(nf, dll = dll, asTopLevel = asTopLevel)
900900
ok <- !is.null(ans)
901901
}
902-
if(!ok) stop("Oops, there is something in this compilation job that doesn\'t fit together. This can happen in some cases if you are trying to compile new pieces into an exising project. If that is the situation, please try including \"resetFunctions = TRUE\" as an argument to compileNimble. Alternatively please try rebuilding the project from the beginning with more pieces in the same call to compileNimble. For example, if you are compiling multiple algorithms for the same model in multiple calls to compileNimble, try compiling them all with one call.", call. = FALSE)
902+
if(!ok) stop("There is something in this compilation job that doesn\'t fit together. This can happen in some cases if you are trying to compile new pieces into an existing project. If that is the situation, please try including \"resetFunctions = TRUE\" as an argument to compileNimble. Alternatively please try rebuilding the project from the beginning with more pieces in the same call to compileNimble. For example, if you are compiling multiple algorithms for the same model in multiple calls to compileNimble, try compiling them all with one call.", call. = FALSE)
903903

904904
ans
905905
},

packages/nimble/tests/testthat/test-models.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,6 +1026,14 @@ test_that("Warning printed when indexing info in user environment.", {
10261026
"Information has been found in the user's environment")
10271027
})
10281028

1029+
test_that("Informative error when using parentheses with model indexing.", {
1030+
code <- nimbleCode({
1031+
y[1,(1:2)] ~ ddirch(alpha[1:2])
1032+
})
1033+
m <- nimbleModel(code)
1034+
expect_error(cm <- compileNimble(m), "detected unexpected use of `\\(` in model code")
1035+
})
1036+
10291037

10301038
options(warn = RwarnLevel)
10311039
nimbleOptions(verbose = nimbleVerboseSetting)

0 commit comments

Comments
 (0)