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 9 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
35 changes: 35 additions & 0 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,21 @@ object RefChecks {
}
}

/** Check that a stable identifier pattern is indeed stable (SLS 8.1.5)
*/
private def checkStableIdentPattern(tree: Tree)(implicit ctx: Context) = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it would be simpler to put this check in Typer.

Here, we could just add if (ctx.mode.is(Mode.Pattern) && !tree.tpe.isStable) error to the checks for Ident and Select.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I am also no sure why we should demand stability? What would go wrong if we didn't?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The following check seems too strong to me:

if (ctx.mode.is(Mode.Pattern) && !tree.tpe.isStable) error

As a selection of unapply would fail this check. The check should only happen for Stable Identifier Pattern (SLS 8.1.5). I don't know what's the motivation for the Scala specification. Arguably, a var could be allowed in a pattern, but then it complicates the specification and check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so let's keep the test for stable idents but move it to Typer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, now it's done.

def error = ctx.error(s"stable identifier required, but ${tree.show} found", tree.pos)
tree match {
case _: Ident if !isWildcardArg(tree) =>
if (!tree.tpe.isStable) error
case _: Select =>
if (!tree.tpe.isStable) error
case _: Apply =>
error
case _ =>
}
}

/** The this-type of `cls` which should be used when looking at the types of
* inherited members. If `cls` has a non-trivial self type, this returns a skolem
* with the class type instead of the `this-type` of the class as usual.
Expand Down Expand Up @@ -884,6 +899,26 @@ class RefChecks extends MiniPhase { thisPhase =>
currentLevel.enterReference(sym, tree.pos)
tree
}

override def transformUnApply(tree: UnApply)(implicit ctx: Context): Tree = {
tree.patterns.foreach(checkStableIdentPattern(_))
tree
}

override def transformAlternative(tree: Alternative)(implicit ctx: Context): Tree = {
tree.trees.foreach(checkStableIdentPattern(_))
tree
}

override def transformBind(tree: Bind)(implicit ctx: Context): Tree = {
checkStableIdentPattern(tree.body)
tree
}

override def transformCaseDef(tree: CaseDef)(implicit ctx: Context) = {
checkStableIdentPattern(tree.pat)
tree
}
}

/* todo: rewrite and re-enable
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
}
}