Skip to content

Commit

Permalink
Test compatiblity fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
richfitz committed Jan 16, 2025
1 parent 78a9309 commit 5864ddc
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
8 changes: 4 additions & 4 deletions R/parse_compat.R
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ parse_compat_fix_output_self <- function(expr, call) {
rlang::is_call(expr$value[[2]], "output") &&
!isTRUE(expr$value[[3]])
if (is_output_expr) {
lhs <- expr$value[[2]]
lhs <- expr$value[[2]][[2]]
rhs <- expr$value[[3]]
rewrite <-
(is.symbol(lhs) &&
Expand All @@ -261,10 +261,10 @@ parse_compat_fix_output_self <- function(expr, call) {
identical(lhs[[2]], rhs[[2]]))
if (rewrite) {
original <- expr$value
if (is_call(lhs, "[[")) {
expr$value[[2]] <- expr$value[[2]][[2]]
}
expr$value[[3]] <- TRUE
if (rlang::is_call(lhs, "[")) {
expr$value[[2]][[2]] <- lhs[[2]]
}
expr <- parse_add_compat(expr, "output_self", original)
}
}
Expand Down
4 changes: 3 additions & 1 deletion R/parse_system.R
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ parse_system_overall <- function(exprs, call) {

is_output_flag <- vlapply(exprs[is_output], function(x) isTRUE(x$rhs$expr))
is_output_expr <- !is_output_flag
nms <- lapply(exprs[is_output], function(x) x$lhs$name)

## Rewrite expressions in output(x) <- expr style to just drop the
## special output bit now, and treat them as normal expressions.
Expand Down Expand Up @@ -644,7 +645,8 @@ parse_system_arrays <- function(exprs, call) {
err <- vlapply(exprs[i], function(x) {
is.null(x$lhs$array) &&
!identical(x$special, "parameter") &&
!identical(x$special, "delay")
!identical(x$special, "delay") &&
!(identical(x$special, "output") && isTRUE(x$rhs$expr))
})
if (any(err)) {
src <- lapply(exprs[i][err], "[[", "src")
Expand Down
20 changes: 20 additions & 0 deletions tests/testthat/test-generate.R
Original file line number Diff line number Diff line change
Expand Up @@ -2805,3 +2805,23 @@ test_that("output <- TRUE version generates same code", {

expect_equal(generate_dust_system(dat2), generate_dust_system(dat1))
})


test_that("cope with array output", {
dat1 <- odin_parse({
initial(x[]) <- 0
deriv(x[]) <- x[i]
a[] <- x[i] + 1
output(a) <- TRUE
dim(x, a) <- 5
})

dat2 <- odin_parse({
initial(x[]) <- 0
deriv(x[]) <- x[i]
output(a[]) <- x[i] + 1
dim(x, a) <- 5
})

expect_equal(generate_dust_system(dat2), generate_dust_system(dat1))
})
27 changes: 27 additions & 0 deletions tests/testthat/test-parse-compat.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,30 @@ test_that("disallow parsing interpolation to slice", {
"Drop arrays from lhs of assignments from 'interpolate()'",
fixed = TRUE)
})


test_that("warn about old-style output assignments", {
w <- expect_warning(
odin_parse({
initial(x) <- 0
deriv(x) <- 1
a <- x + 1
output(a) <- a
}),
"Use `TRUE` on rhs for 'output(x) <- x' expressions",
fixed = TRUE)
})


test_that("warn about old-style output assignments in arrays", {
w <- expect_warning(
odin_parse({
initial(x) <- 0
deriv(x) <- 1
a[] <- x + 1
output(a[]) <- a[i]
dim(a) <- 1
}),
"Use `TRUE` on rhs for 'output(x) <- x' expressions",
fixed = TRUE)
})

0 comments on commit 5864ddc

Please sign in to comment.