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 6 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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2453,7 +2453,7 @@ class Typer extends Namer
*/
protected def checkEqualityEvidence(tree: tpd.Tree, pt: Type)(implicit ctx: Context) : Unit = {
tree match {
case _: RefTree | _: Literal
case _: RefTree | _: Literal | _: Apply
if !isVarPattern(tree) &&
!(tree.tpe <:< pt) (ctx.addMode(Mode.GADTflexible)) =>
val cmp =
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
21 changes: 21 additions & 0 deletions tests/neg/i1846.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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 => () } // ok
x match { case { Y.toInt } => () } // error
x match { case { Y }.toInt => () } // error
x match { case Y.toString => () } // error
x match { case { Y }.toString => () } // error
x match { case { Y.toString } => () } // error
}
}
14 changes: 14 additions & 0 deletions tests/neg/i3812.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
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
x match { case Y.toInt => () } // ok
}
}
8 changes: 8 additions & 0 deletions tests/run/i3812.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
object Test {
def main(args: Array[String]): Unit = {
val x = 42
val Y = "42"

x match { case Y.toInt => () } // ok
Copy link
Contributor

Choose a reason for hiding this comment

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

This is an error in scalac, I don't think we should support it either

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, a check for stable prefix is missing for patterns here.

Copy link
Contributor Author

@liufengyun liufengyun Jan 18, 2018

Choose a reason for hiding this comment

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

Now I'm a little unsure about Scalac behavior, as it also rejects the following code:

    var X = 42
    42 match { case X          => () }

It seems to me it can be accepted without any issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}
}