Skip to content

Commit 110688d

Browse files
Backport "Avoid conversion of Unit type into () term" to LTS (#21044)
Backports #20295 to the LTS branch. PR submitted by the release tooling.
2 parents 86bb4cf + e3e03fa commit 110688d

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

compiler/src/dotty/tools/dotc/transform/BetaReduce.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ object BetaReduce:
153153
val expansion1 = new TreeMap {
154154
override def transform(tree: Tree)(using Context) = tree.tpe.widenTermRefExpr match
155155
case ConstantType(const) if isPureExpr(tree) => cpy.Literal(tree)(const)
156-
case tpe: TypeRef if tpe.derivesFrom(defn.UnitClass) && isPureExpr(tree) => cpy.Literal(tree)(Constant(()))
156+
case tpe: TypeRef if tree.isTerm && tpe.derivesFrom(defn.UnitClass) && isPureExpr(tree) =>
157+
cpy.Literal(tree)(Constant(()))
157158
case _ => super.transform(tree)
158159
}.transform(expansion)
159160

tests/pos-macros/i20286/Macro_1.scala

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import scala.quoted.*
2+
3+
type P[+T] = ParsingRun[T]
4+
trait ParsingRun[+T] {
5+
var successValue: Any
6+
def freshSuccessUnit(): ParsingRun[Unit]
7+
8+
}
9+
10+
object MacroInlineImpls {
11+
inline def flatMapXInline[T, V](
12+
lhs: ParsingRun[T]
13+
)(inline f: T => ParsingRun[V]): ParsingRun[V] = {
14+
f(lhs.successValue.asInstanceOf[T])
15+
}
16+
17+
def parsedSequence0[T: Type, V: Type, R: Type](
18+
lhs: Expr[ParsingRun[T]],
19+
rhs: Expr[ParsingRun[V]]
20+
)(using quotes: Quotes): Expr[ParsingRun[R]] = {
21+
import quotes.reflect.*
22+
'{ $rhs.asInstanceOf[ParsingRun[R]] }
23+
}
24+
}

tests/pos-macros/i20286/Test_2.scala

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
implicit inline def LiteralStr(s: String)(implicit ctx: P[Any]): P[Unit] = ???
2+
3+
extension [T](inline parse0: P[T]) {
4+
inline def ~[V, R](inline other: P[V])(using
5+
ctx: P[?]
6+
): P[R] = ${ MacroInlineImpls.parsedSequence0[T, V, R]('parse0, 'other) }
7+
8+
inline def flatMapX[V](inline f: T => P[V]): P[V] =
9+
MacroInlineImpls.flatMapXInline[T, V](parse0)(f)
10+
}
11+
12+
def deeper[$: P]: P[Int] = ???
13+
def newline[$: P]: P[Unit] = ???
14+
def blockBody[p: P]: P[Seq[Int]] = newline ~ deeper.flatMapX { i =>
15+
val y = LiteralStr("")(using ???)
16+
???
17+
}

0 commit comments

Comments
 (0)