From 08f7436c1f04d51f1b15c1937c4fe4ab4cd68633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kirill=20M=C3=BCller?= Date: Thu, 15 Aug 2024 19:38:30 +0200 Subject: [PATCH] Fix edge case when coercing data frames to matrices --- R/data-mask.R | 11 ++++++++++- tests/testthat/test-data-mask.R | 7 +++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-data-mask.R diff --git a/R/data-mask.R b/R/data-mask.R index b72a708289..5b04db7091 100644 --- a/R/data-mask.R +++ b/R/data-mask.R @@ -11,7 +11,16 @@ DataMask <- R6Class("DataMask", frame <- caller_env(n = 2) local_mask(self, frame) - names_bindings <- chr_unserialise_unicode(names2(data)) + names <- names(data) + + if (is.null(names)) { + abort("Can't transform a data frame with `NULL` names.") + } + if (vec_any_missing(names)) { + abort("Can't transform a data frame with missing names.") + } + + names_bindings <- chr_unserialise_unicode(names) if (any(names_bindings == "")) { # `names2()` converted potential `NA` names to `""` already abort("Can't transform a data frame with `NA` or `\"\"` names.", call = error_call) diff --git a/tests/testthat/test-data-mask.R b/tests/testthat/test-data-mask.R new file mode 100644 index 0000000000..675f8001a9 --- /dev/null +++ b/tests/testthat/test-data-mask.R @@ -0,0 +1,7 @@ +test_that("Empty matrix can be coerced to a data frame (#7004)", { + skip_if_not(getRversion() >= "4.4") + expect_error( + slice(as.data.frame(matrix(nrow = 0, ncol = 0)), 1), + NA + ) +})