Skip to content

Commit b71f9b9

Browse files
committed
error when unrolling trait constructors
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.
1 parent 850b256 commit b71f9b9

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,11 @@ class PostTyper extends MacroTransform with InfoTransformer { thisPhase =>
137137
if method.is(Deferred) then
138138
report.error("Unrolled method must be final and concrete", method.srcPos)
139139
res = false
140-
if !(method.isConstructor || method.is(Final) || method.owner.is(ModuleClass)) then
140+
val isCtor = method.isConstructor
141+
if isCtor && method.owner.is(Trait) then
142+
report.error("implementation restriction: Unrolled method cannot be a trait constructor", method.srcPos)
143+
res = false
144+
if !(isCtor || method.is(Final) || method.owner.is(ModuleClass)) then
141145
report.error("Unrolled method must be final", method.srcPos)
142146
res = false
143147
res
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/unroll-traitConstructor.scala:5:12 -----------------------------------------------------------------
2+
5 |trait Unroll(a: String, @unroll b: Boolean = true): // error
3+
| ^
4+
| implementation restriction: Unrolled method cannot be a trait constructor
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//> using options -experimental
2+
3+
import scala.annotation.unroll
4+
5+
trait Unroll(a: String, @unroll b: Boolean = true): // error
6+
def show: String = a + b
7+
8+
class Bar(arg: String, bool: Boolean) extends Unroll(arg, bool)

0 commit comments

Comments
 (0)