Skip to content

Commit 6bcadbb

Browse files
committed
Let hoists report emptiness instead of comparing rendered text
The while handler inferred "nothing hoisted" from render() returning its input unchanged -- true today, but any formatting change in render() would silently flip every plain do-while into the exit-check lowering. new_hoist() exposes is_empty() and render() reuses it. Review finding (fable-final-review.md #6); no behavior change.
1 parent b92efe7 commit 6bcadbb

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

R/r2f-aab-core.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ new_hoist <- function(scope) {
2020

2121
has_block <- function() !is.null(block_scope)
2222

23+
# TRUE when render(code) would return `code` unchanged: nothing emitted,
24+
# no block-scoped temporaries declared.
25+
is_empty <- function() !length(hoisted) && !has_block()
26+
2327
ensure_block_scope <- function() {
2428
if (is.null(block_scope)) {
2529
block_scope <<- scope_new_child(scope, "block")
@@ -42,7 +46,7 @@ new_hoist <- function(scope) {
4246

4347
render <- function(code) {
4448
code <- str_split_lines(code)
45-
if (!length(hoisted) && !has_block()) {
49+
if (is_empty()) {
4650
return(str_flatten_lines(code))
4751
}
4852

@@ -65,6 +69,7 @@ new_hoist <- function(scope) {
6569
list(
6670
emit = emit,
6771
declare_tmp = declare_tmp,
72+
is_empty = is_empty,
6873
render = render
6974
),
7075
parent = emptyenv()

R/r2f-control-flow.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ r2f_handlers[["while"]] <- function(args, scope, ..., hoist = NULL) {
8282
# (`{` bodies already isolate each statement.)
8383
body <- r2f(args[[2]], scope, ..., hoist = NULL)
8484
check_pending_parallel_consumed(scope)
85-
exit_check <- glue("if (.not. ({cond})) exit")
86-
cond_code <- cond_hoist$render(exit_check)
87-
if (identical(as.character(cond_code), as.character(exit_check))) {
85+
if (cond_hoist$is_empty()) {
8886
# nothing hoisted: keep the plain do-while form
8987
return(Fortran(glue(
9088
"do while ({cond})
@@ -93,6 +91,7 @@ r2f_handlers[["while"]] <- function(args, scope, ..., hoist = NULL) {
9391
"
9492
)))
9593
}
94+
cond_code <- cond_hoist$render(glue("if (.not. ({cond})) exit"))
9695
Fortran(glue(
9796
"do
9897
{indent(cond_code)}

0 commit comments

Comments
 (0)