Skip to content

Rename implicit match to implied match #6614

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 2 commits into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/untpd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {

case class Inline()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Inline)

case class Instance()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Implied)
case class Implied()(implicit @constructorOnly src: SourceFile) extends Mod(Flags.Implied)
}

/** Modifiers and annotations for definitions
Expand Down
36 changes: 23 additions & 13 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ object Parsers {
!in.isSoftModifierInModifierPosition

def isExprIntro: Boolean =
canStartExpressionTokens.contains(in.token) &&
!in.isSoftModifierInModifierPosition
if (in.token == IMPLIED) in.lookaheadIn(BitSet(MATCH))
else (canStartExpressionTokens.contains(in.token) && !in.isSoftModifierInModifierPosition)

def isDefIntro(allowedMods: BitSet): Boolean =
in.token == AT ||
Expand Down Expand Up @@ -1265,7 +1265,7 @@ object Parsers {
* | SimpleExpr1 ArgumentExprs `=' Expr
* | Expr2
* | [‘inline’] Expr2 `match' `{' CaseClauses `}'
* | `implicit' `match' `{' ImplicitCaseClauses `}'
* | `implied' `match' `{' ImplicitCaseClauses `}'
* Bindings ::= `(' [Binding {`,' Binding}] `)'
* Binding ::= (id | `_') [`:' Type]
* Expr2 ::= PostfixExpr [Ascription]
Expand All @@ -1279,10 +1279,19 @@ object Parsers {

def expr(location: Location.Value): Tree = {
val start = in.offset
if (closureMods.contains(in.token)) {
if(in.token == IMPLIED) {
val span = in.offset
in.nextToken()
if (in.token == MATCH)
impliedMatch(start, EmptyModifiers)
else {
syntaxError(em"illegal modifier for implied match", span)
EmptyTree
}
}
else if (closureMods.contains(in.token)) {
val imods = modifiers(closureMods)
if (in.token == MATCH) implicitMatch(start, imods)
else implicitClosure(start, location, imods)
implicitClosure(start, location, imods)
} else {
val saved = placeholderParams
placeholderParams = Nil
Expand Down Expand Up @@ -1457,13 +1466,13 @@ object Parsers {

/** `match' { ImplicitCaseClauses }
*/
def implicitMatch(start: Int, imods: Modifiers) = {
def impliedMatch(start: Int, imods: Modifiers) = {
def markFirstIllegal(mods: List[Mod]) = mods match {
case mod :: _ => syntaxError(em"illegal modifier for implicit match", mod.span)
case mod :: _ => syntaxError(em"illegal modifier for implied match", mod.span)
case _ =>
}
imods.mods match {
case Mod.Implicit() :: mods => markFirstIllegal(mods)
case Mod.Implied() :: mods => markFirstIllegal(mods)
case mods => markFirstIllegal(mods)
}
val result @ Match(t, cases) =
Expand All @@ -1474,7 +1483,7 @@ object Parsers {
case pat => isVarPattern(pat)
}
if (!isImplicitPattern(pat))
syntaxError(em"not a legal pattern for an implicit match", pat.span)
syntaxError(em"not a legal pattern for an implied match", pat.span)
}
result
}
Expand Down Expand Up @@ -1999,6 +2008,7 @@ object Parsers {
case ABSTRACT => Mod.Abstract()
case FINAL => Mod.Final()
case IMPLICIT => Mod.Implicit()
case IMPLIED => Mod.Implied()
case GIVEN => Mod.Given()
case ERASED => Mod.Erased()
case LAZY => Mod.Lazy()
Expand Down Expand Up @@ -2668,7 +2678,7 @@ object Parsers {
case ENUM =>
enumDef(start, posMods(start, mods | Enum))
case IMPLIED =>
instanceDef(start, mods, atSpan(in.skipToken()) { Mod.Instance() })
instanceDef(start, mods, atSpan(in.skipToken()) { Mod.Implied() })
case _ =>
syntaxErrorOrIncomplete(ExpectedStartOfTopLevelDefinition())
EmptyTree
Expand Down Expand Up @@ -3084,11 +3094,11 @@ object Parsers {
else if (isDefIntro(localModifierTokens))
if (closureMods.contains(in.token)) {
val start = in.offset
var imods = modifiers(closureMods)
var imods = modifiers(closureMods + IMPLIED)
if (isBindingIntro)
stats += implicitClosure(start, Location.InBlock, imods)
else if (in.token == MATCH)
stats += implicitMatch(start, imods)
stats += impliedMatch(start, imods)
else
stats +++= localDef(start, imods)
} else {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/parsing/Tokens.scala
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ object Tokens extends TokensCommon {
USCORE, NULL, THIS, SUPER, TRUE, FALSE, RETURN, QUOTEID, XMLSTART)

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

final val canStartTypeTokens: TokenSet = literalTokens | identifierTokens | BitSet(
THIS, SUPER, USCORE, LPAREN, AT)
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
def Match_copy(original: Tree)(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match =
tpd.cpy.Match(original)(selector, cases)

type ImplicitMatch = tpd.Match
type ImpliedMatch = tpd.Match

def matchImplicitMatch(x: Term)(implicit ctx: Context): Option[Match] = x match {
case x: tpd.Match if x.selector.isEmpty => Some(x)
Expand All @@ -559,10 +559,10 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.

def ImplicitMatch_cases(self: Match)(implicit ctx: Context): List[CaseDef] = self.cases

def ImplicitMatch_apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
def ImplicitMatch_apply(cases: List[CaseDef])(implicit ctx: Context): ImpliedMatch =
withDefaultPos(ctx => tpd.Match(tpd.EmptyTree, cases)(ctx))

def ImplicitMatch_copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
def ImplicitMatch_copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImpliedMatch =
tpd.cpy.Match(original)(tpd.EmptyTree, cases)

type Try = tpd.Try
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -720,9 +720,9 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
/** Reduce an inline match
* @param mtch the match tree
* @param scrutinee the scrutinee expression, assumed to be pure, or
* EmptyTree for an implicit match
* EmptyTree for an implied match
* @param scrutType its fully defined type, or
* ImplicitScrutineeTypeRef for an implicit match
* ImplicitScrutineeTypeRef for an implied match
* @param typer The current inline typer
* @return optionally, if match can be reduced to a matching case: A pair of
* bindings for all pattern-bound variables and the RHS of the case.
Expand Down Expand Up @@ -1036,7 +1036,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
def patStr(cdef: untpd.CaseDef) = i"case ${cdef.pat}${guardStr(cdef.guard)}"
val msg =
if (tree.selector.isEmpty)
em"""cannot reduce implicit match with
em"""cannot reduce implied match with
| patterns : ${tree.cases.map(patStr).mkString("\n ")}"""
else
em"""cannot reduce inline match with
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ class Typer extends Namer
tree.selector match {
case EmptyTree =>
if (tree.isInline) {
checkInInlineContext("implicit match", tree.posd)
checkInInlineContext("implied match", tree.posd)
val cases1 = tree.cases.mapconserve {
case cdef @ CaseDef(pat @ Typed(Ident(nme.WILDCARD), _), _, _) =>
// case _ : T --> case evidence$n : T
Expand Down
41 changes: 41 additions & 0 deletions library/src-bootstrapped/dotty/DottyPredef.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dotty

object DottyPredef {

@forceInline final def assert(assertion: => Boolean, message: => Any): Unit = {
if (!assertion)
assertFail(message)
}

@forceInline final def assert(assertion: => Boolean): Unit = {
if (!assertion)
assertFail()
}

def assertFail(): Unit = throw new java.lang.AssertionError("assertion failed")
def assertFail(message: => Any): Unit = throw new java.lang.AssertionError("assertion failed: " + message)

@forceInline final def implicitly[T](implicit ev: T): T = ev

@forceInline def locally[T](body: => T): T = body

/**
* Retrieve the single value of a type with a unique inhabitant.
*
* @example {{{
* object Foo
* val foo = valueOf[Foo.type]
* // foo is Foo.type = Foo
*
* val bar = valueOf[23]
* // bar is 23.type = 23
* }}}
* @group utilities
*/
inline def valueOf[T]: T = implied match {
case ev: ValueOf[T] => ev.value
}

inline def the[T] given (x: T): x.type = x

}
6 changes: 3 additions & 3 deletions library/src/scala/tasty/reflect/Core.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package scala.tasty.reflect
* | +- Lambda
* | +- If
* | +- Match
* | +- ImplicitMatch
* | +- ImpliedMatch
* | +- Try
* | +- Return
* | +- Repeated
Expand Down Expand Up @@ -209,8 +209,8 @@ trait Core {
/** Tree representing a pattern match `x match { ... }` in the source code */
type Match = kernel.Match

/** Tree representing a pattern match `implicit match { ... }` in the source code */
type ImplicitMatch = kernel.ImplicitMatch
/** Tree representing a pattern match `implied match { ... }` in the source code */
type ImpliedMatch = kernel.ImpliedMatch

/** Tree representing a try catch `try x catch { ... } finally { ... }` in the source code */
type Try = kernel.Try
Expand Down
14 changes: 7 additions & 7 deletions library/src/scala/tasty/reflect/Kernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ package scala.tasty.reflect
* | +- Lambda
* | +- If
* | +- Match
* | +- ImplicitMatch
* | +- ImpliedMatch
* | +- Try
* | +- Return
* | +- Repeated
Expand Down Expand Up @@ -476,15 +476,15 @@ trait Kernel {
def Match_apply(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match
def Match_copy(original: Tree)(selector: Term, cases: List[CaseDef])(implicit ctx: Context): Match

/** Tree representing a pattern match `implicit match { ... }` in the source code */
type ImplicitMatch <: Term
/** Tree representing a pattern match `implied match { ... }` in the source code */
type ImpliedMatch <: Term

def matchImplicitMatch(tree: Tree)(implicit ctx: Context): Option[ImplicitMatch]
def matchImplicitMatch(tree: Tree)(implicit ctx: Context): Option[ImpliedMatch]

def ImplicitMatch_cases(self: ImplicitMatch)(implicit ctx: Context): List[CaseDef]
def ImplicitMatch_cases(self: ImpliedMatch)(implicit ctx: Context): List[CaseDef]

def ImplicitMatch_apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch
def ImplicitMatch_copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch
def ImplicitMatch_apply(cases: List[CaseDef])(implicit ctx: Context): ImpliedMatch
def ImplicitMatch_copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImpliedMatch

/** Tree representing a tyr catch `try x catch { ... } finally { ... }` in the source code */
type Try <: Term
Expand Down
8 changes: 4 additions & 4 deletions library/src/scala/tasty/reflect/Printers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ trait Printers
this += "Lambda(" += meth += ", " += tpt += ")"
case Match(selector, cases) =>
this += "Match(" += selector += ", " ++= cases += ")"
case ImplicitMatch(cases) =>
this += "ImplicitMatch(" ++= cases += ")"
case ImpliedMatch(cases) =>
this += "ImpliedMatch(" ++= cases += ")"
case Return(expr) =>
this += "Return(" += expr += ")"
case While(cond, body) =>
Expand Down Expand Up @@ -909,8 +909,8 @@ trait Printers
this += highlightKeyword(" match", color)
inBlock(printCases(cases, lineBreak()))

case ImplicitMatch(cases) =>
this += highlightKeyword("implicit match", color)
case ImpliedMatch(cases) =>
this += highlightKeyword("implied match", color)
inBlock(printCases(cases, lineBreak()))

case Try(body, cases, finallyOpt) =>
Expand Down
16 changes: 8 additions & 8 deletions library/src/scala/tasty/reflect/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -591,27 +591,27 @@ trait TreeOps extends Core {
}

object IsImplicitMatch {
/** Matches any ImplicitMatch and returns it */
def unapply(tree: Tree)(implicit ctx: Context): Option[ImplicitMatch] = kernel.matchImplicitMatch(tree)
/** Matches any ImpliedMatch and returns it */
def unapply(tree: Tree)(implicit ctx: Context): Option[ImpliedMatch] = kernel.matchImplicitMatch(tree)
}

/** Scala implicit `match` term */
object ImplicitMatch {
object ImpliedMatch {

/** Creates a pattern match `implicit match { <cases: List[CaseDef]> }` */
def apply(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
/** Creates a pattern match `implied match { <cases: List[CaseDef]> }` */
def apply(cases: List[CaseDef])(implicit ctx: Context): ImpliedMatch =
kernel.ImplicitMatch_apply(cases)

def copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImplicitMatch =
def copy(original: Tree)(cases: List[CaseDef])(implicit ctx: Context): ImpliedMatch =
kernel.ImplicitMatch_copy(original)(cases)

/** Matches a pattern match `implicit match { <cases: List[CaseDef]> }` */
/** Matches a pattern match `implied match { <cases: List[CaseDef]> }` */
def unapply(tree: Tree)(implicit ctx: Context): Option[List[CaseDef]] =
kernel.matchImplicitMatch(tree).map(_.cases)

}

implicit class ImplicitMatchAPI(self: ImplicitMatch) {
implicit class ImplicitMatchAPI(self: ImpliedMatch) {
def cases(implicit ctx: Context): List[CaseDef] = kernel.ImplicitMatch_cases(self)
}

Expand Down
2 changes: 1 addition & 1 deletion tests/invalid/neg/implicitMatch-ambiguous.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object Test {
implicit val a1: A = new A
implicit val a2: A = new A

inline def f: Any = implicit match {
inline def f: Any = implied match {
case _: A => ??? // error: ambiguous implicits
}

Expand Down
2 changes: 1 addition & 1 deletion tests/invalid/run/typelevel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ object Test extends App {
case S[type N] => toInt[N] + 1
}

inline def toInt1[T]: Nat = implicit match {
inline def toInt1[T]: Nat = implied match {
case C[type T, type U], T =:= U =>
case T <:< S[type N] => toInt[N] + 1
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/implicit-match-ambiguous-bind.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ object `implicit-match-ambiguous-bind` {
case class Box[T](value: T)
implicit val ibox: Box[Int] = Box(0)
implicit val sbox: Box[String] = Box("")
inline def unbox = implicit match {
inline def unbox = implied match {
case b: Box[t] => b.value // error
}
val unboxed = unbox
Expand Down
18 changes: 9 additions & 9 deletions tests/neg/implicitMatch-syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,32 @@ object Test {
import collection.immutable.TreeSet
import collection.immutable.HashSet

inline def f1[T] = implicit implicit match { // error: repeated modifier // error: illegal modifier
case ord: Ordered[T] => new TreeSet[T] // error: no implicit
inline def f1[T] = implied implied match { // error: illegal modifier // error: ';' expected, but 'match' found // error: Declaration of method f1 not allowed here
case ord: Ordered[T] => new TreeSet[T]
case _ => new HashSet[T]

}

inline def f2[T] = implicit erased match { // error: illegal modifier
case ord: Ordered[T] => new TreeSet[T] // error: no implicit
inline def f2[T] = implied erased match { // error: illegal modifier // error: illegal modifier // error: Declaration of method f1 not allowed here
case ord: Ordered[T] => new TreeSet[T]
case _ => new HashSet[T]
}

inline def f3[T] = erased implicit match { // error: illegal modifier
case ord: Ordered[T] => new TreeSet[T] // error: no implicit
inline def f3[T] = erased implied match { // error: illegal modifier
case ord: Ordered[T] => new TreeSet[T]
case _ => new HashSet[T]
}

inline def f4() = implicit match {
inline def f4() = implied match {
case Nil => ??? // error: not a legal pattern
case x :: xs => ??? // error: not a legal pattern
}

inline def f5[T] = locally { implicit match { // Ok
inline def f5[T] = locally { implied match { // Ok
case _ => new HashSet[T]
}}

def f6[T] = implicit match { // error: implicit match cannot be used here
def f6[T] = implied match { // error: implied match cannot be used here
case _ => new HashSet[T]
}
}
2 changes: 1 addition & 1 deletion tests/neg/typeclass-derivation2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ object Show {
import scala.compiletime.{erasedValue, error}
import TypeLevel._

inline def tryShow[T](x: T): String = implicit match {
inline def tryShow[T](x: T): String = implied match {
case s: Show[T] => s.show(x)
}

Expand Down
Loading