Skip to content

Commit 8c2b7ad

Browse files
authored
Prevent crash in SAM conversion with mismatched arity (#23877)
closes #23577
2 parents ab71be7 + 8adc284 commit 8c2b7ad

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,11 +1692,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
16921692
val restpe = mt.resultType match
16931693
case mt: MethodType => mt.toFunctionType(isJava = samParent.classSymbol.is(JavaDefined))
16941694
case tp => tp
1695-
(formals,
1696-
if (mt.isResultDependent)
1697-
untpd.InLambdaTypeTree(isResult = true, (_, syms) => restpe.substParams(mt, syms.map(_.termRef)))
1698-
else
1699-
typeTree(restpe))
1695+
val tree =
1696+
if (mt.isResultDependent) {
1697+
if (formals.length != defaultArity)
1698+
typeTree(WildcardType)
1699+
else
1700+
untpd.InLambdaTypeTree(isResult = true, (_, syms) => restpe.substParams(mt, syms.map(_.termRef)))
1701+
} else
1702+
typeTree(restpe)
1703+
(formals, tree)
17001704
case _ =>
17011705
(List.tabulate(defaultArity)(alwaysWildcardType), untpd.TypeTree())
17021706
}

tests/neg/i123577.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- [E007] Type Mismatch Error: tests/neg/i123577.scala:11:4 ------------------------------------------------------------
2+
11 | (msg: String) => ??? // error
3+
| ^^^^^^^^^^^^^^^^^^^^
4+
| Found: String => Nothing
5+
| Required: MillRpcChannel
6+
|
7+
| longer explanation available when compiling with `-explain`

tests/neg/i123577.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
trait MillRpcMessage {
2+
type Response
3+
}
4+
5+
trait MillRpcChannel {
6+
def apply(requestId: Long, input: MillRpcMessage): input.Response
7+
}
8+
9+
object MillRpcChannel {
10+
def createChannel: MillRpcChannel = {
11+
(msg: String) => ??? // error
12+
}
13+
}

0 commit comments

Comments
 (0)