Skip to content

Harden Type Inference #12560

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 8 commits into from
May 25, 2021
Merged

Harden Type Inference #12560

merged 8 commits into from
May 25, 2021

Conversation

odersky
Copy link
Contributor

@odersky odersky commented May 21, 2021

Various improvements to make type inference and internal checking more robust. Best reviewed commit by commit.

These all address issues that were first uncovered when trying to run the type inferencer on inferred code again, where all type variables are reset to undefined. So the program is different since it contains adaptations like implicit arguments, but the
type variables are inferred again.

@odersky
Copy link
Contributor Author

odersky commented May 21, 2021

Based on #12535. Best reviewed commit by commit.

odersky added 5 commits May 21, 2021 19:21
The purpose of canDefineFurther is to discover new members. That is not
helped by instantiating a type variable to `Nothing`. Avoiding the instantiation
helpos error messages. There was one test (i864.scala) that fails now
but compiled previously with a surprising instantiation to Nothing. I believe
it is better to issue an error message in this case.
We should retry adapt if we can instantiate typevars in either the type
of the tree or the expected type
@odersky odersky force-pushed the improve-typeinf branch from 073202b to 12d2e20 Compare May 21, 2021 17:22
This was deemed too hard before for type inference, but it works now.
@odersky odersky requested a review from smarter May 23, 2021 15:19
Copy link
Member

@smarter smarter left a comment

Choose a reason for hiding this comment

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

Otherwise LGTM.

Comment on lines +347 to +348
MonadFlatten.flattened(List(List(1, 2, 3), List(4, 5))) // ok, synthesizes (using ListMonad)
MonadFlatten.flattened(List(List(1, 2, 3), List(4, 5)))(using ListMonad) // error
Copy link
Member

Choose a reason for hiding this comment

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

To be clear: passing ListMonad explicitly already failed on master and this PR doesn't improve the situation but just documents it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, correct.

Comment on lines 1135 to 1137
case SAMType(sam @ MethodTpe(_, formals, restpe)) =>
case RefinedType(parent, nme.apply, mt @ MethodTpe(_, formals, restpe))
if defn.isNonRefinedFunction(parent) && formals.length == defaultArity =>
(formals, untpd.DependentTypeTree(syms => restpe.substParams(mt, syms.map(_.termRef))))
Copy link
Member

Choose a reason for hiding this comment

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

Do we have a testcase for this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact I could not come up with one. The only test that fails in the SAM case was scala/reflect/TypeTest.scala. Making the analogous example based on dependent functions instead of SAMs passed even without this patch. I am going to include that test. I have reverted the change, since it's not clear we need it.

@smarter smarter assigned odersky and unassigned smarter May 25, 2021
@odersky odersky force-pushed the improve-typeinf branch from b123253 to f674c18 Compare May 25, 2021 17:34
@smarter smarter enabled auto-merge May 25, 2021 19:34
@smarter smarter merged commit 17a6ab0 into scala:master May 25, 2021
@smarter smarter deleted the improve-typeinf branch May 25, 2021 20:09
@Kordyjan Kordyjan added this to the 3.0.1 milestone Aug 2, 2023
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.

3 participants