|
| 1 | +- Start Date: 2015-2-3 |
| 2 | +- RFC PR: [rust-lang/rfcs#803](https://github.com/rust-lang/rfcs/pull/803) |
| 3 | +- Rust Issue: [rust-lang/rust#23416](https://github.com/rust-lang/rust/issues/23416) |
| 4 | +- Feature: `ascription` |
| 5 | + |
| 6 | +# Summary |
| 7 | + |
| 8 | +Add type ascription to expressions. (An earlier version of this RFC covered type |
| 9 | +ascription in patterns too, that has been postponed). |
| 10 | + |
| 11 | +Type ascription on expression has already been implemented. |
| 12 | + |
| 13 | +See also discussion on [#354](https://github.com/rust-lang/rfcs/issues/354) and |
| 14 | +[rust issue 10502](https://github.com/rust-lang/rust/issues/10502). |
| 15 | + |
| 16 | + |
| 17 | +# Motivation |
| 18 | + |
| 19 | +Type inference is imperfect. It is often useful to help type inference by |
| 20 | +annotating a sub-expression with a type. Currently, this is only possible by |
| 21 | +extracting the sub-expression into a variable using a `let` statement and/or |
| 22 | +giving a type for a whole expression or pattern. This is un- ergonomic, and |
| 23 | +sometimes impossible due to lifetime issues. Specifically, where a variable has |
| 24 | +lifetime of its enclosing scope, but a sub-expression's lifetime is typically |
| 25 | +limited to the nearest semi-colon. |
| 26 | + |
| 27 | +Typical use cases are where a function's return type is generic (e.g., collect) |
| 28 | +and where we want to force a coercion. |
| 29 | + |
| 30 | +Type ascription can also be used for documentation and debugging - where it is |
| 31 | +unclear from the code which type will be inferred, type ascription can be used |
| 32 | +to precisely communicate expectations to the compiler or other programmers. |
| 33 | + |
| 34 | +By allowing type ascription in more places, we remove the inconsistency that |
| 35 | +type ascription is currently only allowed on top-level patterns. |
| 36 | + |
| 37 | +## Examples: |
| 38 | + |
| 39 | +(Somewhat simplified examples, in these cases there are sometimes better |
| 40 | +solutions with the current syntax). |
| 41 | + |
| 42 | +Generic return type: |
| 43 | + |
| 44 | +``` |
| 45 | +// Current. |
| 46 | +let z = if ... { |
| 47 | + let x: Vec<_> = foo.enumerate().collect(); |
| 48 | + x |
| 49 | +} else { |
| 50 | + ... |
| 51 | +}; |
| 52 | +
|
| 53 | +// With type ascription. |
| 54 | +let z = if ... { |
| 55 | + foo.enumerate().collect(): Vec<_> |
| 56 | +} else { |
| 57 | + ... |
| 58 | +}; |
| 59 | +``` |
| 60 | + |
| 61 | +Coercion: |
| 62 | + |
| 63 | +``` |
| 64 | +fn foo<T>(a: T, b: T) { ... } |
| 65 | +
|
| 66 | +// Current. |
| 67 | +let x = [1u32, 2, 4]; |
| 68 | +let y = [3u32]; |
| 69 | +... |
| 70 | +let x: &[_] = &x; |
| 71 | +let y: &[_] = &y; |
| 72 | +foo(x, y); |
| 73 | +
|
| 74 | +// With type ascription. |
| 75 | +let x = [1u32, 2, 4]; |
| 76 | +let y = [3u32]; |
| 77 | +... |
| 78 | +foo(x: &[_], y: &[_]); |
| 79 | +``` |
| 80 | + |
| 81 | +Generic return type and coercion: |
| 82 | + |
| 83 | +``` |
| 84 | +// Current. |
| 85 | +let x: T = { |
| 86 | + let temp: U<_> = foo(); |
| 87 | + temp |
| 88 | +}; |
| 89 | +
|
| 90 | +// With type ascription. |
| 91 | +let x: T = foo(): U<_>; |
| 92 | +``` |
| 93 | + |
| 94 | + |
| 95 | +# Detailed design |
| 96 | + |
| 97 | +The syntax of expressions is extended with type ascription: |
| 98 | + |
| 99 | +``` |
| 100 | +e ::= ... | e: T |
| 101 | +``` |
| 102 | + |
| 103 | +where `e` is an expression and `T` is a type. Type ascription has the same |
| 104 | +precedence as explicit coercions using `as`. |
| 105 | + |
| 106 | +When type checking `e: T`, `e` must have type `T`. The `must have type` test |
| 107 | +includes implicit coercions and subtyping, but not explicit coercions. `T` may |
| 108 | +be any well-formed type. |
| 109 | + |
| 110 | +At runtime, type ascription is a no-op, unless an implicit coercion was used in |
| 111 | +type checking, in which case the dynamic semantics of a type ascription |
| 112 | +expression are exactly those of the implicit coercion. |
| 113 | + |
| 114 | +@eddyb has implemented the expressions part of this RFC, |
| 115 | +[PR](https://github.com/rust-lang/rust/pull/21836). |
| 116 | + |
| 117 | +This feature should land behind the `ascription` feature gate. |
| 118 | + |
| 119 | + |
| 120 | +### coercion and `as` vs `:` |
| 121 | + |
| 122 | +A downside of type ascription is the overlap with explicit coercions (aka casts, |
| 123 | +the `as` operator). To the programmer, type ascription makes implicit coercions |
| 124 | +explicit (however, the compiler makes no distinction between coercions due to |
| 125 | +type ascription and other coercions). In RFC 401, it is proposed that all valid |
| 126 | +implicit coercions are valid explicit coercions. However, that may be too |
| 127 | +confusing for users, since there is no reason to use type ascription rather than |
| 128 | +`as` (if there is some coercion). Furthermore, if programmers do opt to use `as` |
| 129 | +as the default whether or not it is required, then it loses its function as a |
| 130 | +warning sign for programmers to beware of. |
| 131 | + |
| 132 | +To address this I propose two lints which check for: trivial casts and trivial |
| 133 | +numeric casts. Other than these lints we stick with the proposal from #401 that |
| 134 | +unnecessary casts will no longer be an error. |
| 135 | + |
| 136 | +A trivial cast is a cast `x as T` where `x` has type `U` and `x` can be |
| 137 | +implicitly coerced to `T` or is already a subtype of `T`. |
| 138 | + |
| 139 | +A trivial numeric cast is a cast `x as T` where `x` has type `U` and `x` is |
| 140 | +implicitly coercible to `T` or `U` is a subtype of `T`, and both `U` and `T` are |
| 141 | +numeric types. |
| 142 | + |
| 143 | +Like any lints, these can be customised per-crate by the programmer. Both lints |
| 144 | +are 'warn' by default. |
| 145 | + |
| 146 | +Although this is a somewhat complex scheme, it allows code that works today to |
| 147 | +work with only minor adjustment, it allows for a backwards compatible path to |
| 148 | +'promoting' type conversions from explicit casts to implicit coercions, and it |
| 149 | +allows customisation of a contentious kind of error (especially so in the |
| 150 | +context of cross-platform programming). |
| 151 | + |
| 152 | + |
| 153 | +### Type ascription and temporaries |
| 154 | + |
| 155 | +There is an implementation choice between treating `x: T` as an lvalue or |
| 156 | +rvalue. Note that when a rvalue is used in lvalue context (e.g., the subject of |
| 157 | +a reference operation), then the compiler introduces a temporary variable. |
| 158 | +Neither option is satisfactory, if we treat an ascription expression as an |
| 159 | +lvalue (i.e., no new temporary), then there is potential for unsoundness: |
| 160 | + |
| 161 | +``` |
| 162 | +let mut foo: S = ...; |
| 163 | +{ |
| 164 | + let bar = &mut (foo: T); // S <: T, no coercion required |
| 165 | + *bar = ... : T; |
| 166 | +} |
| 167 | +// Whoops, foo has type T, but the compiler thinks it has type S, where potentially T </: S |
| 168 | +``` |
| 169 | + |
| 170 | +If we treat ascription expressions as rvalues (i.e., create a temporary in |
| 171 | +lvalue position), then we don't have the soundness problem, but we do get the |
| 172 | +unexpected result that `&(x: T)` is not in fact a reference to `x`, but a |
| 173 | +reference to a temporary copy of `x`. |
| 174 | + |
| 175 | +The proposed solution is that type ascription expressions are rvalues, but |
| 176 | +taking a reference of such an expression is forbidden. I.e., type asciption is |
| 177 | +forbidden in the following contexts (where `<expr>` is a type ascription |
| 178 | +expression): |
| 179 | + |
| 180 | +``` |
| 181 | +&[mut] <expr> |
| 182 | +let ref [mut] x = <expr> |
| 183 | +match <expr> { .. ref [mut] x .. => { .. } .. } |
| 184 | +<expr>.foo() // due to autoref |
| 185 | +``` |
| 186 | + |
| 187 | +Like other rvalues, type ascription would not be allowed as the lhs of assignment. |
| 188 | + |
| 189 | +Note that, if type asciption is required in such a context, an lvalue can be |
| 190 | +forced by using `{}`, e.g., write `&mut { foo: T }`, rather than `&mut (foo: T)`. |
| 191 | + |
| 192 | + |
| 193 | +# Drawbacks |
| 194 | + |
| 195 | +More syntax, another feature in the language. |
| 196 | + |
| 197 | +Interacts poorly with struct initialisers (changing the syntax for struct |
| 198 | +literals has been [discussed and rejected](https://github.com/rust-lang/rfcs/pull/65) |
| 199 | +and again in [discuss](http://internals.rust-lang.org/t/replace-point-x-3-y-5-with-point-x-3-y-5/198)). |
| 200 | + |
| 201 | +If we introduce named arguments in the future, then it would make it more |
| 202 | +difficult to support the same syntax as field initialisers. |
| 203 | + |
| 204 | + |
| 205 | +# Alternatives |
| 206 | + |
| 207 | +We could do nothing and force programmers to use temporary variables to specify |
| 208 | +a type. However, this is less ergonomic and has problems with scopes/lifetimes. |
| 209 | + |
| 210 | +Rely on explicit coercions - the current plan [RFC 401](https://github.com/rust-lang/rfcs/blob/master/text/0401-coercions.md) |
| 211 | +is to allow explicit coercion to any valid type and to use a customisable lint |
| 212 | +for trivial casts (that is, those given by subtyping, including the identity |
| 213 | +case). If we allow trivial casts, then we could always use explicit coercions |
| 214 | +instead of type ascription. However, we would then lose the distinction between |
| 215 | +implicit coercions which are safe and explicit coercions, such as narrowing, |
| 216 | +which require more programmer attention. This also does not help with patterns. |
| 217 | + |
| 218 | +We could use a different symbol or keyword instead of `:`, e.g., `is`. |
| 219 | + |
| 220 | + |
| 221 | +# Unresolved questions |
| 222 | + |
| 223 | +Is the suggested precedence correct? |
| 224 | + |
| 225 | +Should we remove integer suffixes in favour of type ascription? |
| 226 | + |
| 227 | +Style guidelines - should we recommend spacing or parenthesis to make type |
| 228 | +ascription syntax more easily recognisable? |
0 commit comments