Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions R/pkg/R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,14 @@ processClosure <- function(node, oldEnv, defVars, checkedFuncs, newEnv) {
funcList <- mget(nodeChar, envir = checkedFuncs, inherits = F,
ifnotfound = list(list(NULL)))[[1]]
found <- sapply(funcList, function(func) {
ifelse(identical(func, obj), TRUE, FALSE)
ifelse(
identical(func, obj) &&
# Also check if the parent environment is identical to current parent
identical(parent.env(environment(func)), func.env),
TRUE, FALSE)
})
if (sum(found) > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline. The change would lead to dead loop. We should make sure newEnv contains the (cleaned) node.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the dead loop case when the same function with the same environments is recursively called in the closure?

# If function has been examined, ignore.
# If function has been examined ignore
break
}
# Function has not been examined, record it and recursively clean its closure.
Expand Down
9 changes: 9 additions & 0 deletions R/pkg/tests/fulltests/test_utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ test_that("cleanClosure on R functions", {
actual <- get("y", envir = env, inherits = FALSE)
expect_equal(actual, y)

# Test for combination for nested and sequenctial functions in a closure
f1 <- function(x) x + 1
f2 <- function(x) f1(x) + 2
userFunc <- function(x) { f1(x); f2(x) }
cUserFuncEnv <- environment(cleanClosure(userFunc))
expect_equal(length(cUserFuncEnv), 2)
innerCUserFuncEnv <- environment(cUserFuncEnv$f2)
expect_equal(length(innerCUserFuncEnv), 1)

# Test for function (and variable) definitions.
f <- function(x) {
g <- function(y) { y * 2 }
Expand Down