Skip to content

Commit ebd16a1

Browse files
committed
Make type parameters after extension method def illegal
We'd have to at least check that there's no use before definition. Forbid for now, and come back once we support multiple type parameter clauses.
1 parent 80bebc9 commit ebd16a1

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -910,9 +910,8 @@ object desugar {
910910
/** Transform extension construct to list of extension methods */
911911
def extMethods(ext: ExtMethods)(using Context): Tree = flatTree {
912912
for mdef <- ext.methods yield
913-
if ext.tparams.nonEmpty && mdef.tparams.nonEmpty then
914-
ctx.error(em"extension method cannot have type parameters since some were already given in extension clause",
915-
mdef.tparams.head.sourcePos)
913+
if mdef.tparams.nonEmpty then
914+
ctx.error("no type parameters allowed here", mdef.tparams.head.sourcePos)
916915
defDef(
917916
cpy.DefDef(mdef)(
918917
name = mdef.name.toExtensionName,

docs/docs/reference/contextual/extension-methods.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ Extensions can also take using clauses. For instance, the `+` extension above co
8585
def - (y: T): T = n.minus(x, y)
8686
```
8787

88-
**Note**: If an extension defines type parameters in its prefix, the extension method itself is not allowed to have additional type parameters. This restriction might be lifted in the future once we support multiple type parameter clauses in a method.
88+
**Note**: Type parameters have to be given after the `extension` keyword;
89+
they cannot be given after the `def`. This restriction might be lifted in the future once we support multiple type parameter clauses in a method.
8990

9091
### Collective Extensions
9192

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
def (self: T).foo[T] = ??? // error
2-
def [T1](self: T1) bar[T2] = ??? // error // error
1+
extension (self: T) def foo[T] = ??? // error
2+
extension [T1](self: T1) def bar[T2] = ??? // error

0 commit comments

Comments
 (0)