Skip to content

Commit 28454fa

Browse files
committed
Require scrutinees to be Scrutable
1 parent 50fd5ff commit 28454fa

File tree

6 files changed

+20
-8
lines changed

6 files changed

+20
-8
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,5 +140,6 @@ class ReTyper extends Typer with ReChecking {
140140

141141
override protected def addAccessorDefs(cls: Symbol, body: List[Tree])(using Context): List[Tree] = body
142142
override protected def checkEqualityEvidence(tree: tpd.Tree, pt: Type)(using Context): Unit = ()
143+
override protected def checkScrutable(sel: Tree)(using Context): Unit = ()
143144
override protected def matchingApply(methType: MethodOrPoly, pt: FunProto)(using Context): Boolean = true
144-
}
145+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,8 @@ class Typer extends Namer
12941294
assignType(cpy.Closure(tree)(env1, meth1, target), meth1, target)
12951295
}
12961296

1297+
protected def checkScrutable(sel: Tree)(using Context): Unit = adapt(sel, defn.ScrutableType)
1298+
12971299
def typedMatch(tree: untpd.Match, pt: Type)(using Context): Tree =
12981300
tree.selector match {
12991301
case EmptyTree =>
@@ -1315,9 +1317,10 @@ class Typer extends Namer
13151317
typed(desugar.makeCaseLambda(tree.cases, checkMode, protoFormals.length).withSpan(tree.span), pt)
13161318
}
13171319
case _ =>
1318-
if (tree.isInline) checkInInlineContext("inline match", tree.srcPos)
13191320
val sel1 = typedExpr(tree.selector)
13201321
val selType = fullyDefinedType(sel1.tpe, "pattern selector", tree.span).widen
1322+
if tree.isInline then checkInInlineContext("inline match", tree.srcPos)
1323+
else checkScrutable(sel1)
13211324

13221325
/** Extractor for match types hidden behind an AppliedType/MatchAlias */
13231326
object MatchTypeInDisguise {

tests/run-macros/i8514.check

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
List(class Object, class Classable, class Any)
2-
List(class A, class Object, class Classable, class Any)
3-
List(class B, class A, class Object, class Classable, class Any)
4-
List(class C, class B, class A, class Object, class Classable, class Any)
1+
List(class Object, class Scrutable, class Any)
2+
List(class A, class Object, class Scrutable, class Any)
3+
List(class B, class A, class Object, class Scrutable, class Any)
4+
List(class C, class B, class A, class Object, class Scrutable, class Any)

tests/run-macros/i8514b.check

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
B
22
A[[T >: scala.Nothing <: scala.Any] => P[T], scala.Predef.String]
33
java.lang.Object
4-
scala.Classable
4+
scala.Scrutable
55
scala.Any

tests/run/scrutable.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
class java.lang.Integer
2+
int
23
class java.lang.String
4+
string

tests/run/scrutable.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
def foo[T](x: Any): Scrutable =
22
println(x.getClass())
3-
x
3+
x match
4+
case x: Int =>
5+
println("int")
6+
x
7+
case x: String =>
8+
println("string")
9+
x
410

511
@main def Test =
612
val x: Scrutable = foo(1)

0 commit comments

Comments
 (0)