From e74b6316b93993e3c798bd9df45d83fc0665c52e Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Fri, 6 Dec 2013 14:39:30 +0100 Subject: Fixing expansion and merge companion defs. --- src/dotty/tools/dotc/typer/Namer.scala | 39 +++++++++++++++++++++------------- test/dotc/tests.scala | 2 ++ tests/pos/desugar.scala | 14 ++++++++++++ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/dotty/tools/dotc/typer/Namer.scala b/src/dotty/tools/dotc/typer/Namer.scala index bcdb4a43f..716e7a8b9 100644 --- a/src/dotty/tools/dotc/typer/Namer.scala +++ b/src/dotty/tools/dotc/typer/Namer.scala @@ -205,12 +205,13 @@ class Namer { typer: Typer => else ctx.newCompletePackageSymbol(pkgOwner, pid.name.asTermName).entered } - /** The expansion of a member def */ - def expansion(mdef: DefTree)(implicit ctx: Context): Tree = { - val expanded = desugar.defTree(mdef) - println(i"Expansion: $mdef expands to $expanded") - if (expanded ne mdef) expandedTree(mdef) = expanded - expanded + /** Expand tree and store in `expandedTree` */ + def expand(tree: Tree)(implicit ctx: Context): Unit = tree match { + case mdef: DefTree => + val expanded = desugar.defTree(mdef) + println(i"Expansion: $mdef expands to $expanded") + if (expanded ne mdef) expandedTree(mdef) = expanded + case _ => } /** The expanded version of this tree, or tree itself if not expanded */ @@ -233,8 +234,16 @@ class Namer { typer: Typer => localCtx } - /** Create top-level symbols for statement and enter them into symbol table */ - def index(stat: Tree)(implicit ctx: Context): Context = stat match { + /** Expand tree and create top-level symbols for statement and enter them into symbol table */ + def index(stat: Tree)(implicit ctx: Context): Context = { + expand(stat) + indexExpanded(stat) + } + + /** Create top-level symbols for all statements in the expansion of this statement and + * enter them into symbol table + */ + def indexExpanded(stat: Tree)(implicit ctx: Context): Context = stat match { case pcl: PackageDef => val pkg = createPackageSymbol(pcl.pid) index(pcl.stats)(ctx.fresh.withOwner(pkg.moduleClass)) @@ -242,7 +251,7 @@ class Namer { typer: Typer => case imp: Import => importContext(createSymbol(imp), imp.selectors) case mdef: DefTree => - expansion(mdef).toList foreach (tree => enterSymbol(createSymbol(tree))) + expandedTree(mdef).toList foreach (tree => enterSymbol(createSymbol(tree))) ctx case _ => ctx @@ -261,20 +270,20 @@ class Namer { typer: Typer => for (mdef @ ModuleDef(_, name, _) <- stats) caseClassDef get name.toTypeName match { case Some(cdef) => - val Thicket((mcls @ TypeDef(_, _, impl: Template)) :: mrest) = expandedTree(mdef) - val Thicket(cls :: TypeDef(_, _, compimpl: Template) :: crest) = expandedTree(cdef) + val Thicket(vdef :: (mcls @ TypeDef(_, _, impl: Template)) :: Nil) = expandedTree(mdef) + val Thicket(cls :: mval :: TypeDef(_, _, compimpl: Template) :: crest) = expandedTree(cdef) val mcls1 = cpy.TypeDef(mcls, mcls.mods, mcls.name, cpy.Template(impl, impl.constr, impl.parents, impl.self, compimpl.body ++ impl.body)) - expandedTree(mdef) = Thicket(mcls1 :: mrest) + expandedTree(mdef) = Thicket(vdef :: mcls1 :: Nil) expandedTree(cdef) = Thicket(cls :: crest) case none => } - } + } - val result = (ctx /: stats) ((ctx, stat) => index(stat)(ctx)) + stats foreach expand mergeCompanionDefs() - result + (ctx /: stats) ((ctx, stat) => indexExpanded(stat)(ctx)) } /** The completer of a symbol defined by a member def or import (except ClassSymbols) */ diff --git a/test/dotc/tests.scala b/test/dotc/tests.scala index acd57532f..d680435c4 100644 --- a/test/dotc/tests.scala +++ b/test/dotc/tests.scala @@ -23,6 +23,8 @@ class tests extends CompilerTest { @Test def pos_varargs() = compileFile(posDir, "varargs") @Test def pos_opassign() = compileFile(posDir, "opassign") @Test def pos_typedapply() = compileFile(posDir, "typedapply") + @Test def pos_nameddefaults() = compileFile(posDir, "nameddefaults") + @Test def pos_desugar() = compileFile(posDir, "desugar") @Test def neg_blockescapes() = compileFile(negDir, "blockescapesNeg", xerrors = 2) @Test def neg_typedapply() = compileFile(negDir, "typedapply", xerrors = 4) diff --git a/tests/pos/desugar.scala b/tests/pos/desugar.scala index ffd114652..ca52ac6ec 100644 --- a/tests/pos/desugar.scala +++ b/tests/pos/desugar.scala @@ -9,5 +9,19 @@ object desugar { def foo0(first: Int, second: Int = 2, third: Int = 3) = first + second def foo1(first: Int, second: Int = 2)(third: Int = 3) = first + second def foo2(first: Int)(second: Int = 2)(third: Int = 3) = first + second + + object lists { + trait List[+T] { + def head: T + def tail: List[T] + } + + case class Cons[+T](val head: T, val tail: List[T]) extends List[T] + + object Cons { + def apply[T](head: T): Cons[T] = apply(head, Nil) + } + case object Nil extends List[Nothing] + } } \ No newline at end of file -- cgit v1.2.3