@@ -2761,22 +2761,25 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
2761
2761
* an alternative TODO: add partial function AST node or equivalent and get rid of this synthesis --> do everything in uncurry (or later)
2762
2762
* however, note that pattern matching codegen is designed to run *before* uncurry
2763
2763
*/
2764
- def synthesizePartialFunction (paramName : TermName , paramPos : Position , paramSynthetic : Boolean ,
2764
+ def synthesizePartialFunction (paramName : TermName , paramPos : Position , paramType : Type , paramSynthetic : Boolean ,
2765
2765
tree : Tree , mode : Mode , pt : Type ): Tree = {
2766
2766
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)
2768
2768
2769
2769
// if argTp isn't fully defined, we can't translate --> error
2770
2770
// NOTE: resTp still might not be fully defined
2771
- if (! isFullyDefined(argTp )) {
2771
+ if (! isFullyDefined(argTp0 )) {
2772
2772
MissingParameterTypeAnonMatchError (tree, pt)
2773
2773
return setError(tree)
2774
2774
}
2775
+ val argTp =
2776
+ if (paramType.ne(NoType )) paramType
2777
+ else argTp0
2775
2778
2776
2779
// targs must conform to Any for us to synthesize an applyOrElse (fallback to apply otherwise -- typically for @cps annotated targs)
2777
2780
val targsValidParams = (argTp <:< AnyTpe ) && (resTp <:< AnyTpe )
2778
2781
2779
- val anonClass = context.owner newAnonymousFunctionClass tree.pos addAnnotation SerialVersionUIDAnnotation
2782
+ val anonClass = context.owner. newAnonymousFunctionClass( tree.pos). addAnnotation( SerialVersionUIDAnnotation )
2780
2783
2781
2784
import CODE ._
2782
2785
@@ -2816,7 +2819,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
2816
2819
}
2817
2820
2818
2821
// `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) }`}
2820
2823
def applyOrElseMethodDef = {
2821
2824
val methodSym = anonClass.newMethod(nme.applyOrElse, tree.pos, FINAL | OVERRIDE )
2822
2825
@@ -2836,8 +2839,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
2836
2839
2837
2840
// First, type without the default case; only the cases provided
2838
2841
// 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
2841
2843
val match0 = methodBodyTyper.typedMatch(selector(x), cases, mode, resTp)
2842
2844
val matchResTp = match0.tpe
2843
2845
@@ -3178,9 +3180,8 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
3178
3180
// you won't know you're using the wrong owner until lambda lift crashes (unless you know better than to use the wrong owner)
3179
3181
val outerTyper = newTyper(context.outer)
3180
3182
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)
3184
3185
} else doTypedFunction(fun, resProto)
3185
3186
}
3186
3187
}
@@ -4916,7 +4917,7 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
4916
4917
val cases = tree.cases
4917
4918
if (selector == EmptyTree ) {
4918
4919
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)
4920
4921
else {
4921
4922
val arity = functionArityFromType(pt) match { case - 1 => 1 case arity => arity } // scala/bug#8429: consider sam and function type equally in determining function arity
4922
4923
0 commit comments