Skip to content

Commit 92bac25

Browse files
committed
fix: Check if a PolyFunction TypeTree has no ByName parameters
1 parent 5ec1e8b commit 92bac25

File tree

5 files changed

+27
-7
lines changed

5 files changed

+27
-7
lines changed

compiler/src/dotty/tools/dotc/reporting/messages.scala

+2-3
Original file line numberDiff line numberDiff line change
@@ -1829,12 +1829,11 @@ class NotAPath(tp: Type, usage: String)(using Context) extends TypeMsg(NotAPathI
18291829
if sym.isAllOf(Flags.InlineParam) then
18301830
i"""
18311831
|Inline parameters are not considered immutable paths and cannot be used as
1832-
|singleton types.
1833-
|
1832+
|singleton types.
1833+
|
18341834
|Hint: Removing the `inline` qualifier from the `${sym.name}` parameter
18351835
|may help resolve this issue."""
18361836
else ""
1837-
18381837

18391838
class WrongNumberOfParameters(tree: untpd.Tree, foundCount: Int, pt: Type, expectedCount: Int)(using Context)
18401839
extends SyntaxMsg(WrongNumberOfParametersID) {

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -1914,11 +1914,26 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
19141914
.showing(i"desugared fun $tree --> $desugared with pt = $pt", typr)
19151915
}
19161916

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

19181930
def typedPolyFunction(tree: untpd.PolyFunction, pt: Type)(using Context): Tree =
19191931
val tree1 = desugar.normalizePolyFunction(tree)
1920-
if (ctx.mode is Mode.Type) typed(desugar.makePolyFunctionType(tree1), pt)
1921-
else typedPolyFunctionValue(tree1, pt)
1932+
checkPolyTypeTree(tree1) match
1933+
case tree2: untpd.PolyFunction =>
1934+
if (ctx.mode is Mode.Type) typed(desugar.makePolyFunctionType(tree2), pt)
1935+
else typedPolyFunctionValue(tree2, pt)
1936+
case untpd.EmptyTree => TypeTree(NoType)
19221937

19231938
def typedPolyFunctionValue(tree: untpd.PolyFunction, pt: Type)(using Context): Tree =
19241939
val untpd.PolyFunction(tparams: List[untpd.TypeDef] @unchecked, fun) = tree: @unchecked

tests/neg/21538.check

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
| ^^^^^^^^^^
44
| (value : V) is not a valid singleton type, since it is not an immutable path
55
| Inline parameters are not considered immutable paths and cannot be used as
6-
| singleton types.
7-
|
6+
| singleton types.
7+
|
88
| Hint: Removing the `inline` qualifier from the `value` parameter
99
| may help resolve this issue.
1010
|

tests/neg/i21652.check

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/i21652.scala:1:15 ----------------------------------------------------------------------------------
2+
1 |def k: [A] => (=> A) => A = // error
3+
| ^^^^
4+
| By-name parameters are not supported in Polymorphic Functions

tests/neg/i21652.scala

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def k: [A] => (=> A) => A = // error
2+
[A] => a => a

0 commit comments

Comments
 (0)