Skip to content

Commit 8bb62a3

Browse files
committed
Add test for secondary constructor
1 parent 04b5eb1 commit 8bb62a3

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

compiler/src/dotty/tools/dotc/transform/init/Semantic.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,13 +505,13 @@ class Semantic {
505505
*
506506
* This is a fast track for early promotion of values.
507507
*/
508-
def canPromoteExtrinsic: Contextual[Boolean] =
508+
def canPromoteExtrinsic: Contextual[Boolean] = log("canPromoteExtrinsic " + value + ", promoted = " + promoted, printer) {
509509
value match
510510
case Hot => true
511511
case Cold => false
512512

513513
case warm: Warm =>
514-
warm.outer.canPromoteExtrinsic && {
514+
(warm.outer :: warm.args).forall(_.canPromoteExtrinsic) && {
515515
promoted.add(warm)
516516
true
517517
}
@@ -537,10 +537,10 @@ class Semantic {
537537
case RefSet(refs) =>
538538
refs.forall(_.canPromoteExtrinsic)
539539

540-
end canPromoteExtrinsic
540+
}
541541

542542
/** Promotion of values to hot */
543-
def promote(msg: String, source: Tree): Contextual[List[Error]] =
543+
def promote(msg: String, source: Tree): Contextual[List[Error]] = log("promoting " + value + ", promoted = " + promoted, printer) {
544544
value match
545545
case Hot => Nil
546546

@@ -574,6 +574,7 @@ class Semantic {
574574

575575
case RefSet(refs) =>
576576
refs.flatMap(_.promote(msg, source))
577+
}
577578
end extension
578579

579580
extension (warm: Warm)
@@ -594,7 +595,7 @@ class Semantic {
594595
* system more flexible in other dimentions: e.g. leak to
595596
* methods or constructors, or use ownership for creating cold data structures.
596597
*/
597-
def tryPromote(msg: String, source: Tree): Contextual[List[Error]] = log("promote " + warm.show, printer) {
598+
def tryPromote(msg: String, source: Tree): Contextual[List[Error]] = log("promote " + warm.show + ", promoted = " + promoted, printer) {
598599
val classRef = warm.klass.appliedRef
599600
if classRef.memberClasses.nonEmpty then
600601
return PromoteError(msg, source, trace.toVector) :: Nil

tests/init/neg/secondary-ctor.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class A(b: B, x: Int) {
2+
def this(b: B) = {
3+
this(b, 5)
4+
println(b.n) // error
5+
}
6+
}
7+
8+
class B(val d: D) {
9+
val n: Int = 10
10+
}
11+
12+
class C(b: B) extends A(b) {
13+
def this(b: B, x: Int) = this(b)
14+
}
15+
16+
class D {
17+
val b = new B(this)
18+
val c = new C(b, 5)
19+
}

0 commit comments

Comments
 (0)