Skip to content

Commit 11a0071

Browse files
jad-hamzadrganam
authored andcommitted
Fix extraction of Array.updated
1 parent 1952ed9 commit 11a0071

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

frontends/benchmarks/extraction/valid/Arrays.scala

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/* Copyright 2009-2021 EPFL, Lausanne */
22

3+
// use `.updated` from Stainless library
34
import stainless.lang._
45

5-
object Arrays {
6+
// unimport implicit conversions for `.updated`
7+
import scala.Predef.{ genericArrayOps => _, genericWrapArray => _, _ }
68

7-
def len[T](a: Array[T]): Boolean = {
8-
a.length == a.size
9-
}.holds
9+
object Arrays {
1010

1111
def update[T](a: Array[T], i: Int, t: T) = {
1212
require(i >= 0 && i < a.length)
@@ -17,24 +17,11 @@ object Arrays {
1717
a.update(i, t)
1818
}
1919

20+
def updated[T](a: Array[T], i: Int, t: T): Array[T] = {
21+
require(i >= 0 && i < a.length)
2022

21-
// ArraySeq not supported.
22-
/*
23-
* import scala.collection.mutable.ArraySeq
24-
* def updated[T](a: Array[T], i: Int, t: T): ArraySeq[T] = {
25-
* require(i >= 0 && i < a.length)
26-
* a.updated(i, t)
27-
* }
28-
*/
29-
30-
// ClassTag not supported.
31-
/*
32-
* import scala.reflect.ClassTag
33-
* def updated[T](a: Array[T], i: Int, u: T)(implicit m: ClassTag[T]): Array[T] = {
34-
* require(i >= 0 && i < a.length)
35-
* a.updated(i, u)
36-
* }
37-
*/
23+
a.updated(i, t)
24+
}
3825

3926
def updated(a: Array[Int], i: Int, v: Int): Array[Int] = {
4027
require(i >= 0 && i < a.length)

frontends/library/stainless/lang/package.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,4 +145,14 @@ package object lang {
145145

146146
@library
147147
def specialize[T](call: T): T = call
148+
149+
@ignore @library
150+
implicit class ArrayUpdating[T](a: Array[T]) {
151+
def updated(index: Int, value: T): Array[T] = {
152+
val res = a.clone
153+
res(index) = value
154+
res
155+
}
156+
}
157+
148158
}

frontends/scalac/src/main/scala/stainless/frontends/scalac/ASTExtractors.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,18 @@ trait ASTExtractors {
844844
if (arrayOps.toString endsWith "ArrayOps") && (update.toString == "updated")
845845
=> Some((array, index, value))
846846

847+
case Apply(
848+
Select(
849+
Apply(
850+
TypeApply(ExSelected("stainless", "lang", "package", "ArrayUpdating"), tpe :: Nil),
851+
array :: Nil
852+
),
853+
ExNamed("updated")
854+
),
855+
index :: value :: Nil) =>
856+
857+
Some((array, index, value))
858+
847859
// There's no `updated` method in the Array class itself, only though implicit conversion.
848860
case _ => None
849861
}

0 commit comments

Comments
 (0)