Skip to content

No compilation errors on xml.Elem pattern matching with wrong number of arguments #7623

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
scabug opened this issue Jun 28, 2013 · 3 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Jun 28, 2013

There is no compilation error with wrong number of arguments for xml.Elem:

<root /> match { case Elem(prefix, lable, attribs, scope, minimizeEmpty, content @ _*) => ??? }

Method unapplySeq returns Option[(String, String, scala.xml.MetaData, scala.xml.NamespaceBinding, Seq[scala.xml.Node])].

It should be called unapply to produce a compilation error:

object ElemHelper {
  def unapply(n: Node) = Elem.unapplySeq(n)
}

<root /> match { case ElemHelper(prefix, lable, attribs, scope, minimizeEmpty, content @ _*) => ??? }
<console>:15: error: wrong number of arguments for object ElemHelper

See this SO question for error case. http://stackoverflow.com/q/17357359/406435

@scabug
Copy link
Author

scabug commented Jun 28, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7623?orig=1
Reporter: Senia Popugaev (senia)
Affected Versions: 2.10.1, 2.11.0-M3
See #6524

@scabug
Copy link
Author

scabug commented Dec 17, 2014

@som-snytt said:
s/wrong number of args/too many args/

You get an error if there are not enough args, since the varargs don't align. That is true in general.

The Elem object serves as an extractor for Nodes, which have no notion of minimizeEmpty.

(That extractor tripped someone else up recently, so maybe that's a design issue.)

It might make a nice Abide rule to warn about this sort of thing.

Or maybe a compiler lint option to warn about "splitting" a varargs. Is that a general issue? Maybe only when mixing args and varargs.

scala> val r = "(a*)(b*)(c*)".r
r: scala.util.matching.Regex = (a*)(b*)(c*)

scala> "abc" match { case r(as, rest @ _*) => as }  // dangerous?
res5: String = a

scala> "abc" match { case r(as, _, _) => as }
res6: String = a

@scabug
Copy link
Author

scabug commented Dec 18, 2014

@som-snytt said:
Maybe scala/scala#4216

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants