Skip to content

Commit 973e84a

Browse files
author
Alexey Romanov
committed
Include the type converted to as part of ImplicitConvert
1 parent c620534 commit 973e84a

File tree

2 files changed

+29
-13
lines changed

2 files changed

+29
-13
lines changed

src/common/ImplicitOps.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ trait ImplicitOps extends Base {
1515
}
1616

1717
trait ImplicitOpsExp extends ImplicitOps with BaseExp {
18-
case class ImplicitConvert[X,Y](x: Exp[X])(implicit val mX: Manifest[X], val mY: Manifest[Y]) extends Def[Y]
18+
case class ImplicitConvert[X,Y](x: Exp[X], mY: Manifest[Y])(implicit val mX: Manifest[X]) extends Def[Y]
1919

2020
def implicit_convert[X,Y](x: Exp[X])(implicit c: X => Y, mX: Manifest[X], mY: Manifest[Y], pos: SourceContext) : Rep[Y] = {
21-
if (mX == mY) x.asInstanceOf[Rep[Y]] else ImplicitConvert[X,Y](x)
21+
if (mX == mY) x.asInstanceOf[Rep[Y]] else ImplicitConvert[X,Y](x, mY)
2222
}
2323

2424
override def mirror[A:Manifest](e: Def[A], f: Transformer)(implicit pos: SourceContext): Exp[A] = (e match {
25-
case im@ImplicitConvert(x) => toAtom(ImplicitConvert(f(x))(im.mX,im.mY))(mtype(manifest[A]),pos)
25+
case im@ImplicitConvert(x, mY) => toAtom(ImplicitConvert(f(x), mY)(im.mX))(mtype(manifest[A]),pos)
2626
case _ => super.mirror(e,f)
2727
}).asInstanceOf[Exp[A]]
2828

@@ -33,9 +33,9 @@ trait ScalaGenImplicitOps extends ScalaGenBase {
3333
import IR._
3434

3535
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = rhs match {
36-
// TODO: this valDef is redundant; we really just want the conversion to be a no-op in the generated code.
37-
// TODO: but we still need to link the defs together
38-
case ImplicitConvert(x) => emitValDef(sym, quote(x))
36+
// Make sure it's typed to trigger the implicit conversion
37+
// Otherwise we can get type mismatch in generated code
38+
case ImplicitConvert(x, mY) => emitTypedValDef(sym, quote(x))
3939
case _ => super.emitNode(sym, rhs)
4040
}
4141
}
@@ -46,8 +46,8 @@ trait CLikeGenImplicitOps extends CLikeGenBase {
4646

4747
override def emitNode(sym: Sym[Any], rhs: Def[Any]) = {
4848
rhs match {
49-
case im@ImplicitConvert(x) =>
50-
gen"${im.mY} $sym = (${im.mY})$x;"
49+
case ImplicitConvert(x, mY) =>
50+
gen"$mY $sym = ($mY)$x;"
5151
case _ => super.emitNode(sym, rhs)
5252
}
5353
}

src/internal/ScalaCodegen.scala

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,21 @@ trait ScalaCodegen extends GenericCodegen with Config {
9090
fileName.substring(i + 1)
9191
}
9292

93-
def emitValDef(sym: Sym[Any], rhs: String): Unit = {
94-
val extra = if ((sourceinfo < 2) || sym.pos.isEmpty) "" else {
95-
val context = sym.pos(0)
93+
private def valDefExtra(sym: IR.Sym[Any]): String = {
94+
if ((sourceinfo < 2) || sym.pos.isEmpty)
95+
""
96+
else {
97+
val context = sym.pos.head
9698
" // " + relativePath(context.fileName) + ":" + context.line
9799
}
98-
stream.println("val " + quote(sym) + " = " + rhs + extra)
100+
}
101+
102+
def emitValDef(sym: Sym[Any], rhs: String): Unit = {
103+
stream.println("val " + quote(sym) + " = " + rhs + valDefExtra(sym))
104+
}
105+
106+
def emitTypedValDef(sym: Sym[Any], rhs: String): Unit = {
107+
stream.println(src"val $sym: ${sym.tp} = $rhs" + valDefExtra(sym))
99108
}
100109

101110
def emitVarDef(sym: Sym[Variable[Any]], rhs: String): Unit = {
@@ -132,7 +141,14 @@ trait ScalaNestedCodegen extends GenericNestedCodegen with ScalaCodegen {
132141
else
133142
super.emitValDef(sym,rhs)
134143
}
135-
144+
145+
// special case for recursive vals
146+
override def emitTypedValDef(sym: Sym[Any], rhs: String): Unit = {
147+
if (recursive contains sym)
148+
stream.println(quote(sym) + " = " + rhs) // we have a forward declaration above.
149+
else
150+
super.emitTypedValDef(sym,rhs)
151+
}
136152
}
137153

138154

0 commit comments

Comments
 (0)