You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
| Jan 19th 2024 | Clarification about current @unchecked behavior |
18
+
| Jun 3rd 2025 | Rename `.runtimeCheck` to `.runtimeChecked`|
18
19
19
20
## Summary
20
21
21
22
We propose to replace the mechanism to silence warnings for "unchecked" patterns, in the cases where silencing the warning will still result in the pattern being checked at runtime.
22
23
23
-
Currently, a user can silence warnings that a scrutinee may not be matched by a pattern, by annotating the scrutinee with the `@unchecked` annotation. This SIP proposes to use a new annotation `@RuntimeCheck` to replace `@unchecked` for this purpose. For convenience, an extension method will be added to `Predef` that marks the receiver with the annotation (used as follows: `foo.runtimeCheck`). Functionally it behaves the same as the old annotation, but improves readability at the callsite.
24
+
Currently, a user can silence warnings that a scrutinee may not be matched by a pattern, by annotating the scrutinee with the `@unchecked` annotation. This SIP proposes to use a new annotation `@RuntimeCheck` to replace `@unchecked` for this purpose. For convenience, an extension method will be added to `Predef` that marks the receiver with the annotation (used as follows: `foo.runtimeChecked`). Functionally it behaves the same as the old annotation, but improves readability at the callsite.
24
25
25
26
## Motivation
26
27
@@ -92,15 +93,15 @@ In all usages where the compiler looks for `@unchecked` for this purpose, we ins
92
93
93
94
By placing the annotation in the `internal` package, we communicate that the user is not meant to directly use the annotation.
94
95
95
-
Instead, for convenience, we provide an extension method `Predef.runtimeCheck`, which can be applied to any expression.
96
+
Instead, for convenience, we provide an extension method `Predef.runtimeChecked`, which can be applied to any expression.
96
97
97
98
The new usage to assert that a pattern is checked at runtime then becomes as follows:
98
99
```scala
99
100
defxs:List[Any] =???
100
-
valy:: ys = xs.runtimeCheck
101
+
valy:: ys = xs.runtimeChecked
101
102
```
102
103
103
-
We also make `runtimeCheck` a transparent inline method. This ensures that the elaboration of the method defines its semantics. (i.e. `runtimeCheck` is not meaningful because it is immediately inlined at type-checking).
104
+
We also make `runtimeChecked` a transparent inline method. This ensures that the elaboration of the method defines its semantics. (i.e. `runtimeChecked` is not meaningful because it is immediately inlined at type-checking).
Previously this code would print `fake runtimeCheck`, however with the proposed change then recompiling this code will _succeed_ and no longer will print.
146
+
Previously this code would print `fake runtimeChecked`, however with the proposed change then recompiling this code will _succeed_ and no longer will print.
146
147
147
148
Potentially we could mitigate this if necessary with a migration warning when the new method is resolved (`@experimental` annotation would be a start)
148
149
149
150
150
-
In general however, the new `runtimeCheck` method will not change any previously linking method without causing an ambiguity compilation error.
151
+
In general however, the new `runtimeChecked` method will not change any previously linking method without causing an ambiguity compilation error.
151
152
152
153
### Other concerns
153
154
@@ -216,9 +217,9 @@ On line 2: warning: non-variable type argument Int in type pattern scala.collect
216
217
valres2:Int=1
217
218
```
218
219
219
-
####Aligning to Scala2.13 semantics withruntimeCheck
220
+
####Aligning to Scala2.13 semantics with`runtimeChecked`
220
221
221
-
with`xs.runtimeCheck` we should still produce an unchecked warning for `case is: ::[Int] =>`
222
+
with`xs.runtimeChecked` we should still produce an unchecked warning for `case is: ::[Int] =>`
222
223
```scala
223
224
scala> xs.runtimeChecked match {
224
225
|caseis: ::[Int] => is.head
@@ -239,15 +240,15 @@ scala> xs.runtimeChecked match {
239
240
| }
240
241
valres14:Int=1
241
242
```
242
-
This has a small extra migration cost because if the scrutinee changes from `(xs: @unchecked)` to `xs.runtimeCheck` now some individual cases might need to add `@unchecked` on typearguments to avoid creating new warnings - however this cost is offset by perhaps revealing unsafe patterns previously unaccounted for.
243
+
This has a small extra migration cost because if the scrutinee changes from `(xs: @unchecked)` to `xs.runtimeChecked` now some individual cases might need to add `@unchecked` on typearguments to avoid creating new warnings - however this cost is offset by perhaps revealing unsafe patterns previously unaccounted for.
243
244
244
245
Once again `@nowarn` can be used to fully restore any old behavior
245
246
246
247
##Alternatives
247
248
248
-
1) make `runtimeCheck` a method on `Any` that returns the receiver (not inline). The compiler would check for presence of a call to this method when deciding to perform static checking of pattern exhaustivity. This idea was criticised for being brittle with respect to refactoring, or automatic code transformations via macro.
249
+
1) make `runtimeChecked` a method on `Any` that returns the receiver (not inline). The compiler would check for presence of a call to this method when deciding to perform static checking of pattern exhaustivity. This idea was criticised for being brittle with respect to refactoring, or automatic code transformations via macro.
249
250
250
-
2) `runtimeCheck` should elaborate to code that matches the expected type, e.g. to heal `t: Any` to `Int` when the expected typeis `Int`. The problem is that this is not useful for patterns that can not be runtime checked by typealone. Also, it implies a greater change to the spec, because now `runtimeCheck` would have to be specially treated.
251
+
2) `runtimeChecked` should elaborate to code that matches the expected type, e.g. to heal `t: Any` to `Int` when the expected typeis `Int`. The problem is that this is not useful for patterns that can not be runtime checked by typealone. Also, it implies a greater change to the spec, because now `runtimeChecked` would have to be specially treated.
0 commit comments