Skip to content

Commit fcc1af1

Browse files
authored
Merge 5cb2a95 into da5976a
2 parents da5976a + 5cb2a95 commit fcc1af1

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# teal.code 0.6.0.9001
22

3+
### Bug fixes
4+
5+
* Fix an infinite recursion happening when lhs contains two or more symbols occurring in the rhs of the same call.
6+
37
# teal.code 0.6.0
48

59
### Enhancements

R/utils-get_code_dependency.R

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ extract_dependency <- function(parsed_code) {
364364
#' @keywords internal
365365
#' @noRd
366366
graph_parser <- function(x, graph) {
367+
# x occurrences (lhs)
367368
occurrence <- vapply(
368369
graph, function(call) {
369370
ind <- match("<-", call, nomatch = length(call) + 1L)
@@ -372,20 +373,21 @@ graph_parser <- function(x, graph) {
372373
logical(1)
373374
)
374375

376+
# x-dependent objects (rhs)
375377
dependencies <- lapply(graph[occurrence], function(call) {
376378
ind <- match("<-", call, nomatch = 0L)
377379
call[(ind + 1L):length(call)]
378380
})
379381
dependencies <- setdiff(unlist(dependencies), x)
380382

381-
if (length(dependencies) && any(occurrence)) {
382-
dependency_ids <- lapply(dependencies, function(dependency) {
383-
graph_parser(dependency, graph[1:max(which(occurrence))])
384-
})
385-
sort(unique(c(which(occurrence), unlist(dependency_ids))))
386-
} else {
387-
which(occurrence)
388-
}
383+
dependency_occurrences <- lapply(dependencies, function(dependency) {
384+
# track down dependencies and where they occur on the lhs in previous calls
385+
last_x_occurrence <- max(which(occurrence))
386+
reduced_graph <- utils::head(graph[seq_len(last_x_occurrence)], -1)
387+
c(graph_parser(dependency, reduced_graph), last_x_occurrence)
388+
})
389+
390+
sort(unique(c(which(occurrence), unlist(dependency_occurrences))))
389391
}
390392

391393

tests/testthat/test-qenv_get_code.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,3 +951,10 @@ testthat::test_that("original formatting and comments are preserved when express
951951
expr <- parse(text = code, keep.source = TRUE)
952952
testthat::expect_identical(get_code(eval_code(qenv(), expr)), code)
953953
})
954+
955+
testthat::test_that("extracting code doesn't fail when lhs contains two or more symbols occurring in rhs", {
956+
code <- "l <- list(a = 1, b = 2)
957+
class(l) <- c('new class', class(l))" # l depends on class and class depends on l
958+
q <- eval_code(qenv(), code)
959+
testthat::expect_silent(get_code(q, names = "l"))
960+
})

0 commit comments

Comments
 (0)