Skip to content

Commit

Permalink
Anonymous method with using (#2128)
Browse files Browse the repository at this point in the history
  • Loading branch information
kpbochenek committed Oct 5, 2020
1 parent 60507a0 commit 5ea2e00
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 11 deletions.
17 changes: 9 additions & 8 deletions community-test/src/test/scala/CommunityDottySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class CommunityDottySuite extends FunSuite {
val communityBuilds = List(
CommunityBuild(
"https://github.com/lampepfl/dotty.git",
//commit hash from 24.09
"85d1322c4e8a7254b67dd7b88fa8fdf87b4dac72",
//commit hash from 5.10
"4b8a1de304f9ec5f0b39d0e69ed9ae8a2f9fb151",
"dotty",
dottyExclusionList
),
Expand Down Expand Up @@ -156,17 +156,18 @@ class CommunityDottySuite extends FunSuite {
"library/src/scala/Tuple.scala", // [t] => t => F[t]
"src/main/scala/dotty/tools/benchmarks/tuples/TupleOps.scala", // [A] => A => Tuple
"src/main/scala/dotty/tools/benchmarks/tuples/Map.scala", // [T] => (x:T) => x
"input/src/main/scala/example/level2/Documentation.scala", // val refinementTest: (wtf??)
"tastydoc/src/dotty/tastydoc/comment/WikiParser.scala", // list mkString ""
"input/src/main/scala/example/level2/Documentation.scala", // val refinementTest: // Type def in newline :/
"/tools/dotc/core/Annotations.scala", // (Context ?=> Tree) = (using ctx) => bodyFn(using ctx)
"/tools/dotc/core/Flags.scala", // val (^Private^ @ _, PrivateTerm @ _, PrivateType @ _) = newFlags
"/tools/dotc/core/classfile/ClassfileParser.scala", // (using ctx: Context) ^=>^ annotType.classSymbol
"/tools/dotc/core/Symbols.scala", // extension [N <: Name](sym: Symbol { type ThisName = N })^(^using Context)
"tools/dotc/quoted/PickledQuotes.scala", // if (arg.isTerm) (using qctx: QuoteContext) ^=>^ new .Expr(arg)
"compiler/src/dotty/tools/dotc/util/LinearSet.scala", // ???
"/compiler/src/dotty/tools/dotc/util/LinearMap.scala", // ???
"src/dotty/tools/dotc/semanticdb/Tools.scala", // ???

// extension (c: Circle)(using Context)
"/tools/dotc/core/Symbols.scala",
"compiler/src/dotty/tools/dotc/typer/ConstFold.scala",
// wrong alignment, PR for dotty issued.
"tools/dotc/quoted/PickledQuotes.scala",
// ident.match { ... }
"tools/dotc/core/tasty/TreePickler.scala",
"dotty/tools/dotc/core/Types.scala",
Expand Down Expand Up @@ -198,6 +199,7 @@ class CommunityDottySuite extends FunSuite {
"tools/dotc/typer/QuotesAndSplices.scala",
"tools/dotc/typer/ErrorReporting.scala", // for if then yield
"tools/dotc/ast/Desugar.scala", // for if yield
"tools/dotc/transform/localopt/StringContextChecker.scala", // for flag <- flags ^;^ if flag._1 == '#' do
"/tools/dotc/typer/Typer.scala", // case ref @ OrNull(tpnn) ^:^ TermRef
"compiler/src/dotty/tools/dotc/transform/Splicer.scala",
// if () block
Expand All @@ -207,7 +209,6 @@ class CommunityDottySuite extends FunSuite {
// match <indent> case => match <indent> case => (match in match indented)
"tools/dotc/semanticdb/ExtractSemanticDB.scala",
// test reproduces: using-lambda-method-parameter
"tools/dotc/core/tasty/TreeUnpickler.scala",
"test/dotty/tools/dottydoc/GenDocs.scala" // ???
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,12 @@ class ScalametaParser(input: Input, dialect: Dialect) { parser =>
if (token.is[LeftParen]) {
val cond = condExpr()
newLinesOpt()
val body = expr()
val body = if (token.is[KwDo]) {
accept[KwDo]
exprMaybeIndented()
} else {
expr()
}
Term.While(cond, body)
} else if (token.is[Indentation.Indent]) {
val cond = block()
Expand Down Expand Up @@ -2027,7 +2032,8 @@ class ScalametaParser(input: Input, dialect: Dialect) { parser =>
// 4a. `x: Int => ...` means self-type annotation in template position
// 4b. `x: Int => ...` means lambda in block position
// 4c. `x: Int => ...` means ascription, i.e. `x: (Int => ...)`, in expression position
// 5. `(x: Int) => ...` means lambda
// 5a. `(x: Int) => ...` means lambda
// 5b. `(using x: Int) => ...` means lambda for dotty
// 6. `(x, y) => ...` or `(x: Int, y: Int) => ...` or with more entries means lambda
//
// A funny thing is that scalac's parser tries to disambiguate between self-type annotations and lambdas
Expand All @@ -2041,6 +2047,9 @@ class ScalametaParser(input: Input, dialect: Dialect) { parser =>
object NameLike {
def unapply(tree: Tree): Boolean = tree match {
case Term.Quasi(0, _) => true
case Term.Ascribe(Term.Select(Term.Name(SkUsing.name), _), _)
if dialect.allowGivenUsing =>
true
case _: Term.Name => true
case _: Term.Placeholder => true
case _ => false
Expand Down Expand Up @@ -2089,6 +2098,8 @@ class ScalametaParser(input: Input, dialect: Dialect) { parser =>
Term.Param(Nil, atPos(name, name)(Name.Anonymous()), Some(tpt), None)
)
)
case Term.Ascribe(Term.Select(Term.Name(SkUsing.name), name), tpt) =>
Some(atPos(tree, tree)(Term.Param(List(Mod.Using()), name, Some(tpt), None)))
case Lit.Unit() =>
None
case other =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,11 @@ object TreeSyntax {
}
m(Expr, param, " ", kw("=>"), " ", p(Expr, body))
case Term.Function(params, body) =>
m(Expr, s("(", r(params, ", "), ") ", kw("=>"), " ", p(Expr, body)))
if (params.headOption.exists(_.mods.exists(_.is[Mod.Using]))) {
m(Expr, s("(", kw("using"), " ", r(params, ", "), ") ", kw("=>"), " ", p(Expr, body)))
} else {
m(Expr, s("(", r(params, ", "), ") ", kw("=>"), " ", p(Expr, body)))
}
}
case Term.QuotedMacroExpr(Term.Block(stats)) =>
stats match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,31 @@ class ControlSyntaxSuite extends BaseDottySuite {
)
}

test("while-parens-yet-do") {
val code = """|def read(): String = {
| while (cond) do {}
| other()
|}
|""".stripMargin
val output = """|def read(): String = {
| while (cond) {}
| other()
|}
|""".stripMargin
runTestAssert[Stat](code, assertLayout = Some(output))(
Defn.Def(
Nil,
Term.Name("read"),
Nil,
List(List()),
Some(Type.Name("String")),
Term.Block(
List(Term.While(Term.Name("cond"), Term.Block(Nil)), Term.Apply(Term.Name("other"), Nil))
)
)
)
}

// --------------------------
// OTHER
// --------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,20 @@ class ExtensionMethodsSuite extends BaseDottySuite {
)
}

test("extension-using-combo".ignore) {
val code = """|extension (c: Circle)(using Context) {
| def crc: Int = 2
|}
|""".stripMargin
runTestAssert[Stat](code, assertLayout = Some("extension (c: Circle) def crc: Int = 2"))(
Defn.ExtensionGroup(
Term.Param(Nil, Term.Name("c"), Some(pname("Circle")), None),
Nil,
Defn.Def(Nil, tname("crc"), Nil, Nil, Some(pname("Int")), int(2))
)
)
}

test("extension-soft-keyword") {
runTestAssert[Stat]("val c = f(a + extension)")(
Defn.Val(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,21 @@ class GivenUsingSuite extends BaseDottySuite {
)
}

test("using-anonymous-method") {
runTestAssert[Stat]("val fun = (using ctx: Context) => ctx.open")(
Defn.Val(
Nil,
List(Pat.Var(Term.Name("fun"))),
None,
Term.Function(
List(Term.Param(List(Mod.Using()), Term.Name("ctx"), Some(Type.Name("Context")), None)),
Term.Select(Term.Name("ctx"), Term.Name("open"))
)
)
)

}

// ---------------------------------
// GIVEN IMPORT
// ---------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -441,4 +441,36 @@ class MinorDottySuite extends BaseDottySuite {
)
}

test("type-wildcard-questionmark") {
runTestAssert[Stat]("val x: List[?] = List(1)")(
Defn.Val(
Nil,
List(Pat.Var(Term.Name("x"))),
Some(Type.Apply(Type.Name("List"), List(Type.Placeholder(Type.Bounds(None, None))))),
Term.Apply(Term.Name("List"), List(Lit.Int(1)))
)
)

runTestAssert[Stat]("def x(a: List[?]): Unit = ()")(
Defn.Def(
Nil,
Term.Name("x"),
Nil,
List(
List(
Term.Param(
Nil,
Term.Name("a"),
Some(Type.Apply(Type.Name("List"), List(Type.Placeholder(Type.Bounds(None, None))))),
None
)
)
),
Some(Type.Name("Unit")),
Lit.Unit()
)
)

}

}

0 comments on commit 5ea2e00

Please sign in to comment.