Skip to content

Drop old syntax styles for givens #7245

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

Merged
merged 20 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ object desugar {

/** Invent a name for an anonympus given of type or template `impl`. */
def inventGivenName(impl: Tree)(implicit ctx: Context): SimpleName =
avoidIllegalChars(s"${inventName(impl)}_given".toTermName.asSimpleName)
avoidIllegalChars(s"given_${inventName(impl)}".toTermName.asSimpleName)

/** The normalized name of `mdef`. This means
* 1. Check that the name does not redefine a Scala core class.
Expand Down
19 changes: 9 additions & 10 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2518,7 +2518,6 @@ object Parsers {
case ABSTRACT => Mod.Abstract()
case FINAL => Mod.Final()
case IMPLICIT => Mod.Implicit()
case IMPLIED => Mod.Given()
case GIVEN => Mod.Given()
case ERASED => Mod.Erased()
case LAZY => Mod.Lazy()
Expand Down Expand Up @@ -2622,10 +2621,10 @@ object Parsers {
* FunTypeMods ::= { ‘erased’ | ‘given’}
*/
val closureMods: BitSet =
if allowOldGiven then BitSet(GIVEN, IMPLIED, IMPLICIT, ERASED)
if allowOldGiven then BitSet(GIVEN, IMPLICIT, ERASED)
else BitSet(IMPLICIT, ERASED)

val funTypeMods: BitSet = BitSet(IMPLIED, ERASED)
val funTypeMods: BitSet = BitSet(ERASED)

val funTypeArgMods: BitSet = BitSet(GIVEN, ERASED)

Expand Down Expand Up @@ -2923,7 +2922,7 @@ object Parsers {
*/
def importClause(leading: Token, mkTree: ImportConstr): List[Tree] = {
val offset = accept(leading)
val importGiven = allowOldGiven && in.token == IMPLIED || in.token == GIVEN
val importGiven = allowOldGiven && in.token == GIVEN
if (importGiven) in.nextToken()
commaSeparated(importExpr(importGiven, mkTree)) match {
case t :: rest =>
Expand Down Expand Up @@ -3267,8 +3266,8 @@ object Parsers {
objectDef(start, posMods(start, mods | Case | Module))
case ENUM =>
enumDef(start, posMods(start, mods | Enum))
case IMPLIED | GIVEN =>
instanceDef(in.token == GIVEN, start, mods, atSpan(in.skipToken()) { Mod.Given() })
case GIVEN =>
instanceDef(start, mods, atSpan(in.skipToken()) { Mod.Given() })
case _ =>
syntaxErrorOrIncomplete(ExpectedStartOfTopLevelDefinition())
EmptyTree
Expand Down Expand Up @@ -3384,7 +3383,7 @@ object Parsers {
* GivenSig ::= [id] [DefTypeParamClause] {GivenParamClause}
* ExtParamClause ::= [DefTypeParamClause] DefParamClause {GivenParamClause}
*/
def instanceDef(newStyle: Boolean, start: Offset, mods: Modifiers, instanceMod: Mod) = atSpan(start, nameStart) {
def instanceDef(start: Offset, mods: Modifiers, instanceMod: Mod) = atSpan(start, nameStart) {
var mods1 = addMod(mods, instanceMod)
val hasGivenSig = followingIsGivenSig()
val name = if isIdent && hasGivenSig then ident() else EmptyTermName
Expand All @@ -3409,7 +3408,7 @@ object Parsers {
parseParams(isExtension = !hasGivenSig)
var oldSyntax = false
val parents =
if allowOldGiven && (!newStyle && in.token == FOR || isIdent(nme.as)) then
if allowOldGiven && isIdent(nme.as) then
oldSyntax = true
// for the moment, accept both `delegate for` and `given as`
in.nextToken()
Expand Down Expand Up @@ -3734,12 +3733,12 @@ object Parsers {
setLastStatOffset()
if (in.token == IMPORT)
stats ++= importClause(IMPORT, Import)
else if (in.token == IMPLIED || in.token == GIVEN) {
else if (in.token == GIVEN) {
val start = in.offset
val mods = modifiers(closureMods)
mods.mods match {
case givenMod :: Nil if !isBindingIntro =>
stats += instanceDef(true, start, EmptyModifiers, Mod.Given().withSpan(givenMod.span))
stats += instanceDef(start, EmptyModifiers, Mod.Given().withSpan(givenMod.span))
case _ =>
stats += implicitClosure(in.offset, Location.InBlock, mods)
}
Expand Down
17 changes: 7 additions & 10 deletions compiler/src/dotty/tools/dotc/parsing/Tokens.scala
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,9 @@ object Tokens extends TokensCommon {
final val FORSOME = 61; enter(FORSOME, "forSome") // TODO: deprecate
final val ENUM = 62; enter(ENUM, "enum")
final val ERASED = 63; enter(ERASED, "erased")
final val IMPLIED = 64; enter(IMPLIED, "delegate")
final val GIVEN = 65; enter(GIVEN, "given")
final val EXPORT = 66; enter(EXPORT, "export")
final val MACRO = 67; enter(MACRO, "macro") // TODO: remove
final val GIVEN = 64; enter(GIVEN, "given")
final val EXPORT = 65; enter(EXPORT, "export")
final val MACRO = 66; enter(MACRO, "macro") // TODO: remove

/** special symbols */
final val NEWLINE = 78; enter(NEWLINE, "end of statement", "new line")
Expand Down Expand Up @@ -221,14 +220,14 @@ object Tokens extends TokensCommon {
USCORE, NULL, THIS, SUPER, TRUE, FALSE, RETURN, QUOTEID, XMLSTART)

final val canStartExpressionTokens: TokenSet = atomicExprTokens | BitSet(
LBRACE, LPAREN, INDENT, QUOTE, IF, DO, WHILE, FOR, NEW, TRY, THROW, GIVEN)
LBRACE, LPAREN, INDENT, QUOTE, IF, DO, WHILE, FOR, NEW, TRY, THROW)

final val canStartTypeTokens: TokenSet = literalTokens | identifierTokens | BitSet(
THIS, SUPER, USCORE, LPAREN, AT)

final val templateIntroTokens: TokenSet = BitSet(CLASS, TRAIT, OBJECT, ENUM, CASECLASS, CASEOBJECT)

final val dclIntroTokens: TokenSet = BitSet(DEF, VAL, VAR, TYPE, IMPLIED, GIVEN)
final val dclIntroTokens: TokenSet = BitSet(DEF, VAL, VAR, TYPE, GIVEN)

final val defIntroTokens: TokenSet = templateIntroTokens | dclIntroTokens

Expand All @@ -245,8 +244,6 @@ object Tokens extends TokensCommon {

final val modifierFollowers = modifierTokens | defIntroTokens

final val paramIntroTokens: TokenSet = modifierTokens | identifierTokens | BitSet(AT, VAL, VAR, IMPLICIT)

/** Is token only legal as start of statement (eof also included)? */
final val mustStartStatTokens: TokenSet = defIntroTokens | modifierTokens | BitSet(IMPORT, EXPORT, PACKAGE)

Expand All @@ -268,7 +265,7 @@ object Tokens extends TokensCommon {

final val canStartIndentTokens: BitSet =
statCtdTokens | BitSet(COLONEOL, EQUALS, ARROW, LARROW, WHILE, TRY, FOR)
// `if` is excluded because it often comes after `else` which makes for awkward indentation rules
// `if` is excluded because it often comes after `else` which makes for awkward indentation rules TODO: try to do without the exception

/** Faced with the choice between a type and a formal parameter, the following
* tokens determine it's a formal parameter.
Expand All @@ -285,7 +282,7 @@ object Tokens extends TokensCommon {
final val noIndentAfterConditionTokens = BitSet(THEN, DO)
final val noIndentAfterEnumeratorTokens = BitSet(YIELD, DO)

final val scala3keywords = BitSet(ENUM, ERASED, GIVEN, IMPLIED)
final val scala3keywords = BitSet(ENUM, ERASED, GIVEN)

final val softModifierNames = Set(nme.inline, nme.opaque)
}
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class ReplCompilerTests extends ReplTest {
fromInitialState { implicit state => run("given as Int = 10") }
.andThen { implicit state =>
assertEquals(
"def Int_given: Int",
"def given_Int: Int",
storedOutput().trim
)
run("implicitly[Int]")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Given instances can be mapped to combinations of implicit objects, classes and i
would map to
```scala
final implicit lazy val global: ExecutionContext = new ForkJoinContext()
final implicit def Context_given = ctx
final implicit def given_Context = ctx
```

### Anonymous Given Instances
Expand Down
1 change: 0 additions & 1 deletion tests/new/test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ trait T {
type U = (given Int) => Int
type V = (given Int) => Int

val u = delegate (x: Int) => x
val v = given (x: Int) => x
val w = (given x: Int) => x
val w2: V = (given x) => x
Expand Down
3 changes: 1 addition & 2 deletions tests/pos/i6997b.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package playground

import scala.quoted._, scala.quoted.matching._
import delegate scala.quoted._
import scala.quoted.{_, given}, scala.quoted.matching._

inline def mcr(x: => Any): Any = ${mcrImpl('x)}

Expand Down
6 changes: 3 additions & 3 deletions tests/pos/reference/delegates.scala
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,13 @@ object Implicits extends Common {
}
implicit def ListOrd[T: Ord]: Ord[List[T]] = new ListOrd[T]

class Convertible_List_List_given[From, To](implicit c: Convertible[From, To])
class given_Convertible_List_List[From, To](implicit c: Convertible[From, To])
extends Convertible[List[From], List[To]] {
def (x: List[From]) convert: List[To] = x.map(c.convert)
}
implicit def Convertible_List_List_given[From, To](implicit c: Convertible[From, To])
implicit def given_Convertible_List_List[From, To](implicit c: Convertible[From, To])
: Convertible[List[From], List[To]] =
new Convertible_List_List_given[From, To]
new given_Convertible_List_List[From, To]

def maximum[T](xs: List[T])
(implicit cmp: Ord[T]): T =
Expand Down