Skip to content

Commit d20a5d3

Browse files
author
maechler
committed
setting attributes on primitives is an error now
git-svn-id: https://svn.r-project.org/R/trunk@88032 00db46b3-68df-0310-9c12-caf00c1e9a41
1 parent 9982649 commit d20a5d3

File tree

4 files changed

+28
-7
lines changed

4 files changed

+28
-7
lines changed

doc/NEWS.Rd

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@
3535
\item .
3636
}
3737
}
38+
39+
\subsection{BUG FIXES}{
40+
\itemize{
41+
\item Setting \code{\link{attributes}} on primitive functions is an
42+
error now; previously, it modified without copying, as noticed by
43+
\I{Henrik Bengtsson} on the R-devel mailing list.
44+
}
45+
}
46+
3847
}
3948
\section{\Rlogo CHANGES IN R 4.5.0}{
4049
%% \subsection{SIGNIFICANT USER-VISIBLE CHANGES}{
@@ -781,7 +790,7 @@
781790

782791
\item \code{trace(coerce, ..)} now works correctly, fixing \PR{18823}
783792
thanks to \I{Mikael Jagan}.
784-
793+
785794
\item \command{R CMD check} now also reports bad symbols in
786795
package shared objects linked in from local static libraries
787796
(\PR{18789}).

src/library/base/man/Primitive.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/Primitive.Rd
22
% Part of the R package, https://www.R-project.org
3-
% Copyright 1995-2018 R Core Team
3+
% Copyright 1995-2025 R Core Team
44
% Distributed under GPL 2 or later
55

66
\name{Primitive}
@@ -26,6 +26,8 @@
2626

2727
All primitive functions are in the base namespace.
2828

29+
You cannot set \code{\link{attributes}} on primitive functions.
30+
2931
This function is almost never used: \code{`name`} or, more carefully,
3032
\code{\link{get}(name, envir = baseenv())} work equally well and do
3133
not depend on knowing which functions are primitive (which does change

src/main/attrib.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,15 +1071,14 @@ static SEXP as_char_simpl(SEXP val1)
10711071

10721072
if (inherits(val1, "factor")) /* mimic as.character.factor */
10731073
return asCharacterFactor(val1);
1074-
1075-
if (!isString(val1)) { /* mimic as.character.default */
1074+
if (isString(val1))
1075+
return val1;
1076+
// else mimic as.character.default :
10761077
SEXP this2 = PROTECT(coerceVector(val1, STRSXP));
10771078
SET_ATTRIB(this2, R_NilValue);
10781079
SET_OBJECT(this2, 0);
10791080
UNPROTECT(1);
10801081
return this2;
1081-
}
1082-
return val1;
10831082
}
10841083

10851084

@@ -1349,6 +1348,8 @@ attribute_hidden SEXP do_attributesgets(SEXP call, SEXP op, SEXP args, SEXP env)
13491348
/* Do checks before duplication */
13501349
if (!isNewList(attrs))
13511350
error(_("attributes must be a list or NULL"));
1351+
if (isPrimitive(object))
1352+
error(_("Cannot modify attributes on primitive functions"));
13521353
int i, nattrs = length(attrs);
13531354
if (nattrs > 0) {
13541355
names = getAttrib(attrs, R_NamesSymbol);

tests/reg-tests-1e.R

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,16 @@ if(englishMsgs)
19081908
"invalid type (list) for variable 'mpg'",
19091909
"object 'count' not found",
19101910
"invalid type (closure) for variable 'count'")))
1911-
## the last one differed
1911+
## the last one differed
1912+
1913+
1914+
## Setting attributes on primitive functions .. [Henrik Bengtsson, R-devel]
1915+
stopifnot(is.primitive(sum))
1916+
msum <- sum
1917+
assertErrV(void <- structure(sum, foo = TRUE))
1918+
assertErrV(attributes(msum) <- list(foo = NA))
1919+
## both examples, the first a special case of the 2nd, did not error, but *modified* the base::sum primitive
1920+
19121921

19131922

19141923
## keep at end

0 commit comments

Comments
 (0)