Skip to content

Commit c198d8a

Browse files
authored
Merge pull request #54 from TonalidadeHidrica/or-pattern-macro-rules
「マクロ規則における OR パターン」を翻訳
2 parents f6053bc + d332de8 commit c198d8a

File tree

3 files changed

+86
-3
lines changed

3 files changed

+86
-3
lines changed

TranslationTable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
| exterior | 外側の
9595
| feature | フィーチャ
9696
| foreign | 他言語
97+
| (マクロの) fragment specifier | フラグメント指定子
9798
| free | 解放する
9899
| free-standing function | フリースタンディングな関数
99100
| fully qualified syntax | 完全修飾構文

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,4 @@
7878
- [panic マクロの一貫性](rust-2021/panic-macro-consistency.md)
7979
- [構文の予約](rust-2021/reserving-syntax.md)
8080
- [警告からエラーへの格上げ](rust-2021/warnings-promoted-to-error.md)
81-
- [Or patterns in macro-rules](rust-2021/or-patterns-macro-rules.md)
81+
- [マクロ規則における OR パターン](rust-2021/or-patterns-macro-rules.md)
Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,154 @@
1+
<!--
12
# Or patterns in macro-rules
3+
-->
24

5+
# マクロ規則における OR パターン
6+
7+
<!--
38
## Summary
9+
-->
10+
11+
## 概要
412

13+
<!--
514
- How patterns work in `macro_rules` macros changes slightly:
615
- `$_:pat` in `macro_rules` now matches usage of `|` too: e.g. `A | B`.
716
- The new `$_:pat_param` behaves like `$_:pat` did before; it does not match (top level) `|`.
817
- `$_:pat_param` is available in all editions.
18+
-->
19+
20+
- `macro_rules` におけるパターンの挙動がほんの少し変更されました:
21+
- `macro_rules` において、`$_:pat` で `|` を使ったパターンにもマッチするようになりました。例えば、`A | B` にマッチします。
22+
- 新しく導入された `$_:pat_param` は、かつての `$_:pat` と同じ挙動を再現します。すなわち、こちらは(トップレベルの)`|` にはマッチしません。
23+
- `$_:pat_param` は全てのエディションで使用可能です。
24+
25+
926

27+
<!--
1028
## Details
29+
-->
1130

