Skip to content

Fix #6253: Handle repeated args in quoted patterns #6254

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

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Apr 7, 2019

  • Handle repeated args in quoted patterns in Typer
  • Handle repeated args in quoted patterns in Matcher
  • Add Repeated extractor to get Seq[Expr[T]] from an Expr[Seq[T]]

@nicolasstucki nicolasstucki force-pushed the add-quote-pattern-on-repeated-args branch 2 times, most recently from 7b0992f to 6f9cd74 Compare April 7, 2019 14:59
@nicolasstucki nicolasstucki self-assigned this Apr 7, 2019
@nicolasstucki nicolasstucki force-pushed the add-quote-pattern-on-repeated-args branch 3 times, most recently from 14eadd0 to 46e7c7a Compare April 8, 2019 08:22
* Handle repeated args in quoted patterns in typer
* Handle repeated args in quoted patterns in Matcher
* Add Repeated extractor to get Seq[Expr[T]] from a Expr[Seq[T]]
@nicolasstucki nicolasstucki force-pushed the add-quote-pattern-on-repeated-args branch from 46e7c7a to b2679f8 Compare April 8, 2019 08:26
@nicolasstucki nicolasstucki marked this pull request as ready for review April 8, 2019 08:48
@nicolasstucki nicolasstucki changed the title Add pattern matching on varargs Fix #6253: Handle repeated args in quoted patterns Apr 8, 2019
@nicolasstucki nicolasstucki removed their assignment Apr 8, 2019
@nicolasstucki nicolasstucki requested a review from liufengyun April 8, 2019 08:51
Copy link
Contributor

@liufengyun liufengyun left a comment

Choose a reason for hiding this comment

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

Otherwise, LGTM

finally {
val pat1 = pat.subst(defn.RepeatedParamClass :: Nil, defn.SeqClass :: Nil)
patBuf += pat1
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The change here and above looks dubious to me. @odersky could you have a look?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure a global substitution is the right thing here. I fear it would also affect nested varargs methods that would then become Seq methods. I'd do instead:

val patType = pat.tpe.widen
val patType1 = patType.underlyingIfRepeated(isJava = false)
val pat1 = if (patType eq patType1) pat else pat.withType(patType1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use this code now and also updated another place that had the subst to use underlyingIfRepeated.

@nicolasstucki nicolasstucki assigned odersky and unassigned liufengyun Apr 9, 2019
finally {
val pat1 = pat.subst(defn.RepeatedParamClass :: Nil, defn.SeqClass :: Nil)
patBuf += pat1
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure a global substitution is the right thing here. I fear it would also affect nested varargs methods that would then become Seq methods. I'd do instead:

val patType = pat.tpe.widen
val patType1 = patType.underlyingIfRepeated(isJava = false)
val pat1 = if (patType eq patType1) pat else pat.withType(patType1)

@odersky odersky assigned nicolasstucki and unassigned odersky Apr 10, 2019
@liufengyun
Copy link
Contributor

I reverted the changes in Typer, and the tests in this PR pass:

--- a/compiler/src/dotty/tools/dotc/typer/Typer.scala
+++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala
@@ -581,7 +581,7 @@ class Typer extends Namer
     if (untpd.isWildcardStarArg(tree)) {
       def typedWildcardStarArgExpr = {
         val ptArg =
-          if (ctx.mode.is(Mode.QuotedPattern)) pt.underlyingIfRepeated(isJava = false)
+          if (ctx.mode.is(Mode.QuotedPattern)) pt
           else WildcardType
         val tpdExpr = typedExpr(tree.expr, ptArg)
         tpdExpr.tpe.widenDealias match {
@@ -1963,17 +1963,12 @@ class Typer extends Namer
     object splitter extends tpd.TreeMap {
       val patBuf = new mutable.ListBuffer[Tree]
       override def transform(tree: Tree)(implicit ctx: Context) = tree match {
-        case Typed(Splice(pat), tpt) if !tpt.tpe.derivesFrom(defn.RepeatedParamClass) =>
+        case Typed(Splice(pat), tpt) =>
           val exprTpt = AppliedTypeTree(TypeTree(defn.QuotedExprType), tpt :: Nil)
           transform(Splice(Typed(pat, exprTpt)))
         case Splice(pat) =>
           try patternHole(tree)
-          finally {
-            val patType = pat.tpe.widen
-            val patType1 = patType.underlyingIfRepeated(isJava = false)
-            val pat1 = if (patType eq patType1) pat else pat.withType(patType1)
-            patBuf += pat1
-          }
+          finally patBuf += pat
         case _ =>
           super.transform(tree)
       }

I still fail to understand the rational to go to an underlying type. Could you please elaborate @nicolasstucki ?

@nicolasstucki nicolasstucki assigned odersky and unassigned liufengyun Apr 11, 2019
@nicolasstucki
Copy link
Contributor Author

We want to avoid having expressions of type T* as we could not write val a: T* = ???. Instead we use val a: Seq[T] = ???.

@nicolasstucki nicolasstucki requested a review from odersky April 11, 2019 14:03
@odersky odersky merged commit e1b10db into scala:master Apr 11, 2019
@ghost ghost removed the stat:needs review label Apr 11, 2019
@allanrenucci allanrenucci deleted the add-quote-pattern-on-repeated-args branch April 11, 2019 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants