Skip to content

Commit e9e9e32

Browse files
author
Alexey Romanov
committed
Include the type when emitting Array.apply calls
This prevents Array[Nothing] being inferred when called without arguments.
1 parent 75e3ea4 commit e9e9e32

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

src/common/ArrayOps.scala

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ trait ScalaGenArrayOps extends BaseGenArrayOps with ScalaGenBase {
206206
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
207207
case a@ArrayNew(n) => emitValDef(sym, src"new Array[${remap(a.m)}]($n)")
208208
case e@ArrayFromSeq(xs) => {
209+
val m = e.m
209210
emitData(sym, xs)
210211
emitValDef(sym,
211212
if(xs.size > ARRAY_LITERAL_MAX_SIZE) {
@@ -217,10 +218,15 @@ trait ScalaGenArrayOps extends BaseGenArrayOps with ScalaGenBase {
217218
}
218219
val numBlocks = Math.ceil(xs.size / ARRAY_LITERAL_MAX_SIZE).intValue
219220
"{val buf=new Array[" + remap(e.mt) + "](" + xs.size + ")\n" + ((0 until numBlocks).map(append)).mkString("\n") + "buf}" */
220-
"{import scala.io.Source;(Source.fromFile(\"" + symDataPath(sym) + "\").getLines.map{Integer.parseInt(_)}).toArray}"
221-
}
222-
else {
223-
"Array(" + (xs map quote).mkString(",") + ")"
221+
val parseMethod = m.asInstanceOf[Manifest[_]] match {
222+
case Manifest.Int => "Integer.parseInt"
223+
case Manifest.Long | Manifest.Float | Manifest.Double => s"java.lang.$m.parse$m"
224+
case _ if m == manifest[String] => "x => x"
225+
case _ => throw new GenerationFailedException(s"Can't store an array of type $m in a file")
226+
}
227+
s"""scala.io.Source.fromFile("${symDataPath(sym)}").getLines.map($parseMethod).toArray"""
228+
} else {
229+
src"Array[$m]($xs)"
224230
}
225231
)
226232
}

test-out/epfl/test12-array-seq-creation.check

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*******************************************/
44
class IntArrayCreation extends ((Int)=>(Unit)) {
55
def apply(x0:Int): Unit = {
6-
val x1 = Array(1,2,3)
6+
val x1 = Array[Int](1,2,3)
77
val x2 = x1(0)
88
val x3 = println(x2)
99
x3
@@ -17,7 +17,7 @@ x3
1717
*******************************************/
1818
class CharArrayCreation extends ((Int)=>(Unit)) {
1919
def apply(x5:Int): Unit = {
20-
val x6 = Array('a','b','c')
20+
val x6 = Array[Char]('a','b','c')
2121
val x7 = x6(0)
2222
val x8 = println(x7)
2323
x8

test-out/epfl/test2-fft3.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ val x15 = x3-x7
2727
val x24 = x12-x15
2828
val x25 = x11-x16
2929
val x26 = x12+x15
30-
val x27 = Array(x17,x18,x23,x24,x19,x20,x25,x26)
30+
val x27 = Array[Double](x17,x18,x23,x24,x19,x20,x25,x26)
3131
x27
3232
}
3333
}

test-src/epfl/test1-arith/Arrays.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ trait Arrays extends Base {
2222
trait ArraysExp extends Arrays with BaseExp {
2323
case class ArrayApply[T:Manifest](x:Rep[Array[T]], i:Int) extends Def[T]
2424
//case class ArrayUpdate[T](x:Rep[Array[T]], i:Int) extends Def[T]
25-
case class MakeArray[T:Manifest](x:List[Rep[T]]) extends Def[Array[T]]
25+
case class MakeArray[T](x:List[Rep[T]])(implicit val m: Manifest[T]) extends Def[Array[T]]
2626

2727
def arrayApply[T:Manifest](x: Rep[Array[T]], i:Int) = ArrayApply(x, i)
2828
//def arrayUpdate(x: Rep[Double]) = ArrayUpdate(x)
@@ -34,8 +34,8 @@ trait ScalaGenArrays extends ScalaGenBase {
3434
import IR._
3535

3636
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
37-
case ArrayApply(x,i) => emitValDef(sym, "" + quote(x) + ".apply(" + i + ")")
38-
case MakeArray(x) => emitValDef(sym, "Array(" + x.map(quote).mkString(",") + ")")
37+
case ArrayApply(x,i) => emitValDef(sym, src"$x.apply(${i.toString})")
38+
case a @ MakeArray(x) => emitValDef(sym, src"Array[${a.m}]($x)")
3939
case _ => super.emitNode(sym, rhs)
4040
}
4141
}

0 commit comments

Comments
 (0)