Skip to content

Commit 1fc771a

Browse files
committed
Changing error message in UnsafePromotion
Updating the error message of UnsafePromotion
1 parent ffa0a48 commit 1fc771a

File tree

7 files changed

+33
-46
lines changed

7 files changed

+33
-46
lines changed

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ object Errors {
4848
}
4949
sb.toString
5050
}
51-
51+
5252
/** Flatten UnsafePromotion errors
53-
*/
53+
*/
5454
def flatten: Errors = this match {
5555
case unsafe: UnsafePromotion => unsafe.errors.flatMap(_.flatten)
5656
case _ => this :: Nil
@@ -90,19 +90,15 @@ object Errors {
9090

9191
/** Promote a value under initialization to fully-initialized */
9292
case class UnsafePromotion(msg: String, source: Tree, trace: Seq[Tree], errors: Errors) extends Error {
93-
assert(errors.nonEmpty)
9493

9594
override def issue(using Context): Unit =
9695
report.warning(show, source.srcPos)
9796

9897
def show(using Context): String = {
9998
var index = 0
100-
"Promoting the value to fully-initialized is unsafe. " + msg + "\n" + stacktrace +
101-
"\nThe unsafe promotion may cause the following problem(s):\n" +
102-
(errors.flatMap(_.flatten).map { error =>
103-
index += 1
104-
s"\n$index. " + error.show + error.stacktrace
105-
}.mkString)
99+
"Cannot prove that the value is fully-initialized. " + msg + "\n" + stacktrace +
100+
"\nThe unsafe promotion may cause the following problem:\n" +
101+
errors.head.show + errors.head.stacktrace
106102
}
107103
}
108104
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ class Semantic {
574574
val res = withEnv(env) { eval(body, thisV, klass) }
575575
val errors2 = res.value.promote(msg, source)
576576
if (res.errors.nonEmpty || errors2.nonEmpty)
577-
UnsafePromotion(msg, source, trace.toVector, (res.errors ++ errors2).head) :: Nil
577+
UnsafePromotion(msg, source, trace.toVector, res.errors ++ errors2) :: Nil
578578
else
579579
promoted.add(fun)
580580
Nil
@@ -611,30 +611,28 @@ class Semantic {
611611

612612
warm.klass.baseClasses.exists { klass =>
613613
klass.hasSource && klass.info.decls.exists { member =>
614-
if member.isOneOf(Flags.Method | Flags.Lazy | Flags.Deferred) then {
615-
if !member.isConstructor && member.hasSource then
614+
if !member.isType && !member.isConstructor && member.hasSource && !member.is(Flags.Deferred) then
615+
if member.isOneOf(Flags.Method | Flags.Lazy) then
616616
val trace2 = trace.add(member.defTree)
617617
locally {
618618
given Trace = trace2
619619
val args = member.info.paramInfoss.flatten.map(_ => ArgInfo(Hot, EmptyTree))
620620
val res = warm.call(member, args, superType = NoType, source = source)
621621
buffer ++= res.ensureHot(msg, source).errors
622622
}
623-
} else if !member.isType then {
624-
if !member.isOneOf(Flags.Deferred | Flags.Private | Flags.Protected) && member.hasSource then
623+
else
625624
val trace2 = trace.add(member.defTree)
626625
val res = warm.select(member, source)
627626
locally {
628627
given Trace = trace2
629628
buffer ++= res.ensureHot(msg, source).errors
630629
}
631-
}
632630
buffer.nonEmpty
633631
}
634632
}
635633

636634
if buffer.isEmpty then Nil
637-
else UnsafePromotion(msg, source, trace.toVector, buffer(0)) :: Nil
635+
else UnsafePromotion(msg, source, trace.toVector, buffer.toList) :: Nil
638636
}
639637

640638
end extension

tests/init/neg/closureLeak.check

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
-- Error: tests/init/neg/closureLeak.scala:11:14 -----------------------------------------------------------------------
22
11 | l.foreach(a => a.addX(this)) // error
33
| ^^^^^^^^^^^^^^^^^
4-
| Promoting the value to fully-initialized is unsafe. May only use initialized value as arguments
4+
| Cannot prove that the value is fully-initialized. May only use initialized value as arguments
55
|
6-
| The unsafe promotion may cause the following problem(s):
7-
|
8-
| 1. Promote the value under initialization to fully-initialized. May only use initialized value as arguments.
6+
| The unsafe promotion may cause the following problem:
7+
| Promote the value under initialization to fully-initialized. May only use initialized value as arguments.

tests/init/neg/enum-desugared.check

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
-- Error: tests/init/neg/enum-desugared.scala:17:15 --------------------------------------------------------------------
22
17 | Array(this.LazyErrorId, this.NoExplanationID) // error // error
33
| ^^^^^^^^^^^^^^^^
4-
| Promoting the value to fully-initialized is unsafe. May only use initialized value as method arguments
4+
| Cannot prove that the value is fully-initialized. May only use initialized value as method arguments
55
|
6-
| The unsafe promotion may cause the following problem(s):
7-
|
8-
| 1. Calling the external method method name may cause initialization errors. Calling trace:
6+
| The unsafe promotion may cause the following problem:
7+
| Calling the external method method name may cause initialization errors. Calling trace:
98
| -> override def productPrefix: String = this.name() [ enum-desugared.scala:29 ]
109
| -> Array(this.LazyErrorId, this.NoExplanationID) // error // error [ enum-desugared.scala:17 ]
1110
-- Error: tests/init/neg/enum-desugared.scala:17:33 --------------------------------------------------------------------
1211
17 | Array(this.LazyErrorId, this.NoExplanationID) // error // error
1312
| ^^^^^^^^^^^^^^^^^^^^
14-
| Promoting the value to fully-initialized is unsafe. May only use initialized value as method arguments
15-
|
16-
| The unsafe promotion may cause the following problem(s):
13+
| Cannot prove that the value is fully-initialized. May only use initialized value as method arguments
1714
|
18-
| 1. Calling the external method method ordinal may cause initialization errors. Calling trace:
19-
| -> def errorNumber: Int = this.ordinal() - 2 [ enum-desugared.scala:8 ]
20-
| -> Array(this.LazyErrorId, this.NoExplanationID) // error // error [ enum-desugared.scala:17 ]
15+
| The unsafe promotion may cause the following problem:
16+
| Calling the external method method ordinal may cause initialization errors. Calling trace:
17+
| -> def errorNumber: Int = this.ordinal() - 2 [ enum-desugared.scala:8 ]
18+
| -> Array(this.LazyErrorId, this.NoExplanationID) // error // error [ enum-desugared.scala:17 ]

tests/init/neg/enum.check

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
-- Error: tests/init/neg/enum.scala:4:8 --------------------------------------------------------------------------------
22
4 | NoExplanationID // error
33
| ^
4-
| Promoting the value to fully-initialized is unsafe. May only use initialized value as method arguments
4+
| Cannot prove that the value is fully-initialized. May only use initialized value as method arguments
55
|
6-
| The unsafe promotion may cause the following problem(s):
7-
|
8-
| 1. Calling the external method method name may cause initialization errors. Calling trace:
6+
| The unsafe promotion may cause the following problem:
7+
| Calling the external method method name may cause initialization errors. Calling trace:
98
| -> NoExplanationID // error [ enum.scala:4 ]

tests/init/neg/promotion-loop.check

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
-- Error: tests/init/neg/promotion-loop.scala:16:10 --------------------------------------------------------------------
22
16 | println(b) // error
33
| ^
4-
|Promoting the value to fully-initialized is unsafe. May only use initialized value as arguments
4+
|Cannot prove that the value is fully-initialized. May only use initialized value as arguments
55
|
6-
|The unsafe promotion may cause the following problem(s):
7-
|
8-
|1. Promote the value under initialization to fully-initialized. May only use initialized value as arguments. Calling trace:
6+
|The unsafe promotion may cause the following problem:
7+
|Promote the value under initialization to fully-initialized. May only use initialized value as arguments. Calling trace:
98
| -> val outer = test [ promotion-loop.scala:12 ]

tests/init/neg/t3273.check

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
-- Error: tests/init/neg/t3273.scala:4:42 ------------------------------------------------------------------------------
22
4 | val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error
33
| ^^^^^^^^^^^^^^^
4-
| Promoting the value to fully-initialized is unsafe. May only use initialized value as arguments
4+
| Cannot prove that the value is fully-initialized. May only use initialized value as arguments
55
|
6-
| The unsafe promotion may cause the following problem(s):
7-
|
8-
| 1. Access non-initialized value num1. Calling trace:
6+
| The unsafe promotion may cause the following problem:
7+
| Access non-initialized value num1. Calling trace:
98
| -> val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error [ t3273.scala:4 ]
109
-- Error: tests/init/neg/t3273.scala:5:61 ------------------------------------------------------------------------------
1110
5 | val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error
1211
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
13-
| Promoting the value to fully-initialized is unsafe. May only use initialized value as arguments
14-
|
15-
| The unsafe promotion may cause the following problem(s):
12+
| Cannot prove that the value is fully-initialized. May only use initialized value as arguments
1613
|
17-
| 1. Access non-initialized value num2. Calling trace:
14+
| The unsafe promotion may cause the following problem:
15+
| Access non-initialized value num2. Calling trace:
1816
| -> val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error [ t3273.scala:5 ]

0 commit comments

Comments
 (0)