Skip to content

Commit e08a72a

Browse files
author
maechler
committed
from 88060: environment(<primitive) <- <not_NULL> now deprecated + no-op
git-svn-id: https://svn.r-project.org/R/trunk@88067 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 5dad8f4 commit e08a72a

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/library/base/man/environment.Rd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% File src/library/base/man/environment.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2012 R Core Team
3+
% Copyright 1995-2012, 2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{environment}
@@ -67,6 +67,8 @@ env.profile(env)
6767

6868
The replacement form sets the environment of the function or formula
6969
\code{fun} to the \code{value} given.
70+
Note that \code{\link{primitive}} functions \code{fun} have no environment and
71+
trying to set it to a non-\code{NULL} value is deprecated.
7072

7173
\code{is.environment(obj)} returns \code{TRUE} if and only if
7274
\code{obj} is an \code{environment}.

src/main/builtin.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ attribute_hidden SEXP do_bodyCode(SEXP call, SEXP op, SEXP args, SEXP rho)
290290
#define simple_as_environment(arg) (IS_S4_OBJECT(arg) && (TYPEOF(arg) == OBJSXP) ? R_getS4DataSlot(arg, ENVSXP) : arg)
291291

292292

293+
// environment(fun)
293294
attribute_hidden SEXP do_envir(SEXP call, SEXP op, SEXP args, SEXP rho)
294295
{
295296
checkArity(op, args);
@@ -300,16 +301,15 @@ attribute_hidden SEXP do_envir(SEXP call, SEXP op, SEXP args, SEXP rho)
300301
else return getAttrib(CAR(args), R_DotEnvSymbol);
301302
}
302303

304+
// environment(fun) <- <env>
303305
attribute_hidden SEXP do_envirgets(SEXP call, SEXP op, SEXP args, SEXP rho)
304306
{
305-
SEXP env, s = CAR(args);
306-
307307
checkArity(op, args);
308-
check1arg(args, call, "x");
309-
310-
env = CADR(args);
308+
/* check1arg(args, call, "x"); as it had no effect: should be "fun" */
309+
SEXP s = CAR(args),
310+
env = CADR(args);
311311

312-
if (TYPEOF(CAR(args)) == CLOSXP
312+
if (TYPEOF(s) == CLOSXP
313313
&& (isEnvironment(env) ||
314314
isEnvironment(env = simple_as_environment(env)) ||
315315
isNull(env))) {
@@ -321,12 +321,17 @@ attribute_hidden SEXP do_envirgets(SEXP call, SEXP op, SEXP args, SEXP rho)
321321
s = duplicate(s);
322322
if (TYPEOF(BODY(s)) == BCODESXP)
323323
/* switch to interpreted version if compiled */
324-
SET_BODY(s, R_ClosureExpr(CAR(args)));
324+
SET_BODY(s, R_ClosureExpr(s));
325325
SET_CLOENV(s, env);
326326
}
327327
else if (isNull(env) || isEnvironment(env) ||
328328
isEnvironment(env = simple_as_environment(env)))
329-
setAttrib(s, R_DotEnvSymbol, env);
329+
{
330+
if(!isNull(env) && isPrimitive(s)) // temporary, to become error()
331+
warning(_("setting environment(<primitive function>) is not possible and trying it is deprecated"));
332+
else
333+
setAttrib(s, R_DotEnvSymbol, env);
334+
}
330335
else
331336
error(_("replacement object is not an environment"));
332337
return s;

tests/reg-tests-1e.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,12 @@ stopifnot(identical(attr(t3, "specials"),
19411941
## was unchanged y ~ x1 + (x2 | f) + (x3 | g)
19421942

19431943

1944+
## setting environment(<primitive>) -- was mutilating the base object (in R <= 4.4.x)
1945+
s <- sum ; assertWarnV(environment(s) <- baseenv())
1946+
environment(s) <- NULL # no warning (and no effect)
1947+
r <- return; assertWarnV(environment(r) <- baseenv())
1948+
## then an error for about one day; now is deprecated (and no longer mutating).
1949+
19441950

19451951

19461952
## keep at end

0 commit comments

Comments
 (0)