File tree 5 files changed +59
-10
lines changed
compiler/src/dotty/tools/dotc/typer
5 files changed +59
-10
lines changed Original file line number Diff line number Diff line change @@ -75,6 +75,15 @@ object RefChecks {
75
75
}
76
76
}
77
77
78
+ /** Check that a stable identifier pattern is indeed stable (SLS 8.1.5)
79
+ */
80
+ private def checkStableIdentPattern (tree : Tree )(implicit ctx : Context ) = tree match {
81
+ case _ : Select | _ : Ident if ! isWildcardArg(tree) =>
82
+ if (! tree.tpe.isStable)
83
+ ctx.error(s " stable identifier required, but ${tree.show} found " , tree.pos)
84
+ case _ =>
85
+ }
86
+
78
87
/** The this-type of `cls` which should be used when looking at the types of
79
88
* inherited members. If `cls` has a non-trivial self type, this returns a skolem
80
89
* with the class type instead of the `this-type` of the class as usual.
@@ -884,6 +893,26 @@ class RefChecks extends MiniPhase { thisPhase =>
884
893
currentLevel.enterReference(sym, tree.pos)
885
894
tree
886
895
}
896
+
897
+ override def transformUnApply (tree : UnApply )(implicit ctx : Context ): Tree = {
898
+ tree.patterns.foreach(checkStableIdentPattern(_))
899
+ tree
900
+ }
901
+
902
+ override def transformAlternative (tree : Alternative )(implicit ctx : Context ): Tree = {
903
+ tree.trees.foreach(checkStableIdentPattern(_))
904
+ tree
905
+ }
906
+
907
+ override def transformBind (tree : Bind )(implicit ctx : Context ): Tree = {
908
+ checkStableIdentPattern(tree.body)
909
+ tree
910
+ }
911
+
912
+ override def transformCaseDef (tree : CaseDef )(implicit ctx : Context ) = {
913
+ checkStableIdentPattern(tree.pat)
914
+ tree
915
+ }
887
916
}
888
917
889
918
/* todo: rewrite and re-enable
Original file line number Diff line number Diff line change @@ -11,7 +11,6 @@ object Test {
11
11
x match { case { " 42" }.toInt => () } // error
12
12
x match { case { " 42" .toInt } => () } // error
13
13
x match { case Y => () } // error
14
- x match { case Y .toInt => () } // ok
15
14
x match { case { Y .toInt } => () } // error
16
15
x match { case { Y }.toInt => () } // error
17
16
x match { case Y .toString => () } // error
Original file line number Diff line number Diff line change 1
1
object Test {
2
2
def main (args : Array [String ]): Unit = {
3
+ case class Box (v : Int )
4
+
3
5
val x = 42
4
6
val Y = " 42"
7
+ var Z1 = Box (4 )
8
+ val Z2 = Box (4 )
5
9
6
10
x match { case { 42 } => () } // error
7
11
x match { case { " 42" .toInt } => () } // error
8
12
x match { case { " 42" }.toInt => () } // error
9
13
x match { case { " 42" .toInt } => () } // error
10
14
x match { case { Y .toInt } => () } // error
11
15
x match { case { Y }.toInt => () } // error
12
- x match { case Y .toInt => () } // ok
13
16
}
14
17
}
Original file line number Diff line number Diff line change
1
+ object Test {
2
+ def main (args : Array [String ]): Unit = {
3
+ case class Box (v : Int )
4
+
5
+ val x = 42
6
+ var Y1 = 42
7
+ val Y2 = " 42"
8
+ var Z1 = Box (4 )
9
+ val Z2 = Box (4 )
10
+
11
+ x match { case Y1 => () } // error
12
+ x match { case Y2 .toInt => () } // error
13
+
14
+ x match { case Z1 .v => () } // error
15
+ x match { case Z2 .v => () } // ok
16
+
17
+ Some (x) match { case Some (Z1 .v) => () } // error
18
+ Some (x) match { case Some (Z2 .v) => () } // ok
19
+
20
+ Some (x) match { case Some (4 ) | Some (Z1 .v) => () } // error
21
+ Some (x) match { case a @ Some (Z1 .v) => () } // error
22
+
23
+ Some (x) match { case Some (4 ) | Some (Z2 .v) => () } // ok
24
+ Some (x) match { case a @ Some (Z2 .v) => () } // ok
25
+ }
26
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments