Skip to content

Commit 7ffc78c

Browse files
committed
Use same kind of Attachment for Using and Infix applies
1 parent 6a81c4c commit 7ffc78c

File tree

7 files changed

+22
-27
lines changed

7 files changed

+22
-27
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+11-10
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ object desugar {
537537
val nu = vparamss.foldLeft(makeNew(classTypeRef)) { (nu, vparams) =>
538538
val app = Apply(nu, vparams.map(refOfDef))
539539
vparams match {
540-
case vparam :: _ if vparam.mods.is(Given) => app.setUsingApply()
540+
case vparam :: _ if vparam.mods.is(Given) => app.setApplyKind(ApplyKind.Using)
541541
case _ => app
542542
}
543543
}
@@ -1188,18 +1188,19 @@ object desugar {
11881188
case Assign(Ident(name), rhs) => cpy.NamedArg(arg)(name, rhs)
11891189
case _ => arg
11901190
}
1191-
def makeOp(fn: Tree, arg: Tree, selectPos: Span) = {
1192-
val args: List[Tree] = arg match {
1193-
case Parens(arg) => assignToNamedArg(arg) :: Nil
1194-
case Tuple(args) if args.nonEmpty => // this case should be dropped if auto-tupling is removed
1195-
args.mapConserve(assignToNamedArg)
1196-
case _ => arg :: Nil
1197-
}
1191+
def makeOp(fn: Tree, arg: Tree, selectPos: Span) =
11981192
val sel = Select(fn, op.name).withSpan(selectPos)
11991193
if (left.sourcePos.endLine < op.sourcePos.startLine)
12001194
sel.pushAttachment(MultiLineInfix, ())
1201-
InfixApply(sel, args)
1202-
}
1195+
arg match
1196+
case Parens(arg) =>
1197+
Apply(sel, assignToNamedArg(arg) :: Nil)
1198+
case Tuple(Nil) =>
1199+
Apply(sel, arg :: Nil).setApplyKind(ApplyKind.InfixUnit)
1200+
case Tuple(args) if args.nonEmpty => // this case should be dropped if auto-tupling is removed
1201+
Apply(sel, args.mapConserve(assignToNamedArg))
1202+
case _ =>
1203+
Apply(sel, arg :: Nil)
12031204

12041205
if (isLeftAssoc(op.name))
12051206
makeOp(left, right, Span(left.span.start, op.span.end, op.span.start))

compiler/src/dotty/tools/dotc/ast/Trees.scala

+6-5
Original file line numberDiff line numberDiff line change
@@ -444,20 +444,21 @@ object Trees {
444444
}
445445

446446
enum ApplyKind:
447-
case Regular, Using, Infix
447+
case Regular // r.f(x)
448+
case Using // r.f(using x)
449+
case InfixUnit // r f (), needs to be treated specially for an error message in typedApply
448450

449451
/** fun(args) */
450452
case class Apply[-T >: Untyped] private[ast] (fun: Tree[T], args: List[Tree[T]])(implicit @constructorOnly src: SourceFile)
451453
extends GenericApply[T] {
452454
type ThisTree[-T >: Untyped] = Apply[T]
453455

454-
def setUsingApply() =
455-
putAttachment(untpd.ApplyGiven, ())
456+
def setApplyKind(kind: ApplyKind) =
457+
putAttachment(untpd.KindOfApply, kind)
456458
this
457459

458460
def applyKind: ApplyKind =
459-
if hasAttachment(untpd.ApplyGiven) then ApplyKind.Using
460-
else ApplyKind.Regular
461+
attachmentOrElse(untpd.KindOfApply, ApplyKind.Regular)
461462
}
462463

463464

compiler/src/dotty/tools/dotc/ast/untpd.scala

+1-8
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
134134
case _ => name
135135
}
136136

137-
/** Short-lived class created by desugar.binop and used by typedApply to
138-
* improve error messages for infix applications
139-
*/
140-
class InfixApply(fun: Tree, args: List[Tree])(implicit @constructorOnly src: SourceFile)
141-
extends Apply(fun, args):
142-
override def applyKind = ApplyKind.Infix
143-
144137
case class Number(digits: String, kind: NumberKind)(implicit @constructorOnly src: SourceFile) extends TermTree
145138

146139
enum NumberKind {
@@ -351,7 +344,7 @@ object untpd extends Trees.Instance[Untyped] with UntypedTreeInfo {
351344
val OriginalSymbol: Property.Key[Symbol] = Property.Key()
352345

353346
/** Property key for contextual Apply trees of the form `fn given arg` */
354-
val ApplyGiven: Property.StickyKey[Unit] = Property.StickyKey()
347+
val KindOfApply: Property.StickyKey[ApplyKind] = Property.StickyKey()
355348

356349
// ------ Creation methods for untyped only -----------------
357350

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -2354,7 +2354,7 @@ object Parsers {
23542354

23552355
def mkApply(fn: Tree, args: (List[Tree], Boolean)): Tree =
23562356
val res = Apply(fn, args._1)
2357-
if args._2 then res.setUsingApply()
2357+
if args._2 then res.setApplyKind(ApplyKind.Using)
23582358
res
23592359

23602360
val argumentExpr: () => Tree = () => expr(Location.InArgs) match

compiler/src/dotty/tools/dotc/typer/Applications.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ trait Applications extends Compatibility {
615615
case arg :: args1 =>
616616
val msg = arg match
617617
case untpd.Tuple(Nil)
618-
if applyKind == ApplyKind.Infix && funType.widen.isNullaryMethod =>
618+
if applyKind == ApplyKind.InfixUnit && funType.widen.isNullaryMethod =>
619619
i"can't supply unit value with infix notation because nullary $methString takes no arguments; use dotted invocation instead: (...).${methRef.name}()"
620620
case _ =>
621621
i"too many arguments for $methString"

compiler/src/dotty/tools/dotc/typer/EtaExpansion.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ object EtaExpansion extends LiftImpure {
242242
if (mt.paramInfos.nonEmpty && mt.paramInfos.last.isRepeatedParam)
243243
ids = ids.init :+ repeated(ids.last)
244244
val app = Apply(lifted, ids)
245-
if (mt.isContextualMethod) app.setUsingApply()
245+
if (mt.isContextualMethod) app.setApplyKind(ApplyKind.Using)
246246
val body = if (isLastApplication) app else PostfixOp(app, Ident(nme.WILDCARD))
247247
val fn =
248248
if (mt.isContextualMethod) new untpd.FunctionWithMods(params, body, Modifiers(Given))

compiler/src/dotty/tools/dotc/typer/Typer.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -3047,7 +3047,7 @@ class Typer extends Namer
30473047
}
30483048
tryEither {
30493049
val app = cpy.Apply(tree)(untpd.TypedSplice(tree), namedArgs)
3050-
if (wtp.isContextualMethod) app.setUsingApply()
3050+
if (wtp.isContextualMethod) app.setApplyKind(ApplyKind.Using)
30513051
typr.println(i"try with default implicit args $app")
30523052
typed(app, pt, locked)
30533053
} { (_, _) =>

0 commit comments

Comments
 (0)