Skip to content

Commit

Permalink
Let the poly functions validation be handled in Checking
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Oct 3, 2024
1 parent c83ff66 commit 60e957c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 22 deletions.
1 change: 0 additions & 1 deletion compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1221,7 +1221,6 @@ class Definitions {

/** Creates a refined `PolyFunction` with an `apply` method with the given info. */
def apply(mt: MethodOrPoly)(using Context): Type =
assert(isValidPolyFunctionInfo(mt), s"Not a valid PolyFunction refinement: $mt")
RefinedType(PolyFunctionClass.typeRef, nme.apply, mt)

/** Matches a refined `PolyFunction` type and extracts the apply info.
Expand Down
20 changes: 2 additions & 18 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1914,26 +1914,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
.showing(i"desugared fun $tree --> $desugared with pt = $pt", typr)
}

/** Check that the PolyFunction doesn't have by-name parameters.
* Return the unchanged tree if it's valid, or EmptyTree otherwise.
*/
private def checkPolyTypeTree(tree: untpd.Tree)(using Context): untpd.Tree =
val untpd.PolyFunction(tparams: List[untpd.TypeDef] @unchecked, fun @ untpd.Function(vparamTypes, res)) = tree: @unchecked
var tree1 = tree
vparamTypes.foreach:
case t: ByNameTypeTree =>
report.error("By-name parameters are not supported in Polymorphic Functions", t.srcPos)
tree1 = untpd.EmptyTree
case _ =>
tree1

def typedPolyFunction(tree: untpd.PolyFunction, pt: Type)(using Context): Tree =
val tree1 = desugar.normalizePolyFunction(tree)
checkPolyTypeTree(tree1) match
case tree2: untpd.PolyFunction =>
if (ctx.mode is Mode.Type) typed(desugar.makePolyFunctionType(tree2), pt)
else typedPolyFunctionValue(tree2, pt)
case untpd.EmptyTree => TypeTree(NoType)
if (ctx.mode is Mode.Type) typed(desugar.makePolyFunctionType(tree1), pt)
else typedPolyFunctionValue(tree1, pt)

def typedPolyFunctionValue(tree: untpd.PolyFunction, pt: Type)(using Context): Tree =
val untpd.PolyFunction(tparams: List[untpd.TypeDef] @unchecked, fun) = tree: @unchecked
Expand Down
6 changes: 3 additions & 3 deletions tests/neg/i21652.check
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- Error: tests/neg/i21652.scala:1:15 ----------------------------------------------------------------------------------
-- Error: tests/neg/i21652.scala:1:8 -----------------------------------------------------------------------------------
1 |def k: [A] => (=> A) => A = // error
| ^^^^
| By-name parameters are not supported in Polymorphic Functions
| ^^^^^^^^^^^^^^^^^
|Implementation restriction: PolyFunction apply must have exactly one parameter list and optionally type arguments. No by-name nor varags are allowed.

0 comments on commit 60e957c

Please sign in to comment.