Skip to content

Commit

Permalink
Avoid conversion of Unit type into () term
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Apr 30, 2024
1 parent e2c456f commit 321881a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/BetaReduce.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ object BetaReduce:
val expansion1 = new TreeMap {
override def transform(tree: Tree)(using Context) = tree.tpe.widenTermRefExpr match
case ConstantType(const) if isPureExpr(tree) => cpy.Literal(tree)(const)
case tpe: TypeRef if tpe.derivesFrom(defn.UnitClass) && isPureExpr(tree) => cpy.Literal(tree)(Constant(()))
case tpe: TypeRef if tree.isTerm && tpe.derivesFrom(defn.UnitClass) && isPureExpr(tree) =>
cpy.Literal(tree)(Constant(()))
case _ => super.transform(tree)
}.transform(expansion)

Expand Down
24 changes: 24 additions & 0 deletions tests/pos-macros/i20286/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import scala.quoted.*

type P[+T] = ParsingRun[T]
trait ParsingRun[+T] {
var successValue: Any
def freshSuccessUnit(): ParsingRun[Unit]

}

object MacroInlineImpls {
inline def flatMapXInline[T, V](
lhs: ParsingRun[T]
)(inline f: T => ParsingRun[V]): ParsingRun[V] = {
f(lhs.successValue.asInstanceOf[T])
}

def parsedSequence0[T: Type, V: Type, R: Type](
lhs: Expr[ParsingRun[T]],
rhs: Expr[ParsingRun[V]]
)(using quotes: Quotes): Expr[ParsingRun[R]] = {
import quotes.reflect.*
'{ $rhs.asInstanceOf[ParsingRun[R]] }
}
}
17 changes: 17 additions & 0 deletions tests/pos-macros/i20286/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
implicit inline def LiteralStr(s: String)(implicit ctx: P[Any]): P[Unit] = ???

extension [T](inline parse0: P[T]) {
inline def ~[V, R](inline other: P[V])(using
ctx: P[?]
): P[R] = ${ MacroInlineImpls.parsedSequence0[T, V, R]('parse0, 'other) }

inline def flatMapX[V](inline f: T => P[V]): P[V] =
MacroInlineImpls.flatMapXInline[T, V](parse0)(f)
}

def deeper[$: P]: P[Int] = ???
def newline[$: P]: P[Unit] = ???
def blockBody[p: P]: P[Seq[Int]] = newline ~ deeper.flatMapX { i =>
val y = LiteralStr("")(using ???)
???
}

0 comments on commit 321881a

Please sign in to comment.