31+
## 詳細
32+
33+
<!--
1234
Starting in Rust 1.53.0, [patterns](https://doc.rust-lang.org/stable/reference/patterns.html)
1335
are extended to support `|` nested anywhere in the pattern.
1436
This enables you to write `Some(1 | 2)` instead of `Some(1) | Some(2)`.
1537
Since this was simply not allowed before, this is not a breaking change.
38+
-->
39+
40+
Rust 1.53.0 から、[パターン](https://doc.rust-lang.org/stable/reference/patterns.html)中のどこでも、`|` をネストして使えるようになりました。
41+
これにより、`Some(1) | Some(2)` でなく `Some(1 | 2)` と書くことができるようになりました。
42+
今まではこうは書けなかったので、これは破壊的変更ではありません。
1643

44+
<!--
1745
However, this change also affects [`macro_rules` macros](https://doc.rust-lang.org/stable/reference/macros-by-example.html).
1846
Such macros can accept patterns using the `:pat` fragment specifier.
1947
Currently, `:pat` does *not* match top level `|`, since before Rust 1.53,
2048
not all patterns (at all nested levels) could contain a `|`.
2149
Macros that accept patterns like `A | B`,
2250
such as [`matches!()`](https://doc.rust-lang.org/1.51.0/std/macro.matches.html)
2351
use something like `$($_:pat)|+`.
52+
-->
2453

54+
ところが、この変更は [`macro_rules` で定義されたマクロ](https://doc.rust-lang.org/stable/reference/macros-by-example.html) にも影響します。
55+
`macro_rules` では、`:pat` というフラグメント指定子で、パターンを受け付けることができます。
56+
現在のところ、`:pat` はトップレベルの `|` にマッチ*しません*
57+
なぜなら Rust 1.53 以前は、全てのパターンが(どのネストレベルにでも)`|` を含むことができるわけではなかったからです。
58+
[`matches!()`](https://doc.rust-lang.org/1.51.0/std/macro.matches.html) のように、
59+
`A | B` のようなパターンを受け付けるマクロを書くには、
60+
`$($_:pat)|+` のような書き方をしなくてはなりませんでした。
61+
62+
<!--
2563
Because this would potentially break existing macros, the meaning of `:pat` did
2664
not change in Rust 1.53.0 to include `|`. Instead, that change happens in Rust 2021.
2765
In the new edition, the `:pat` fragment specifier *will* match `A | B`.
66+
-->
67+
68+
既存のマクロを壊す可能性があるため、Rust 1.53.0 では `:pat``|` を含むことができるようには変更されませんでした。
69+
代わりに、Rust 2021 で変更がなされました。
70+
新しいエディションでは、`:pat` フラグメント指定子は `A | B` にマッチ*します*
2871

72+
<!--
2973
`$_:pat` fragments in Rust 2021 cannot be followed by an explicit `|`. Since there are times
3074
that one still wishes to match pattern fragments followed by a `|`, the fragment specified `:pat_param`
3175
has been added to retain the older behavior.
76+
-->
3277

78+
Rust 2021 では、`$_:pat` フラグメントに `|` そのものを続けることはできません。
79+
パターンフラグメントに `|` が続いてるものにマッチさせたいような場合は、新しく追加された `:pat_param` が過去と同じ挙動を示すようになっています。
80+
81+
<!--
3382
It's important to remember that editions are _per crate_, so the only relevant edition is the edition
3483
of the crate where the macro is defined. The edition of the crate where the macro is used does not
3584
change how the macro works.
85+
-->
86+
87+
ただし、エディションは<!-- -->_クレートごとに_<!-- -->設定されることに注意してください。
88+
つまり、マクロが定義されているクレートのエディションだけが関係します。
89+
マクロを使用する方のクレートのエディションは、マクロの挙動に影響しません。
3690

91+
<!--
3792
## Migration
93+
-->
94+
95+
## 移行
3896

97+
<!--
3998
A lint, `rust_2021_incompatible_or_patterns`, gets triggered whenever there is a use `$_:pat` which
4099
will change meaning in Rust 2021.
100+
-->
41101

102+
`$_:pat` が使われている場所のうち、Rust 2021 で意味が変わるようなものに対しては、`rust_2021_incompatible_or_patterns` というリントが発生します。
103+
104+
<!--
42105
You can automatically migrate your code to be Rust 2021 Edition compatible or ensure it is already compatible by
43106
running:
107+
-->
108+
109+
コードを自動的に Rust 2021 エディションに適合するよう自動移行するか、既に適合するものであることを確認するためには、以下のように実行すればよいです:
44110

45111
```sh
46112
cargo fix --edition
47113
```
48114

115+
<!--
49116
If you have a macro which relies on `$_:pat` not matching the top level use of `|` in patterns,
50117
you'll need to change each occurrence of `$_:pat` to `$_:pat_param`.
118+
-->
51119

120+
あなたのマクロが、`$_:pat` がトップレベルの `|` にマッチしないという挙動に依存している場合は、
121+
`$_:pat``$_:pat_param` に書き換える必要があります。
122+
123+
<!--
52124
For example:
125+
-->
126+
127+
例えば以下のようになります。
53128

54129
```rust
55130
macro_rules! my_macro {
56131
($x:pat | $y:pat) => {
57132
// TODO: implementation
58-
}
133+
// TODO: 実装
134+
}
59135
}
60136

61137
// This macro works in Rust 2018 since `$x:pat` does not match against `|`:
138+
// Rust 2018 では、`$x:pat` が `|` にマッチしないので、以下のマクロは正常に動きます:
62139
my_macro!(1 | 2);
63140

64141
// In Rust 2021 however, the `$_:pat` fragment matches `|` and is not allowed
65142
// to be followed by a `|`. To make sure this macro still works in Rust 2021
66143
// change the macro to the following:
144+
// 一方 Rust 2021 では、`$_:pat` フラグメントは `|` にもマッチし、
145+
// `|` が続くのは許されなくなりました。
146+
// Rust 2021 でもマクロが動作するためには、マクロを以下のように変更しなくてはなりません:
67147
macro_rules! my_macro {
68148
($x:pat_param | $y:pat) => { // <- this line is different
149+
// この行を変えた
69150
// TODO: implementation
151+
// TODO: 実装
70152
}
71153
}
72-
```
154+
```

0 commit comments

Comments
 (0)