Skip to content

Fix #8111: Use better algorithms to infer parameter types #8232

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
Feb 16, 2020

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Feb 6, 2020

An inferred parameter type I has two possible sources:

  • the type S known from the context
  • the type T known from the callee f if the lambda is of a form like x => f(x)

If T exists, we know that S <: I <: T. In this commit we make use of this information in order
to be more intelligent how the type I is inferred.

This change required two other fixes.

First, we need to be more precise in IsFullyDefinedAccumulator. Previously, a maximized type variable could be instantiated to a type that contains more type variables. These have to be instantiated
in turn, but were not. 5e59f87

Second, we need to make calleeType retractable since it is no longer just a part of last-ditch effort with no alternatives remaining.. Previously, it was not. 4c84ee8

@odersky odersky force-pushed the fix-#8111 branch 2 times, most recently from a858ec0 to 7030544 Compare February 7, 2020 08:43
A maximized type variable could be instantiated to a type
that contains more type variables. These have to be instantiated
in turn.
When we reach out to get the callee type, we might get an error, e.g.
if the callee is overloaded. Example:
```
ch => sb.append(ch)
```
Here we might want to call calleeType to find the argument type
of `sb.append` in order to use this type as the type of `ch`. But
this fails since `append` is overloaded on StringBuffer.

Prevously this did not matter since we called calleeType only as a last
effort, so if it failed the whole typing failed. But with the changes
forseen for fiing scala#8111 we need to be able to compute calleeType and
retract it if it does not work.
An inferred parameter type I has two passible sources:

 - the type S known from the context
 - the type T known from the callee `f` if the lambda is of a form like `x => f(x)`

If `T` exists, we know that `S <: I <: T`. In this commit we make use of this information
to be more intelligent how the type I is inferred.
@odersky odersky changed the title Fix a problem in IsFullyDefinedAccumulator Fix #8111: Use better algorithms to infer parameter types Feb 7, 2020
We need to split `notBottom` into two states

 - flipBottom   the old noBottom: when encountering bottom, maximimze instead
 - failBottom   when encountering bottom, return false

That way, we can have an inferredParamType which is closer to the old one.
Try first to instantiate the formal type known from the context, but fail on
bottom. If that fails, compute target type and proceed as before.
@odersky odersky requested a review from smarter February 8, 2020 14:53
@odersky odersky merged commit b4e037c into scala:master Feb 16, 2020
@odersky odersky deleted the fix-#8111 branch February 16, 2020 10:51
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