scala3
scala3 copied to clipboard
SIP Implementation: Precise type annotation
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:
- Add
scala.annotation.preciseannotation 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. - Add printing of
@preciseinPlainPrinter. - Add
def paramPrecises: List[Boolean]toLambdaTypeand thus propagate precise parameter indications toPolyTypeandHKLambdaType.paramPrecisesforMethodTypealways yieldsNil. 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. - Add
applyconstructors withparamPrecisesargument for thePolyType/HKLambdaTypeQuotesAPI under@experimental. When this SIP is officially accepted, at the next minor release the apply methods can be combined withparamPrecisesas an optional argument. - Add
Precisemode as bit 31. This is used to track whether we need to apply precise typing or not. - Add support for
precisedefault arguments methods by enabling precise mode for them inNamer. - 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. - Fix covariance interaction with
compiletime.opsbug (that surfaced due to changes made by this SIP). - Add
def isPrecise: BooleantoType. This is overriden is various situations. All are covered by the tests. - 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.
- Add check for overriding with the same precise annotations.
- Add check for poly types to match only with the same precise annotations.
- 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.
- Add check for class extension can be more precise, but not the other way around.
- Add precise conversion history stack to
TyperState. This is used to store arguments that experienced implicit conversions that should have been precisely typed. - Changed typing logic in
ProtoTypes,ApplicationandTyperto 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. - 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.
Added formal proposal https://github.com/scala/improvement-proposals/pull/48
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.
This SIP is withdrawn. See https://github.com/scala/improvement-proposals/pull/48#issuecomment-1719336078