Skip to content

Commit

Permalink
Avoid triggering active bindings in env_binding_are_active() (#1377)
Browse files Browse the repository at this point in the history
* Avoid triggering active bindings in `which_env_binding()`

* NEWS bullet
  • Loading branch information
DavisVaughan authored Mar 14, 2022
1 parent affe978 commit 22fe9e9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# rlang (development version)

* `env_binding_are_active()` no longer accidentally triggers active bindings
(#1376).

# rlang 1.0.2

* Backtraces of parent errors are now reused on rethrow. This avoids
Expand Down
10 changes: 6 additions & 4 deletions src/rlang/env-binding.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ static r_obj* new_binding_types(r_ssize n) {
}

static enum r_env_binding_type which_env_binding(r_obj* env, r_obj* sym) {
if (r_env_binding_is_promise(env, sym)) {
return R_ENV_BINDING_TYPE_promise;
}

if (r_env_binding_is_active(env, sym)) {
// Check for active bindings first, since promise detection triggers
// active bindings through `r_env_find()` (#1376)
return R_ENV_BINDING_TYPE_active;
}

if (r_env_binding_is_promise(env, sym)) {
return R_ENV_BINDING_TYPE_promise;
}

return R_ENV_BINDING_TYPE_value;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-env-binding.R
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ test_that("env_binding_are_active() doesn't force promises", {
expect_identical(env_binding_are_lazy(env), lgl(foo = TRUE))
})

test_that("env_binding_are_active() doesn't trigger active bindings (#1376)", {
env <- env()
env_bind_active(env, foo = ~stop("kaboom"))
expect_no_error(env_binding_are_active(env))
expect_identical(env_binding_are_active(env), lgl(foo = TRUE))
expect_identical(env_binding_are_lazy(env), lgl(foo = FALSE))
})

test_that("env_binding_type_sum() detects types", {
env <- env()
env_bind_active(env, a = ~"foo")
Expand Down

0 comments on commit 22fe9e9

Please sign in to comment.