Skip to content

Fix #11078: avoid widening F-bounds in type avoidance #11192

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
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 29 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dotc
package core

import Contexts._, Types._, Symbols._, Names._, Flags._
import SymDenotations._
import Denotations._, SymDenotations._
import util.Spans._
import util.Stats
import NameKinds.DepParamName
Expand Down Expand Up @@ -462,6 +462,34 @@ object TypeOps:
else
range(defn.NothingType, defn.AnyType)
}

/** Deviation from standard tryWiden:
* - Don't widen F-bounds
Copy link
Member

Choose a reason for hiding this comment

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

Maybe the standard tryWiden shouldn't widen f-bounds either?

Copy link
Contributor Author

@liufengyun liufengyun Jan 22, 2021

Choose a reason for hiding this comment

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

Yes, this is a trial to see how it works. It may incur a performance penalty.

If it works, I think we need a more systematic approach here: possible a field in SymDenotation to cache whether a symbol is F-bounded. The field can be set in checkNonCyclic, so we can avoid the computation here (or in ApproximatingTypeMap).

Copy link
Contributor

@odersky odersky Jan 24, 2021

Choose a reason for hiding this comment

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

F-bounded situations are already captured by LazyRefs. Maybe just add a case for LazyRefs to TypeOps.avoid?
Oh, I see you tried that already.

*/
override def tryWiden(tp: NamedType, pre: Type): Type = pre.member(tp.name) match {
case d: SingleDenotation =>
val tp1 = d.info.dealiasKeepAnnots
tp1.stripAnnots match {
case TypeAlias(alias) =>
// if H#T = U, then for any x in L..H, x.T =:= U,
// hence we can replace with U under all variances
reapply(alias.rewrapAnnots(tp1))

case tb: TypeBounds =>
// Don't widen F-bounds
val isFBounds = tb.existsPart(p => p.isInstanceOf[LazyRef], forceLazy = false)
if isFBounds then NoType
else expandBounds(tb)
case info: SingletonType =>
// if H#x: y.type, then for any x in L..H, x.type =:= y.type,
// hence we can replace with y.type under all variances
reapply(info)
case _ =>
NoType
}
case _ => NoType
}

}

widenMap(tp)
Expand Down
29 changes: 29 additions & 0 deletions tests/pos/i11078.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
trait Foo[A <: Foo[A]]
trait FooCreator[A <: Foo[A]] {
def createFoo(): A
}

trait FooWrapper {
type A <: Foo[A]
def foo: A
}
object FooWrapper {
def apply[A0 <: Foo[A0]](toWrap: A0): FooWrapper { type A = A0 } = new FooWrapper {
type A = A0
def foo: A0 = toWrap
}
}

trait FooCreatorWrapper {
type A <: Foo[A]
def fooCreator: FooCreator[A]
}

sealed trait Bar
object Bar {
case class Baz(wrapper: FooCreatorWrapper) extends Bar
}

def process(bar: Bar): FooWrapper = bar match {
case Bar.Baz(wrapper) => FooWrapper(wrapper.fooCreator.createFoo())
}
11 changes: 11 additions & 0 deletions tests/pos/i11078b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Test {
trait Foo[A <: Foo[A]]

trait FooWrapper with self =>
type A <: Foo[A]
def doThing(foo: FooWrapper): FooWrapper { type A = self.A } = ???
end FooWrapper

val foos: scala.Seq[FooWrapper] = ???
val newFoo = foos.foldLeft(??? : FooWrapper)((topFoo, foo) => topFoo.doThing(foo))
}