|
| 1 | +package scala |
| 2 | +package compiletime |
| 3 | + |
| 4 | +import annotation.compileTimeOnly |
| 5 | + |
| 6 | +/** Use this method when you have a type, do not have a value for it but want to |
| 7 | + * pattern match on it. For example, given a type `Tup <: Tuple`, one can |
| 8 | + * pattern-match on it as follows: |
| 9 | + * ``` |
| 10 | + * inline erasedValue[Tup] match { |
| 11 | + * case _: EmptyTuple => ... |
| 12 | + * case _: h *: t => ... |
| 13 | + * } |
| 14 | + * ``` |
| 15 | + * This value can only be used in an inline match and the value cannot be used in |
| 16 | + * the branches. |
| 17 | + */ |
| 18 | +erased def erasedValue[T]: T = ??? |
| 19 | + |
| 20 | +/** Used as the initializer of a mutable class or object field, like this: |
| 21 | + * |
| 22 | + * var x: T = uninitialized |
| 23 | + * |
| 24 | + * This signifies that the field is not initialized on its own. It is still initialized |
| 25 | + * as part of the bulk initialization of the object it belongs to, which assigns zero |
| 26 | + * values such as `null`, `0`, `0.0`, `false` to all object fields. |
| 27 | + */ |
| 28 | +@compileTimeOnly("`uninitialized` can only be used as the right hand side of a mutable field definition") |
| 29 | +def uninitialized: Nothing = ??? |
| 30 | + |
| 31 | +/** The error method is used to produce user-defined compile errors during inline expansion. |
| 32 | + * If an inline expansion results in a call error(msgStr) the compiler produces an error message containing the given msgStr. |
| 33 | + * |
| 34 | + * ```scala |
| 35 | + * error("My error message") |
| 36 | + * ``` |
| 37 | + * or |
| 38 | + * ```scala |
| 39 | + * inline def errorOnThisCode(inline x: Any) = |
| 40 | + * error("My error of this code: " + codeOf(x)) |
| 41 | + * ``` |
| 42 | + */ |
| 43 | +inline def error(inline msg: String): Nothing = ??? |
| 44 | + |
| 45 | +/** Returns the string representation of argument code: |
| 46 | + * |
| 47 | + * ```scala |
| 48 | + * inline def logged(inline p1: Any) = |
| 49 | + * ("code: " + codeOf(p1), p1) |
| 50 | + * |
| 51 | + * logged(identity("foo")) |
| 52 | + * // above is equivalent to: |
| 53 | + * // ("code: scala.Predef.identity("foo")", identity("foo")) |
| 54 | + * ``` |
| 55 | + * |
| 56 | + * The formatting of the code is not stable across version of the compiler. |
| 57 | + * |
| 58 | + * @note only `inline` arguments will be displayed as "code". |
| 59 | + * Other values may display unintutively. |
| 60 | + */ |
| 61 | +transparent inline def codeOf(arg: Any): String = |
| 62 | + // implemented in dotty.tools.dotc.typer.Inliner.Intrinsics |
| 63 | + error("Compiler bug: `codeOf` was not evaluated by the compiler") |
| 64 | + |
| 65 | +/** Checks at compiletime that the provided values is a constant after |
| 66 | + * inlining and constant folding. |
| 67 | + * |
| 68 | + * Usage: |
| 69 | + * ```scala |
| 70 | + * inline def twice(inline n: Int): Int = |
| 71 | + * requireConst(n) // compile-time assertion that the parameter `n` is a constant |
| 72 | + * n + n |
| 73 | + * |
| 74 | + * twice(1) |
| 75 | + * val m: Int = ... |
| 76 | + * twice(m) // error: expected a constant value but found: m |
| 77 | + * ``` |
| 78 | + */ |
| 79 | +inline def requireConst(inline x: Boolean | Byte | Short | Int | Long | Float | Double | Char | String): Unit = |
| 80 | + // implemented in dotty.tools.dotc.typer.Inliner |
| 81 | + error("Compiler bug: `requireConst` was not evaluated by the compiler") |
| 82 | + |
| 83 | +/** Same as `constValue` but returns a `None` if a constant value |
| 84 | + * cannot be constructed from the provided type. Otherwise returns |
| 85 | + * that value wrapped in `Some`. |
| 86 | + */ |
| 87 | +transparent inline def constValueOpt[T]: Option[T] = |
| 88 | + // implemented in dotty.tools.dotc.typer.Inliner |
| 89 | + error("Compiler bug: `constValueOpt` was not evaluated by the compiler") |
| 90 | + |
| 91 | +/** Given a constant, singleton type `T`, convert it to a value |
| 92 | + * of the same singleton type. For example: `assert(constValue[1] == 1)`. |
| 93 | + */ |
| 94 | +transparent inline def constValue[T]: T = |
| 95 | + // implemented in dotty.tools.dotc.typer.Inliner |
| 96 | + error("Compiler bug: `constValue` was not evaluated by the compiler") |
| 97 | + |
| 98 | +/** |
| 99 | + * Use this type to widen a self-type to a tuple. E.g. |
| 100 | + * ``` |
| 101 | + * val x: (1, 3) = (1, 3) |
| 102 | + * val y: Widen[x.type] = x |
| 103 | + * ``` |
| 104 | + */ |
| 105 | +type Widen[Tup <: Tuple] <: Tuple = Tup match { |
| 106 | + case EmptyTuple => EmptyTuple |
| 107 | + case h *: t => h *: t |
| 108 | +} |
| 109 | + |
| 110 | +/** Given a tuple type `(X1, ..., Xn)`, returns a tuple value |
| 111 | + * `(constValue[X1], ..., constValue[Xn])`. |
| 112 | + */ |
| 113 | +inline def constValueTuple[T <: Tuple]: Widen[T]= |
| 114 | + val res = |
| 115 | + inline erasedValue[T] match |
| 116 | + case _: EmptyTuple => EmptyTuple |
| 117 | + case _: (t *: ts) => constValue[t] *: constValueTuple[ts] |
| 118 | + end match |
| 119 | + res.asInstanceOf[Widen[T]] |
| 120 | +end constValueTuple |
| 121 | + |
| 122 | +/** Summons first given matching one of the listed cases. E.g. in |
| 123 | + * |
| 124 | + * given B { ... } |
| 125 | + * |
| 126 | + * summonFrom { |
| 127 | + * case given A => 1 |
| 128 | + * case given B => 2 |
| 129 | + * case given C => 3 |
| 130 | + * case _ => 4 |
| 131 | + * } |
| 132 | + * |
| 133 | + * the returned value would be `2`. |
| 134 | + */ |
| 135 | +transparent inline def summonFrom[T](f: Nothing => T): T = |
| 136 | + error("Compiler bug: `summonFrom` was not evaluated by the compiler") |
| 137 | + |
| 138 | +/** Summon a given value of type `T`. Usually, the argument is not passed explicitly. |
| 139 | + * The summoning is delayed until the call has been fully inlined. |
| 140 | + * |
| 141 | + * @tparam T the type of the value to be summoned |
| 142 | + * @return the given value typed as the provided type parameter |
| 143 | + */ |
| 144 | +transparent inline def summonInline[T]: T = summonFrom { |
| 145 | + case t: T => t |
| 146 | +} |
| 147 | + |
| 148 | +/** Given a tuple T, summons each of its member types and returns them in |
| 149 | + * a Tuple. |
| 150 | + * |
| 151 | + * @tparam T the tuple containing the types of the values to be summoned |
| 152 | + * @return the given values typed as elements of the tuple |
| 153 | + */ |
| 154 | +inline def summonAll[T <: Tuple]: Widen[T] = |
| 155 | + val res = |
| 156 | + inline erasedValue[T] match |
| 157 | + case _: EmptyTuple => EmptyTuple |
| 158 | + case _: (t *: ts) => summonInline[t] *: summonAll[ts] |
| 159 | + end match |
| 160 | + res.asInstanceOf[Widen[T]] |
| 161 | +end summonAll |
| 162 | + |
| 163 | +/** Assertion that an argument is by-name. Used for nullability checking. */ |
| 164 | +def byName[T](x: => T): T = x |
0 commit comments