Skip to content

Add/trait parameters #639

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 11 commits into from
Jun 19, 2015
Merged

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jun 11, 2015

Implements #640. @retronym do you want to take a look?

@retronym
Copy link
Member

trait U(a: Any) extends T {
  def d = a // okay
  val v = a // okay
  a // crash
}
exception while transforming (a: Object) extends Object with T {
  private <accessor> def a(): Object
  def d(): Object = U.this.a()
  <accessor> def v(): Object = U.this.a()
  U.this.a()
} of class class dotty.tools.dotc.ast.Trees$Template # 136
exception while transforming <trait> trait U(a: Object) extends Object with T {
  private <accessor> def a(): Object
  def d(): Object = U.this.a()
  <accessor> def v(): Object = U.this.a()
  U.this.a()
} of class class dotty.tools.dotc.ast.Trees$TypeDef # 137
exception while transforming package <empty> {
  <trait> trait U(a: Object) extends Object with T {
    private <accessor> def a(): Object
    def d(): Object = U.this.a()
    <accessor> def v(): Object = U.this.a()
    U.this.a()
  }
  final lazy module val U: U$ = new U$()
  final module class U$() extends Object() { this: U$ =>}
} of class class dotty.tools.dotc.ast.Trees$PackageDef # 154
exception occurred while compiling sandbox/test.scala
Exception in thread "main" java.util.NoSuchElementException: head of empty list
    at scala.collection.immutable.Nil$.head(List.scala:420)
    at scala.collection.immutable.Nil$.head(List.scala:417)
    at dotty.tools.dotc.transform.SymUtils$.loop$1(SymUtils.scala:64)
    at dotty.tools.dotc.transform.SymUtils$.subst$extension(SymUtils.scala:66)
    at dotty.tools.dotc.transform.Constructors$intoConstr$2$.transform(Constructors.scala:100)
    at dotty.tools.dotc.transform.Constructors$intoConstr$2$.transform(Constructors.scala:103)
    at dotty.tools.dotc.transform.Constructors$intoConstr$2$.apply(Constructors.scala:112)
    at dotty.tools.dotc.transform.Constructors.splitStats$1(Constructors.scala:180)

@retronym
Copy link
Member

I think this is a clearer way to express the test cases:

object Trace {
  private var results = List[Any]()
  def apply[A](a: A) = {results ::= a; a}
  def fetchAndClear(): Seq[Any] = try results.reverse finally results = Nil
}
trait T(a: Any) {
  val ta = a
  Trace(s"T.<init>($ta)")
  val t_val = Trace("T.val")
}

trait U(a: Any) extends T {
  val ua = a
  Trace(s"U.<init>($ua)")
}

object Test {
  def check(expected: Any) = {
    val actual = Trace.fetchAndClear()
    if (actual != expected)
      sys.error(s"\n$actual\n$expected")
  }
  def main(args: Array[String]): Unit = {
    new T(Trace("ta")) with U(Trace("ua")) {}
    check(List("ta", "T.<init>(ta)", "T.val", "ua", "U.<init>(ua)"))

    new U(Trace("ua")) with T(Trace("ta")) {}
    check(List("ta", "T.<init>(ta)", "T.val", "ua", "U.<init>(ua)"))
  }
}

@odersky odersky force-pushed the add/trait-parameters branch 3 times, most recently from 87f0f93 to 9dcb95b Compare June 12, 2015 16:57
@odersky
Copy link
Contributor Author

odersky commented Jun 12, 2015

@retronym Yes, that was the corner case I meant.

@odersky odersky force-pushed the add/trait-parameters branch from 9dcb95b to 04a5372 Compare June 19, 2015 08:41
@odersky
Copy link
Contributor Author

odersky commented Jun 19, 2015

rebased to master

odersky added 11 commits June 19, 2015 12:10
Need to suppress the flag when copying symbols.
A parent trait may not be parameterized (as in T()) if the calling class does not
directly implement that trait.
Add necessary logic to Mixin. Also add tests
that all parameterized traits are called with
parameters set.
Verify that the initilialization order described in scala#640 is correctly implemented.
(reverted from commit 2c55900)

Mixin should drop the ParamAccessor flag anyway in traits, so there is no
need to mask it when creating implementations of trait definitions.
Parameter accessors in traits have the ParamAccessor flag removed
in the Mixin transformSym method.
Reason: The ParamAccessor flag will be reset later in Mixin. We do not
want to rewrite the references to a trait parameter accessor then, so it's
better to generate all references with a `this.` prefix from the start.
Lazy Scala2 fields and trait parameters touched the same code in Mixin and the merge
dropped essential logic. Also cleaned up some of the code having to do with lazy
Scala2 fields.
@odersky odersky force-pushed the add/trait-parameters branch from ee1277a to 0369f1e Compare June 19, 2015 10:35
odersky added a commit that referenced this pull request Jun 19, 2015
@odersky odersky merged commit 1b31f06 into scala:master Jun 19, 2015
@allanrenucci allanrenucci deleted the add/trait-parameters branch December 14, 2017 16:58
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.

2 participants