Skip to content

Commit 8f08b71

Browse files
committed
Annotate repeated params with case flag to indicate that they are legal
One drawback with this approach is that the type seems to propagate. I.e. if the return type of an expression is `repeated` then the enclosing variable will get the `repeated` type instead of getting the expected `Seq` type
1 parent 87b779b commit 8f08b71

File tree

5 files changed

+14
-9
lines changed

5 files changed

+14
-9
lines changed

src/dotty/tools/dotc/transform/TreeChecker.scala

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ class TreeChecker extends Phase with SymTransformer {
119119
val prevPhase = ctx.phase.prev // can be a mini-phase
120120
val squahsedPhase = ctx.squashed(prevPhase)
121121
ctx.echo(s"checking ${ctx.compilationUnit} after phase ${squahsedPhase}")
122-
val checkingCtx = ctx.fresh.setReporter(new ThrowingReporter(ctx.reporter))
122+
123+
val checkingCtx = ctx
124+
.fresh
125+
.setMode(Mode.ImplicitsEnabled)
126+
.setReporter(new ThrowingReporter(ctx.reporter))
127+
123128
val checker = new Checker(previousPhases(phasesToRun.toList)(ctx))
124129
try checker.typedExpr(ctx.compilationUnit.tpdTree)(checkingCtx)
125130
catch {
@@ -307,7 +312,9 @@ class TreeChecker extends Phase with SymTransformer {
307312
}.apply(tp)
308313

309314
def checkNotRepeated(tree: Tree)(implicit ctx: Context): tree.type = {
310-
assert(!tree.tpe.widen.isRepeatedParam, i"repeated parameter type not allowed here: $tree")
315+
def allowedRepeated = (tree.symbol.flags is Case) && tree.tpe.widen.isRepeatedParam
316+
317+
assert(!tree.tpe.widen.isRepeatedParam || allowedRepeated, i"repeated parameter type not allowed here: $tree")
311318
tree
312319
}
313320

@@ -322,6 +329,7 @@ class TreeChecker extends Phase with SymTransformer {
322329
assert(tree.isTerm || !ctx.isAfterTyper, tree.show + " at " + ctx.phase)
323330
assert(tree.isType || !needsSelect(tree.tpe), i"bad type ${tree.tpe} for $tree # ${tree.uniqueId}")
324331
assertDefined(tree)
332+
325333
checkNotRepeated(super.typedIdent(tree, pt))
326334
}
327335

src/dotty/tools/dotc/typer/ReTyper.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import config.Printers
2323
class ReTyper extends Typer {
2424
import tpd._
2525

26+
/** Checks that the given tree has been typed */
2627
protected def promote(tree: untpd.Tree)(implicit ctx: Context): tree.ThisTree[Type] = {
2728
assert(tree.hasType, i"$tree ${tree.getClass} ${tree.uniqueId}")
2829
tree.withType(tree.typeOpt)

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,6 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
427427
}
428428
if (untpd.isWildcardStarArg(tree))
429429
cases(
430-
//ifPat = ascription(TypeTree(defn.SeqType.appliedTo(pt :: Nil)), isWildcard = true),
431430
ifPat = ascription(TypeTree(defn.RepeatedParamType.appliedTo(pt)), isWildcard = true),
432431
ifExpr = seqToRepeated(typedExpr(tree.expr, defn.SeqType)),
433432
wildName = nme.WILDCARD_STAR)
@@ -976,7 +975,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
976975
typed(untpd.Bind(tree.name, arg).withPos(tree.pos), arg.tpe) :: Nil)
977976
case _ =>
978977
val flags = if (tree.isType) BindDefinedType else EmptyFlags
979-
val sym = ctx.newSymbol(ctx.owner, tree.name, flags, body1.tpe, coord = tree.pos)
978+
val sym = ctx.newSymbol(ctx.owner, tree.name, flags | Case, body1.tpe, coord = tree.pos)
980979
assignType(cpy.Bind(tree)(tree.name, body1), sym)
981980
}
982981
}

tests/disabled/pos/t3480.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
object Test {
22
val List(_: _*) = List(1)
3-
val Array( who, what : _* ) = "Eclipse plugin cannot not handle this" split (" ")
3+
val Array(who, what: _*) = "Eclipse plugin cannot not handle this" split (" ")
44
}

tests/pos/vararg-pattern.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
object Test {
2-
32
List(1, 2, 3, 4) match {
43
case List(1, 2, xs: _*) =>
54
val ys: Seq[Int] = xs
65
println(ys)
76
}
8-
val List(1, 2, x: _*) = List(1, 2, 3, 4)
97

8+
val List(1, 2, x: _*) = List(1, 2, 3, 4)
109
}
11-
12-

0 commit comments

Comments
 (0)