Skip to content

fix #11897: error on given pattern in val def #12631

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 1 commit into from
Jun 3, 2021
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
43 changes: 34 additions & 9 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,16 @@ object desugar {
case IdPattern(named, tpt) =>
derivedValDef(original, named, tpt, rhs, mods)
case _ =>

def filterWildcardGivenBinding(givenPat: Bind): Boolean =
givenPat.name != nme.WILDCARD

def errorOnGivenBinding(bind: Bind)(using Context): Boolean =
report.error(
em"""${hl("given")} patterns are not allowed in a ${hl("val")} definition,
|please bind to an identifier and use an alias given.""".stripMargin, bind)
false

def isTuplePattern(arity: Int): Boolean = pat match {
case Tuple(pats) if pats.size == arity =>
pats.forall(isVarPattern)
Expand All @@ -1108,13 +1118,23 @@ object desugar {
// - `pat` is a tuple of N variables or wildcard patterns like `(x1, x2, ..., xN)`
val tupleOptimizable = forallResults(rhs, isMatchingTuple)

val inAliasGenerator = original match
case _: GenAlias => true
case _ => false

val vars =
if (tupleOptimizable) // include `_`
pat match {
case Tuple(pats) =>
pats.map { case id: Ident => id -> TypeTree() }
}
else getVariables(pat) // no `_`
pat match
case Tuple(pats) => pats.map { case id: Ident => id -> TypeTree() }
else
getVariables(
tree = pat,
shouldAddGiven =
if inAliasGenerator then
filterWildcardGivenBinding
else
errorOnGivenBinding
) // no `_`

val ids = for ((named, _) <- vars) yield Ident(named.name)
val caseDef = CaseDef(pat, EmptyTree, makeTuple(ids))
Expand Down Expand Up @@ -1800,16 +1820,21 @@ object desugar {
/** Returns list of all pattern variables, possibly with their types,
* without duplicates
*/
private def getVariables(tree: Tree)(using Context): List[VarInfo] = {
private def getVariables(tree: Tree, shouldAddGiven: Context ?=> Bind => Boolean)(using Context): List[VarInfo] = {
val buf = ListBuffer[VarInfo]()
def seenName(name: Name) = buf exists (_._1.name == name)
def add(named: NameTree, t: Tree): Unit =
if (!seenName(named.name) && named.name.isTermName) buf += ((named, t))
def collect(tree: Tree): Unit = tree match {
case Bind(nme.WILDCARD, tree1) =>
case tree @ Bind(nme.WILDCARD, tree1) =>
if tree.mods.is(Given) then
val Typed(_, tpt) = tree1: @unchecked
if shouldAddGiven(tree) then
add(tree, tpt)
collect(tree1)
case tree @ Bind(_, Typed(tree1, tpt)) =>
add(tree, tpt)
if !(tree.mods.is(Given) && !shouldAddGiven(tree)) then
add(tree, tpt)
collect(tree1)
case tree @ Bind(_, tree1) =>
add(tree, TypeTree())
Expand All @@ -1827,7 +1852,7 @@ object desugar {
case SeqLiteral(elems, _) =>
elems foreach collect
case Alternative(trees) =>
for (tree <- trees; (vble, _) <- getVariables(tree))
for (tree <- trees; (vble, _) <- getVariables(tree, shouldAddGiven))
report.error(IllegalVariableInPatternAlternative(), vble.srcPos)
case Annotated(arg, _) =>
collect(arg)
Expand Down
10 changes: 5 additions & 5 deletions tests/neg/given-pattern.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ class Test {
import scala.collection.immutable.{TreeSet, HashSet}

def f2[T](x: Ordering[T]) = {
val (given Ordering[T]) = x
new TreeSet[T] // error: no implicit ordering defined for T
val (given Ordering[T]) = x // error: given Ordering[T] not allowed here
new TreeSet[T] // error: no implicit ordering defined for T
}
def f3[T](x: Ordering[T]) = {
val given Ordering[T] = x
new TreeSet[T] // error: no implicit ordering defined for T
val given Ordering[T] = x // error: given Ordering[T] not allowed here
new TreeSet[T] // error: no implicit ordering defined for T
}
}
}
45 changes: 45 additions & 0 deletions tests/neg/i11897.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- Error: tests/neg/i11897.scala:11:10 ---------------------------------------------------------------------------------
11 | val (x, given A) = (1, A(23)) // error
| ^^^^^^^
| given patterns are not allowed in a val definition,
| please bind to an identifier and use an alias given.
-- Error: tests/neg/i11897.scala:12:10 ---------------------------------------------------------------------------------
12 | val (_, given B) = (true, B(false)) // error
| ^^^^^^^
| given patterns are not allowed in a val definition,
| please bind to an identifier and use an alias given.
-- Error: tests/neg/i11897.scala:13:8 ----------------------------------------------------------------------------------
13 | val D(given C) = D(C("c")) // error
| ^^^^^^^
| given patterns are not allowed in a val definition,
| please bind to an identifier and use an alias given.
-- Error: tests/neg/i11897.scala:14:11 ---------------------------------------------------------------------------------
14 | val F(y, given E) = F(47, E(93)) // error
| ^^^^^^^
| given patterns are not allowed in a val definition,
| please bind to an identifier and use an alias given.
-- Error: tests/neg/i11897.scala:15:11 ---------------------------------------------------------------------------------
15 | val H(z, q @ given G) = H(47, G(101)) // error
| ^^^^^^^^^^^
| given patterns are not allowed in a val definition,
| please bind to an identifier and use an alias given.
-- Error: tests/neg/i11897.scala:16:18 ---------------------------------------------------------------------------------
16 | assert(summon[A] == A(23)) // error
| ^
| no implicit argument of type A was found for parameter x of method summon in object Predef
-- Error: tests/neg/i11897.scala:17:18 ---------------------------------------------------------------------------------
17 | assert(summon[B] == B(false)) // error
| ^
| no implicit argument of type B was found for parameter x of method summon in object Predef
-- Error: tests/neg/i11897.scala:18:18 ---------------------------------------------------------------------------------
18 | assert(summon[C] == C("c")) // error
| ^
| no implicit argument of type C was found for parameter x of method summon in object Predef
-- Error: tests/neg/i11897.scala:19:18 ---------------------------------------------------------------------------------
19 | assert(summon[E] == E(93)) // error
| ^
| no implicit argument of type E was found for parameter x of method summon in object Predef
-- Error: tests/neg/i11897.scala:20:18 ---------------------------------------------------------------------------------
20 | assert(summon[G] == G(101)) // error
| ^
| no implicit argument of type G was found for parameter x of method summon in object Predef
20 changes: 20 additions & 0 deletions tests/neg/i11897.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
case class A(i: Int)
case class B(b: Boolean)
case class C(s: String)
case class D(c: C)
case class E(i: Int)
case class F(i: Int, e: E)
case class G(i: Int)
case class H(i: Int, e: G)

def Test =
val (x, given A) = (1, A(23)) // error
val (_, given B) = (true, B(false)) // error
val D(given C) = D(C("c")) // error
val F(y, given E) = F(47, E(93)) // error
val H(z, q @ given G) = H(47, G(101)) // error
assert(summon[A] == A(23)) // error
assert(summon[B] == B(false)) // error
assert(summon[C] == C("c")) // error
assert(summon[E] == E(93)) // error
assert(summon[G] == G(101)) // error
39 changes: 39 additions & 0 deletions tests/run/i10178.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,42 @@
x <- Option(23)
given Int = x
do assert(summon[Int] == 23)

for
y <- Option("ok")
q @ given String = y
do assert(summon[String] == "ok")

for
z <- Option("key" -> true)
(q @ given String, u @ given Boolean) = z
do
assert(summon[String] == "key")
assert(summon[Boolean] == true)

for
w <- Option("no" -> false)
(given String, given Boolean) = w
do
assert(summon[String] == "no")
assert(summon[Boolean] == false)

for
given Int <- Option(23)
do assert(summon[Int] == 23)

for
q @ given String <- Option("ok")
do assert(summon[String] == "ok")

for
(q @ given String, u @ given Boolean) <- Option("key" -> true)
do
assert(summon[String] == "key")
assert(summon[Boolean] == true)

for
(given String, given Boolean) <- Option("no" -> false)
do
assert(summon[String] == "no")
assert(summon[Boolean] == false)