From 5170dca03f12f8c2b070f9250fc9e39fbc130d3e Mon Sep 17 00:00:00 2001 From: odersky Date: Fri, 23 May 2025 10:06:07 +0200 Subject: [PATCH] Tighten condition when to do SAM type conversion - No conversion from contextual lambdas - No conversion after erasure Fixes #23234 --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 3 ++- tests/neg-custom-args/captures/i23234.scala | 5 +++++ 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 tests/neg-custom-args/captures/i23234.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 3f24f3f8f62b..bc0b295c421f 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -2030,7 +2030,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer case mt: MethodType => pt.findFunctionType match { case SAMType(samMeth, samParent) - if !defn.isFunctionNType(samParent) && mt <:< samMeth => + if !ctx.erasedTypes && !defn.isFunctionNType(samParent) + && mt <:< samMeth && !mt.isImplicitMethod => if defn.isContextFunctionType(mt.resultType) then report.error( em"""Implementation restriction: cannot convert this expression to `$samParent` diff --git a/tests/neg-custom-args/captures/i23234.scala b/tests/neg-custom-args/captures/i23234.scala new file mode 100644 index 000000000000..61d7123c8b78 --- /dev/null +++ b/tests/neg-custom-args/captures/i23234.scala @@ -0,0 +1,5 @@ +abstract class MyFun: + def apply(x: Int): Int + +object Test: + val myFun: MyFun = (x: Int) ?=> x + 10 // error \ No newline at end of file