Skip to content

#9268 Port genMatch from scala-js/GenJSCode (WIP, do not merge) #9297

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

Closed
wants to merge 3 commits into from

Conversation

devkat
Copy link

@devkat devkat commented Jul 6, 2020

This is a copy of the genMatch method and its dependencies from GenJSCode with minor modifications to make it compile.

Some remarks / open issues:

Copy link
Member

@dottybot dottybot left a comment

Choose a reason for hiding this comment

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

Hello, and thank you for opening this PR! 🎉

All contributors have signed the CLA, thank you! ❤️

Commit Messages

We want to keep history, but for that to actually be useful we have
some rules on how to format our commit messages (relevant xkcd).

Please stick to these guidelines for commit messages:

  1. Separate subject from body with a blank line
  2. When fixing an issue, start your commit message with Fix #<ISSUE-NBR>:
  3. Limit the subject line to 72 characters
  4. Capitalize the subject line
  5. Do not end the subject line with a period
  6. Use the imperative mood in the subject line ("Add" instead of "Added")
  7. Wrap the body at 80 characters
  8. Use the body to explain what and why vs. how

adapted from https://chris.beams.io/posts/git-commit

Have an awesome day! ☀️

@devkat devkat changed the title #9268 Port genMatch from scala-js/GenJSCode (WIP) #9268 Port genMatch from scala-js/GenJSCode (WIP, do not merge) Jul 6, 2020

val defaultLabelSym = cases
.collectFirst {
case CaseDef(Ident(nme.WILDCARD), EmptyTree, body @ Labeled(_, rhs))
Copy link
Member

Choose a reason for hiding this comment

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

As I said on the issue. You can scrap everything that takes care of Labeled things. In scalac we have to reverse-engineer LabelDef definitions and jumps to them, but not in Dotty, because the Labeled blocks are correctly compiled to the Scala.js IR once and for all in case Labeled(bind, expr) =>.

Copy link
Author

@devkat devkat Jul 7, 2020

Choose a reason for hiding this comment

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

Thanks for the explanation, but I'm still too much of a newbie to understand what exactly I can remove. Is there any documentation about this?

Can I remove the whole defaultLabelSym value? What shall I do where it is used, e.g.

        case app @ Apply(_, Nil) if app.symbol == defaultLabelSym =>
          genJumpToElseClause

Copy link
Member

Choose a reason for hiding this comment

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

Yes, you can remove it. Basically you can consider that defaultLabelSym is always NoSymbol. That means that if app.symbol == defaultLabelSym is basically if false, and that consequently you can remove that case entirely.

From there, you'll figure that genJumpToElseClause is dead code, and hence optElseClauseLabel will never be assigned to a Some. You can therefore replace it by None and remove dead code from there as well.

The domino effect of this strategy will cause a lot of code to be removed.

Comment on lines +3308 to +3310
val op =
if (isInt(genSelector) && isInt(uniqueAlt)) js.BinaryOp.Int_==
else js.BinaryOp.===
Copy link
Member

Choose a reason for hiding this comment

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

This is what would cause a === to be emitted instead of the correct Int_==. The === case should be used only when we are switching on Strings (including null values). In scalac, patmat makes sure that only primitive Ints or java.lang.Strings can be left in Match and leave the back-end. That test therefore uses === only for strings in scalac. Apparently in dotty other kinds of numeric types survive here, like Chars. Either the back-end should convert them to ints, or PatMat should convert them to Ints.

In any case, here we should end up using === only for Strings and null. Everything should use Int_==.

@smarter smarter assigned devkat and unassigned sjrd Jul 18, 2020
@sjrd
Copy link
Member

sjrd commented Jul 22, 2020

It turns out the issues raised by this PR come from the fact that the pattern matcher does not force switch matches to operate on Ints. Instead it can leave (a mix of) Chars, Bytes, Shorts and Ints. scalac does force switch matches coming out of the PatMat phase to operate on Ints, potentially adapting the scrutinee and alternatives to Int.

I have submitted an alternative PR #9411 that deals with this problem.

@smarter
Copy link
Member

smarter commented Aug 6, 2020

Closing since #9411 got merged.

@smarter smarter closed this Aug 6, 2020
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