forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disallow extending PolyFunction in user code (scala#18920)
The `PolyFunction` trait should only be used for compiler generated encoded lambdas. Any other use case that was allowed before was accidental. In the future, we might consider supporting these if there is a good use case. This would probably require a SIP. Fixes scala#10075
- Loading branch information
Showing
20 changed files
with
99 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
-- Error: tests/neg/i10075.scala:8:24 ---------------------------------------------------------------------------------- | ||
8 |trait PolyTrait extends PolyFunction // error | ||
| ^^^^^^^^^^^^ | ||
| `PolyFunction` marker trait is reserved for compiler generated refinements | ||
-- Error: tests/neg/i10075.scala:10:24 --------------------------------------------------------------------------------- | ||
10 |class PolyClass extends PolyTrait { // error | ||
| ^^^^^^^^^ | ||
| `PolyFunction` marker trait is reserved for compiler generated refinements | ||
-- Error: tests/neg/i10075.scala:14:26 --------------------------------------------------------------------------------- | ||
14 |object PolyObject extends PolyFunction // error | ||
| ^^^^^^^^^^^^ | ||
| `PolyFunction` marker trait is reserved for compiler generated refinements | ||
-- Error: tests/neg/i10075.scala:2:14 ---------------------------------------------------------------------------------- | ||
2 |val foo = new PolyFunction { } // error | ||
| ^^^^^^^^^^^^ | ||
| `PolyFunction` marker trait is reserved for compiler generated refinements | ||
-- Error: tests/neg/i10075.scala:3:14 ---------------------------------------------------------------------------------- | ||
3 |val bar = new PolyFunction { def bar = 23 } // error | ||
| ^^^^^^^^^^^^ | ||
| `PolyFunction` marker trait is reserved for compiler generated refinements | ||
-- Error: tests/neg/i10075.scala:4:14 ---------------------------------------------------------------------------------- | ||
4 |val baz = new PolyFunction { def apply = 23 } // error | ||
| ^^^^^^^^^^^^ | ||
| `PolyFunction` marker trait is reserved for compiler generated refinements | ||
-- Error: tests/neg/i10075.scala:5:14 ---------------------------------------------------------------------------------- | ||
5 |val qux = new PolyFunction { def apply[T] = 47 } // error | ||
| ^^^^^^^^^^^^ | ||
| `PolyFunction` marker trait is reserved for compiler generated refinements | ||
-- Error: tests/neg/i10075.scala:6:15 ---------------------------------------------------------------------------------- | ||
6 |val quxx = new PolyFunction { def apply[T](x: T): T = x } // error | ||
| ^^^^^^^^^^^^ | ||
| `PolyFunction` marker trait is reserved for compiler generated refinements |
This file contains 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
val poly = [T] => (x: T) => x | ||
val foo = new PolyFunction { } // error | ||
val bar = new PolyFunction { def bar = 23 } // error | ||
val baz = new PolyFunction { def apply = 23 } // error | ||
val qux = new PolyFunction { def apply[T] = 47 } // error | ||
val quxx = new PolyFunction { def apply[T](x: T): T = x } // error | ||
|
||
trait PolyTrait extends PolyFunction // error | ||
|
||
class PolyClass extends PolyTrait { // error | ||
def apply[T](x: T): T = x | ||
} | ||
|
||
object PolyObject extends PolyFunction // error |
This file contains 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 |
---|---|---|
@@ -0,0 +1,10 @@ | ||
type Upgrade[T] = T match | ||
case Int => Double | ||
case Char => String | ||
case Boolean => Boolean | ||
|
||
val upgrade: [t] => t => Upgrade[t] = new PolyFunction: // error | ||
def apply[T](x: T): Upgrade[T] = x match | ||
case x: Int => x.toDouble | ||
case x: Char => x.toString | ||
case x: Boolean => !x |
This file contains 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
def test = polyFun(1) | ||
|
||
def polyFun: PolyFunction { def apply(x: Int): Int } = | ||
new PolyFunction { def apply(x: Int): Int = x + 1 } | ||
new PolyFunction { def apply(x: Int): Int = x + 1 } // error |
This file contains 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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
def test = polyFun(1)(2) | ||
|
||
def polyFun: PolyFunction { def apply(x: Int)(y: Int): Int } = // error | ||
new PolyFunction: | ||
new PolyFunction: // error | ||
def apply(x: Int)(y: Int): Int = x + y |
This file contains 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 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 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 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 |
---|---|---|
@@ -1,2 +1,2 @@ | ||
def polyFun: PolyFunction { def apply: Int } = // error | ||
new PolyFunction { def apply: Int = 1 } | ||
new PolyFunction { def apply: Int = 1 } // error |
This file contains 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
def polyFun: PolyFunction { } = // error | ||
new PolyFunction { } | ||
new PolyFunction { } // error | ||
|
||
def polyFun(f: PolyFunction { }) = () // error |
This file contains 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 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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
def polyFun: PolyFunction = // error | ||
new PolyFunction { } | ||
new PolyFunction { } // error | ||
|
||
def polyFun2(a: PolyFunction) = () // error | ||
|
||
val polyFun3: PolyFunction = // error | ||
new PolyFunction { } | ||
new PolyFunction { } // error |
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
def polyFunByName: PolyFunction { def apply(thunk: => Int): Int } = // error | ||
new PolyFunction { def apply(thunk: => Int): Int = 1 } | ||
new PolyFunction { def apply(thunk: => Int): Int = 1 } // error | ||
|
||
def polyFunVarArgs: PolyFunction { def apply(args: Int*): Int } = // error | ||
new PolyFunction { def apply(thunk: Int*): Int = 1 } | ||
new PolyFunction { def apply(thunk: Int*): Int = 1 } // error |
This file contains 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 was deleted.
Oops, something went wrong.