Skip to content

Merge isInstanceOfEvaluator with TypeTestCasts #2820

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 6 commits into from
Jul 9, 2017

Conversation

odersky
Copy link
Contributor

@odersky odersky commented Jun 29, 2017

As the saying goes: "let erasure handle it... "

The reason for the change is that things were a mess before

  • isInstanceOfEvaluator did not handle &/| types. It would
    be hard to change that since it was looking at erased types.
  • isInstanceEvaluator, but not TypeTestCasts added null-checks
  • therefore, necessary null-checks were not emitted on some tests of
    the form x.isInstanceOf[A & B].
  • the pattern matcher patched this up by inserting some null checks
    on its own, whereas it should just have let the translation of
    isInstanceOf do the job.

odersky added 3 commits June 29, 2017 16:51
As the saying goes: "let erasure handle it... "

The reason for the change is that things were a mess before

 - isInstanceOfEvaluator did not handle &/| types. It would
   be hard to change that since it was looking at erased types.
 - isInstanceEvaluator, but not TypeTestCasts added null-checks
 - therefore, necessary null-checks were not emitted on some tests of
   the form `x.isInstanceOf[A & B]`.
 - the pattern matcher patched this up by inserting some null checks
   on its own, whereas it should just have let the translation of
   isInstanceOf do the job.
To be documented once we figured out whether it works.
The optimizer seems to do some optimizations regarding
simplifications of pattern matchings. This masks
errors to be reported later at erasure.

The optimizer should perform these actions only
after erasure.
@DarkDimius
Copy link
Contributor

  • isInstanceEvaluator, but not TypeTestCasts added null-checks
  • therefore, necessary null-checks were not emitted on some tests of
    the form x.isInstanceOf[A & B].
  • the pattern matcher patched this up by inserting some null checks
    on its own, whereas it should just have let the translation of
    isInstanceOf do the job.

Adding those null checks is not necessary: x.isInstanceOf[Foo] is always false for null.
IsInstanceOfEvaluator was rewriting x.isInstanceOf[Foo] to x != null if static type of x was known to be a subclass of Foo.

Am I missing some other case?

@odersky
Copy link
Contributor Author

odersky commented Jun 29, 2017

isInstanceOfEvaluator does not work if the tested type is of the form A | B or A & B.

@DarkDimius
Copy link
Contributor

Aaah, I missed that those points rely on the first one.

Thanks!

odersky added 2 commits June 29, 2017 18:38
 - drop a test which prevented a check in isInstanceOfEvaluator but which
   can be dropped with the more precise types in TypeTestCasts

 - move a test which was only partially effective in RefChecks to TypeTestCasts
   (problem is again testing against &/| types).
@odersky odersky force-pushed the fix-isInstanceOf branch from 1005e69 to 11961bb Compare June 29, 2017 19:37
@odersky
Copy link
Contributor Author

odersky commented Jun 29, 2017

I believe we have now concentrated all logic around isInstanceOf in TypeTestCasts. Before this was distributed in various checks and rewrites in RefChecks, isInstanceOfEvaluator and TypeTestCasts. There remain some null checks in the pattern matcher which should now be redundant. There's also the outer test generated by pattern matcher, but inserting that at Erasure is too late (we'd need to do an ensureExplicitOuter before).

Local opts before erasure should be changed so that always failing instance tests are not replaced by false (these need to be reported in TypeTestsCasts). If someone knows of other places where isInstanceOf is handled it would be good to know and to check whether we can also move this to TypeTestsCasts.

@odersky odersky requested a review from felixmulder June 29, 2017 20:01
@odersky
Copy link
Contributor Author

odersky commented Jun 29, 2017

Looking at line counts, IsInstanceOfEvaluator is now no longer used, even though it is left in the repo for now, in case we want to transfer some of its bits. That's a savings of 173 lines, for a total reduction of ~130 lines.

@smarter
Copy link
Member

smarter commented Jun 29, 2017

even though it is left in the repo for now, in case we want to transfer some of its bits

I suggest getting rid of it, otherwise it's too easy to get confused and think it's used code.

Also a big 👍 from me for this PR, this is much more sane than the previous situation.

@smarter
Copy link
Member

smarter commented Jun 29, 2017

See also scala/bug#10379 which also affects dotty

@odersky
Copy link
Contributor Author

odersky commented Jun 29, 2017

@smarter We actually have quite a few miniphases which are not linked but are still in the repo. Maybe it's time for a global sweep to remove all of them?

@smarter
Copy link
Member

smarter commented Jun 29, 2017

Sounds good to me.

Copy link
Contributor

@felixmulder felixmulder left a comment

Choose a reason for hiding this comment

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

LGTM, but why wasn't the IsInstanceOfEvaluator itself removed?

cpy.TypeApply(tree)(expr1.select(sym).withPos(expr.pos), List(TypeTree(tp)))

def foundCls = expr.tpe.widen.classSymbol
// println(i"ta $tree, found = $foundCls")
Copy link
Contributor

Choose a reason for hiding this comment

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

Forgotten removal - or leave in as a nice point for debug?

@DarkDimius DarkDimius modified the milestone: 0.2 Tech Preview Jul 4, 2017
@odersky odersky merged commit 23e64a3 into scala:master Jul 9, 2017
@allanrenucci allanrenucci deleted the fix-isInstanceOf branch December 14, 2017 19:20
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.

4 participants