Skip to content

Commit 6ad35c9

Browse files
committed
Addressed comments
1 parent 10925bf commit 6ad35c9

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

R/pkg/R/utils.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,15 @@ processClosure <- function(node, oldEnv, defVars, checkedFuncs, newEnv) {
543543
funcList <- mget(nodeChar, envir = checkedFuncs, inherits = F,
544544
ifnotfound = list(list(NULL)))[[1]]
545545
found <- sapply(funcList, function(func) {
546-
ifelse(identical(func, obj), TRUE, FALSE)
546+
ifelse(
547+
identical(func, obj) &&
548+
# Also check if the parent environment is identical to current parent
549+
identical(parent.env(environment(func)), func.env),
550+
TRUE, FALSE)
547551
})
548552
if (sum(found) > 0) {
549-
# If function has been examined
550-
if (identical(parent.env(environment(funcList[found][[1]])), func.env)) {
551-
# If the parent environment is identical to current parent
552-
break
553-
}
553+
# If function has been examined ignore
554+
break
554555
}
555556
# Function has not been examined, record it and recursively clean its closure.
556557
assign(nodeChar,

R/pkg/tests/fulltests/test_utils.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ test_that("cleanClosure on R functions", {
113113
# Test for combination for nested and sequenctial functions in a closure
114114
f1 <- function(x) x + 1
115115
f2 <- function(x) f1(x) + 2
116-
user_func <- function(x) { f1(x); f2(x) }
117-
c_user_func_env <- environment(cleanClosure(user_func))
118-
expect_equal(length(c_user_func_env), 2)
119-
inner_c_user_func_env <- environment(c_user_func_env$f2)
120-
expect_equal(length(inner_c_user_func_env), 1)
116+
userFunc <- function(x) { f1(x); f2(x) }
117+
cUserFuncEnv <- environment(cleanClosure(userFunc))
118+
expect_equal(length(cUserFuncEnv), 2)
119+
innerCUserFuncEnv <- environment(cUserFuncEnv$f2)
120+
expect_equal(length(innerCUserFuncEnv), 1)
121121

122122
# Test for function (and variable) definitions.
123123
f <- function(x) {

0 commit comments

Comments
 (0)