Skip to content

Commit 56205ad

Browse files
committed
Handle by-name parameters properly
1 parent 15a2084 commit 56205ad

File tree

6 files changed

+46
-8
lines changed

6 files changed

+46
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ object Errors {
6060
case class AccessNonInit(field: Symbol, trace: Vector[Tree]) extends Error {
6161
def source: Tree = trace.last
6262
def show(implicit ctx: Context): String =
63-
"Access non-initialized field " + field.show + "."
63+
"Access non-initialized field " + field.name.show + "."
6464

6565
override def report(implicit ctx: Context): Unit = ctx.error(show + stacktrace, field.sourcePos)
6666
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ object Summarization {
7070
val summary = analyze(fun)
7171
val ignoredCall = env.ignoredMethods.contains(expr.symbol)
7272

73-
val res = args.foldLeft(summary) { (sum, arg) =>
73+
val argTps = fun.tpe.widen match
74+
case mt: MethodType => mt.paramInfos
75+
76+
val res = args.zip(argTps).foldLeft(summary) { case (sum, (arg, argTp)) =>
7477
val (pots1, effs1) = analyze(arg)
7578
if (ignoredCall) sum.withEffs(effs1)
79+
else if (argTp.isInstanceOf[ExprType]) sum + Promote(Fun(pots1, effs1)(arg))(arg)
7680
else sum.withEffs(pots1.promote(arg) ++ effs1)
7781
}
7882

tests/init/neg/lazylist1.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ class LazyList[A]
22

33
object LazyList {
44
inline implicit def toDeferred[A](l: LazyList[A]): Deferred[A] =
5-
new Deferred(l)
5+
new Deferred(l) // error
66

77
final class Deferred[A](l: => LazyList[A]) {
88
def #:: [B >: A](elem: => B): LazyList[B] = ???
@@ -16,5 +16,5 @@ final class Test {
1616
lazy val b: LazyList[Int] = 10 #:: a
1717

1818
val x: LazyList[Int] = 5 #:: y
19-
val y: LazyList[Int] = 10 #:: x // error
19+
val y: LazyList[Int] = 10 #:: x
2020
}

tests/init/neg/lazylist2.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ final class Test1 {
2626
a.head // ok
2727
b.head // ok
2828

29-
val x: LazyList[Int] = 5 #:: y
30-
val y: LazyList[Int] = 10 #:: x // error
29+
val x: LazyList[Int] = 5 #:: y // error
30+
val y: LazyList[Int] = 10 #:: x
3131
}
3232

3333
final class Test2 {
@@ -37,7 +37,7 @@ final class Test2 {
3737
}
3838

3939
final class Test3 {
40-
val a: LazyList[Int] = n #:: (a: @unchecked)
40+
val a: LazyList[Int] = n #:: (a: @unchecked) // error
4141
a.head
42-
val n: Int = 20 // error
42+
val n: Int = 20
4343
}

tests/init/neg/t3273.check

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
-- Error: tests/init/neg/t3273.scala:4:42 ------------------------------------------------------------------------------
2+
4 | val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error
3+
| ^^^^^^^^^^^^^^^
4+
| Promoting the value to fully-initialized is unsafe.
5+
| Calling trace:
6+
| -> val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error [ t3273.scala:4 ]
7+
|
8+
| The unsafe promotion may cause the following problem(s):
9+
|
10+
| 1. Access non-initialized field num1. Calling trace:
11+
| -> val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error [ t3273.scala:4 ]
12+
-- Error: tests/init/neg/t3273.scala:5:61 ------------------------------------------------------------------------------
13+
5 | val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
| Promoting the value to fully-initialized is unsafe.
16+
| Calling trace:
17+
| -> val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error [ t3273.scala:5 ]
18+
|
19+
| The unsafe promotion may cause the following problem(s):
20+
|
21+
| 1. Access non-initialized field num2. Calling trace:
22+
| -> val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error [ t3273.scala:5 ]

tests/init/neg/t3273.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.language.implicitConversions
2+
3+
object Test {
4+
val num1: LazyList[Int] = 1 #:: num1.map(_ + 1) // error
5+
val num2: LazyList[Int] = 1 #:: num2.iterator.map(_ + 1).to(LazyList) // error
6+
7+
def main(args: Array[String]): Unit = {
8+
val x1 = (num1 take 10).toList
9+
val x2 = (num2 take 10).toList
10+
assert(x1 == x2)
11+
}
12+
}

0 commit comments

Comments
 (0)