-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Refactor dependent function refinement logic #18305
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
nicolasstucki
wants to merge
1
commit into
scala:main
from
dotty-staging:refactor-dependent-refinemet-logic
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1109,23 +1109,34 @@ class Definitions { | |
sym.owner.linkedClass.typeRef | ||
|
||
object FunctionOf { | ||
/** Matches a `FunctionN[...]`/`ContextFunctionN[...]` or refined `PolyFunction`/`FunctionN[...]`/`ContextFunctionN[...]`. | ||
* Extracts the method type type and apply info. | ||
*/ | ||
def unapply(ft: Type)(using Context): Option[MethodOrPoly] = { | ||
ft match | ||
case RefinedFunctionOf(mt) => Some(mt) | ||
case FunctionNOf(argTypes, resultType, isContextual) => | ||
val methodType = if isContextual then ContextualMethodType else MethodType | ||
Some(methodType(argTypes, resultType)) | ||
case _ => None | ||
} | ||
} | ||
|
||
object FunctionNOf { | ||
/** Create a `FunctionN` or `ContextFunctionN` type applied to the arguments and result type */ | ||
def apply(args: List[Type], resultType: Type, isContextual: Boolean = false)(using Context): Type = | ||
val mt = MethodType.companion(isContextual, false)(args, resultType) | ||
if mt.hasErasedParams then | ||
RefinedType(PolyFunctionClass.typeRef, nme.apply, mt) | ||
else | ||
FunctionType(args.length, isContextual).appliedTo(args ::: resultType :: Nil) | ||
def unapply(ft: Type)(using Context): Option[(List[Type], Type, Boolean)] = { | ||
ft.dealias match | ||
case PolyFunctionOf(mt: MethodType) => | ||
Some(mt.paramInfos, mt.resType, mt.isContextualMethod) | ||
case dft => | ||
val tsym = dft.typeSymbol | ||
if isFunctionSymbol(tsym) && ft.isRef(tsym) then | ||
val targs = dft.argInfos | ||
if (targs.isEmpty) None | ||
else Some(targs.init, targs.last, tsym.name.isContextFunction) | ||
else None | ||
FunctionType(args.length, isContextual).appliedTo(args ::: resultType :: Nil) | ||
|
||
/** Matches a (possibly aliased) `FunctionN[...]` or `ContextFunctionN[...]`. | ||
* Extracts the list of function argument types, the result type and whether function is contextual. | ||
*/ | ||
def unapply(tpe: Type)(using Context): Option[(List[Type], Type, Boolean)] = { | ||
val tsym = tpe.typeSymbol | ||
if isFunctionSymbol(tsym) && tpe.isRef(tsym) then | ||
val targs = tpe.argInfos | ||
if (targs.isEmpty) None | ||
else Some(targs.init, targs.last, tsym.name.isContextFunction) | ||
else None | ||
} | ||
} | ||
|
||
|
@@ -1165,6 +1176,7 @@ class Definitions { | |
def isValidMethodType(info: Type) = info match | ||
case info: MethodType => | ||
!info.resType.isInstanceOf[MethodOrPoly] // Has only one parameter list | ||
&& !info.isParamDependent | ||
case _ => false | ||
info match | ||
case info: PolyType => isValidMethodType(info.resType) | ||
|
@@ -1731,26 +1743,20 @@ class Definitions { | |
|
||
def isProductSubType(tp: Type)(using Context): Boolean = tp.derivesFrom(ProductClass) | ||
|
||
/** Is `tp` (an alias) of either a scala.FunctionN or a scala.ContextFunctionN | ||
* instance? | ||
/** Returns whether `tp` is an instance or a refined instance of: | ||
* - scala.FunctionN | ||
* - scala.ContextFunctionN | ||
*/ | ||
def isNonRefinedFunction(tp: Type)(using Context): Boolean = | ||
val arity = functionArity(tp) | ||
val sym = tp.dealias.typeSymbol | ||
def isFunctionNType(tp: Type)(using Context): Boolean = | ||
val tp1 = tp.dropDependentRefinement | ||
val arity = functionArity(tp1) | ||
val sym = tp1.dealias.typeSymbol | ||
|
||
arity >= 0 | ||
&& isFunctionClass(sym) | ||
&& tp.isRef( | ||
&& tp1.isRef( | ||
FunctionType(arity, sym.name.isContextFunction).typeSymbol, | ||
skipRefined = false) | ||
end isNonRefinedFunction | ||
|
||
/** Returns whether `tp` is an instance or a refined instance of: | ||
* - scala.FunctionN | ||
* - scala.ContextFunctionN | ||
*/ | ||
def isFunctionNType(tp: Type)(using Context): Boolean = | ||
isNonRefinedFunction(tp.dropDependentRefinement) | ||
|
||
/** Returns whether `tp` is an instance or a refined instance of: | ||
* - scala.FunctionN | ||
|
@@ -1873,24 +1879,6 @@ class Definitions { | |
def isContextFunctionType(tp: Type)(using Context): Boolean = | ||
asContextFunctionType(tp).exists | ||
|
||
/** An extractor for context function types `As ?=> B`, possibly with | ||
* dependent refinements. Optionally returns a triple consisting of the argument | ||
* types `As`, the result type `B` and a whether the type is an erased context function. | ||
*/ | ||
object ContextFunctionType: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't it still nice for convenience to keep an extractor just for context functions? |
||
def unapply(tp: Type)(using Context): Option[(List[Type], Type, List[Boolean])] = | ||
if ctx.erasedTypes then | ||
atPhase(erasurePhase)(unapply(tp)) | ||
else | ||
asContextFunctionType(tp) match | ||
case PolyFunctionOf(mt: MethodType) => | ||
Some((mt.paramInfos, mt.resType, mt.erasedParams)) | ||
case tp1 if tp1.exists => | ||
val args = tp1.functionArgInfos | ||
val erasedParams = List.fill(functionArity(tp1)) { false } | ||
Some((args.init, args.last, erasedParams)) | ||
case _ => None | ||
|
||
/** A whitelist of Scala-2 classes that are known to be pure */ | ||
def isAssuredNoInits(sym: Symbol): Boolean = | ||
(sym `eq` SomeClass) || isTupleClass(sym) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is nice from a conceptual viewpoint, but I'm a bit worried about the performance impact of creating all these method types given that they are uncached types. Maybe the name of the extractor could make it clearer that there's a conversion, e.g. if we call it
FromMethod
(and we call have a corresponding apply to go in the other direction).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Followup in #18443
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree it's good to make the conversion explicit in the name. The performance aspects are probably OK since the alternative is usually to take the list of AppliedType arguments and split it into inits and last, which is also expensive.