Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions compiler/src/dotty/tools/dotc/cc/SafeRefs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,35 @@ object SafeRefs {
rejectSafe("scala.annotation.unchecked.uncheckedStable")
rejectSafe("scala.annotation.unchecked.uncheckedVariance")
rejectSafe("scala.annotation.unchecked.uncheckedCaptures")
// Reject mutable classes in scala.runtime
rejectSafe("scala.runtime.BooleanRef")
rejectSafe("scala.runtime.ByteRef")
rejectSafe("scala.runtime.CharRef")
rejectSafe("scala.runtime.DoubleRef")
rejectSafe("scala.runtime.FloatRef")
rejectSafe("scala.runtime.IntRef")
rejectSafe("scala.runtime.LongRef")
rejectSafe("scala.runtime.ShortRef")
rejectSafe("scala.runtime.ObjectRef")
rejectSafe("scala.runtime.VolatileBooleanRef")
rejectSafe("scala.runtime.VolatileByteRef")
rejectSafe("scala.runtime.VolatileCharRef")
rejectSafe("scala.runtime.VolatileDoubleRef")
rejectSafe("scala.runtime.VolatileFloatRef")
rejectSafe("scala.runtime.VolatileIntRef")
rejectSafe("scala.runtime.VolatileLongRef")
rejectSafe("scala.runtime.VolatileShortRef")
rejectSafe("scala.runtime.VolatileObjectRef")
rejectSafe("scala.runtime.LazyRef")
rejectSafe("scala.runtime.LazyBoolean")
rejectSafe("scala.runtime.LazyByte")
rejectSafe("scala.runtime.LazyChar")
rejectSafe("scala.runtime.LazyShort")
rejectSafe("scala.runtime.LazyInt")
rejectSafe("scala.runtime.LazyLong")
rejectSafe("scala.runtime.LazyFloat")
rejectSafe("scala.runtime.LazyDouble")
rejectSafe("scala.runtime.LazyUnit")

private def fail(sym: Symbol, reason: String, pos: SrcPos)(using Context) =
report.error(em"Cannot refer to ${sym.sanitizedDescription}${sym.showExtendedLocation} from safe code since $reason", pos)
Expand Down
48 changes: 48 additions & 0 deletions tests/neg-custom-args/captures/safemode-refs.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:4:16 ------------------------------------------------------
4 | val ref = new scala.runtime.LongRef(0L) // error
| ^^^^^^^^^^^^^^^^^^^^^
| Cannot refer to class LongRef in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:6:8 -------------------------------------------------------
6 | ref.elem = 42 // error
| ^^^^^^^^
| Cannot refer to class LongRef in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:8:16 ------------------------------------------------------
8 | val ref = new scala.runtime.ObjectRef("") // error
| ^^^^^^^^^^^^^^^^^^^^^^^
| Cannot refer to class ObjectRef in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:10:8 ------------------------------------------------------
10 | ref.elem = "sk-123456" // error
| ^^^^^^^^
| Cannot refer to class ObjectRef in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:12:16 -----------------------------------------------------
12 | val ref = new scala.runtime.VolatileIntRef(0) // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Cannot refer to class VolatileIntRef in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:14:8 ------------------------------------------------------
14 | ref.elem = 42 // error
| ^^^^^^^^
| Cannot refer to class VolatileIntRef in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:16:16 -----------------------------------------------------
16 | val ref = new scala.runtime.VolatileObjectRef[String]("") // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Cannot refer to class VolatileObjectRef in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:18:8 ------------------------------------------------------
18 | ref.elem = "secret" // error
| ^^^^^^^^
| Cannot refer to class VolatileObjectRef in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:20:16 -----------------------------------------------------
20 | val ref = new scala.runtime.LazyInt // error
| ^^^^^^^^^^^^^^^^^^^^^
| Cannot refer to class LazyInt in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:22:8 ------------------------------------------------------
22 | ref.initialize(42) // error
| ^^^^^^^^^^^^^^
| Cannot refer to class LazyInt in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:24:16 -----------------------------------------------------
24 | val ref = new scala.runtime.LazyRef[String] // error
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| Cannot refer to class LazyRef in package scala.runtime from safe code since it is tagged @rejectSafe
-- Error: tests/neg-custom-args/captures/safemode-refs.scala:26:8 ------------------------------------------------------
26 | ref.initialize("hello") // error
| ^^^^^^^^^^^^^^
| Cannot refer to class LazyRef in package scala.runtime from safe code since it is tagged @rejectSafe
26 changes: 26 additions & 0 deletions tests/neg-custom-args/captures/safemode-refs.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import language.experimental.safe
def assertPure(op: () -> Unit): Unit = ()
def test1(): Unit =
val ref = new scala.runtime.LongRef(0L) // error
assertPure: () =>
ref.elem = 42 // error
def test2(): Unit =
val ref = new scala.runtime.ObjectRef("") // error
assertPure: () =>
ref.elem = "sk-123456" // error
def test3(): Unit =
val ref = new scala.runtime.VolatileIntRef(0) // error
assertPure: () =>
ref.elem = 42 // error
def test4(): Unit =
val ref = new scala.runtime.VolatileObjectRef[String]("") // error
assertPure: () =>
ref.elem = "secret" // error
def test5(): Unit =
val ref = new scala.runtime.LazyInt // error
assertPure: () =>
ref.initialize(42) // error
def test6(): Unit =
val ref = new scala.runtime.LazyRef[String] // error
assertPure: () =>
ref.initialize("hello") // error
Loading