Skip to content

Commit a6548d6

Browse files
committed
Drop infix given for parameters and arguments
1 parent 5051612 commit a6548d6

File tree

368 files changed

+810
-787
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

368 files changed

+810
-787
lines changed

compiler/src/dotty/tools/dotc/core/Annotations.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ object Annotations {
163163
}
164164

165165
/** A regular, non-deferred Child annotation */
166-
def apply(sym: Symbol, span: Span)(implicit ctx: Context): Annotation = later(given _ => sym, span)
166+
def apply(sym: Symbol, span: Span)(implicit ctx: Context): Annotation = later(sym, span)
167167

168168
def unapply(ann: Annotation)(implicit ctx: Context): Option[Symbol] =
169169
if (ann.symbol == defn.ChildAnnot) {

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ object Parsers {
943943
def applyGiven(t: Tree, operand: () => Tree): Tree =
944944
atSpan(startOffset(t), in.offset) {
945945
in.nextToken()
946-
val args = if (in.token == LPAREN) parArgumentExprs() else operand() :: Nil
946+
val args = if (in.token == LPAREN) parArgumentExprs()._1 else operand() :: Nil
947947
Apply(t, args)
948948
}.setGivenApply()
949949

@@ -2070,7 +2070,7 @@ object Parsers {
20702070
val tapp = atSpan(startOffset(t), in.offset) { TypeApply(t, typeArgs(namedOK = true, wildOK = false)) }
20712071
simpleExprRest(tapp, canApply = true)
20722072
case LPAREN | LBRACE | INDENT if canApply =>
2073-
val app = atSpan(startOffset(t), in.offset) { Apply(t, argumentExprs()) }
2073+
val app = atSpan(startOffset(t), in.offset) { mkApply(t, argumentExprs()) }
20742074
simpleExprRest(app, canApply = true)
20752075
case USCORE =>
20762076
atSpan(startOffset(t), in.skipToken()) { PostfixOp(t, Ident(nme.WILDCARD)) }
@@ -2115,16 +2115,26 @@ object Parsers {
21152115
/** ParArgumentExprs ::= `(' [‘given’] [ExprsInParens] `)'
21162116
* | `(' [ExprsInParens `,'] PostfixExpr `:' `_' `*' ')'
21172117
*/
2118-
def parArgumentExprs(): List[Tree] = inParens {
2119-
if in.token == RPAREN then Nil
2120-
else commaSeparated(argumentExpr)
2118+
def parArgumentExprs(): (List[Tree], Boolean) = inParens {
2119+
if in.token == RPAREN then
2120+
(Nil, false)
2121+
else if in.token == GIVEN then
2122+
in.nextToken()
2123+
(commaSeparated(argumentExpr), true)
2124+
else
2125+
(commaSeparated(argumentExpr), false)
21212126
}
21222127

21232128
/** ArgumentExprs ::= ParArgumentExprs
21242129
* | [nl] BlockExpr
21252130
*/
2126-
def argumentExprs(): List[Tree] =
2127-
if (in.isNestedStart) blockExpr() :: Nil else parArgumentExprs()
2131+
def argumentExprs(): (List[Tree], Boolean) =
2132+
if (in.isNestedStart) (blockExpr() :: Nil, false) else parArgumentExprs()
2133+
2134+
def mkApply(fn: Tree, args: (List[Tree], Boolean)): Tree =
2135+
val res = Apply(fn, args._1)
2136+
if args._2 then res.setGivenApply()
2137+
res
21282138

21292139
val argumentExpr: () => Tree = () => exprInParens() match {
21302140
case arg @ Assign(Ident(id), rhs) => cpy.NamedArg(arg)(id, rhs)
@@ -2135,7 +2145,7 @@ object Parsers {
21352145
*/
21362146
def argumentExprss(fn: Tree): Tree = {
21372147
possibleBracesStart()
2138-
if (in.token == LPAREN || in.isNestedStart) argumentExprss(Apply(fn, argumentExprs()))
2148+
if (in.token == LPAREN || in.isNestedStart) argumentExprss(mkApply(fn, argumentExprs()))
21392149
else fn
21402150
}
21412151

@@ -2162,7 +2172,7 @@ object Parsers {
21622172
}
21632173
if (in.token == LPAREN && (!inClassConstrAnnots || isLegalAnnotArg))
21642174
parArgumentExprss(
2165-
atSpan(startOffset(fn)) { Apply(fn, parArgumentExprs()) }
2175+
atSpan(startOffset(fn)) { mkApply(fn, parArgumentExprs()) }
21662176
)
21672177
else fn
21682178
}
@@ -2696,10 +2706,11 @@ object Parsers {
26962706
def typeParamClauseOpt(ownerKind: ParamOwner.Value): List[TypeDef] =
26972707
if (in.token == LBRACKET) typeParamClause(ownerKind) else Nil
26982708

2699-
/** GivenTypes ::= AnnotType {‘,’ AnnotType}
2709+
/** OLD: GivenTypes ::= AnnotType {‘,’ AnnotType}
2710+
* NEW: GivenTypes ::= Type {‘,’ Type}
27002711
*/
2701-
def givenTypes(nparams: Int, ofClass: Boolean): List[ValDef] =
2702-
val tps = commaSeparated(() => annotType())
2712+
def givenTypes(newStyle: Boolean, nparams: Int, ofClass: Boolean): List[ValDef] =
2713+
val tps = commaSeparated(() => if newStyle then typ() else annotType())
27032714
var counter = nparams
27042715
def nextIdx = { counter += 1; counter }
27052716
val paramFlags = if ofClass then Private | Local | ParamAccessor else Param
@@ -2799,7 +2810,7 @@ object Parsers {
27992810
|| startParamTokens.contains(in.token)
28002811
|| isIdent && (in.name == nme.inline || in.lookaheadIn(BitSet(COLON)))
28012812
if isParams then commaSeparated(() => param())
2802-
else givenTypes(nparams, ofClass)
2813+
else givenTypes(true, nparams, ofClass)
28032814
checkVarArgsRules(clause)
28042815
clause
28052816
}
@@ -2889,7 +2900,7 @@ object Parsers {
28892900
val lastClause = params.nonEmpty && params.head.mods.flags.is(Implicit)
28902901
params :: (if (lastClause) Nil else recur(firstClause = false, nparams + params.length, isGiven))
28912902
else if isGiven then
2892-
val params = givenTypes(nparams, ofClass)
2903+
val params = givenTypes(false, nparams, ofClass)
28932904
params :: recur(firstClause = false, nparams + params.length, isGiven)
28942905
else Nil
28952906
}
@@ -3186,7 +3197,7 @@ object Parsers {
31863197
def selfInvocation(): Tree =
31873198
atSpan(accept(THIS)) {
31883199
possibleBracesStart()
3189-
argumentExprss(Apply(Ident(nme.CONSTRUCTOR), argumentExprs()))
3200+
argumentExprss(mkApply(Ident(nme.CONSTRUCTOR), argumentExprs()))
31903201
}
31913202

31923203
/** TypeDcl ::= id [TypeParamClause] TypeBounds [‘=’ Type]

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -135,27 +135,27 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
135135
def toTextTuple(args: List[Type]): Text =
136136
"(" ~ argsText(args) ~ ")"
137137

138-
def toTextFunction(args: List[Type], isContextual: Boolean, isErased: Boolean): Text =
138+
def toTextFunction(args: List[Type], isGiven: Boolean, isErased: Boolean): Text =
139139
changePrec(GlobalPrec) {
140140
val argStr: Text =
141141
if args.length == 2
142142
&& !defn.isTupleType(args.head)
143-
&& !isContextual && !isErased
143+
&& !isGiven && !isErased
144144
then
145145
atPrec(InfixPrec) { argText(args.head) }
146146
else
147147
"("
148-
~ (keywordText("given ") provided isContextual)
149-
~ (keywordText("erased ") provided isErased)
148+
~ keywordText("given ").provided(isGiven)
149+
~ keywordText("erased ").provided(isErased)
150150
~ argsText(args.init)
151151
~ ")"
152152
argStr ~ " => " ~ argText(args.last)
153153
}
154154

155155
def toTextDependentFunction(appType: MethodType): Text =
156156
"("
157-
~ (keywordText("given ") provided appType.isImplicitMethod)
158-
~ (keywordText("erased ") provided appType.isErasedMethod)
157+
~ keywordText("given ").provided(appType.isImplicitMethod)
158+
~ keywordText("erased ").provided(appType.isErasedMethod)
159159
~ paramsText(appType)
160160
~ ") => "
161161
~ toText(appType.resultType)
@@ -247,8 +247,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
247247
case _ => toTextGlobal(args, ", ")
248248
}
249249
"[applied to ("
250-
~ (Str("given ") provided tp.isContextualMethod)
251-
~ (Str("erased ") provided tp.isErasedMethod)
250+
~ keywordText("given ").provided(tp.isContextualMethod)
251+
~ keywordText("erased ").provided(tp.isErasedMethod)
252252
~ argsText
253253
~ ") returning "
254254
~ toText(resultType)
@@ -343,8 +343,8 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
343343
case id :: Nil => toText(id)
344344
case _ => "{" ~ Text(selectors map selectorText, ", ") ~ "}"
345345
}
346-
(keywordText("given ") provided givenOnly) ~
347-
toTextLocal(expr) ~ "." ~ selectorsText
346+
keywordText("given ").provided(givenOnly)
347+
~ toTextLocal(expr) ~ "." ~ selectorsText
348348
}
349349

350350
tree match {
@@ -383,13 +383,12 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
383383
keywordStr("'{") ~ toTextGlobal(args, ", ") ~ keywordStr("}")
384384
else if (!printDebug && fun.hasType && fun.symbol == defn.InternalQuoted_exprSplice)
385385
keywordStr("${") ~ toTextGlobal(args, ", ") ~ keywordStr("}")
386-
else if (app.isGivenApply && !homogenizedView)
387-
changePrec(InfixPrec) {
388-
toTextLocal(fun) ~ " given " ~
389-
(if (args.length == 1) toTextLocal(args.head) else "(" ~ toTextGlobal(args, ", ") ~ ")")
390-
}
391386
else
392-
toTextLocal(fun) ~ "(" ~ toTextGlobal(args, ", ") ~ ")"
387+
toTextLocal(fun)
388+
~ "("
389+
~ keywordText("given ").provided(app.isGivenApply && !homogenizedView)
390+
~ toTextGlobal(args, ", ")
391+
~ ")"
393392
case tree: TypeApply =>
394393
typeApplyText(tree)
395394
case Literal(c) =>
@@ -493,7 +492,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
493492
if (lo eq hi) optText(lo)(" = " ~ _)
494493
else optText(lo)(" >: " ~ _) ~ optText(hi)(" <: " ~ _)
495494
case Bind(name, body) =>
496-
("given ": Text).provided(tree.symbol.isOneOf(GivenOrImplicit) && !homogenizedView) ~ // Used for scala.quoted.Type in quote patterns (not pickled)
495+
keywordText("given ").provided(tree.symbol.isOneOf(GivenOrImplicit) && !homogenizedView) ~ // Used for scala.quoted.Type in quote patterns (not pickled)
497496
changePrec(InfixPrec) { toText(name) ~ " @ " ~ toText(body) }
498497
case Alternative(trees) =>
499498
changePrec(OrPrec) { toText(trees, " | ") }
@@ -560,12 +559,12 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
560559
toText(id) ~ "\"" ~ Text(segments map segmentText, "") ~ "\""
561560
case Function(args, body) =>
562561
var implicitSeen: Boolean = false
563-
var contextual: Boolean = false
562+
var isGiven: Boolean = false
564563
var isErased: Boolean = false
565564
def argToText(arg: Tree) = arg match {
566565
case arg @ ValDef(name, tpt, _) =>
567566
val implicitText =
568-
if ((arg.mods.is(Given))) { contextual = true; "" }
567+
if ((arg.mods.is(Given))) { isGiven = true; "" }
569568
else if ((arg.mods.is(Erased))) { isErased = true; "" }
570569
else if ((arg.mods.is(Implicit)) && !implicitSeen) { implicitSeen = true; keywordStr("implicit ") }
571570
else ""
@@ -575,13 +574,14 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
575574
}
576575
val argsText = args match {
577576
case (arg @ ValDef(_, tpt, _)) :: Nil if tpt.isEmpty => argToText(arg)
578-
case _ => "(" ~ Text(args map argToText, ", ") ~ ")"
579-
}
580-
changePrec(GlobalPrec) {
581-
(keywordText("given ") provided contextual) ~
582-
(keywordText("erased ") provided isErased) ~
583-
argsText ~ " => " ~ toText(body)
577+
case _ =>
578+
"("
579+
~ keywordText("given ").provided(isGiven)
580+
~ keywordText("erased ").provided(isErased)
581+
~ Text(args.map(argToText), ", ")
582+
~ ")"
584583
}
584+
argsText ~ " => " ~ toText(body)
585585
case PolyFunction(targs, body) =>
586586
val targsText = "[" ~ Text(targs.map((arg: Tree) => toText(arg)), ", ") ~ "]"
587587
changePrec(GlobalPrec) {
@@ -761,6 +761,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
761761
txt ~
762762
(Str(" given ") provided params.nonEmpty && params.head.mods.is(Given)) ~
763763
paramsText(params))
764+
764765
protected def valDefToText[T >: Untyped](tree: ValDef[T]): Text = {
765766
import untpd.{modsDeco => _}
766767
dclTextOr(tree) {
@@ -770,7 +771,11 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
770771
}
771772
}
772773

773-
private def paramsText[T>: Untyped](params: List[ValDef[T]]) = "(" ~ toText(params, ", ") ~ ")"
774+
private def paramsText[T>: Untyped](params: List[ValDef[T]]) =
775+
"("
776+
~ keywordText("given ").provided(params.nonEmpty && params.head.mods.is(Given))
777+
~ toText(params, ", ")
778+
~ ")"
774779

775780
protected def defDefToText[T >: Untyped](tree: DefDef[T]): Text = {
776781
import untpd.{modsDeco => _}

compiler/src/dotty/tools/dotc/transform/CheckReentrant.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class CheckReentrant extends MiniPhase {
3636
private[this] var seen: Set[ClassSymbol] = Set()
3737
private[this] var indent: Int = 0
3838

39-
private val sharableAnnot = new CtxLazy(given ctx =>
40-
ctx.requiredClass("scala.annotation.internal.sharable"))
41-
private val unsharedAnnot = new CtxLazy(given ctx =>
42-
ctx.requiredClass("scala.annotation.internal.unshared"))
39+
private val sharableAnnot = new CtxLazy(
40+
summon[Context].requiredClass("scala.annotation.internal.sharable"))
41+
private val unsharedAnnot = new CtxLazy(
42+
summon[Context].requiredClass("scala.annotation.internal.unshared"))
4343

4444
def isIgnored(sym: Symbol)(implicit ctx: Context): Boolean =
4545
sym.hasAnnotation(sharableAnnot()) ||

compiler/test-resources/repl/3932

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
scala> def fun[T](x: T): given List[T] => Int = ???
2-
def fun[T](x: T): given List[T] => Int
1+
scala> def fun[T](x: T): (given List[T]) => Int = ???
2+
def fun[T](x: T): (given List[T]) => Int

docs/docs/internals/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ DefParamClause ::= [nl] ‘(’ DefParams ‘)’
311311
GivenParamClause ::= ‘(’ ‘given’ (DefParams | GivenTypes) ‘)’
312312
DefParams ::= DefParam {‘,’ DefParam}
313313
DefParam ::= {Annotation} [‘inline’] Param ValDef(mods, id, tpe, expr) -- point of mods at id.
314-
GivenTypes ::= AnnotType {‘,’ AnnotType}
314+
GivenTypes ::= Type {‘,’ Type}
315315
ClosureMods ::= { ‘implicit’ | ‘given’}
316316
```
317317

tests/disabled/neg-with-compiler/quote-run-in-macro-2/quoted_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import given scala.quoted.autolift._
44
object Macros {
55

66
inline def foo(i: => Int): Int = ${ fooImpl('i) }
7-
def fooImpl(i: Expr[Int]) given QuoteContext: Expr[Int] = {
7+
def fooImpl(i: Expr[Int])(given QuoteContext): Expr[Int] = {
88
given Toolbox = Toolbox.make(getClass.getClassLoader)
99
val y: Int = run(i)
1010
y

tests/generic-java-signatures/i3653.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class Foo {
55
c0: T, c1: T, c2: T, c3: T, c4: T, c5: T, c6: T, c7: T, c8: T, c9: T) => 0
66

77
// #6946
8-
def baz = (x: (given String) => Unit) => x given ""
8+
def baz = (x: (given String) => Unit) => x(given "")
99
}
1010

1111
object Test {

tests/neg-custom-args/fatal-warnings/quote-simple-hole.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import scala.quoted.QuoteContext
22

3-
def test given QuoteContext = {
3+
def test(given QuoteContext) = {
44
val x = '{0}
55
val y = '{ // error: Canceled splice directly inside a quote. '{ ${ XYZ } } is equivalent to XYZ.
66
$x

tests/neg-macros/delegate-match-1/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import scala.quoted.matching._
33

44
inline def f: Any = ${ fImpl }
55

6-
private def fImpl given (qctx: QuoteContext): Expr[Unit] = {
6+
private def fImpl(given qctx: QuoteContext): Expr[Unit] = {
77
import qctx.tasty._
88
searchImplicit(('[A]).unseal.tpe) match {
99
case IsImplicitSearchSuccess(x) =>

tests/neg-macros/delegate-match-2/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import scala.quoted.matching._
33

44
inline def f: Any = ${ fImpl }
55

6-
private def fImpl given (qctx: QuoteContext): Expr[Unit] = {
6+
private def fImpl(given qctx: QuoteContext): Expr[Unit] = {
77
import qctx.tasty._
88
searchImplicit(('[A]).unseal.tpe) match {
99
case IsImplicitSearchSuccess(x) =>

tests/neg-macros/delegate-match-3/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import scala.quoted.matching._
33

44
inline def f: Any = ${ fImpl }
55

6-
private def fImpl given (qctx: QuoteContext): Expr[Unit] = {
6+
private def fImpl(given qctx: QuoteContext): Expr[Unit] = {
77
import qctx.tasty._
88
searchImplicit(('[A]).unseal.tpe) match {
99
case IsImplicitSearchSuccess(x) =>

tests/neg-macros/i6432/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.quoted.matching._
66
object Macro {
77
inline def (sc: => StringContext) foo (args: String*): Unit = ${ impl('sc) }
88

9-
def impl(sc: Expr[StringContext]) given (qctx: QuoteContext): Expr[Unit] = {
9+
def impl(sc: Expr[StringContext])(given qctx: QuoteContext): Expr[Unit] = {
1010
import qctx.tasty._
1111
sc match {
1212
case '{ StringContext(${ExprSeq(parts)}: _*) } =>

tests/neg-macros/i6432b/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import scala.quoted.matching._
66
object Macro {
77
inline def (sc: => StringContext) foo (args: String*): Unit = ${ impl('sc) }
88

9-
def impl(sc: Expr[StringContext]) given (qctx: QuoteContext): Expr[Unit] = {
9+
def impl(sc: Expr[StringContext])(given qctx: QuoteContext): Expr[Unit] = {
1010
import qctx.tasty._
1111
sc match {
1212
case '{ StringContext(${ExprSeq(parts)}: _*) } =>

tests/neg-macros/i6976/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import scala.tasty._
77
object macros {
88
inline def mcr(x: => Any) = ${mcrImpl('x)}
99

10-
def mcrImpl(body: Expr[Any]) given (ctx: QuoteContext): Expr[Any] = {
10+
def mcrImpl(body: Expr[Any])(given ctx: QuoteContext): Expr[Any] = {
1111
import ctx.tasty._
1212
body.unseal match { case Block(_, _) => '{2} }
1313
}

tests/neg-macros/inline-case-objects/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import scala.quoted._
33

44
object Macros {
5-
def impl(foo: Any) given QuoteContext: Expr[String] = foo.getClass.getCanonicalName.toExpr
5+
def impl(foo: Any)(given QuoteContext): Expr[String] = foo.getClass.getCanonicalName.toExpr
66
}
77

88
class Bar {

0 commit comments

Comments
 (0)