-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add partial evaluation of isInstanceOf
mini-phase
#1275
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
Add partial evaluation of isInstanceOf
mini-phase
#1275
Conversation
* match | ||
*/ | ||
def handleStaticallyKnown(tree: Select, scrutinee: Type, selector: Type, inMatch: Boolean, pos: Position): Tree = | ||
if (!(scrutinee <:< selector) && inMatch) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it may be better to compute scrutinee <:< selector
only once.
dce4f48
to
b632e62
Compare
/rebuild |
else if (falseIfUnrelated && !(selector <:< scrutinee)) | ||
// selector and scrutinee are unrelated | ||
handleFalseUnrelated(s, scrutinee, selector, inMatch) | ||
else if (happens) tree |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no difference between happens
and !happens
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, it was kept to showcase all cases - but I'll change it to and note this in a comment.
b632e62
to
2794657
Compare
* `scrutinee.isInstanceOf[Selector]` if `scrutinee eq null` | ||
*/ | ||
def rewrite(tree: Select, to: Boolean): Tree = | ||
if (!to || !tree.qualifier.tpe.widen.derivesFrom(defn.AnyRefAlias)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Counterexample:
def foo = {println(1); 1}.isInstanceOf[Int]
need to check if tpd.isPure(tree.qual)
. If not, rewrite to Block(List(tree.qual), Literal(Constant(to)))
.
/rebuild |
if (!to || !tree.qualifier.tpe.widen.derivesFrom(defn.AnyRefAlias)) | ||
Literal(Constant(to)) | ||
else | ||
Apply(tree.qualifier.select(defn.Object_ne), List(Literal(Constant(null)))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if tree.qualifier
is not pure, it will be computed twice
The tests `i1059.scala` and `t3480.scala` are failing due to a bug in pattern matcher that evaluates the `x` in `List(x: _*)` incorrectly. Concerned issue: scala#1276
33df851
to
73f099b
Compare
LGTM |
In #1255 it was suggested to add a mini-phase that would simply partially evaluate calls to
isInstanceOf
and issue warnings/errors where appropriate.This PR adds the ability to discern if the transformed tree came from a
match
or from anif
as well as the proper error messages and warnings.The following example will yield an error:
While the following will yield a warning: