Skip to content

Fix #5521: prefix not checked in realizability check #5522

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 3 commits into from
Dec 2, 2018
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
8 changes: 5 additions & 3 deletions compiler/src/dotty/tools/dotc/core/CheckRealizable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ class CheckRealizable(implicit ctx: Context) {
else {
val r =
if (!sym.isStable) NotStable
else if (!isLateInitialized(sym)) realizability(tp.prefix)
else if (!isLateInitialized(sym)) Realizable
else if (!sym.isEffectivelyFinal) new NotFinal(sym)
else realizability(tp.info).mapError(r => new ProblemInUnderlying(tp.info, r))
if (r == Realizable) sym.setFlag(Stable)
r
r andAlso {
sym.setFlag(Stable)
realizability(tp.prefix)
Copy link
Contributor

Choose a reason for hiding this comment

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

I looked at this time ago in #4036, I should look more closely, and I'm in a rush, but here are some initial comments.

I agree this is a bug and this change should give the correct behavior; but this change risks being pretty computationally expensive (possible fix below). This can duplicate recursive calls, and that risks causing exponential slowdowns. Triggering it might require a chain of final lazy vals or be impossible, but it's more robust to not duplicate calls.

If isLateInitialized(sym), this calls realizability(tp.prefix) again, and that can be too expensive. Caching only affects symbols, but we don't cache realizability on whole types; tp could not even be a TermRef and fall through boundsRealizability(tp).andAlso(memberRealizability(tp)).

I asked dottybot for a performance test. But maybe one could just reorganize the logic.
Looking at #4036 (which is where I "studied" this), I propose to add to your change the following:

-else if (!isLateInitialized(sym)) realizability(tp.prefix)
+else if (!isLateInitialized(sym)) Realizable

because in this branch we can certainly mark the symbol as stable (that PR also achieves this goal, though in a hackier way). If that works, this PR can go in with that fix.

Optional requests

Here's a couple of bonus requests one could fix here as well — if they are non-trivial, feel free to defer them. I should probably do these changes myself and PR them, I'm in a rush right now.

On performance: https://github.com/lampepfl/dotty/pull/4036/files#diff-0535d82f8f57a2204d6452bae575efc9 might be a useful test here (if it doesn't cause a stack overflow).

On error reporting, I think one could cherry-pick from #4036 the following:

-realizability(tp.prefix)
+realizability(tp.prefix).mapError(r => new ProblemInUnderlying(tp.prefix, r))

}
}
case _: SingletonType | NoPrefix =>
Realizable
Expand Down
12 changes: 12 additions & 0 deletions tests/neg/i5521.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Hello {
class Foo {
class Bar
final lazy val s: Bar = ???
}

lazy val foo: Foo = ???

val x: foo.s.type = ??? // error: `foo` must be final since it's a lazy val
val x2: foo.s.type = ??? // error: `foo` must be final since it's a lazy val
}