Skip to content

Fix #7392: Interpolate all wildcards in the resulttype of a closure #7396

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
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -833,12 +833,18 @@ class Typer extends Namer
case _: WildcardType => untpd.TypeTree()
case _ => untpd.TypeTree(tp)
}
def interpolateWildcards = new TypeMap {
def apply(t: Type) = t match
case WildcardType => newTypeVar(TypeBounds.empty)
Copy link
Member

Choose a reason for hiding this comment

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

newTypeVar will call constrained(poly, untpd.EmptyTree, ...) but we do actually have an owning tree that we could use here I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It does not actually change anything. We need an owning tree only to set the position of a TypeVarBinder tree. But in this case we throw the TypeVarBinder away anyway and just keep the type.

case WildcardType(bounds: TypeBounds) => newTypeVar(bounds)
Copy link
Member

Choose a reason for hiding this comment

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

Could there be WildcardTypes in the bounds themselves that need to be replaced recursively ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe. Better to be on the safe side.

case _ => mapOver(t)
}
pt.stripTypeVar.dealias match {
case pt1 if defn.isNonRefinedFunction(pt1) =>
// if expected parameter type(s) are wildcards, approximate from below.
// if expected result type is a wildcard, approximate from above.
// this can type the greatest set of admissible closures.
(pt1.argTypesLo.init, typeTree(pt1.argTypesHi.last))
(pt1.argTypesLo.init, typeTree(interpolateWildcards(pt1.argTypesHi.last)))
case SAMType(sam @ MethodTpe(_, formals, restpe)) =>
(formals,
if (sam.isResultDependent)
Expand Down Expand Up @@ -1900,7 +1906,6 @@ class Typer extends Namer
else tree1
}


def typedAsFunction(tree: untpd.PostfixOp, pt: Type)(implicit ctx: Context): Tree = {
val untpd.PostfixOp(qual, Ident(nme.WILDCARD)) = tree
val pt1 = if (defn.isFunctionType(pt)) pt else AnyFunctionProto
Expand Down
16 changes: 16 additions & 0 deletions tests/pos/i7392.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
object Test {

trait ZStream[-R, +E, +A]

object ZStream {
def empty: ZStream[Any, Nothing, Nothing] =
???
}

trait Gen[-R, +A](sample: ZStream[R, Nothing, A])

def fromIterable[R, A](
as: Iterable[A],
shrinker: (A => ZStream[R, Nothing, A]) = (_: A) => ZStream.empty
): Gen[R, A] = ???
}