From 02c1a7fee3ab0c233d7f9eac9edf45c51c2418ca Mon Sep 17 00:00:00 2001
From: Florian Angerer <florian.angerer@oracle.com>
Date: Thu, 24 Aug 2017 15:23:21 +0200
Subject: [PATCH] Weakened precondition for reusing vectors. Also allow to
 reuse shared vectors.

---
 .../oracle/truffle/r/nodes/unary/CastBaseNode.java |  6 +-----
 .../data/closures/RToComplexVectorClosure.java     |  2 +-
 .../runtime/data/closures/RToIntVectorClosure.java | 14 +++++++++++---
 3 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastBaseNode.java b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastBaseNode.java
index e978790e99..46d1c12b92 100644
--- a/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastBaseNode.java
+++ b/com.oracle.truffle.r.nodes/src/com/oracle/truffle/r/nodes/unary/CastBaseNode.java
@@ -184,11 +184,7 @@ public abstract class CastBaseNode extends CastNode {
     }
 
     protected boolean isReusable(RAbstractVector v) {
-        // Reusing only of complete vectors otherwise we will produce different warning output
-        if (reuseNonShared && v.isComplete() && v instanceof RSharingAttributeStorage) {
-            return !((RSharingAttributeStorage) v).isShared();
-        }
-        return false;
+        return reuseNonShared;
     }
 
     protected RAbstractVector castWithReuse(RType targetType, RAbstractVector v, ConditionProfile naProfile) {
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToComplexVectorClosure.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToComplexVectorClosure.java
index 14fd6c9782..3932e3c982 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToComplexVectorClosure.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToComplexVectorClosure.java
@@ -71,7 +71,7 @@ abstract class RToComplexVectorClosure extends RToVectorClosure implements RAbst
 
     @Override
     public final RAbstractComplexVector copyWithNewDimensions(int[] newDimensions) {
-        if (!keepAttributes) {
+        if (keepAttributes) {
             return materialize().copyWithNewDimensions(newDimensions);
         }
         return this;
diff --git a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToIntVectorClosure.java b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToIntVectorClosure.java
index ee9fafbb91..6c92572997 100644
--- a/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToIntVectorClosure.java
+++ b/com.oracle.truffle.r.runtime/src/com/oracle/truffle/r/runtime/data/closures/RToIntVectorClosure.java
@@ -69,7 +69,7 @@ abstract class RToIntVectorClosure extends RToVectorClosure implements RAbstract
 
     @Override
     public final RAbstractIntVector copyWithNewDimensions(int[] newDimensions) {
-        if (!keepAttributes) {
+        if (keepAttributes) {
             return materialize().copyWithNewDimensions(newDimensions);
         }
         return this;
@@ -103,6 +103,7 @@ final class RLogicalToIntVectorClosure extends RToIntVectorClosure implements RA
 final class RDoubleToIntVectorClosure extends RToIntVectorClosure implements RAbstractIntVector {
 
     private final RDoubleVector vector;
+    private boolean naReported;
 
     RDoubleToIntVectorClosure(RDoubleVector vector, boolean keepAttributes) {
         super(keepAttributes);
@@ -122,7 +123,10 @@ final class RDoubleToIntVectorClosure extends RToIntVectorClosure implements RAb
         }
         int result = (int) value;
         if (result == Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
-            RError.warning(RError.SHOW_CALLER2, RError.Message.NA_INTRODUCED_COERCION);
+            if (!naReported) {
+                RError.warning(RError.SHOW_CALLER2, RError.Message.NA_INTRODUCED_COERCION);
+                naReported = true;
+            }
             return RRuntime.INT_NA;
         }
         return result;
@@ -132,6 +136,7 @@ final class RDoubleToIntVectorClosure extends RToIntVectorClosure implements RAb
 final class RDoubleSequenceToIntVectorClosure extends RToIntVectorClosure implements RAbstractIntVector {
 
     private final RDoubleSequence vector;
+    private boolean naReported;
 
     RDoubleSequenceToIntVectorClosure(RDoubleSequence vector, boolean keepAttributes) {
         super(keepAttributes);
@@ -151,7 +156,10 @@ final class RDoubleSequenceToIntVectorClosure extends RToIntVectorClosure implem
         }
         int result = (int) value;
         if (result == Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
-            RError.warning(RError.SHOW_CALLER2, RError.Message.NA_INTRODUCED_COERCION);
+            if (!naReported) {
+                RError.warning(RError.SHOW_CALLER2, RError.Message.NA_INTRODUCED_COERCION);
+                naReported = true;
+            }
             return RRuntime.INT_NA;
         }
         return result;
-- 
GitLab