Skip to content

Commit

Permalink
RD-10522 Truffle execution error in recursive function call (#339)
Browse files Browse the repository at this point in the history
- fixed the recursion bug and list take semantics
  • Loading branch information
alexzerntev authored Jan 25, 2024
1 parent ff9f387 commit 5f2f572
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ trait CollectionPackageTest extends CompilerTestContext {
it should evaluateTo("Collection.Build()")
}

test("""Collection.Take(Collection.Build(1,2,3,4,5), -10)""".stripMargin) { it =>
it should typeAs("collection(int)")
it should evaluateTo("Collection.Build()")
}

test("""Collection.Take(Collection.Build(1,2,3,4,5), 0)""".stripMargin) { it =>
it should typeAs("collection(int)")
it should evaluateTo("Collection.Build()")
}

// Unnest

test("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,16 @@ trait ListPackageTest extends CompilerTestContext {
it should evaluateTo("List.Build(1,2,3,4,5)")
}

test("""List.Take(List.Build(1,2,3,4,5), -10)""".stripMargin) { it =>
it should typeAs("list(int)")
it should evaluateTo("[]")
}

test("""List.Take(List.Build(1,2,3,4,5), 0)""".stripMargin) { it =>
it should typeAs("list(int)")
it should evaluateTo("[]")
}

// Unnest

test("""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ trait LetFunRecTest extends CompilerTestContext {
|in f(4)
|""".stripMargin)(it => it should evaluateTo("24"))

test("""let rec fibo(maxN: int, n1: int, n2: int): list(int) =
| if maxN < n1
| then List.Empty(type int)
| else List.Union([n1], fibo(maxN, n2, n2 + n1))
|in fibo(50, 1, 2)""".stripMargin)(_ should orderEvaluateTo("[1,2,3,5,8,13,21,34]"))

// RD-5691
test("""let primes(max: int) =
| let process(numbers: list(int)) = List.OrderBy(numbers, n -> n, "ASC"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,9 @@ protected static Object doDirect(
Closure closure,
String[] namedArgNames,
Object[] arguments,
@Cached(
value = "getObjectArray(closure.getArgNames().length)",
allowUncached = true,
dimensions = 0)
Object[] finalArgs,
@Cached("closure.getCallTarget()") RootCallTarget cachedTarget,
@Cached("create(cachedTarget)") DirectCallNode callNode) {
Object[] finalArgs = getObjectArray(closure.getArgNames().length);
finalArgs[0] = closure.frame;
System.arraycopy(closure.defaultArguments, 0, finalArgs, 1, closure.getArgNames().length);
setArgsWithNames(closure, namedArgNames, arguments, finalArgs);
Expand All @@ -233,13 +229,8 @@ protected static Object doIndirect(
Closure closure,
String[] namedArgNames,
Object[] arguments,
@Cached(
value = "getObjectArray(closure.getArgNames().length)",
allowUncached = true,
dimensions = 0,
neverDefault = true)
Object[] finalArgs,
@Cached(inline = false) IndirectCallNode callNode) {
Object[] finalArgs = getObjectArray(closure.getArgNames().length);
finalArgs[0] = closure.frame;
System.arraycopy(closure.defaultArguments, 0, finalArgs, 1, closure.getArgNames().length);
setArgsWithNames(closure, namedArgNames, arguments, finalArgs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ public BooleanList sort() {
public BooleanList take(int num) {
if (num >= this.getInnerList().length) {
return this;
} else if (num <= 0) {
return new BooleanList(new boolean[0]);
} else {
boolean[] result = new boolean[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new BooleanList(result);
}
boolean[] result = new boolean[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new BooleanList(result);
}

// InteropLibrary: Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ public ByteList sort() {
public ByteList take(int num) {
if (num >= this.getInnerList().length) {
return this;
} else if (num <= 0) {
return new ByteList(new byte[0]);
} else {
byte[] result = new byte[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new ByteList(result);
}
byte[] result = new byte[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new ByteList(result);
}

// InteropLibrary: Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ public DoubleList sort() {
public DoubleList take(int num) {
if (num >= this.getInnerList().length) {
return this;
} else if (num <= 0) {
return new DoubleList(new double[0]);
} else {
double[] result = new double[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new DoubleList(result);
}
double[] result = new double[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new DoubleList(result);
}

// InteropLibrary: Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,13 @@ public FloatList sort() {
public FloatList take(int num) {
if (num >= this.getInnerList().length) {
return this;
} else if (num <= 0) {
return new FloatList(new float[0]);
} else {
float[] result = new float[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new FloatList(result);
}
float[] result = new float[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new FloatList(result);
}

// InteropLibrary: Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ public IntList sort() {
public IntList take(int num) {
if (num >= this.getInnerList().length) {
return this;
} else if (num <= 0) {
return new IntList(new int[0]);
} else {
int[] result = new int[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new IntList(result);
}
int[] result = new int[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new IntList(result);
}

// InteropLibrary: Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ public LongList sort() {
public LongList take(int num) {
if (num >= this.getInnerList().length) {
return this;
} else if (num <= 0) {
return new LongList(new long[0]);
} else {
long[] result = new long[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new LongList(result);
}
long[] result = new long[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new LongList(result);
}

// InteropLibrary: Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ public ListIterable toIterable() {
public ObjectList take(int num) {
if (num >= this.getInnerList().length) {
return this;
} else if (num <= 0) {
return new ObjectList(new Object[0]);
} else {
Object[] result = new Object[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new ObjectList(result);
}
Object[] result = new Object[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new ObjectList(result);
}

@ExportMessage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ public ListIterable toIterable() {
public RawArrayList take(int num) {
if (num >= this.list.size()) {
return this;
} else if (num <= 0) {
return new RawArrayList(new ArrayList<>());
} else {
return new RawArrayList(new ArrayList<>(list.subList(0, num)));
}
return new RawArrayList(new ArrayList<>(list.subList(0, num)));
}

// InteropLibrary: Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ public ShortList sort() {
public ShortList take(int num) {
if (num >= this.getInnerList().length) {
return this;
} else if (num <= 0) {
return new ShortList(new short[0]);
} else {
short[] result = new short[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new ShortList(result);
}
short[] result = new short[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new ShortList(result);
}

// InteropLibrary: Array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,13 @@ public StringList take(int num) {
if (num >= this.getInnerList().length) {
return this;
}
String[] result = new String[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new StringList(result);
if (num <= 0) {
return new StringList(new String[0]);
} else {
String[] result = new String[num];
System.arraycopy(this.list, 0, result, 0, result.length);
return new StringList(result);
}
}

// InteropLibrary: Array
Expand Down

0 comments on commit 5f2f572

Please sign in to comment.