Skip to content

Fix #1846: add regression test #3810

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

Merged
merged 10 commits into from
Jan 27, 2018
Merged
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
3 changes: 0 additions & 3 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1564,7 +1564,6 @@ object Parsers {
* | `(' [Patterns] `)'
* | SimplePattern1 [TypeArgs] [ArgumentPatterns]
* SimplePattern1 ::= Path
* | `{' Block `}'
* | SimplePattern1 `.' id
* PatVar ::= id
* | `_'
Expand All @@ -1587,8 +1586,6 @@ object Parsers {
} else wildIndent
case LPAREN =>
atPos(in.offset) { makeTupleOrParens(inParens(patternsOpt())) }
case LBRACE =>
dotSelectors(blockExpr())
case XMLSTART =>
xmlLiteralPattern()
case _ =>
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/ReTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class ReTyper extends Typer {

override def typedSelect(tree: untpd.Select, pt: Type)(implicit ctx: Context): Tree = {
assert(tree.hasType, tree)
val qual1 = typed(tree.qualifier, AnySelectionProto)
// a qualifier cannot be a pattern
val qual1 = typed(tree.qualifier, AnySelectionProto)(ctx.retractMode(Mode.Pattern))
untpd.cpy.Select(tree)(qual1, tree.name).withType(tree.typeOpt)
}

Expand Down
16 changes: 15 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,23 @@ class Typer extends Namer
tree.withType(ownType)
}

checkStableIdentPattern(tree1, pt)
checkValue(tree1, pt)
}

/** Check that a stable identifier pattern is indeed stable (SLS 8.1.5)
*/
private def checkStableIdentPattern(tree: Tree, pt: Type)(implicit ctx: Context): Tree = {
if (ctx.mode.is(Mode.Pattern) &&
!tree.isType &&
!pt.isInstanceOf[ApplyingProto] &&
!tree.tpe.isStable &&
!isWildcardArg(tree))
ctx.error(s"stable identifier required, but ${tree.show} found", tree.pos)

tree
}

private def typedSelect(tree: untpd.Select, pt: Type, qual: Tree)(implicit ctx: Context): Select =
checkValue(assignType(cpy.Select(tree)(qual, tree.name), qual), pt)

Expand All @@ -418,7 +432,7 @@ class Typer extends Namer
val qual1 = typedExpr(tree.qualifier, selectionProto(tree.name, pt, this))
if (tree.name.isTypeName) checkStable(qual1.tpe, qual1.pos)
val select = typedSelect(tree, pt, qual1)
if (select.tpe ne TryDynamicCallType) select
if (select.tpe ne TryDynamicCallType) checkStableIdentPattern(select, pt)
else if (pt.isInstanceOf[PolyProto] || pt.isInstanceOf[FunProto] || pt == AssignProto) select
else typedDynamicSelect(tree, Nil, pt)
}
Expand Down
3 changes: 1 addition & 2 deletions docs/docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ ParArgumentExprs ::= ‘(’ ExprsInParens ‘)’
| ‘(’ [ExprsInParens] PostfixExpr ‘:’ ‘_’ ‘*’ ‘)’ exprs :+ Typed(expr, Ident(wildcardStar))
ArgumentExprs ::= ParArgumentExprs
| [nl] BlockExpr
BlockExpr ::= ‘{’ BlockExprContents ‘}’
BlockExpr ::= ‘{’ BlockExprContents ‘}’
BlockExprContents ::= CaseClauses | Block
Block ::= {BlockStat semi} [BlockResult] Block(stats, expr?)
BlockStat ::= Import
Expand Down Expand Up @@ -234,7 +234,6 @@ SimplePattern ::= PatVar
| XmlPattern
| SimplePattern1 [TypeArgs] [ArgumentPatterns]
SimplePattern1 ::= Path
| ‘{’ Block ‘}’
| SimplePattern1 ‘.’ id
PatVar ::= varid
| ‘_’
Expand Down
19 changes: 19 additions & 0 deletions tests/neg/i1846.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
object Test {
def main(args: Array[String]): Unit = {
val x = 42
val Y = "42"

x match { case { 42 } => () } // error
x match { case { 42.toString } => () } // error
x match { case { 42 }.toString => () } // error
x match { case "42".toInt => () } // error
x match { case { "42".toInt } => () } // error
x match { case { "42" }.toInt => () } // error
x match { case { "42".toInt } => () } // error
x match { case Y => () } // error
x match { case { Y.toInt } => () } // error
x match { case { Y }.toInt => () } // error
x match { case { Y }.toString => () } // error
x match { case { Y.toString } => () } // error
}
}
13 changes: 13 additions & 0 deletions tests/neg/i3812.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
object Test {
def main(args: Array[String]): Unit = {
val x = 42
val Y = "42"

x match { case { 42 } => () } // error
x match { case { "42".toInt } => () } // error
x match { case { "42" }.toInt => () } // error
x match { case { "42".toInt } => () } // error
x match { case { Y.toInt } => () } // error
x match { case { Y }.toInt => () } // error
}
}
27 changes: 27 additions & 0 deletions tests/neg/i3812b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
object Test {
def main(args: Array[String]): Unit = {
case class Box(v: Int)

val x = 42
var Y1 = 42
val Y2 = "42"
var Z1 = Box(4)
val Z2 = Box(4)

x match { case Y1 => () } // error
x match { case Y2.toInt => () } // error
x match { case Y1.toString => () } // error

x match { case Z1.v => () } // error
x match { case Z2.v => () } // ok

Some(x) match { case Some(Z1.v) => () } // error
Some(x) match { case Some(Z2.v) => () } // ok

Some(x) match { case Some(4) | Some(Z1.v) => () } // error
Some(x) match { case a @ Some(Z1.v) => () } // error

Some(x) match { case Some(4) | Some(Z2.v) => () } // ok
Some(x) match { case a @ Some(Z2.v) => () } // ok
}
}