Skip to content

Commit a0121f3

Browse files
authored
Do not beta-reduce/eta-expand pattern splices with contextual function types (#18198)
The change in `typedUnnamed` fixes beta-reduction on the type that eliminated the implicit argument. Then the second fix in `adaptNoArgsOther` ensures that we do not transform `${x}` into `(ev ?=> ${x}.apply(x))`. Fixes #18197
2 parents d0b2151 + 3daa4e7 commit a0121f3

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

+2
Original file line numberDiff line numberDiff line change
@@ -3169,6 +3169,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
31693169
&& xtree.isTerm
31703170
&& !untpd.isContextualClosure(xtree)
31713171
&& !ctx.mode.is(Mode.Pattern)
3172+
&& !xtree.isInstanceOf[SplicePattern]
31723173
&& !ctx.isAfterTyper
31733174
&& !ctx.isInlineContext
31743175
then
@@ -4015,6 +4016,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
40154016
&& pt != SingletonTypeProto
40164017
&& pt != AssignProto
40174018
&& !ctx.mode.is(Mode.Pattern)
4019+
&& !tree.isInstanceOf[SplicePattern]
40184020
&& !ctx.isAfterTyper
40194021
&& !ctx.isInlineContext
40204022
then

tests/pos-macros/i18197a.scala

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.quoted.*
2+
3+
def readPathMacro[A: Type, B: Type](expr: Expr[Any])(using Quotes) =
4+
expr match
5+
case '{ foo($y) } => y: Expr[Int ?=> Int]
6+
7+
def foo(x: Int ?=> Int): Any = ???

tests/pos-macros/i18197b/Macro.scala

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import scala.quoted.*
2+
3+
object ReproMacro {
4+
inline def readPath[A, B](inline config: Config[A, B]) = ${ readPathMacro[A, B]('config) }
5+
6+
def readPathMacro[A: Type, B: Type](expr: Expr[Config[A, B]])(using Quotes) = {
7+
import quotes.reflect.report
8+
9+
expr match {
10+
case '{ Field.const[a, b, tpe]($selector) } =>
11+
val selector2: Expr[Selector ?=> a => tpe] = selector
12+
report.info(s"Matched!")
13+
'{}
14+
case other =>
15+
report.errorAndAbort("woops, I did not match")
16+
}
17+
}
18+
}

tests/pos-macros/i18197b/Test.scala

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
trait Selector {
2+
extension [A](self: A) def at[B <: A]: B
3+
}
4+
5+
trait Config[A, B]
6+
7+
object Field {
8+
def const[A, B, FieldTpe](selector: Selector ?=> A => FieldTpe): Config[A, B] = ???
9+
}
10+
11+
final case class Example(int: Int)
12+
13+
@main def main = {
14+
// compiles just fine
15+
ReproMacro.readPath[Example, Example](Field.const(_.int))
16+
17+
// doesn't compile
18+
ReproMacro.readPath[Example, Example](Field.const(_.int.at[Int]))
19+
}

0 commit comments

Comments
 (0)