Skip to content

Commit 7028ca1

Browse files
committed
Add FunctionOf(MethodOrPoly) and improve derivedLambdaType
1 parent 9e88458 commit 7028ca1

File tree

3 files changed

+16
-7
lines changed

3 files changed

+16
-7
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,16 @@ class Definitions {
11091109
sym.owner.linkedClass.typeRef
11101110

11111111
object FunctionOf {
1112-
/** Create a `FunctionN`, `ContextFunctionN` or refined `PolyFunction` type applied to the arguments and result type
1112+
/** Create a `FunctionN`, `ContextFunctionN`, refined `PolyFunction` or dependent refinement of a `FunctionN`/`ContextFunctionN`.
1113+
*
1114+
* `PolyFunction` is used if at least one of the parameters is annotated as erased.
1115+
*/
1116+
def apply(mt: MethodOrPoly)(using Context): Type =
1117+
val ft = mt.toFunctionType(isJava = false)
1118+
assert(ft.exists, s"not a valid function type: $mt")
1119+
ft
1120+
1121+
/** Create a `FunctionN`/`ContextFunctionN` type applied to the arguments and result type or a refined `PolyFunction`.
11131122
*
11141123
* `PolyFunction` is used if at least one of the arguments is annotated as erased.
11151124
*/

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3675,7 +3675,7 @@ object Types {
36753675
trait LambdaType extends BindingType with TermType { self =>
36763676
type ThisName <: Name
36773677
type PInfo <: Type
3678-
type This <: LambdaType{type PInfo = self.PInfo}
3678+
type This >: this.type <: LambdaType{type PInfo = self.PInfo}
36793679
type ParamRefType <: ParamRef
36803680

36813681
def paramNames: List[ThisName]
@@ -3733,7 +3733,7 @@ object Types {
37333733

37343734
final def derivedLambdaType(paramNames: List[ThisName] = this.paramNames,
37353735
paramInfos: List[PInfo] = this.paramInfos,
3736-
resType: Type = this.resType)(using Context): LambdaType =
3736+
resType: Type = this.resType)(using Context): This =
37373737
if ((paramNames eq this.paramNames) && (paramInfos eq this.paramInfos) && (resType eq this.resType)) this
37383738
else newLikeThis(paramNames, paramInfos, resType)
37393739

@@ -3852,7 +3852,7 @@ object Types {
38523852
import DepStatus._
38533853
type ThisName = TermName
38543854
type PInfo = Type
3855-
type This <: TermLambda
3855+
type This >: this.type <: TermLambda
38563856
type ParamRefType = TermParamRef
38573857

38583858
override def resultType(using Context): Type =
@@ -4149,7 +4149,7 @@ object Types {
41494149
trait TypeLambda extends LambdaType {
41504150
type ThisName = TypeName
41514151
type PInfo = TypeBounds
4152-
type This <: TypeLambda
4152+
type This >: this.type <: TypeLambda
41534153
type ParamRefType = TypeParamRef
41544154

41554155
def isResultDependent(using Context): Boolean = true

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -845,9 +845,9 @@ object ProtoTypes {
845845
tp
846846
case pt: ApplyingProto =>
847847
if (rt eq mt.resultType) tp
848-
else mt.derivedLambdaType(mt.paramNames, mt.paramInfos, rt)
848+
else mt.derivedLambdaType(resType = rt)
849849
case _ =>
850-
val ft = defn.FunctionOf(mt.paramInfos, rt)
850+
val ft = defn.FunctionOf(mt.derivedLambdaType(resType = rt))
851851
if mt.paramInfos.nonEmpty || (ft frozen_<:< pt) then ft else rt
852852
}
853853
}

0 commit comments

Comments
 (0)