Skip to content

Commit

Permalink
Allow -_ and +_ as type names in order to support lastest changes
Browse files Browse the repository at this point in the history
for kind-projector that works under -Xsource3 flag.
  • Loading branch information
dos65 committed May 18, 2021
1 parent d6a864b commit 1c3bbdf
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
20 changes: 16 additions & 4 deletions scalameta/dialects/shared/src/main/scala/scala/meta/Dialect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ final class Dialect private (
// Scala 3 no longer allows def hello(){} - `=` is always needed
val allowProcedureSyntax: Boolean,
// Scala 3 no longer allows `do {...} while(...)`
val allowDoWhile: Boolean
val allowDoWhile: Boolean,
/* Kind-project support
* works under -Xsource3 flag
* https://github.com/scala/scala/pull/9605
*/
val allowPlusMinusUnderscoreIdent: Boolean
) extends Product with Serializable {

// NOTE(olafur) checklist for adding a new dialect field in a binary compatible way:
Expand Down Expand Up @@ -253,7 +258,8 @@ final class Dialect private (
allowAsForImportRename = false,
allowStarWildcardImport = false,
allowProcedureSyntax = true,
allowDoWhile = true
allowDoWhile = true,
allowPlusMinusUnderscoreIdent = false
// NOTE(olafur): declare the default value for new fields above this comment.
)
}
Expand Down Expand Up @@ -455,6 +461,10 @@ final class Dialect private (
def withAllowDoWhile(newValue: Boolean): Dialect = {
privateCopy(allowDoWhile = newValue)
}

def withAllowPlusMinusUnderscoreIdent(newValue: Boolean): Dialect = {
privateCopy(allowPlusMinusUnderscoreIdent = newValue)
}
// NOTE(olafur): add the next `withX()` method above this comment. Please try
// to use consistent formatting, use `newValue` as the parameter name and wrap
// the body inside curly braces.
Expand Down Expand Up @@ -520,7 +530,8 @@ final class Dialect private (
allowAsRenames: Boolean = this.allowAsForImportRename,
allowStarWildcardImport: Boolean = this.allowStarWildcardImport,
allowProcedureSyntax: Boolean = this.allowProcedureSyntax,
allowDoWhile: Boolean = this.allowDoWhile
allowDoWhile: Boolean = this.allowDoWhile,
allowPlusMinusUnderscoreIdent: Boolean = this.allowPlusMinusUnderscoreIdent
// NOTE(olafur): add the next parameter above this comment.
): Dialect = {
new Dialect(
Expand Down Expand Up @@ -583,7 +594,8 @@ final class Dialect private (
allowAsRenames,
allowStarWildcardImport,
allowProcedureSyntax,
allowDoWhile
allowDoWhile,
allowPlusMinusUnderscoreIdent
// NOTE(olafur): add the next argument above this comment.
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ package object dialects {
.withAllowInfixMods(true)
.withAllowPostfixStarVarargSplices(true)
.withAllowAndTypes(true)
.withAllowPlusMinusUnderscoreIdent(true)

/**
* Dialect starting with Scala 2.12.14 for `-Xsource:3` option
Expand All @@ -81,6 +82,7 @@ package object dialects {
.withAllowInfixMods(true)
.withAllowPostfixStarVarargSplices(true)
.withAllowAndTypes(true)
.withAllowPlusMinusUnderscoreIdent(true)

implicit val Scala = Scala213 // alias for latest Scala dialect.

Expand Down Expand Up @@ -152,6 +154,7 @@ package object dialects {
.withAllowStarWildcardImport(true)
.withAllowProcedureSyntax(false)
.withAllowDoWhile(false)
.withAllowPlusMinusUnderscoreIdent(true)

@deprecated("Use Scala3 instead", "4.4.2")
implicit val Dotty = Scala3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1729,12 +1729,21 @@ class ScalametaParser(input: Input)(implicit dialect: Dialect) { parser =>
Type.Macro(macroSplice())
case Ident("?") if dialect.allowQuestionMarkPlaceholder =>
next(); atPos(in.prevTokenPos, auto)(Type.Placeholder(typeBounds()))
case Literal() =>
if (dialect.allowLiteralTypes) literal()
else syntaxError(s"$dialect doesn't support literal types", at = path())
case ident: Ident
if (ident.value == "+" || ident.value == "-") &&
ahead(token.is[Underscore]) &&
dialect.allowPlusMinusUnderscoreIdent =>
autoPos {
accept[Ident]
accept[Underscore]
Type.Name(s"${ident.value}_")
}
case Ident("-") if ahead { token.is[NumericLiteral] } && dialect.allowLiteralTypes =>
val term = termName()
atPos(term, auto)(literal(isNegated = true))
case Literal() =>
if (dialect.allowLiteralTypes) literal()
else syntaxError(s"$dialect doesn't support literal types", at = path())
case _ =>
val ref = path() match {
case q: Quasi => q.become[Term.Ref.Quasi]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,11 @@ class TypeSuite extends ParseSuite {
assertNoDiff(exceptionScala2.shortMessage, "illegal literal type (), use Unit instead")

}

test("plus-minus-then-undescore-source3") {
val Type.Function(List(Type.Name("+_")), Type.Name("Int")) =
tpe("+_ => Int")(dialects.Scala213Source3)
val Type.Apply(Type.Name("Option"), List(Type.Name("-_"))) =
tpe("Option[- _]")(dialects.Scala213Source3)
}
}

0 comments on commit 1c3bbdf

Please sign in to comment.