Skip to content

Commit 8954ad5

Browse files
authored
Remove dependency on R_NamespaceRegistry (#1890)
The R-level `ns_registry_env()` is only used in pkgload and I can work around there. On older R versions it keeps working to facilitate transition. On newer R versions, it now fails. The C-level function can call into R. Note that this is also what the existing `R_FindNamespace()` does (https://github.com/r-devel/r-svn/blob/c709c242bae2aac5018385d24ab2a6a6c6de3ded/src/main/envir.c#L4232). I've opted to call `.getNamespace()` which does not load the namespace if not found (see https://github.com/r-devel/r-svn/blob/c709c242bae2aac5018385d24ab2a6a6c6de3ded/src/library/base/R/namespace.R#L26-L30).
2 parents 7837ec1 + 2c24062 commit 8954ad5

6 files changed

Lines changed: 27 additions & 13 deletions

File tree

‎NEWS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# rlang (development version)
22

3+
* `ns_registry_env()` is defunct in R >= 4.6.0 for compliance with the C API of R.
4+
35
* New type-checking functions exported from rlang: `check_bool()`,
46
`check_string()`, `check_name()`, `check_number_decimal()`,
57
`check_number_whole()`, and `check_data_frame()`. These were

‎R/env-special.R‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,14 @@ env_label <- function(env) {
343343
}
344344

345345
#' Return the namespace registry env
346-
#'
347-
#' Note that the namespace registry does not behave like a normal
348-
#' environment because the parent is `NULL` instead of the empty
349-
#' environment. This is exported for expert usage in development tools
350-
#' only.
351-
#'
346+
#' @description
347+
#' `r lifecycle::badge("defunct")`
352348
#' @keywords internal
353349
#' @export
354350
ns_registry_env <- function() {
351+
if (getRversion() >= "4.6") {
352+
deprecate_stop("`ns_registry_env()` is defunct.")
353+
}
355354
.Call(ffi_ns_registry_env)
356355
}
357356

‎man/ns_registry_env.Rd‎

Lines changed: 1 addition & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/internal/exported.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,11 @@ r_obj* ffi_env_bind_list(r_obj* env, r_obj* names, r_obj* data) {
510510
}
511511

512512
r_obj* ffi_ns_registry_env(void) {
513+
#if R_VERSION < R_Version(4, 6, 0)
513514
return R_NamespaceRegistry;
515+
#else
516+
r_abort("`ns_registry_env()` is defunct as of R 4.6.");
517+
#endif
514518
}
515519

516520

‎src/rlang/decl/env-decl.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@ r_obj* list2env_call;
2828
static
2929
r_obj* missing_prim;
3030

31+
static
32+
r_obj* get_ns_call;
33+
3134
static
3235
void env_coalesce_plain(r_obj* env, r_obj* from, r_obj* nms);

‎src/rlang/env.c‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ r_obj* rlang_ns_env;
66

77

88
r_obj* r_ns_env(const char* pkg) {
9-
r_obj* pkg_sym = r_sym(pkg);
10-
if (!r_env_has(R_NamespaceRegistry, pkg_sym)) {
9+
r_obj* pkg_str = KEEP(r_chr(pkg));
10+
r_obj* ns = r_eval_with_x(get_ns_call, pkg_str, r_envs.base);
11+
FREE(1);
12+
13+
if (ns == r_null) {
1114
r_abort("Can't find namespace `%s`", pkg);
1215
}
1316

14-
return r_env_get(R_NamespaceRegistry, pkg_sym);
17+
return ns;
1518
}
1619

1720
r_obj* r_base_ns_get(const char* name) {
@@ -278,6 +281,9 @@ void r_init_library_env(void) {
278281
r_preserve(remove_call);
279282

280283
r_methods_ns_env = r_parse_eval("asNamespace('methods')", r_envs.base);
284+
285+
get_ns_call = r_parse(".getNamespace(x)");
286+
r_preserve(get_ns_call);
281287
}
282288

283289
r_obj* rlang_ns_env = NULL;
@@ -308,3 +314,6 @@ r_obj* list2env_call = NULL;
308314

309315
static
310316
r_obj* missing_prim = NULL;
317+
318+
static
319+
r_obj* get_ns_call = NULL;

0 commit comments

Comments
 (0)