Skip to content

More tweaks to type inference #1482

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 4, 2016

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Aug 26, 2016

It turned out we need to merge constraints when backtracking, after all.

Not used yet, but we might use it as an alternative to typedArg invalidation later.
Test case in isApplicableSafe.scala. It turns out that this
requires a context merge using the new `&' operator. Sequence of actions:

 1) Typecheck argument in typerstate 1.
 2) Cache argument.
 3) Evolve same typer state (to typecheck other arguments, say)
    leading to a different constraint.
 4) Take typechecked argument in same state.

It turns out that the merge in TyperState is needed not just for
isApplicableSafe but also for (e.g. erased-lubs.scala) as well as
many parts of dotty itself.
…lement type.

Test case is isApplicableSafe -Ycheck:first.
Need to export just uncommittedAncestor, can hide
isCommitted and parent.
@odersky odersky force-pushed the fix-asapplicable-safe branch from 58b1b49 to 2dfe4db Compare August 26, 2016 15:57
@odersky
Copy link
Contributor Author

odersky commented Aug 26, 2016

Rebased to master

@odersky
Copy link
Contributor Author

odersky commented Aug 27, 2016

Review by @smarter?

// Here's the sequence of events that leads to this problem:
//
// 1. the outer Array.apply is overloaded, so we need to typecheck the inner one
// without an expected prototype
Copy link
Member

Choose a reason for hiding this comment

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

What if instead of giving up when this fails, we tried to re-typecheck the inner call using every possible overload? Of course this would have to be done carefully to avoid ending up like the Swift typechecker which apparently gives up when trying to compile let a: Double = -(1 + 2) + -(3 + 4) + 5 because of overloading and constraint resolutions: https://www.cocoawithlove.com/blog/2016/07/12/type-checker-issues.html

On a more general note, it seems that there is a tension in Scala caused by overloading: on one hand it significantly reduces the power of type inference, on the other hand it makes some APIs much nicer to use. So either we figure out how to get overloading to play well with type inference, or we come up with a more principled alternative to overloading that helps us keep our APIs nice.

Copy link
Contributor Author

@odersky odersky Aug 27, 2016

Choose a reason for hiding this comment

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

Yeah, I am very nervous about trying all combinations. That typically leads to blowup in compile times. Then you either become too restrictive and can't even typecheck trivial expressions or your compile times go through the roof. In both cases there's little a programmer can do.

@smarter
Copy link
Member

smarter commented Aug 31, 2016

The commit message for 3664854 says:

the merge in TyperState is needed not just for
isApplicableSafe but also for (e.g. erased-lubs.scala)

Looks like a word is mising after for

@smarter
Copy link
Member

smarter commented Aug 31, 2016

Otherwise LGTM

targetState.constraint = constraint
targetState.constraint =
if (targetState.constraint eq previousConstraint) constraint
else targetState.constraint & constraint
Copy link
Member

@smarter smarter Aug 31, 2016

Choose a reason for hiding this comment

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

The commit message of 3664854 explains why merging is needed but this is tricky enough that it should also be explained in a comment here.

@odersky
Copy link
Contributor Author

odersky commented Sep 4, 2016

I'll add the note wrt merging in the next PR.

@odersky odersky merged commit a0e7adb into scala:master Sep 4, 2016
@allanrenucci allanrenucci deleted the fix-asapplicable-safe branch December 14, 2017 19:19
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