diff --git a/R/expr.R b/R/expr.R index c9c0c5ff6..36ceded8b 100644 --- a/R/expr.R +++ b/R/expr.R @@ -133,7 +133,12 @@ is_syntactic_literal <- function(x) { length(x) == 1 && is.null(attributes(x)) && (is.na(x) || x >= 0) }, complex = { - length(x) == 1 && is.null(attributes(x)) && (is.na(x) || (Re(x) == 0 && Im(x) >= 0)) + length(x) == 1 && + is.null(attributes(x)) && + ( + (is.na(Re(x)) && is.na(Im(x))) || + (!is.na(x) && Re(x) == 0 && Im(x) >= 0) + ) }, FALSE ) diff --git a/tests/testthat/test-expr.R b/tests/testthat/test-expr.R index 0759bbd5a..7adbce6f9 100644 --- a/tests/testthat/test-expr.R +++ b/tests/testthat/test-expr.R @@ -144,4 +144,10 @@ test_that("negative numbers are not syntactic", { expect_false(is_syntactic_literal(-1)) expect_false(is_syntactic_literal(-1L)) expect_false(is_syntactic_literal(-1i)) +}) + +test_that("NA + 1i is not syntactic", { + skip_if_not_installed("base", "4.4") + expect_false(is_syntactic_literal(NA + 1i)) + expect_false(is_syntactic_literal(NA - 1i)) }) \ No newline at end of file