|
| 1 | +<!-- |
1 | 2 | # Reserving syntax |
| 3 | +--> |
2 | 4 |
|
| 5 | +# 構文の予約 |
| 6 | + |
| 7 | +<!-- |
3 | 8 | ## Summary |
| 9 | +--> |
| 10 | + |
| 11 | +## 概要 |
4 | 12 |
|
| 13 | +<!-- |
5 | 14 | - `any_identifier#`, `any_identifier"..."`, and `any_identifier'...'` are now reserved |
6 | 15 | syntax, and no longer tokenize. |
7 | 16 | - This is mostly relevant to macros. E.g. `quote!{ #a#b }` is no longer accepted. |
8 | 17 | - It doesn't treat keywords specially, so e.g. `match"..." {}` is no longer accepted. |
9 | 18 | - Insert whitespace between the identifier and the subsequent `#`, `"`, or `'` |
10 | 19 | to avoid errors. |
11 | 20 | - Edition migrations will help you insert whitespace in such cases. |
| 21 | +--> |
| 22 | + |
| 23 | +- `shikibetsushi#`, `shikibetsushi"..."`, `shikibetsushi'...'` の3つの構文が新たに予約され、トークン分割されなくなりました。 |
| 24 | +- 主に影響を受けるのはマクロです。例えば、`quote!{ #a#b }` と書くことはできなくなりました。 |
| 25 | +- キーワードが特別扱いされることもないので、例えば `match"..." {}` と書くこともできなくなりました。 |
| 26 | +- 識別子と後続の `#`, `"`, `'` の間に空白文字を挿入することで、エラーを回避できます。 |
| 27 | +- エディション移行ツールは、必要な場所に空白を挿入してくれます。 |
12 | 28 |
|
| 29 | +<!-- |
13 | 30 | ## Details |
| 31 | +--> |
14 | 32 |
|
| 33 | +## 詳細 |
| 34 | + |
| 35 | +<!-- |
15 | 36 | To make space for new syntax in the future, |
16 | 37 | we've decided to reserve syntax for prefixed identifiers and literals: |
17 | 38 | `prefix#identifier`, `prefix"string"`, `prefix'c'`, and `prefix#123`, |
18 | 39 | where `prefix` can be any identifier. |
19 | 40 | (Except those prefixes that already have a meaning, such as `b'...'` (byte |
20 | 41 | strings) and `r"..."` (raw strings).) |
| 42 | +--> |
| 43 | + |
| 44 | +私達は、将来新しい構文を導入する余地を残すため、接頭辞付きの識別子とリテラルの構文を予約することにしました。 |
| 45 | +予約されたのは、任意の識別子 `prefix` を用いて `prefix#identifier`, `prefix"string"`, `prefix'c'`, `prefix#123` のいずれかの形で書かれるものです。 |
| 46 | +(ただし、`b'...'`(バイト文字列)や`r"..."`(生文字列)のように、すでに意味が割り当てられているものを除きます。) |
21 | 47 |
|
| 48 | +<!-- |
22 | 49 | This provides syntax we can expand into in the future without requiring an |
23 | 50 | edition boundary. We may use this for temporary syntax until the next edition, |
24 | 51 | or for permanent syntax if appropriate. |
| 52 | +--> |
| 53 | + |
| 54 | +これにより、将来エディションをまたくごとなく構文を拡張できるようになります。 |
| 55 | +これを、次のエディションまでの一時的な構文のために使ったり、もし適切なら、恒久的な構文のために使ったりするでしょう。 |
25 | 56 |
|
| 57 | +<!-- |
26 | 58 | Without an edition, this would be a breaking change, since macros can currently |
27 | 59 | accept syntax such as `hello"world"`, which they will see as two separate |
28 | 60 | tokens: `hello` and `"world"`. The (automatic) fix is simple though: just |
29 | 61 | insert a space: `hello "world"`. Likewise, `prefix#ident` should become |
30 | 62 | `prefix #ident`. Edition migrations will help with this fix. |
| 63 | +--> |
31 | 64 |
|
| 65 | +エディションの区切りがないと、これは破壊的変更に当たります。 |
| 66 | +なぜなら、現在のマクロは、例えば `hello"world"` という構文を、 `hello` と `"world"` の2つのトークンとして受け入れるからです。 |
| 67 | +もっとも、(自動)修正はシンプルで、`hello "world"` のように空白を入れるだけです。 |
| 68 | +同様に、`prefix#ident` は `prefix #ident` とする必要があります。 |
| 69 | +エディション移行ツールは、そのように修正してくれます。 |
| 70 | + |
| 71 | +<!-- |
32 | 72 | Other than turning these into a tokenization error, |
33 | 73 | [the RFC][10] does not attach a meaning to any prefix yet. |
34 | 74 | Assigning meaning to specific prefixes is left to future proposals, |
35 | 75 | which will now—thanks to reserving these prefixes—not be breaking changes. |
| 76 | +--> |
| 77 | + |
| 78 | +[これが提案された RFC][10] は、このような書き方をトークン分割エラーにすると決めているだけで、 |
| 79 | +特定の接頭辞に意味を持たせることはまだしていません。 |
| 80 | +接頭辞に何らかの役割を割り当てるのは、将来の提案にその余地が残されています。 |
| 81 | +接頭辞が予約されたおかげで、今後は破壊的変更なく新しい構文を導入できます。 |
36 | 82 |
|
| 83 | +<!-- |
37 | 84 | Some new prefixes you might potentially see in the future (though we haven't |
38 | 85 | committed to any of them yet): |
| 86 | +--> |
| 87 | + |
| 88 | +例えば、以下のような接頭辞が使えるようになるかもしれません(ただし、いずれもまだ提案が固まったわけではありません): |
39 | 89 |
|
| 90 | +<!-- |
40 | 91 | - `k#keyword` to allow writing keywords that don't exist yet in the current edition. |
41 | 92 | For example, while `async` is not a keyword in edition 2015, |
42 | 93 | this prefix would've allowed us to accept `k#async` in edition 2015 |
43 | 94 | without having to wait for edition 2018 to reserve `async` as a keyword. |
| 95 | +--> |
44 | 96 |
|
| 97 | +- `k#keyword` で、現在のエディションにまだ導入されていないキーワードを書けるようにする。 |
| 98 | + たとえば、2015 エディションでは `async` はキーワードではありませんが、 |
| 99 | + このような接頭辞が使えたのならば、2018 エディションで `async` が予約語になるのを待たずに、2015 エディションでも `k#async` が使えたということになります。 |
| 100 | + |
| 101 | +<!-- |
45 | 102 | - `f""` as a short-hand for a format string. |
46 | 103 | For example, `f"hello {name}"` as a short-hand for the equivalent `format!()` invocation. |
| 104 | +--> |
| 105 | + |
| 106 | +- `f""` で、フォーマット文字列の略記とする。 |
| 107 | + 例えば、`f"hello {name}"` と書いたら、それと等価な `format!()` の呼び出しと見なす。 |
47 | 108 |
|
| 109 | +<!-- |
48 | 110 | - `s""` for `String` literals. |
| 111 | +--> |
49 | 112 |
|
| 113 | +- `s""` で `String` リテラルを表す。 |
| 114 | + |
| 115 | +<!-- |
50 | 116 | - `c""` or `z""` for null-terminated C strings. |
| 117 | +--> |
| 118 | + |
| 119 | +- `c""` か `z""` で、ヌル終端のC言語の文字列を表す。 |
51 | 120 |
|
52 | 121 | [10]: https://github.com/rust-lang/rfcs/pull/3101 |
53 | 122 |
|
54 | 123 |
|
| 124 | +<!-- |
55 | 125 | ## Migration |
| 126 | +--> |
| 127 | + |
| 128 | +## 移行 |
56 | 129 |
|
| 130 | +<!-- |
57 | 131 | As a part of the 2021 edition a migration lint, `rust_2021_prefixes_incompatible_syntax`, has been added in order to aid in automatic migration of Rust 2018 codebases to Rust 2021. |
| 132 | +--> |
58 | 133 |
|
| 134 | +Rust 2018 のコードベースから Rust 2021 への自動移行の支援のため、2021 エディションには、移行用のリント`rust_2021_prefixes_incompatible_syntax` が追加されています。 |
| 135 | + |
| 136 | +<!-- |
59 | 137 | In order to have `rustfix` migrate your code to be Rust 2021 Edition compatible, run: |
| 138 | +--> |
| 139 | + |
| 140 | +`rustfix` でコードを Rust 2021 エディションに適合させるためには、次のように実行します。 |
60 | 141 |
|
61 | 142 | ```sh |
62 | 143 | cargo fix --edition |
63 | 144 | ``` |
64 | 145 |
|
| 146 | +<!-- |
65 | 147 | Should you want or need to manually migrate your code, migration is fairly straight-forward. |
| 148 | +--> |
66 | 149 |
|
| 150 | +コード移行を手で行いたいか、行う必要があっても、移行は非常に簡単です。 |
| 151 | + |
| 152 | +<!-- |
67 | 153 | Let's say you have a macro that is defined like so: |
| 154 | +--> |
| 155 | + |
| 156 | +例えば、次のように定義されたマクロがあったとしましょう: |
68 | 157 |
|
69 | 158 | ```rust |
70 | 159 | macro_rules! my_macro { |
71 | 160 | ($a:tt $b:tt) => {}; |
72 | 161 | } |
73 | 162 | ``` |
74 | 163 |
|
| 164 | +<!-- |
75 | 165 | In Rust 2015 and 2018 it's legal for this macro to be called like so with no space between the first token tree and the second: |
| 166 | +--> |
| 167 | + |
| 168 | +Rust 2015 と 2018 では、以下のように、1つ目と2つ目のトークンの間に空白を入れることなくマクロを呼び出しても問題ありませんでした: |
76 | 169 |
|
77 | 170 | ```rust,ignore |
78 | 171 | my_macro!(z"hey"); |
79 | 172 | ``` |
80 | 173 |
|
| 174 | +<!-- |
81 | 175 | This `z` prefix is no longer allowed in Rust 2021, so in order to call this macro, you must add a space after the prefix like so: |
| 176 | +--> |
| 177 | + |
| 178 | +Rust 2021 では `z` という接頭辞は許されないので、このマクロを呼び出すためには、以下のように接頭辞の後にスペースを入れる必要があります: |
82 | 179 |
|
83 | 180 | ```rust,ignore |
84 | 181 | my_macro!(z "hey"); |
85 | | -``` |
| 182 | +``` |
0 commit comments