Skip to content

Commit

Permalink
error when unrolling trait constructors
Browse files Browse the repository at this point in the history
Traits are forbidden in source code from having secondary constructors,
which is what the current transform would generate.

Trait parameters are encoded in concrete implementing classes as getter methods,
perhaps unroll could provide default implementations, but this is unexplored.
  • Loading branch information
bishabosha committed Oct 3, 2024
1 parent 809f12c commit 1f98384
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
if method.is(Deferred) then
report.error("Unrolled method must be final and concrete", method.srcPos)
res = false
if !(method.isConstructor || method.is(Final) || method.owner.is(ModuleClass)) then
val isCtor = method.isConstructor
if isCtor && method.owner.is(Trait) then
report.error("implementation restriction: Unrolled method cannot be a trait constructor", method.srcPos)
res = false
if !(isCtor || method.is(Final) || method.owner.is(ModuleClass)) then
report.error("Unrolled method must be final", method.srcPos)
res = false
res
Expand Down
4 changes: 4 additions & 0 deletions tests/neg/unroll-traitConstructor.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-- Error: tests/neg/unroll-traitConstructor.scala:5:12 -----------------------------------------------------------------
5 |trait Unroll(a: String, @unroll b: Boolean = true): // error
| ^
| implementation restriction: Unrolled method cannot be a trait constructor
8 changes: 8 additions & 0 deletions tests/neg/unroll-traitConstructor.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//> using options -experimental

import scala.annotation.unroll

trait Unroll(a: String, @unroll b: Boolean = true): // error
def show: String = a + b

class Bar(arg: String, bool: Boolean) extends Unroll(arg, bool)

0 comments on commit 1f98384

Please sign in to comment.