Skip to content

Commit 7015a77

Browse files
nox213WojciechMazur
authored andcommitted
Prevent crash in SAM conversion with mismatched arity
rename test file fix fix test address reviews [Cherry-picked 8adc284]
1 parent a09d41d commit 7015a77

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
@@ -1683,11 +1683,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
16831683
val restpe = mt.resultType match
16841684
case mt: MethodType => mt.toFunctionType(isJava = samParent.classSymbol.is(JavaDefined))
16851685
case tp => tp
1686-
(formals,
1687-
if (mt.isResultDependent)
1688-
untpd.InLambdaTypeTree(isResult = true, (_, syms) => restpe.substParams(mt, syms.map(_.termRef)))
1689-
else
1690-
typeTree(restpe))
1686+
val tree =
1687+
if (mt.isResultDependent) {
1688+
if (formals.length != defaultArity)
1689+
typeTree(WildcardType)
1690+
else
1691+
untpd.InLambdaTypeTree(isResult = true, (_, syms) => restpe.substParams(mt, syms.map(_.termRef)))
1692+
} else
1693+
typeTree(restpe)
1694+
(formals, tree)
16911695
case _ =>
16921696
(List.tabulate(defaultArity)(alwaysWildcardType), untpd.TypeTree())
16931697
}

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)