scala3 icon indicating copy to clipboard operation
scala3 copied to clipboard

SIP Implementation: Precise type annotation

Open soronpo opened this issue 3 years ago • 2 comments

Summary

This PR adds support for the Precise Type Annotation SIP.

Links

Official Proposal: https://github.com/scala/improvement-proposals/pull/48 Discussion: https://contributors.scala-lang.org/t/pre-sip-exact-type-annotation/5835

Implementation

The implementation includes the following:

  1. Add scala.annotation.precise annotation class to be applied on type parameters. This class is only available under @experimental, but the underlying implementation can still affect performance of the compiler for existing applications.
  2. Add printing of @precise in PlainPrinter.
  3. Add def paramPrecises: List[Boolean] to LambdaType and thus propagate precise parameter indications to PolyType and HKLambdaType. paramPrecises for MethodType always yields Nil. This is the most disruptive change that affects most of the files. An alternative and more fragile approach could have used mutation similarly to variance propagation.
  4. Add apply constructors with paramPrecises argument for the PolyType/HKLambdaType Quotes API under @experimental. When this SIP is officially accepted, at the next minor release the apply methods can be combined with paramPrecises as an optional argument.
  5. Add Precise mode as bit 31. This is used to track whether we need to apply precise typing or not.
  6. Add support for precise default arguments methods by enabling precise mode for them in Namer.
  7. Change desugaring for exported defs and implicit classes to keep precise annotations. This problem was only noticed during incremental compilation and therefore we have related test under sbt-test/precise.
  8. Fix covariance interaction with compiletime.ops bug (that surfaced due to changes made by this SIP).
  9. Add def isPrecise: Boolean to Type. This is overriden is various situations. All are covered by the tests.
  10. Change constraints logic to prevent widening when the type parameter is precise or in precise mode, or when the parameter is upper-bound by a precise type, or when the parameter is from a precise position in an applied type.
  11. Add check for overriding with the same precise annotations.
  12. Add check for poly types to match only with the same precise annotations.
  13. Add check for type/opaque aliasing. Type aliasing must match or be "less precise" (the checks for precise params is done after dealiasing). Opaque type aliasing must match the same precise annotations.
  14. Add check for class extension can be more precise, but not the other way around.
  15. Add precise conversion history stack to TyperState. This is used to store arguments that experienced implicit conversions that should have been precisely typed.
  16. Changed typing logic in ProtoTypes, Application and Typer to accommodate various situations where precise typing needs to be applied. In situations where the compiler knows the method before typing the arguments, then typing only occurs once, and according to the precise types. However, when we have unknown method until arguments are typed (overloading, extension methods, and implicit conversions), then we need to redo the typing with the proper precise typing enforced.
  17. Lock precise variables when summoning implicits to prevent widening to bounds.

Testing

I've been "dogfooding" this myself as the implementation progressed via our library that required a lot of precise argument usages. Consequently, this PR has quite extensive checks that try to cover a wide range of language feature interactions with @precise. Please look at the tests and suggest other interactions I may have missed covering. Performance tests have yet to be carried out

Related Issues

Resolves https://github.com/scala/bug/issues/10838 Resolves https://github.com/lampepfl/dotty/issues/8231

Acknowledgement

The work on this PR was sponsored by DFiant.

soronpo avatar Jul 27 '22 04:07 soronpo

Added formal proposal https://github.com/scala/improvement-proposals/pull/48

soronpo avatar Sep 02 '22 21:09 soronpo

OK, I did a complete rebase and split the PR into change-related commits. I'll update the SIP proposal to reflect the latest changes. I consider this work now complete and waiting for reviews.

soronpo avatar Sep 09 '22 00:09 soronpo

This SIP is withdrawn. See https://github.com/scala/improvement-proposals/pull/48#issuecomment-1719336078

soronpo avatar Sep 14 '23 12:09 soronpo