Skip to content

Commit

Permalink
Rewrite code for flexible repeated types
Browse files Browse the repository at this point in the history
  • Loading branch information
noti0na1 committed Mar 25, 2024
1 parent 68085fc commit 14d5635
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/TypeApplications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ class TypeApplications(val self: Type) extends AnyVal {
*/
final def argInfos(using Context): List[Type] = self.stripped match
case AppliedType(tycon, args) => args
case tp: FlexibleType => tp.underlying.argInfos
case _ => Nil

/** If this is an encoding of a function type, return its arguments, otherwise return Nil.
Expand Down
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -644,15 +644,14 @@ trait Applications extends Compatibility {
missingArg(n)
}

val formal1 = formal.stripFlexible
if (formal1.isRepeatedParam)
if formal.isRepeatedParam then
args match {
case arg :: Nil if isVarArg(arg) =>
addTyped(arg)
case (arg @ Typed(Literal(Constant(null)), _)) :: Nil if ctx.isAfterTyper =>
addTyped(arg)
case _ =>
val elemFormal = formal1.widenExpr.argTypesLo.head
val elemFormal = formal.widenExpr.argTypesLo.head
val typedArgs =
harmonic(harmonizeArgs, elemFormal) {
args.map { arg =>
Expand Down
22 changes: 13 additions & 9 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -981,19 +981,23 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
}

if (untpd.isWildcardStarArg(tree)) {

def fromRepeated(pt: Type): Type = pt match
case pt: FlexibleType =>
pt.derivedFlexibleType(fromRepeated(pt.hi))
case _ =>
if ctx.mode.isQuotedPattern then
// FIXME(#8680): Quoted patterns do not support Array repeated arguments
pt.translateFromRepeated(toArray = false, translateWildcard = true)
else
pt.translateFromRepeated(toArray = false, translateWildcard = true)
| pt.translateFromRepeated(toArray = true, translateWildcard = true)

def typedWildcardStarArgExpr = {
// A sequence argument `xs: _*` can be either a `Seq[T]` or an `Array[_ <: T]`,
// irrespective of whether the method we're calling is a Java or Scala method,
// so the expected type is the union `Seq[T] | Array[_ <: T]`.
val pt1 = pt.stripFlexible
val ptArg0 =
// FIXME(#8680): Quoted patterns do not support Array repeated arguments
if ctx.mode.isQuotedPattern then
pt1.translateFromRepeated(toArray = false, translateWildcard = true)
else
pt1.translateFromRepeated(toArray = false, translateWildcard = true)
| pt1.translateFromRepeated(toArray = true, translateWildcard = true)
val ptArg = if pt1 eq pt then ptArg0 else FlexibleType(ptArg0)
val ptArg = fromRepeated(pt)
val expr0 = typedExpr(tree.expr, ptArg)
val expr1 = if ctx.explicitNulls && (!ctx.mode.is(Mode.Pattern)) then
if expr0.tpe.isNullType then
Expand Down

0 comments on commit 14d5635

Please sign in to comment.