From 8640ad71a9d4976996132c8598aae7f2eb5d0656 Mon Sep 17 00:00:00 2001 From: Hadley Wickham Date: Thu, 27 Jul 2023 07:15:56 -0500 Subject: [PATCH] Use minimal defer() instead of local_bindings() Fixes #1089 --- R/coerce.R | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/R/coerce.R b/R/coerce.R index 0fcb1494..624b819b 100644 --- a/R/coerce.R +++ b/R/coerce.R @@ -23,10 +23,18 @@ deprecate_to_char <- function(type) { ) } + +# Can rewrite after https://github.com/r-lib/rlang/issues/1643 local_deprecation_user_env <- function(user_env = caller_env(2), frame = caller_env()) { - local_bindings( - deprecation_user_env = user_env, - .env = the, - .frame = frame - ) + + old <- the$deprecation_user_env + the$deprecation_user_env <- user_env + defer(the$deprecation_user_env <- old, frame) } + +# Lightweight equivalent of withr::defer() +defer <- function(expr, env = caller_env(), after = FALSE) { + thunk <- as.call(list(function() expr)) + do.call(on.exit, list(thunk, TRUE, after), envir = env) +} +