Skip to content

Commit 17eee73

Browse files
authored
Merge pull request #607 from scala/backport-lts-3.3-23877
Backport "Prevent crash in SAM conversion with mismatched arity" to 3.3 LTS
2 parents 5f88203 + 9bc5eda commit 17eee73

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
@@ -1407,11 +1407,15 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
14071407
case defn.ErasedFunctionOf(mt @ MethodTpe(_, formals, restpe)) if formals.length == defaultArity =>
14081408
(formals, untpd.DependentTypeTree(syms => restpe.substParams(mt, syms.map(_.termRef))))
14091409
case SAMType(mt @ MethodTpe(_, formals, restpe), _) =>
1410-
(formals,
1411-
if (mt.isResultDependent)
1412-
untpd.DependentTypeTree(syms => restpe.substParams(mt, syms.map(_.termRef)))
1413-
else
1414-
typeTree(restpe))
1410+
val tree =
1411+
if (mt.isResultDependent) {
1412+
if (formals.length != defaultArity)
1413+
typeTree(WildcardType)
1414+
else
1415+
untpd.DependentTypeTree(syms => restpe.substParams(mt, syms.map(_.termRef)))
1416+
} else
1417+
typeTree(restpe)
1418+
(formals, tree)
14151419
case _ =>
14161420
(List.tabulate(defaultArity)(alwaysWildcardType), untpd.TypeTree())
14171421
}

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)