Skip to content

Fix sequence matching. #669

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 5 commits into from
Jun 19, 2015
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
3 changes: 2 additions & 1 deletion src/dotty/tools/dotc/ast/TreeInfo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ trait TreeInfo[T >: Untyped <: Type] { self: Trees.Instance[T] =>

/** Is this argument node of the form <expr> : _* ?
*/
def isWildcardStarArg(tree: untpd.Tree)(implicit ctx: Context): Boolean = unsplice(tree) match {
def isWildcardStarArg(tree: Tree)(implicit ctx: Context): Boolean = unbind(tree) match {
case Typed(Ident(nme.WILDCARD_STAR), _) => true
case Typed(_, Ident(tpnme.WILDCARD_STAR)) => true
case Typed(_, tpt: TypeTree) => tpt.hasType && tpt.tpe.isRepeatedParam
case _ => false
Expand Down
9 changes: 7 additions & 2 deletions src/dotty/tools/dotc/ast/tpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo {
def Block(stats: List[Tree], expr: Tree)(implicit ctx: Context): Block =
ta.assignType(untpd.Block(stats, expr), stats, expr)

def maybeBlock(stats: List[Tree], expr: Tree)(implicit ctx: Context): Tree =
if (stats.isEmpty) expr else Block(stats, expr)
/** Join `stats` in front of `expr` creating a new block if necessary */
def seq(stats: List[Tree], expr: Tree)(implicit ctx: Context): Tree =
if (stats.isEmpty) expr
else expr match {
case Block(estats, eexpr) => cpy.Block(expr)(stats ::: estats, eexpr)
case _ => Block(stats, expr)
}

def If(cond: Tree, thenp: Tree, elsep: Tree)(implicit ctx: Context): If =
ta.assignType(untpd.If(cond, thenp, elsep), thenp, elsep)
Expand Down
6 changes: 4 additions & 2 deletions src/dotty/tools/dotc/transform/ExplicitOuter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ object ExplicitOuter {

/** A new outer accessor or param accessor */
private def newOuterSym(owner: ClassSymbol, cls: ClassSymbol, name: TermName, flags: FlagSet)(implicit ctx: Context) = {
ctx.newSymbol(owner, name, Synthetic | flags, cls.owner.enclosingClass.typeRef, coord = cls.coord)
val target = cls.owner.enclosingClass.typeRef
val info = if (flags.is(Method)) ExprType(target) else target
ctx.newSymbol(owner, name, Synthetic | flags, info, coord = cls.coord)
}

/** A new param accessor for the outer field in class `cls` */
Expand Down Expand Up @@ -302,7 +304,7 @@ object ExplicitOuter {
val outerAccessorCtx = ctx.withPhaseNoLater(ctx.lambdaLiftPhase) // lambdalift mangles local class names, which means we cannot reliably find outer acessors anymore
ctx.log(i"outer to $toCls of $tree: ${tree.tpe}, looking for ${outerAccName(treeCls.asClass)(outerAccessorCtx)} in $treeCls")
if (treeCls == toCls) tree
else loop(tree select outerAccessor(treeCls.asClass)(outerAccessorCtx))
else loop(tree.select(outerAccessor(treeCls.asClass)(outerAccessorCtx)).ensureApplied)
}
ctx.log(i"computing outerpath to $toCls from ${ctx.outersIterator.map(_.owner).toList}")
loop(This(ctx.owner.enclosingClass.asClass))
Expand Down
15 changes: 12 additions & 3 deletions src/dotty/tools/dotc/transform/LambdaLift.scala
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,23 @@ class LambdaLift extends MiniPhase with IdentityDenotTransformer { thisTransform
private def addFreeParams(tree: Tree, proxies: List[Symbol])(implicit ctx: Context, info: TransformerInfo): Tree = proxies match {
case Nil => tree
case proxies =>
val sym = tree.symbol
val ownProxies =
if (!tree.symbol.isConstructor) proxies
else proxies.map(_.copy(owner = tree.symbol, flags = Synthetic | Param))
if (!sym.isConstructor) proxies
else proxies.map(_.copy(owner = sym, flags = Synthetic | Param))
val freeParamDefs = ownProxies.map(proxy =>
transformFollowingDeep(ValDef(proxy.asTerm).withPos(tree.pos)).asInstanceOf[ValDef])
def proxyInit(field: Symbol, param: Symbol) =
transformFollowingDeep(ref(field).becomes(ref(param)))
def copyParams(rhs: Tree) = {
ctx.log(i"copy params ${proxies.map(_.showLocated)}%, %, own = ${ownProxies.map(_.showLocated)}%, %")
seq((proxies, ownProxies).zipped.map(proxyInit), rhs)
}
tree match {
case tree: DefDef =>
cpy.DefDef(tree)(vparamss = tree.vparamss.map(freeParamDefs ++ _))
cpy.DefDef(tree)(
vparamss = tree.vparamss.map(freeParamDefs ++ _),
rhs = if (sym.isPrimaryConstructor) copyParams(tree.rhs) else tree.rhs)
case tree: Template =>
cpy.Template(tree)(body = freeParamDefs ++ tree.body)
}
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/transform/PatternMatcher.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class PatternMatcher extends MiniPhaseTransform with DenotTransformer {thisTrans
def callDirect = tgt.select(nme.drop).appliedTo(Literal(Constant(n)))
def callRuntime = ref(defn.traversableDropMethod).appliedTo(tgt, Literal(Constant(n)))

def needsRuntime = tgt.tpe derivesFrom defn.SeqClass /*typeOfMemberNamedDrop(tgt.tpe) == NoType*/
def needsRuntime = !(tgt.tpe derivesFrom defn.SeqClass) /*typeOfMemberNamedDrop(tgt.tpe) == NoType*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Redundant parens around the expression.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I was myopic here. All's good.


if (needsRuntime) callRuntime else callDirect
}
Expand Down
5 changes: 3 additions & 2 deletions src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -377,10 +377,11 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
}
tree.expr match {
case id: untpd.Ident if (ctx.mode is Mode.Pattern) && isVarPattern(id) =>
if (id.name == nme.WILDCARD) regularTyped(isWildcard = true)
if (id.name == nme.WILDCARD || id.name == nme.WILDCARD_STAR) regularTyped(isWildcard = true)
else {
import untpd._
typed(Bind(id.name, Typed(Ident(nme.WILDCARD), tree.tpt)).withPos(id.pos), pt)
val name = if (untpd.isWildcardStarArg(tree)) nme.WILDCARD_STAR else nme.WILDCARD
typed(Bind(id.name, Typed(Ident(name), tree.tpt)).withPos(id.pos), pt)
}
case _ =>
if (untpd.isWildcardStarArg(tree))
Expand Down
26 changes: 26 additions & 0 deletions tests/run/i659.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Foo(val a: Int) {
def foo = { {case x => x + a}: PartialFunction[Int, Int]}
class Bar { def result(x: Int) = x + a }
def bar = new Bar
}

class VFoo(val a: Int) extends AnyVal {
def foo = { {case x => x + a}: PartialFunction[Int, Int]}
}

object Test extends dotty.runtime.LegacyApp {

def Foo(a: Int) = {
class Bar { def result(x: Int) = x + a }
new Bar().result(2)
}

val x1 = new Foo(1).bar.result(2)
assert(x1 == 3, s"x1 = $x1")
val x2 = Foo(1)
assert(x2 == 3, s"x2 = $x2")
val x3 = new Foo(1).foo.apply(2)
assert(x3 == 3, s"x3 = $x3")
val x4 = new VFoo(1).foo.apply(2)
assert(x4 == 3, s"x4 = $x4")
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.