Skip to content

Commit e8ea8cf

Browse files
authored
Fix #13757: Explicitly disallow higher-kinded scrutinees of match types. (#17322)
2 parents abf9a25 + 0ba35e6 commit e8ea8cf

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
192192
case UnusedNonUnitValueID // errorNumber 176
193193
case ConstrProxyShadowsID // errorNumber 177
194194
case MissingArgumentListID // errorNumber: 178
195+
case MatchTypeScrutineeCannotBeHigherKindedID // errorNumber: 179
195196

196197
def errorNumber = ordinal - 1
197198

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2878,3 +2878,8 @@ class UnusedNonUnitValue(tp: Type)(using Context)
28782878
def kind = MessageKind.PotentialIssue
28792879
def msg(using Context) = i"unused value of type $tp"
28802880
def explain(using Context) = ""
2881+
2882+
class MatchTypeScrutineeCannotBeHigherKinded(tp: Type)(using Context)
2883+
extends TypeMsg(MatchTypeScrutineeCannotBeHigherKindedID) :
2884+
def msg(using Context) = i"the scrutinee of a match type cannot be higher-kinded"
2885+
def explain(using Context) = ""

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2214,8 +2214,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
22142214
if (tree.bound.isEmpty && isFullyDefined(pt, ForceDegree.none)) TypeTree(pt)
22152215
else typed(tree.bound)
22162216
val sel1 = typed(tree.selector)
2217+
val sel1Tpe = sel1.tpe
2218+
if sel1Tpe.isLambdaSub then
2219+
report.error(MatchTypeScrutineeCannotBeHigherKinded(sel1Tpe), sel1.srcPos)
22172220
val pt1 = if (bound1.isEmpty) pt else bound1.tpe
2218-
val cases1 = tree.cases.mapconserve(typedTypeCase(_, sel1.tpe, pt1))
2221+
val cases1 = tree.cases.mapconserve(typedTypeCase(_, sel1Tpe, pt1))
22192222
assignType(cpy.MatchTypeTree(tree)(bound1, sel1, cases1), bound1, sel1, cases1)
22202223
}
22212224

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Test:
2+
type AnyKindMatchType1[X <: AnyKind] = X match // error: the scrutinee of a match type cannot be higher-kinded // error
3+
case Option[a] => Int
4+
5+
type AnyKindMatchType2[X <: AnyKind] = X match // error: the scrutinee of a match type cannot be higher-kinded
6+
case Option => Int // error: Missing type parameter for Option
7+
8+
type AnyKindMatchType3[X <: AnyKind] = X match // error: the scrutinee of a match type cannot be higher-kinded // error
9+
case _ => Int
10+
11+
type AnyKindMatchType4[X <: Option] = X match // error // error: the scrutinee of a match type cannot be higher-kinded // error
12+
case _ => Int
13+
14+
type AnyKindMatchType5[X[_]] = X match // error: the scrutinee of a match type cannot be higher-kinded // error
15+
case _ => Int
16+
end Test

0 commit comments

Comments
 (0)