Skip to content

Commit bd451c9

Browse files
committed
Delay ptype finalization in list_combine()
Until after `default` has been considered as well
1 parent d485b45 commit bd451c9

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

src/list-combine.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,12 +1803,12 @@ r_obj* ptype_common_with_default(
18031803

18041804
// Okay `ptype` is `NULL`. We determine it from `xs` and `default`.
18051805

1806-
// Use only `xs` and `p_xs_arg` first for best errors
1806+
// Use only `xs` and `p_xs_arg` first for best errors.
1807+
// Not finalising `ptype` yet in case we need to incorporating `default`!
18071808
ptype = KEEP(vec_ptype_common(
18081809
xs,
18091810
ptype,
1810-
// TODO!: This should be `false`
1811-
PTYPE_FINALISE_true,
1811+
PTYPE_FINALISE_false,
18121812
s3_fallback,
18131813
p_xs_arg,
18141814
error_call
@@ -1829,7 +1829,10 @@ r_obj* ptype_common_with_default(
18291829
}
18301830
KEEP(ptype);
18311831

1832-
FREE(2);
1832+
// Now finalise after incorporating `default`
1833+
ptype = KEEP(vec_ptype_finalise(ptype));
1834+
1835+
FREE(3);
18331836
return ptype;
18341837
}
18351838

tests/testthat/test-case-when.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,17 @@ test_that("`default` that is an unused logical `NA` can still be cast to `values
272272
expect_identical(vec_case_when(list(TRUE), list("x"), default = NA), "x")
273273
})
274274

275+
test_that("`default` type is used when all values are logical `NA` (#2094)", {
276+
expect_identical(
277+
vec_case_when(list(TRUE), list(NA), default = "a"),
278+
NA_character_
279+
)
280+
expect_identical(
281+
vec_case_when(list(c(TRUE, FALSE)), list(NA), default = "a"),
282+
c(NA_character_, "a")
283+
)
284+
})
285+
275286
test_that("`default_arg` can be customized", {
276287
expect_snapshot(error = TRUE, {
277288
vec_case_when(list(FALSE), list(1L), default = 2:3, default_arg = "foo")

tests/testthat/test-list-combine.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,6 +1886,27 @@ test_that("can specify a ptype to override common type", {
18861886
})
18871887
})
18881888

1889+
test_that("common type is correctly computed with unspecified values and a `default` (#2094)", {
1890+
expect_identical(
1891+
list_combine(
1892+
x = list(NA),
1893+
indices = list(1),
1894+
size = 1,
1895+
default = "a"
1896+
),
1897+
NA_character_
1898+
)
1899+
expect_identical(
1900+
list_combine(
1901+
x = list(NA),
1902+
indices = list(1),
1903+
size = 2,
1904+
default = "a"
1905+
),
1906+
c(NA, "a")
1907+
)
1908+
})
1909+
18891910
test_that("outer names are kept", {
18901911
x <- list(x = 1, y = 2)
18911912
expect_named_list_combine(

0 commit comments

Comments
 (0)