Skip to content

Fix comment in isAsSpecificValue type and add test #4329

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
May 7, 2018
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
11 changes: 5 additions & 6 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1142,15 +1142,15 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
*
* flip(T) <: flip(U)
*
* where `flip` changes top-level contravariant type aliases to covariant ones.
* Intuitively `<:s` means subtyping `<:`, except that all top-level arguments
* where `flip` changes covariant occurrences of contravariant type parameters to
* covariant ones. Intuitively `<:s` means subtyping `<:`, except that all arguments
* to contravariant parameters are compared as if they were covariant. E.g. given class
*
* class Cmp[-X]
*
* `Cmp[T] <:s Cmp[U]` if `T <: U`. On the other hand, nested occurrences
* of parameters are not affected.
* So `T <: U` would imply `List[Cmp[U]] <:s List[Cmp[T]]`, as usual.
* `Cmp[T] <:s Cmp[U]` if `T <: U`. On the other hand, non-variant occurrences
* of parameters are not affected. So `T <: U` would imply `Set[Cmp[U]] <:s Set[Cmp[T]]`,
* as usual, because `Set` is non-variant.
*
* This relation might seem strange, but it models closely what happens for methods.
* Indeed, if we integrate the existing rules for methods into `<:s` we have now that
Expand All @@ -1167,7 +1167,6 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
else {
val flip = new TypeMap {
def apply(t: Type) = t match {
case t: TypeBounds => t
Copy link
Member

Choose a reason for hiding this comment

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

Is this PR just about changing comments or also about changing behavior?

Copy link
Contributor

Choose a reason for hiding this comment

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

Is the dropping of "top-level" intentional? I'm not sure I see that change reflected in the code.

Copy link
Contributor Author

@odersky odersky Apr 27, 2018

Choose a reason for hiding this comment

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

@smarter @milessabin The PR essentially adapts the comment to what the code now does. Dropping the TypeBounds case should have no effect except for wildcard arguments. When we still used refinements for type parameters it did have the effect of restricting to top-level. But with the change to direct parameter encoding that effect was lost, so that change caused a change in behavior.

I am actually quite unsure what the right behavior would be. Toplevel or not? What does toplevel mean, precisely? I don't have a good testcase that would demonstrate why restricting to toplevel is important. So I opted for the solution that's simpler to specify.

case t @ AppliedType(tycon, args) =>
def mapArg(arg: Type, tparam: TypeParamInfo) =
if (variance > 0 && tparam.paramVariance < 0) defn.FunctionOf(arg :: Nil, defn.UnitType)
Expand Down
26 changes: 26 additions & 0 deletions tests/run/contrarivant.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
object Test extends App {

// toplevel contra
implicit def f: String => String = x => "f"
implicit def g: Object => String = x => "g"
def h(implicit x: String => String) = x("")
assert(h == "f", h)

// nested contra
implicit def fs: List[String => String] = List(f)
implicit def gs: List[Object => String] = List(g)
def hs(implicit xs: List[String => String]) = xs.head("")
assert(hs == "f", hs)

// covariant subapplication nested in toplevel contra
implicit def f2: (Unit => String) => String = x => "f2"
implicit def g2: (Unit => Object) => String = x => "g2"
def h2(implicit x: (Unit => String) => String) = x(_ => "")
assert(h2 == "f2", h2)

// covariant subapplication nested in nested contra
implicit def fs2: List[(Unit => String) => String] = List(f2)
implicit def gs2: List[(Unit => Object) => String] = List(g2)
def hs2(implicit xs: List[(Unit => String) => String]) = xs.head(_ => "")
assert(hs2 == "f2", hs2)
}