Skip to content

Commit 2e64b61

Browse files
authored
trap use of minus in indexing in nf code (#1561)
* Fix misspelling in an error msg. * Error trap use of minus in indexing (1525). * Fix check for minus indexing to allow subtraction. * Fix check for minus indexing to allow subtraction (2).
1 parent 5174f37 commit 2e64b61

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

packages/nimble/R/genCpp_sizeProcessing.R

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,9 @@ sizeConcatenate <- function(code, symTab, typeEnv) { ## This is two argument ver
577577
## overall strategy is to separate runs of scaalrs and non-scalars
578578
## also in C++ we don't take arbitrary arguments. Instead we chain together calls in groups of 4
579579
## e.g. c(a1, a2, a3, a4, a5) will become c( c(a1, a2, a3, a4), a5)
580+
581+
if(!length(code$args))
582+
stop("`c()` must have at least one argument")
580583

581584
## first puzzle is with nimC(scalar1, scalar2, vector1, scalar3)
582585
## we need to extract the runs of scalars like (scalar1, scalar2), so they can be packed up in an object together.
@@ -2719,6 +2722,12 @@ sizeIndexingBracket <- function(code, symTab, typeEnv) {
27192722
## This is deprecated:
27202723
if(code$args[[1]]$type == 'symbolNumericList') return(c(asserts, sizemvAccessBracket(code, symTab, typeEnv)))
27212724

2725+
minuses <- sapply(code$args, function(x) inherits(x, 'exprClass') &&
2726+
exists('name', x) && x$name == "-")
2727+
if(any(minuses))
2728+
if(any(sapply(code$args[minuses], function(x) length(x$args) == 1)))
2729+
stop("use of 'minus' indexing found in `", safeDeparse(code$expr), "` cannot be compiled")
2730+
27222731
## Iterate over arguments, lifting any logical indices into which()
27232732
## e.g. X[i, bool] becomes X[i, Interm1], with Interm1 <- which(bool) as an assert.
27242733
for(i in seq_along(code$args)) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,25 @@ test_that("conflicted nimbleFunction names trapped", {
11101110

11111111
})
11121112

1113+
test_that("Error trap 'minus' indexing.", {
1114+
testMinusIdx <- nimbleFunction(
1115+
run = function(x = double(0)) {
1116+
ans <- x[-3]
1117+
return(ans)
1118+
returnType(double(1))
1119+
} )
1120+
expect_error(cf <- compileNimble(testMinusIdx), "use of 'minus' indexing")
1121+
1122+
testMinusIdx <- nimbleFunction(
1123+
run = function(x = double(2)) {
1124+
ans <- x[5, -c(1,4)]
1125+
return(ans)
1126+
returnType(double(1))
1127+
} )
1128+
expect_error(cf <- compileNimble(testMinusIdx), "use of 'minus' indexing")
1129+
})
1130+
1131+
11131132
nimbleOptions(allowNFobjInModel = currentOption)
11141133

11151134
nimbleOptions(verbose = nimbleVerboseSetting)

0 commit comments

Comments
 (0)