Skip to content

Commit

Permalink
fix: Check if a PolyFunction TypeTree has no ByName parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperFKorban committed Sep 30, 2024
1 parent 5ec1e8b commit 92bac25
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/reporting/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1829,12 +1829,11 @@ class NotAPath(tp: Type, usage: String)(using Context) extends TypeMsg(NotAPathI
if sym.isAllOf(Flags.InlineParam) then
i"""
|Inline parameters are not considered immutable paths and cannot be used as
|singleton types.
|
|singleton types.
|
|Hint: Removing the `inline` qualifier from the `${sym.name}` parameter
|may help resolve this issue."""
else ""


class WrongNumberOfParameters(tree: untpd.Tree, foundCount: Int, pt: Type, expectedCount: Int)(using Context)
extends SyntaxMsg(WrongNumberOfParametersID) {
Expand Down
19 changes: 17 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1914,11 +1914,26 @@ 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)
if (ctx.mode is Mode.Type) typed(desugar.makePolyFunctionType(tree1), pt)
else typedPolyFunctionValue(tree1, pt)
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)

def typedPolyFunctionValue(tree: untpd.PolyFunction, pt: Type)(using Context): Tree =
val untpd.PolyFunction(tparams: List[untpd.TypeDef] @unchecked, fun) = tree: @unchecked
Expand Down
4 changes: 2 additions & 2 deletions tests/neg/21538.check
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
| ^^^^^^^^^^
| (value : V) is not a valid singleton type, since it is not an immutable path
| Inline parameters are not considered immutable paths and cannot be used as
| singleton types.
|
| singleton types.
|
| Hint: Removing the `inline` qualifier from the `value` parameter
| may help resolve this issue.
|
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/i21652.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/i21652.scala:1:15 ----------------------------------------------------------------------------------
1 |def k: [A] => (=> A) => A = // error
| ^^^^
| By-name parameters are not supported in Polymorphic Functions
2 changes: 2 additions & 0 deletions tests/neg/i21652.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def k: [A] => (=> A) => A = // error
[A] => a => a

0 comments on commit 92bac25

Please sign in to comment.