File tree 5 files changed +64
-2
lines changed
compiler/src/dotty/tools/dotc/typer
docs/docs/reference/metaprogramming
5 files changed +64
-2
lines changed Original file line number Diff line number Diff line change @@ -888,9 +888,12 @@ trait Checking {
888
888
// final vals can be marked inline even if they're not pure, see Typer#patchFinalVals
889
889
val purityLevel = if (sym.is(Final )) Idempotent else Pure
890
890
tpt.tpe.widenTermRefExpr.dealias match
891
- case tp : ConstantType if exprPurity(tree) >= purityLevel => // ok
891
+ case tp : ConstantType =>
892
+ if ! (exprPurity(tree) >= purityLevel) then
893
+ ctx.error(em " inline value must be pure " , tree.sourcePos)
892
894
case _ =>
893
- ctx.error(em " type of inline must be a known value " , tree.sourcePos)
895
+ val pos = if tpt.span.isZeroExtent then tree.sourcePos else tpt.sourcePos
896
+ ctx.error(em " inline value must have a literal constant type " , pos)
894
897
}
895
898
896
899
/** A hook to exclude selected symbols from double declaration check */
Original file line number Diff line number Diff line change @@ -228,6 +228,25 @@ constant expressions in the sense defined by the [SLS §
228
228
including _ platform-specific_ extensions such as constant folding of pure
229
229
numeric computations.
230
230
231
+ An inline value must have a literal type such as ` 1 ` or ` true ` .
232
+ ``` scala
233
+ inline val four = 4
234
+ // equivalent to
235
+ inline val four : 4 = 4
236
+ ```
237
+
238
+ It is also possible to have inline vals of types that do not have a syntax, such as ` Short(4) ` .
239
+
240
+ ``` scala
241
+ trait InlineConstants {
242
+ inline val myShort : Short
243
+ }
244
+
245
+ object Constants extends InlineConstants {
246
+ inline val myShort /* : Short(4)*/ = 4
247
+ }
248
+ ```
249
+
231
250
## Transparent Inline Methods
232
251
233
252
Inline methods can additionally be declared ` transparent ` .
Original file line number Diff line number Diff line change
1
+ -- Error: tests/neg/i8841.scala:2:20 -----------------------------------------------------------------------------------
2
+ 2 | inline val log1 : Boolean = false // error
3
+ | ^^^^^^^
4
+ | inline value must have a literal constant type
5
+ -- Error: tests/neg/i8841.scala:3:20 -----------------------------------------------------------------------------------
6
+ 3 | inline val log2 = true: Boolean // error
7
+ | ^^^^^^^^^^^^^
8
+ | inline value must have a literal constant type
9
+ -- Error: tests/neg/i8841.scala:4:28 -----------------------------------------------------------------------------------
10
+ 4 | inline val log3: false = { println(); false } // error
11
+ | ^^^^^^^^^^^^^^^^^^^^
12
+ | inline value must be pure
Original file line number Diff line number Diff line change
1
+ object Foo {
2
+ inline val log1 : Boolean = false // error
3
+ inline val log2 = true : Boolean // error
4
+ inline val log3 : false = { println(); false } // error
5
+ }
Original file line number Diff line number Diff line change
1
+ trait InlineConstants {
2
+ inline val myInlinedBoolean : Boolean
3
+ inline val myInlinedByte : Byte
4
+ inline val myInlinedShort : Short
5
+ inline val myInlinedInt : Int
6
+ inline val myInlinedLong : Long
7
+ inline val myInlinedFloat : Float
8
+ inline val myInlinedDouble : Double
9
+ inline val myInlinedChar : Char
10
+ inline val myInlinedString : String
11
+ }
12
+
13
+ object Constants extends InlineConstants {
14
+ inline val myInlinedBoolean = true
15
+ inline val myInlinedByte = 1
16
+ inline val myInlinedShort = 2
17
+ inline val myInlinedInt = 3
18
+ inline val myInlinedLong = 4
19
+ inline val myInlinedFloat = 5
20
+ inline val myInlinedDouble = 6
21
+ inline val myInlinedChar = 'a'
22
+ inline val myInlinedString = " abc"
23
+ }
You can’t perform that action at this time.
0 commit comments