Bug 15535 - unlist(list(as.raw(11), TRUE)) produces corrupt logical vector
unlist(list(as.raw(11), TRUE)) produces corrupt logical vector
Product: R
Classification: Unclassified
Component: Language
R 3.0.2
ix86 (32-bit) Linux
: P5 minor
Assigned To: R-core
Depends on:
  Show dependency treegraph
Reported: 2013-11-07 20:03 UTC by Alex Bertram
Modified: 2013-11-14 21:39 UTC (History)
1 user (show)

See Also:

Correction and unit test (1.21 KB, patch)
2013-11-07 20:03 UTC, Alex Bertram
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Alex Bertram 2013-11-07 20:03:51 UTC
Created attachment 1509 [details]
Correction and unit test

unlist() promotes raws to logicals with a simple (int) cast, which means that subsequent logical equality checks will fail.

a <- list(as.raw(11), TRUE)
b <- unlist(a, recursive=FALSE)
print(b) # c(TRUE,TRUE)

stopifnot(all(b)) # OK
stopifnot(b == TRUE) # ERROR
stopifnot(identical(b, c(TRUE,TRUE))) #ERROR
as.integer(b) # c(11,1)

N.B. This also seems to be the case for integer vectors in LogicalAnswer, but as far as I can tell this code is unreachable.
Comment 1 Duncan Murdoch 2013-11-14 12:57:44 UTC
This isn't limited to unlist.  You get the same effects from

b <- c(as.raw(11), TRUE)

I'll look into it.
Comment 2 Duncan Murdoch 2013-11-14 13:35:54 UTC
Thanks for the report and fix.

I also fixed the integer case, just in case someone calls it sometime.
Comment 3 Alex Bertram 2013-11-14 21:39:06 UTC
Great, I also included a regression test in the patch, could be good to include:

Index: tests/unlist.R
--- tests/unlist.R	(revision 0)
+++ tests/unlist.R	(working copy)
@@ -0,0 +1,9 @@
+# Raws need to get cast properly to logicals, otherwise subtle inconsistencies arise
+a <- list(as.raw(11), TRUE)
+b <- unlist(a, recursive=FALSE)
+stopifnot(b == TRUE)
+stopifnot(identical(b, c(TRUE,TRUE)))