@@ -2761,22 +2761,25 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
27612761 * an alternative TODO: add partial function AST node or equivalent and get rid of this synthesis --> do everything in uncurry (or later)
27622762 * however, note that pattern matching codegen is designed to run *before* uncurry
27632763 */
2764- def synthesizePartialFunction (paramName : TermName , paramPos : Position , paramSynthetic : Boolean ,
2764+ def synthesizePartialFunction (paramName : TermName , paramPos : Position , paramType : Type , paramSynthetic : Boolean ,
27652765 tree : Tree , mode : Mode , pt : Type ): Tree = {
27662766 assert(pt.typeSymbol == PartialFunctionClass , s " PartialFunction synthesis for match in $tree requires PartialFunction expected type, but got $pt. " )
2767- val (argTp , resTp) = partialFunctionArgResTypeFromProto(pt)
2767+ val (argTp0 , resTp) = partialFunctionArgResTypeFromProto(pt)
27682768
27692769 // if argTp isn't fully defined, we can't translate --> error
27702770 // NOTE: resTp still might not be fully defined
2771- if (! isFullyDefined(argTp )) {
2771+ if (! isFullyDefined(argTp0 )) {
27722772 MissingParameterTypeAnonMatchError (tree, pt)
27732773 return setError(tree)
27742774 }
2775+ val argTp =
2776+ if (paramType.ne(NoType )) paramType
2777+ else argTp0
27752778
27762779 // targs must conform to Any for us to synthesize an applyOrElse (fallback to apply otherwise -- typically for @cps annotated targs)
27772780 val targsValidParams = (argTp <:< AnyTpe ) && (resTp <:< AnyTpe )
27782781
2779- val anonClass = context.owner newAnonymousFunctionClass tree.pos addAnnotation SerialVersionUIDAnnotation
2782+ val anonClass = context.owner. newAnonymousFunctionClass( tree.pos). addAnnotation( SerialVersionUIDAnnotation )
27802783
27812784 import CODE ._
27822785
@@ -2816,7 +2819,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
28162819 }
28172820
28182821 // `def applyOrElse[A1 <: $argTp, B1 >: $matchResTp](x: A1, default: A1 => B1): B1 =
2819- // ${`$selector match { $cases; case default$ => default(x) }`
2822+ // ${`$selector match { $cases; case default$ => default(x) }`}
28202823 def applyOrElseMethodDef = {
28212824 val methodSym = anonClass.newMethod(nme.applyOrElse, tree.pos, FINAL | OVERRIDE )
28222825
@@ -2836,8 +2839,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
28362839
28372840 // First, type without the default case; only the cases provided
28382841 // by the user are typed. The LUB of these becomes `B`, the lower
2839- // bound of `B1`, which in turn is the result type of the default
2840- // case
2842+ // bound of `B1`, which in turn is the result type of the default case
28412843 val match0 = methodBodyTyper.typedMatch(selector(x), cases, mode, resTp)
28422844 val matchResTp = match0.tpe
28432845
@@ -3178,9 +3180,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
31783180 // you won't know you're using the wrong owner until lambda lift crashes (unless you know better than to use the wrong owner)
31793181 val outerTyper = newTyper(context.outer)
31803182 val p = vparams.head
3181- if (p.tpt.tpe == null ) p.tpt setType outerTyper.typedType(p.tpt).tpe
3182-
3183- outerTyper.synthesizePartialFunction(p.name, p.pos, paramSynthetic = false , funBody, mode, pt)
3183+ if (p.tpt.tpe == null ) p.tpt.setType(outerTyper.typedType(p.tpt).tpe)
3184+ outerTyper.synthesizePartialFunction(p.name, p.pos, p.tpt.tpe, paramSynthetic = false , funBody, mode, pt)
31843185 } else doTypedFunction(fun, resProto)
31853186 }
31863187 }
@@ -4916,7 +4917,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
49164917 val cases = tree.cases
49174918 if (selector == EmptyTree ) {
49184919 if (pt.typeSymbol == PartialFunctionClass )
4919- synthesizePartialFunction(newTermName(fresh.newName(" x" )), tree.pos, paramSynthetic = true , tree, mode, pt)
4920+ synthesizePartialFunction(newTermName(fresh.newName(" x" )), tree.pos, paramType = NoType , paramSynthetic = true , tree, mode, pt)
49204921 else {
49214922 val arity = functionArityFromType(pt) match { case - 1 => 1 case arity => arity } // scala/bug#8429: consider sam and function type equally in determining function arity
49224923
0 commit comments