Skip to content

Fix #10994: align typed pattern syntax with Scala 2 #11023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1223,7 +1223,7 @@ object Types {
* then the top-level union isn't widened. This is needed so that type inference can infer nullable types.
*/
def widenUnion(using Context): Type = widen match
case tp @ OrNull(tp1): OrType =>
case tp @ OrNull(tp1) =>
// Don't widen `T|Null`, since otherwise we wouldn't be able to infer nullable unions.
val tp1Widen = tp1.widenUnionWithoutNull
if (tp1Widen.isRef(defn.AnyClass)) tp1Widen
Expand Down Expand Up @@ -3158,7 +3158,7 @@ object Types {
object OrNull {
def apply(tp: Type)(using Context) =
OrType(tp, defn.NullType, soft = false)
def unapply(tp: Type)(using Context): Option[Type] =
def unapply(tp: OrType)(using Context): Option[Type] =
if (ctx.explicitNulls) {
val tp1 = tp.stripNull()
if tp1 ne tp then Some(tp1) else None
Expand Down
6 changes: 4 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2601,11 +2601,13 @@ object Parsers {
if (isIdent(nme.raw.BAR)) { in.nextToken(); pattern1(location) :: patternAlts(location) }
else Nil

/** Pattern1 ::= Pattern2 [Ascription]
/** Pattern1 ::= PatVar Ascription
* | SimpleLiteral Ascription
* | Pattern2
*/
def pattern1(location: Location = Location.InPattern): Tree =
val p = pattern2()
if in.token == COLON then
if (isVarPattern(p) || p.isInstanceOf[Number]) && in.token == COLON then
in.nextToken()
ascription(p, location)
else p
Expand Down
12 changes: 9 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -425,13 +425,19 @@ class Typer extends Namer
* (x: T | Null) => x.$asInstanceOf$[x.type & T]
*/
def toNotNullTermRef(tree: Tree, pt: Type)(using Context): Tree = tree.tpe match
case ref @ OrNull(tpnn) : TermRef
if pt != AssignProto && // Ensure it is not the lhs of Assign
case ref: TermRef
if ctx.explicitNulls &&
pt != AssignProto && // Ensure it is not the lhs of Assign
ctx.notNullInfos.impliesNotNull(ref) &&
// If a reference is in the context, it is already trackable at the point we add it.
// Hence, we don't use isTracked in the next line, because checking use out of order is enough.
!ref.usedOutOfOrder =>
tree.select(defn.Any_typeCast).appliedToType(AndType(ref, tpnn))
val tp1 = ref.widenDealias
val tp2 = tp1.stripNull()
if tp1 ne tp2 then
tree.select(defn.Any_typeCast).appliedToType(AndType(ref, tp2))
else
tree
case _ =>
tree

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/contributing/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,5 @@ Expect files are used as regression tests to detect changes in the compiler.

The test suite will create a new file if it detects any difference, which can be compared with the
original expect file, or if the user wants to globally replace all expect files for semanticdb they can use
`dotty-compiler-bootstrapped/test:runMain dotty.tools.dotc.semanticdb.updateExpect`, and compare the changes via version
`scala3-compiler-bootstrapped/test:runMain dotty.tools.dotc.semanticdb.updateExpect`, and compare the changes via version
control.
4 changes: 3 additions & 1 deletion docs/docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]

Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
Pattern1 ::= PatVar ‘:’ RefinedType Bind(name, Typed(Ident(wildcard), tpe))
| SimpleLiteral ‘:’ RefinedType Typed(pat, tpe)
| Pattern2
Pattern2 ::= [id ‘@’] InfixPattern Bind(name, pat)
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
SimplePattern ::= PatVar Ident(wildcard)
Expand Down
2 changes: 2 additions & 0 deletions tests/neg/i10994.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def foo = true match
case (b: Boolean): Boolean => () // error
2 changes: 1 addition & 1 deletion tests/neg/i8407.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object Test:
val xs = List(1, 2, 3, 4, 5)
xs match {
case List(1, 2, xs1 @ xs2: _*) => println(xs2) // error // error
case List(1, 2, xs1 @ xs2: _*) => println(xs2) // error
case _ => ()
}
7 changes: 1 addition & 6 deletions tests/pos/patmat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ object Test {
}

(xs.length, xs) match {
case (0, Nil: List[Int]) => println("1")
case (0, Nil) => println("1")
case (_, Nil) => println("2")
case (0, _) => println("3")
case (x, y) => println("4")
Expand Down Expand Up @@ -46,9 +46,4 @@ object Test {
case Some(s) => println(s)
case None => println("nothing")
}

type IntPair = (Int, Int)
??? match {
case (x, y): IntPair => x * y
}
}
2 changes: 1 addition & 1 deletion tests/semanticdb/expect/ValPattern.expect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ValPattern/*<-example::ValPattern#*/ {
val Some/*->scala::Some.*//*->scala::Some.unapply().*/(number1/*<-example::ValPattern#number1.*/) =
Some/*->scala::Some.*//*->scala::Some.apply().*/(1)

val List/*->scala::package.List.*//*->scala::collection::SeqFactory#unapplySeq().*/(Some/*->scala::Some.*//*->scala::Some.unapply().*/(q1/*<-example::ValPattern#q1.*/), None/*->scala::None.*/: None/*->scala::None.*/.type, None/*->scala::None.*/) = ???/*->scala::Predef.`???`().*/
val List/*->scala::package.List.*//*->scala::collection::SeqFactory#unapplySeq().*/(Some/*->scala::Some.*//*->scala::Some.unapply().*/(q1/*<-example::ValPattern#q1.*/), None/*->scala::None.*/) = ???/*->scala::Predef.`???`().*/

var (leftVar/*<-example::ValPattern#leftVar().*/, rightVar/*<-example::ValPattern#rightVar().*/) = (/*->scala::Tuple2.apply().*/1, 2)
var Some/*->scala::Some.*//*->scala::Some.unapply().*/(number1Var/*<-example::ValPattern#number1Var().*/) =
Expand Down
2 changes: 1 addition & 1 deletion tests/semanticdb/expect/ValPattern.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ValPattern {
val Some(number1) =
Some(1)

val List(Some(q1), None: None.type, None) = ???
val List(Some(q1), None) = ???

var (leftVar, rightVar) = (1, 2)
var Some(number1Var) =
Expand Down
6 changes: 2 additions & 4 deletions tests/semanticdb/metac.expect
Original file line number Diff line number Diff line change
Expand Up @@ -2710,7 +2710,7 @@ Uri => ValPattern.scala
Text => empty
Language => Scala
Symbols => 22 entries
Occurrences => 63 entries
Occurrences => 61 entries

Symbols:
example/ValPattern# => class ValPattern
Expand Down Expand Up @@ -2754,9 +2754,7 @@ Occurrences:
[8:15..8:15): -> scala/Some.unapply().
[8:16..8:18): q1 <- example/ValPattern#q1.
[8:21..8:25): None -> scala/None.
[8:27..8:31): None -> scala/None.
[8:38..8:42): None -> scala/None.
[8:46..8:49): ??? -> scala/Predef.`???`().
[8:29..8:32): ??? -> scala/Predef.`???`().
[10:7..10:14): leftVar <- example/ValPattern#leftVar().
[10:16..10:24): rightVar <- example/ValPattern#rightVar().
[10:29..10:29): -> scala/Tuple2.apply().
Expand Down