Skip to content

Commit 5543550

Browse files
committed
Address review comments
1 parent 60f7f69 commit 5543550

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class PruneErasedDefs extends MiniPhase with SymTransformer { thisTransform =>
4848
if sym.isEffectivelyErased && !tree.rhs.isEmpty then
4949
cpy.ValDef(tree)(rhs = trivialErasedTree(tree))
5050
else tree.rhs match
51-
case rhs: TypeApply
51+
case rhs: RefTree
5252
if rhs.symbol == defn.Compiletime_uninitialized
5353
&& sym.is(Mutable) && sym.owner.isClass =>
5454
cpy.ValDef(tree)(rhs = cpy.Ident(rhs)(nme.WILDCARD))

library/src/scala/compiletime/package.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package scala
2+
import annotation.compileTimeOnly
23

34
package object compiletime {
45

@@ -24,7 +25,8 @@ package object compiletime {
2425
* as part of the bulk initialization of the object it belongs to, which assigns zero
2526
* values such as `null`, `0`, `0.0`, `false` to all object fields.
2627
*/
27-
erased def uninitialized[T]: T = ???
28+
@compileTimeOnly("`uninitialized` can only be used as the right hand side of a mutable field definition")
29+
def uninitialized: Nothing = ???
2830

2931
/** The error method is used to produce user-defined compile errors during inline expansion.
3032
* If an inline expansion results in a call error(msgStr) the compiler produces an error message containing the given msgStr.

tests/neg/i11225.check

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
-- Error: tests/neg/i11225.scala:5:16 ----------------------------------------------------------------------------------
2+
5 | val x1: Int = uninitialized // error
3+
| ^^^^^^^^^^^^^
4+
| `uninitialized` can only be used as the right hand side of a mutable field definition
5+
-- Error: tests/neg/i11225.scala:6:28 ----------------------------------------------------------------------------------
6+
6 | var x2: Int = if ??? then uninitialized else uninitialized // error // error
7+
| ^^^^^^^^^^^^^
8+
| `uninitialized` can only be used as the right hand side of a mutable field definition
9+
-- Error: tests/neg/i11225.scala:6:47 ----------------------------------------------------------------------------------
10+
6 | var x2: Int = if ??? then uninitialized else uninitialized // error // error
11+
| ^^^^^^^^^^^^^
12+
| `uninitialized` can only be used as the right hand side of a mutable field definition
13+
-- Error: tests/neg/i11225.scala:7:29 ----------------------------------------------------------------------------------
14+
7 | var x3: Int = if true then uninitialized else 1 // error
15+
| ^^^^^^^^^^^^^
16+
| `uninitialized` can only be used as the right hand side of a mutable field definition
17+
-- Error: tests/neg/i11225.scala:9:28 ----------------------------------------------------------------------------------
18+
9 | var x5: () => Int = () => uninitialized // error
19+
| ^^^^^^^^^^^^^
20+
| `uninitialized` can only be used as the right hand side of a mutable field definition
21+
-- Error: tests/neg/i11225.scala:10:18 ---------------------------------------------------------------------------------
22+
10 | var x6: Int = { uninitialized } // error
23+
| ^^^^^^^^^^^^^
24+
| `uninitialized` can only be used as the right hand side of a mutable field definition
25+
-- Error: tests/neg/i11225.scala:13:22 ---------------------------------------------------------------------------------
26+
13 | var cached: Int = uninitialized // error
27+
| ^^^^^^^^^^^^^
28+
| `uninitialized` can only be used as the right hand side of a mutable field definition
29+
-- Error: tests/neg/i11225.scala:14:30 ---------------------------------------------------------------------------------
30+
14 | cached = if x then 1 else uninitialized // error
31+
| ^^^^^^^^^^^^^
32+
| `uninitialized` can only be used as the right hand side of a mutable field definition
33+
-- Error: tests/neg/i11225.scala:17:4 ----------------------------------------------------------------------------------
34+
17 | uninitialized // error
35+
| ^^^^^^^^^^^^^
36+
| `uninitialized` can only be used as the right hand side of a mutable field definition
37+
-- Error: tests/neg/i11225.scala:18:4 ----------------------------------------------------------------------------------
38+
18 | uninitialized // error
39+
| ^^^^^^^^^^^^^
40+
| `uninitialized` can only be used as the right hand side of a mutable field definition
41+
-- Error: tests/neg/i11225.scala:23:4 ----------------------------------------------------------------------------------
42+
23 | uninitialized // error
43+
| ^^^^^^^^^^^^^
44+
| `uninitialized` can only be used as the right hand side of a mutable field definition
45+
-- Error: tests/neg/i11225.scala:30:16 ---------------------------------------------------------------------------------
46+
30 | var x7: Int = uni // error
47+
| ^^^
48+
| `uninitialized` can only be used as the right hand side of a mutable field definition
49+
| This location contains code that was inlined from i11225.scala:25

tests/neg/i11225.scala

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,30 @@
11
import compiletime.uninitialized
22

3-
@main def Test(x: Boolean) =
4-
var cached: Int = uninitialized // error
5-
cached = if x then 1 else uninitialized // error
3+
class Test:
4+
5+
val x1: Int = uninitialized // error
6+
var x2: Int = if ??? then uninitialized else uninitialized // error // error
7+
var x3: Int = if true then uninitialized else 1 // error
8+
var x4: Int = if false then uninitialized else 1 // ok, since inlined away
9+
var x5: () => Int = () => uninitialized // error
10+
var x6: Int = { uninitialized } // error
11+
12+
def f(x: Boolean) =
13+
var cached: Int = uninitialized // error
14+
cached = if x then 1 else uninitialized // error
15+
16+
var c: Int =
17+
uninitialized // error
18+
uninitialized // error
19+
2
20+
21+
var d: Int =
22+
println("pseudo init")
23+
uninitialized // error
24+
25+
transparent inline def uni = uninitialized
26+
27+
inline def g(inline x: Int): Unit = ()
28+
def f2 = g(uninitialized) // this one is ok since `uninitialized` is inlined away
29+
30+
var x7: Int = uni // error

0 commit comments

Comments
 (0)