Skip to content

Backport "A tweak to type improvement" to 3.5.2 #21474

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 2 commits into from
Aug 28, 2024
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
10 changes: 9 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,15 @@ object Inferencing {
t match
case t: TypeRef =>
if t.symbol == defn.NothingClass then
newTypeVar(TypeBounds.empty, nestingLevel = tvar.nestingLevel)
val notExactlyNothing = LazyRef(_ => defn.NothingType)
val bounds = TypeBounds(notExactlyNothing, defn.AnyType)
// The new type variale has a slightly disguised lower bound Nothing.
// This foils the `isExactlyNothing` test in `hasLowerBound` and
// therefore makes the new type variable have a lower bound. That way,
// we favor in `apply` below instantiating from below to `Nothing` instead
// of from above to `Any`. That avoids a spurious flip of the original `Nothing`
// instance to `Any`. See i21275 for a test case.
newTypeVar(bounds, nestingLevel = tvar.nestingLevel)
else if t.symbol.is(ModuleClass) then
tryWidened(t.parents.filter(!_.isTransparent())
.foldLeft(defn.AnyType: Type)(TypeComparer.andType(_, _)))
Expand Down
10 changes: 10 additions & 0 deletions tests/pos/i21275.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Box[+O]:
def ++[O2 >: O](other: Box[O2]): Box[O2] = ???
object Box:
val empty: Box[Nothing] = ???

def test[T]: Box[T] =
List(Box.empty, Box.empty)
// .reduceOption[Box[T]](_ ++ _) // works
.reduceOption(_ ++ _) // fails
.getOrElse(Box.empty)