Skip to content

Avoid interpolating to Nothing in a special case #16328

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
Closed
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
17 changes: 15 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,27 @@ trait Inferencing { this: Typer =>
// isInstantiated needs to be checked again, since previous interpolations could already have
// instantiated `tvar` through unification.
val v = vs(tvar)
val tparam = tvar.origin
if v == null then buf += ((tvar, 0))
else if v.intValue != 0 then buf += ((tvar, v.intValue))
else if v.intValue != 0 then
def hasLowerBound =
(constraint.nonParamBounds(tparam).lo ne tparam.underlying.bounds.lo)
|| constraint.lower(tparam).nonEmpty
def isSelectionPrefix =
(tp eq tvar) && pt.isInstanceOf[SelectionProto]
if v.intValue != 1 || hasLowerBound || !isSelectionPrefix then
// Don't interpolate to lower if there is no lower bound other than
// the declared one, the current type is exactly `tvar`, and the expression is
// followed by a selection. In this case we should wait so that we can
// instantiate `tvar` to its upper bound later in `couldInstantiateTypeVar`.
// Test case is pos/i16323.scala.
buf += ((tvar, v.intValue))
else comparing(cmp =>
if !cmp.levelOK(tvar.nestingLevel, ctx.nestingLevel) then
// Invariant: The type of a tree whose enclosing scope is level
// N only contains type variables of level <= N.
typr.println(i"instantiate nonvariant $tvar of level ${tvar.nestingLevel} to a type variable of level <= ${ctx.nestingLevel}, $constraint")
cmp.atLevel(ctx.nestingLevel, tvar.origin)
cmp.atLevel(ctx.nestingLevel, tparam)
else
typr.println(i"no interpolation for nonvariant $tvar in $state")
)
Expand Down
7 changes: 7 additions & 0 deletions tests/pos/i16323.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Foo {
def member: String = this.toString
}
def method[T](c: T => Boolean): T = ???
val y: Foo => Boolean = _ => true
val x : String = "5"
val _ = method(y).member // error