Skip to content

Add match types mixed with staged macro #5065

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

Conversation

nicolasstucki
Copy link
Contributor

@nicolasstucki nicolasstucki commented Aug 31, 2018

Based on #4964

odersky and others added 30 commits August 29, 2018 14:14
These types are injected as parents of Tuple1,...,Tuple22, but are
eliminated again at erasure.

To support them properly before full dotty bootstrap we needed
a @`rewrite` annotation that erases the annotated method (unlike
`@forceInline`, which generates code).
And replace Tuple.scala with a Scala 2 implementation and a Scala 3
implementation. The Scala 2 implementaion of Tuple.scala is not strictly
necessary but could be useful if non-version-specific library sources
end up referencing scala.Tuple.
Erased code does not keep full Inlined trees. The previous version converted
the Inlined tree back to the call, but this can expose leaks (since calls are
not traversed for references). We now keep the Inlined node, so that its call part
can be subsequently simplified.
With the Scala 2/3 split, we don't need it anymore.
Does not rely anymore on `deep`, which is dropped in Scala 2.14.
Was erased to Object before, but this loses precision and breaks
binary compatibility with Scala 2.
Currently, this is not exploited but some rewrites might become more effective that way in the future.
There's a difference to be made between projections of instance creations in a preceding val
on the one hand and instance creations in a def or directly written creations on the other hand.
Currently only unapplys that return a tuple are supported.
 - no nested rewrites
 - calls to rewrite unapplys only in rewrite code
Reduction of such types is not included in this commit
This way, it is always decidable whether a typed tree represents a type or a term.
Previously, such literals could be used in type position, but were classified as terms.
Need to implement printing of match types from tasty first
This is necessary since we would otherwise get direct type recursion.
We have to special case these abstract types later for subtyping.

The commit means that ad-hoc fixes in cyclic checking and variance checking
can be reverted (done also as part of this commit).
This is a more principled implementation of the situation that
match aliases are not full type aliases because one side cannot
be freely substituted for the other. Match aliases are checked
in some respects like abstract type bounds (in particular,
nested cycles are allowed), but are treated as aliases in others
(in particular, lower and upper "bound" are always guaranteed
to be the same).
...by definition since no code is generated, no side effects
can be performed either.
So far, it's very rudimentary. There's a method `typelevel.error`
which produces an error with a constant string argument. To make
this more powerful we need a rewriting framework that interpolates
strings and can report compiler trees and terms in such strings.
 - add upper bound to MatchType
 - have special MatchTypeTree to represent MatchTypes
Implemented through a "successor" operation `S`, which is
interpreted when applied to constant numbers.
odersky and others added 18 commits August 29, 2018 14:20
Also, fix creators for typed/untyped MatchTypeTrees
Only check the bound, instead of all branches, in the definition of a match type.
A function that produces the constant value represented by a type.
... and move `_(_)`, `_.head`, `_.tail` to it. That way,
we no not need to specify a specific (probably wildcarded) pair type
when invoking these operations.

I first tried to move the three operations simply to Tuple. But since
`Unit` is a `Tuple` as well, this gives us funny error messages for `apply`. For instance
this one in errMsgTests:

```
object Scope{
  def foo(a: Int) = ()
  foo(1)("2")
}
```
If `Tuple` had an `apply` method, this would give `expected: Int, found : String`
Need to take possible reductions into account.
Avoids rewrites of possibly very large terms
This allows to base more Tuple operations on types instead of rewrite terms
useful to allow libraries to fail gracefully if something is not a constant
(not necessarily a parameter, it could be a quantity computed by the rewrite
method).
See test case. This caused two spurious overloding errors,
one when looking for equality evidence and then again in the
emit method of the pattern matcher.

A spurious error was also eliminated in neg/ensureReported.scala.
The previous version worked with a -Xss2m setting, but it seems stack size
used in tests is significantly smaller than that.

Is there a way to set the stack size for a specific test? Then we could
re-enable the commented-out code.
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! ☀️

@nicolasstucki
Copy link
Contributor Author

@biboudis
Here are some early benchmarks compiling tests/pos-deep-subtype/tuples2.scala with all the comented cases enabled https://docs.google.com/spreadsheets/d/1kldUJBt86odse4Z9yi8HnUXlV_8vPxwDw4TpypvshXA/edit#gid=12019036
Should double check if the generated code is the same and the size of the trees in different phases.

@nicolasstucki nicolasstucki changed the title Add matchtype mix staged macro Add match types mixed with staged macro Aug 31, 2018
l == level ||
level == -1 && sym == defn.TastyTasty_macroContext
level == -1 //&& (sym.isType || sym == defn.TastyTasty_macroContext)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this quick fix may make some tests fail

@nicolasstucki
Copy link
Contributor Author

Replaced with #5182

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants