Skip to content

Fix #7249: Search for hidden givens only at whole type #7300

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 4 commits into from
Sep 27, 2019

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Sep 24, 2019

Don't try to find a hidden implicit for a part of type type that
was searched for, unless that part is fully defined.

The reason is that any type variables in a subtype might be associated
with constraints that are lost at the point of issuing a "hidden
givens" addendum.

The commit also contains a tweak to "isFullyDefined" which makes
an uninstantiated type variable that is not bound in the current
constraint count as "not fully defined", where previously it was
"fully defined".

@odersky
Copy link
Contributor Author

odersky commented Sep 25, 2019

This fixes also #7248


inline given f[T <: Foo]: T = inline erasedValue[T] match
case _: A => new A{}.asInstanceOf[T]
case _: B[a] => summon[a].asInstanceOf[T]
Copy link
Contributor

Choose a reason for hiding this comment

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

This summon shouldn't succeed though on compile time. Starting from println:

// 1) initial call
f[B[Int]]

// 2) inlining `f`
inline erasedValue[B[Int]] match
  case _: A => new A{}.asInstanceOf[B[Int]]
  case _: B[a] => summon[a].asInstanceOf[B[Int]]

// 3) second case matched and inlined with a = Int
summon[Int].asInstanceOf[B[Int]]

summon[Int] should fail since there's no Int given in scope, shouldn't it?

scala> summon[Int]
1 |summon[Int]
  |           ^
  |no implicit argument of type Int was found for parameter x of method summon in object DottyPredef

Copy link
Contributor

Choose a reason for hiding this comment

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

Even if it tries Int & Foo it still won't find anything suitable, since f returns T <: Foo and nothing about it says that it is an intersection with Int.

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 actually tries f[Nothing] and succeeds with the A case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh right. So the behaviour here stays the same as in the #7250 second example: if we run this test, it will fail at runtime with ClassCastException. It looks like it is the same problem as #7293, where Nothing appears in unexpected places.

Copy link
Contributor

Choose a reason for hiding this comment

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

Actually I remember testing it with inline given f[T >: Nothing <: Foo]: T, and it still finds f when looking for Nothing (as understood from the unchanged behaviour in that case).

Don't try to find a hidden implicit for a part of type type that
was searched for, unless that part is fully defined.

The reason is that any type variables in a subtype might be associated
with constraints that are lost at the point of issuing a the hidden
givens addendum.

The commit also contains a tweak to "isFullyDefined" which makes
an uninstantiated type variable that is not bound in the current
constraint count as "not fully defined", where previously it was
"fully defined".
The test should have been a run test but it is unclear whether we can make it run correctly
@odersky odersky merged commit 79d2034 into scala:master Sep 27, 2019
@odersky odersky deleted the fix-#7249 branch September 27, 2019 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants