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 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
4 changes: 3 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 @@ -438,6 +438,8 @@ object TypeOps:
tp.origin, fromBelow = variance > 0 || variance == 0 && tp.hasLowerBound)(using mapCtx)
val lo1 = apply(lo)
if (lo1 ne lo) lo1 else tp
case tp: LazyRef if isExpandingBounds =>
emptyRange
case _ =>
mapOver(tp)
}
Expand Down
17 changes: 16 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5328,8 +5328,23 @@ object Types {
case _ => tp
}

private var expandingBounds: Boolean = false

/** Whether it is currently expanding bounds
*
* It is used to avoid following LazyRef in F-Bounds
*/
def isExpandingBounds: Boolean = expandingBounds

protected def expandBounds(tp: TypeBounds): Type =
range(atVariance(-variance)(reapply(tp.lo)), reapply(tp.hi))
if expandingBounds then tp
else {
val saved = expandingBounds
expandingBounds = true
val res = range(atVariance(-variance)(reapply(tp.lo)), reapply(tp.hi))
expandingBounds = saved
res
}

/** Try to widen a named type to its info relative to given prefix `pre`, where possible.
* The possible cases are listed inline in the code.
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))
}