From be07d62071875066d0fb801d2766f15b8f07b050 Mon Sep 17 00:00:00 2001
From: stepan <stepan.sindelar@oracle.com>
Date: Tue, 13 Mar 2018 17:52:45 +0100
Subject: [PATCH] Fix RFFI CAR for empty '...' returns NULL

---
 .../truffle/r/ffi/impl/nodes/ListAccessNodes.java     |  3 +++
 .../packages/testrffi/testrffi/tests/pairlists.R      | 11 +++++++++++
 2 files changed, 14 insertions(+)
 create mode 100644 com.oracle.truffle.r.test.native/packages/testrffi/testrffi/tests/pairlists.R

diff --git a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/ListAccessNodes.java b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/ListAccessNodes.java
index f1e3036783..d33dce2425 100644
--- a/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/ListAccessNodes.java
+++ b/com.oracle.truffle.r.ffi.impl/src/com/oracle/truffle/r/ffi/impl/nodes/ListAccessNodes.java
@@ -63,6 +63,9 @@ public final class ListAccessNodes {
 
         @Specialization
         protected Object car(RArgsValuesAndNames args) {
+            if (args.isEmpty()) {
+                return RNull.instance;
+            }
             return args.getArgument(0);
         }
 
diff --git a/com.oracle.truffle.r.test.native/packages/testrffi/testrffi/tests/pairlists.R b/com.oracle.truffle.r.test.native/packages/testrffi/testrffi/tests/pairlists.R
new file mode 100644
index 0000000000..8260d0d90e
--- /dev/null
+++ b/com.oracle.truffle.r.test.native/packages/testrffi/testrffi/tests/pairlists.R
@@ -0,0 +1,11 @@
+# Some more tests that work with pairlists and related objects like '...' or language
+stopifnot(require(testrffi))
+
+# Note: GNU R returns the promise, FastR the value, we check that at least there are not exceptions
+foo <- function(...) api.CAR(get('...'))
+invisible(foo(a=3))
+invisible(foo(a=4, b=6))
+
+foo <- function(...) api.CDR(get('...'))
+is.null(foo(a=3))
+names(foo(a=4, b=6))
-- 
GitLab