@@ -393,6 +393,16 @@ object desugar {
393393 vparam.withMods(mods & (GivenOrImplicit | Erased | hasDefault) | Param )
394394 }
395395
396+ def mkApply (fn : Tree , paramss : List [ParamClause ])(using Context ): Tree =
397+ paramss.foldLeft(fn) { (fn, params) => params match
398+ case TypeDefs (params) =>
399+ TypeApply (fn, params.map(refOfDef))
400+ case (vparam : ValDef ) :: _ if vparam.mods.is(Given ) =>
401+ Apply (fn, params.map(refOfDef)).setApplyKind(ApplyKind .Using )
402+ case _ =>
403+ Apply (fn, params.map(refOfDef))
404+ }
405+
396406 /** The expansion of a class definition. See inline comments for what is involved */
397407 def classDef (cdef : TypeDef )(using Context ): Tree = {
398408 val impl @ Template (constr0, _, self, _) = cdef.rhs
@@ -588,7 +598,7 @@ object desugar {
588598 }
589599
590600 // new C[Ts](paramss)
591- lazy val creatorExpr = {
601+ lazy val creatorExpr =
592602 val vparamss = constrVparamss match
593603 case (vparam :: _) :: _ if vparam.mods.is(Implicit ) => // add a leading () to match class parameters
594604 Nil :: constrVparamss
@@ -607,7 +617,6 @@ object desugar {
607617 }
608618 }
609619 ensureApplied(nu)
610- }
611620
612621 val copiedAccessFlags = if migrateTo3 then EmptyFlags else AccessFlags
613622
@@ -892,48 +901,50 @@ object desugar {
892901 }
893902 }
894903
904+ def extMethod (mdef : DefDef , extParamss : List [ParamClause ])(using Context ): DefDef =
905+ cpy.DefDef (mdef)(
906+ name = normalizeName(mdef, mdef.tpt).asTermName,
907+ paramss =
908+ if mdef.name.isRightAssocOperatorName then
909+ val (typaramss, paramss) = mdef.paramss.span(isTypeParamClause) // first extract type parameters
910+
911+ paramss match
912+ case params :: paramss1 => // `params` must have a single parameter and without `given` flag
913+
914+ def badRightAssoc (problem : String ) =
915+ report.error(i " right-associative extension method $problem" , mdef.srcPos)
916+ extParamss ++ mdef.paramss
917+
918+ params match
919+ case ValDefs (vparam :: Nil ) =>
920+ if ! vparam.mods.is(Given ) then
921+ // we merge the extension parameters with the method parameters,
922+ // swapping the operator arguments:
923+ // e.g.
924+ // extension [A](using B)(c: C)(using D)
925+ // def %:[E](f: F)(g: G)(using H): Res = ???
926+ // will be encoded as
927+ // def %:[A](using B)[E](f: F)(c: C)(using D)(g: G)(using H): Res = ???
928+ val (leadingUsing, otherExtParamss) = extParamss.span(isUsingOrTypeParamClause)
929+ leadingUsing ::: typaramss ::: params :: otherExtParamss ::: paramss1
930+ else
931+ badRightAssoc(" cannot start with using clause" )
932+ case _ =>
933+ badRightAssoc(" must start with a single parameter" )
934+ case _ =>
935+ // no value parameters, so not an infix operator.
936+ extParamss ++ mdef.paramss
937+ else
938+ extParamss ++ mdef.paramss
939+ ).withMods(mdef.mods | ExtensionMethod )
940+
895941 /** Transform extension construct to list of extension methods */
896942 def extMethods (ext : ExtMethods )(using Context ): Tree = flatTree {
897- for mdef <- ext.methods yield
898- defDef(
899- cpy.DefDef (mdef)(
900- name = normalizeName(mdef, ext).asTermName,
901- paramss =
902- if mdef.name.isRightAssocOperatorName then
903- val (typaramss, paramss) = mdef.paramss.span(isTypeParamClause) // first extract type parameters
904-
905- paramss match
906- case params :: paramss1 => // `params` must have a single parameter and without `given` flag
907-
908- def badRightAssoc (problem : String ) =
909- report.error(i " right-associative extension method $problem" , mdef.srcPos)
910- ext.paramss ++ mdef.paramss
911-
912- params match
913- case ValDefs (vparam :: Nil ) =>
914- if ! vparam.mods.is(Given ) then
915- // we merge the extension parameters with the method parameters,
916- // swapping the operator arguments:
917- // e.g.
918- // extension [A](using B)(c: C)(using D)
919- // def %:[E](f: F)(g: G)(using H): Res = ???
920- // will be encoded as
921- // def %:[A](using B)[E](f: F)(c: C)(using D)(g: G)(using H): Res = ???
922- val (leadingUsing, otherExtParamss) = ext.paramss.span(isUsingOrTypeParamClause)
923- leadingUsing ::: typaramss ::: params :: otherExtParamss ::: paramss1
924- else
925- badRightAssoc(" cannot start with using clause" )
926- case _ =>
927- badRightAssoc(" must start with a single parameter" )
928- case _ =>
929- // no value parameters, so not an infix operator.
930- ext.paramss ++ mdef.paramss
931- else
932- ext.paramss ++ mdef.paramss
933- ).withMods(mdef.mods | ExtensionMethod )
934- )
943+ ext.methods map {
944+ case exp : Export => exp
945+ case mdef : DefDef => defDef(extMethod(mdef, ext.paramss))
946+ }
935947 }
936-
937948 /** Transforms
938949 *
939950 * <mods> type t >: Low <: Hi
0 commit comments