Skip to content
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

Support variance annotations in -Ykind-projector mode #12341

Conversation

neko-kai
Copy link
Contributor

@neko-kai neko-kai commented May 5, 2021

This change expands the supported subset of kind-projector syntax, specifically:

  • +* and -* variance annotated versions of the * placeholder
  • Variance annotated parameters in λ syntax - λ[(`-R`, `+E`, `+A`) => F[R, E, A]]

This change ignores the variance flag specified using kind-projector's syntax – since variance annotations in type lambdas are deprecated in Scala 3, the real variance of a variable will be inferred separately. I believe that may be acceptable since -Ykind-projector's raison d'être is to allow cross-compilation with existing Scala 2 code, not for writing new code, that is the variance annotations may be assumed to have been checked by the Scala 2 compiler.

Related PRs and issues: #7139 #7775

Prompted by discussion in scala/scala#9605

private def makeKindProjectorTypeDef(name: TypeName): TypeDef =
TypeDef(name, WildcardTypeBoundsTree()).withFlags(Param)
private def makeKindProjectorTypeDef(name: TypeName): TypeDef = {
val isVarianceAnnotated = name.lastPart.startsWith("+") || name.lastPart.startsWith("-")
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't name.startsWith be used given that these are user-written names and so should only have one part?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@smarter Changed to name.startsWith

TypeDef(name, WildcardTypeBoundsTree()).withFlags(Param)
private def makeKindProjectorTypeDef(name: TypeName): TypeDef = {
val isVarianceAnnotated = name.lastPart.startsWith("+") || name.lastPart.startsWith("-")
val unannotatedName = if (isVarianceAnnotated) name.mapLast(_.drop(1)) else name
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to add a comment mentioning that we can strip the variance because it's inferred.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@smarter Added a comment explaining the decision to just strip the variance

@neko-kai neko-kai force-pushed the feature/kind-projector-parse-variance-annotations branch from fe8ebd7 to 536661d Compare May 5, 2021 20:09
This change expands the supported subset of `kind-projector` syntax, specifically:

* `+*` and `-*` variance annotated variants of `*` placeholder
* Variance annotated named parameters in `λ` - `λ[(`-R`, `+E`, `+A`) => F[R, E, A]]`
@neko-kai neko-kai force-pushed the feature/kind-projector-parse-variance-annotations branch from 536661d to 653c5fd Compare May 5, 2021 20:10
Copy link
Member

@smarter smarter left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

@smarter smarter enabled auto-merge May 5, 2021 20:12
@smarter smarter merged commit 64351c4 into scala:master May 5, 2021
@Kordyjan Kordyjan added this to the 3.0.1 milestone Aug 2, 2023
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.

None yet

3 participants