Skip to content

Fix #5083: Fix field references in outer accessors #5099

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

Closed
wants to merge 2 commits into from

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Sep 12, 2018

i5083 provides an example where a field reference had NoPrefix as a parameter, which made
it fail for producing an outer path. It's not quite clear where this came from. The fix
addresses it for now.

i5083 provides an example where a field reference had NoPrefix as a parameter, which made
it fail for producing an outer path. It's not quite clear where this came from. The fix
addresses it
@odersky odersky requested a review from liufengyun September 12, 2018 17:01
@liufengyun
Copy link
Contributor

liufengyun commented Sep 12, 2018

TLDR: My suggestion here is to disallow using trait parameters as parent outers, thus reject i5803.

Scalac and dotc behave differently here. The following program is rejected by Scalac, but accepted by Dotty:

class A(a: Int) {
  class Y {
    val y = a
  }

  class Z(val o: A) extends o.Y {
    val z = a
  }
}

Scalac error message:

test.scala:6: error: not found: value o
  class Z(val o: A) extends o.Y {
                            ^
one error found

The restriction in Scalac makes all outers of an object's classes known at the instantiation point. This way, calling a method during object initialization will never result in NPE related to outers, though it may still be unsafe due to possibly uninitialized fields.

Removal of the restriction in Dotty without trait parameters is still outer-safe, because primary constructor parameters of classes are assigned early before any constructor is executed.

However, with the support for trait parameters, this invariant for outer is lost, as trait primary constructor params are initialized according to linearization. We will get an NPE in the following example:

class A(a: Int) {
  abstract class X {
    def f: Int
    val x = a + f      // NPE: the outer for `Y` is not yet set
  }

  trait Y {
    val y = a
    def f: Int = A.this.a      // NPE: the outer for `Y` is not yet set
  }

  trait Z(val o: A) extends o.Y {
    val z = a
  }

  class B extends X with Z(new A(4))
}


object Test {
  def main(args: Array[String]): Unit = {
    val a = new A(3)
    new a.B
  }
}

My suggestion here is to disallow using trait parameters as parent outers, this way we maintain the nice invariant about outers, and it can avoid subtle bugs in practice. Invariants are good, though we still don't have full initialization safety.

Inherited trait parameters do need a prefix
@odersky
Copy link
Contributor Author

odersky commented Sep 13, 2018

@liufengyun I think your suggestion of disallowing trait outers makes sense.

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