@@ -214,7 +214,7 @@ object Applications {
214214 */
215215 class ExtMethodApply (app : Tree )(implicit @ constructorOnly src : SourceFile )
216216 extends IntegratedTypeArgs (app)
217-
217+
218218 /** 1. If we are in an inline method but not in a nested quote, mark the inline method
219219 * as a macro.
220220 *
@@ -803,6 +803,29 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
803803 if (ctx.owner.isClassConstructor && untpd.isSelfConstrCall(app)) ctx.thisCallArgContext
804804 else ctx
805805
806+ /** Typecheck the function part of an application.
807+ * Fallback if this fails: try to convert `E` to `new E`.
808+ */
809+ def typedFunPart (fn : untpd.Tree , pt : Type )(implicit ctx : Context ): Tree =
810+ tryEither { implicit ctx =>
811+ typedExpr(fn, pt)
812+ } { (result, tstate) =>
813+ def fallBack = {
814+ tstate.commit()
815+ result
816+ }
817+ fn match {
818+ case Ident (name) =>
819+ tryNewWithType(cpy.Ident (fn)(name.toTypeName), pt, fallBack)
820+ case Select (qual, name) =>
821+ tryNewWithType(cpy.Select (fn)(qual, name.toTypeName), pt, fallBack)
822+ // TODO: try to keep as much as possible from typed `qual` in order to avoid
823+ // combinatorial explosion
824+ case _ =>
825+ fallBack
826+ }
827+ }
828+
806829 /** Typecheck application. Result could be an `Apply` node,
807830 * or, if application is an operator assignment, also an `Assign` or
808831 * Block node.
@@ -812,7 +835,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
812835 def realApply (implicit ctx : Context ): Tree = track(" realApply" ) {
813836 val originalProto = new FunProto (tree.args, IgnoredProto (pt))(this , tree.isContextual)(argCtx(tree))
814837 record(" typedApply" )
815- val fun1 = typedExpr (tree.fun, originalProto)
838+ val fun1 = typedFunPart (tree.fun, originalProto)
816839
817840 // Warning: The following lines are dirty and fragile. We record that auto-tupling was demanded as
818841 // a side effect in adapt. If it was, we assume the tupled proto-type in the rest of the application,
@@ -947,7 +970,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
947970 val isNamed = hasNamedArg(tree.args)
948971 val typedArgs = if (isNamed) typedNamedArgs(tree.args) else tree.args.mapconserve(typedType(_))
949972 record(" typedTypeApply" )
950- handleMeta(typedExpr (tree.fun, PolyProto (typedArgs, pt)) match {
973+ handleMeta(typedFunPart (tree.fun, PolyProto (typedArgs, pt)) match {
951974 case IntegratedTypeArgs (app) =>
952975 app
953976 case _ : TypeApply if ! ctx.isAfterTyper =>
0 commit comments