Skip to content

マクロ関係の2ページを翻訳 #132

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@
- [`unsafe_op_in_unsafe_fn` warning](rust-2024/unsafe-op-in-unsafe-fn.md)
- [Disallow references to `static mut`](rust-2024/static-mut-references.md)
- [Never type fallback change](rust-2024/never-type-fallback.md)
- [Macro fragment specifiers](rust-2024/macro-fragment-specifiers.md)
- [Missing macro fragment specifiers](rust-2024/missing-macro-fragment-specifiers.md)
- [マクロフラグメント指定子](rust-2024/macro-fragment-specifiers.md)
- [未使用のマクロフラグメント指定子](rust-2024/missing-macro-fragment-specifiers.md)
- [`gen` keyword](rust-2024/gen-keyword.md)
- [Reserved syntax](rust-2024/reserved-syntax.md)
- [標準ライブラリ](rust-2024/standard-library.md)
Expand Down
94 changes: 92 additions & 2 deletions src/rust-2024/macro-fragment-specifiers.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
> **Rust Edition Guide は現在 Rust 2024 のアップデート作業に向けて翻訳作業中です。本ページはある時点での英語版をコピーしていますが、一部のリンクが動作しない場合や、最新情報が更新されていない場合があります。問題が発生した場合は、[原文(英語版)](https://doc.rust-lang.org/nightly/edition-guide/introduction.html)をご参照ください。**

<!--
# Macro Fragment Specifiers
-->

# マクロフラグメント指定子

<!--
## Summary
-->

## 概要

<!--
- The `expr` [fragment specifier] now also supports `const` and `_` expressions.
- The `expr_2021` fragment specifier has been added for backwards compatibility.
-->

- [フラグメント指定子] `expr` が、`const` 式と `_` 式をサポートするようになりました。
- 後方互換性のために、フラグメント指定子 `expr_2021` が追加されました。

<!--
[fragment specifier]: ../../reference/macros-by-example.html#metavariables
-->

[フラグメント指定子]: https://doc.rust-lang.org/reference/macros-by-example.html#metavariables

<!--
## Details
-->

## 詳細

<!--
As new syntax is added to Rust, existing `macro_rules` fragment specifiers are sometimes not allowed to match on the new syntax in order to retain backwards compatibility. Supporting the new syntax in the old fragment specifiers is sometimes deferred until the next edition, which provides an opportunity to update them.
-->

Rust に新しい文法を導入するとき、後方互換性のために既存の `macro_rules` フラグメント指定子を新しい記法の対象外とする場合があります。
すなわち、古いフラグメント指定子の新記法のサポートが次の新エディションまで延期されることがあるのです。

<!--
Indeed this happened with [`const` expressions] added in 1.79 and [`_` expressions] added in 1.59. In the 2021 Edition and earlier, the `expr` fragment specifier does *not* match those expressions. This is because you may have a scenario like:
-->

1.79 で導入された [`const` 式]と 1.59 で導入された [`_` 式]では、実際にそのような決定がなされました。
2021 エディション以前では、フラグメント指定子 `expr` はこれらの新記法にマッチ**しません**。
理由は以下のコードを考えるとわかります。

<!--
```rust,edition2021
macro_rules! example {
($e:expr) => { println!("first rule"); };
Expand All @@ -25,29 +56,88 @@ fn main() {
example!(const { 1 + 1 });
}
```
-->

```rust,edition2021
macro_rules! example {
($e:expr) => { println!("前者"); };
(const $e:expr) => { println!("後者"); };
}

fn main() {
example!(const { 1 + 1 });
}
```

<!--
Here, in the 2021 Edition, the macro will match the *second* rule. If earlier editions had changed `expr` to match the newly introduced `const` expressions, then it would match the *first* rule, which would be a breaking change.
-->

2021 エディションでは、マクロは**後者**のルールにマッチします。
2021 以前のエディションで `expr` が新文法である `const` 式にマッチしてしまうようになると、マクロは**前者**のルールにマッチするようになってしまいます。
これは破壊的変更です。

<!--
In the 2024 Edition, `expr` specifiers now also match `const` and `_` expressions. To support the old behavior, the `expr_2021` fragment specifier has been added which does *not* match the new expressions.
-->

2024 エディションから、指定子 `expr` が `const` 式と `_` 式にもマッチするようになりました。
過去の挙動もサポートするために、これらにマッチ**しない**フラグメント指定子 `expr_2021` も新たに提供されています。

<!--
[`const` expressions]: ../../reference/expressions/block-expr.html#const-blocks
[`_` expressions]: ../../reference/expressions/underscore-expr.html
-->

[`const` 式]: https://doc.rust-lang.org/reference/expressions/block-expr.html#const-blocks
[`_` 式]: https://doc.rust-lang.org/reference/expressions/underscore-expr.html

<!--
## Migration
-->

## 移行

<!--
The [`edition_2024_expr_fragment_specifier`] lint will change all uses of the `expr` specifier to `expr_2021` to ensure that the behavior of existing macros does not change. The lint is part of the `rust-2024-compatibility` lint group which is included in the automatic edition migration. In order to migrate your code to be Rust 2024 Edition compatible, run:
-->

[`edition_2024_expr_fragment_specifier`] リントで、`expr` 指定子をすべて `expr_2021` に自動的に書き換え、マクロの挙動が変わらないようにできます。
このリントは、自動エディション移行に含まれる `rust-2024-compatibility` リントグループの一部です。
コードを Rust 2024 互換に移行するには、以下を実行します。

```sh
cargo fix --edition
```

<!--
In *most* cases, you will likely want to keep the `expr` specifier instead, in order to support the new expressions. You will need to review your macro to determine if there are other rules that would otherwise match with `const` or `_` and determine if there is a conflict. If you want the new behavior, just revert any changes made by the lint.
-->

**ほとんどの**場合、指定子を `expr` のままにしておき、新記法をサポートするべきでしょう。
マクロの定義を再確認して、`const` や `_` にマッチしうるようなルールの重複がないかを再確認してください。
新記法をサポートしてもよければ、リントの自動修正を戻せばよいです。

<!--
Alternatively, you can manually enable the lint to find macros where you may need to update the `expr` specifier.
-->

あるいは、エディション移行ツールを使わずに手動で確認したい場合は、以下のリントをオンにしてください。

<!--
```rust
// Add this to the root of your crate to do a manual migration.
#![warn(edition_2024_expr_fragment_specifier)]
```
-->

```rust
// クレートのトップレベルに以下を追加すると手動移行できる
#![warn(edition_2024_expr_fragment_specifier)]
```

<!--
[`edition_2024_expr_fragment_specifier`]: ../../rustc/lints/listing/allowed-by-default.html#edition-2024-expr-fragment-specifier
-->

[`edition_2024_expr_fragment_specifier`]: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#edition-2024-expr-fragment-specifier
57 changes: 55 additions & 2 deletions src/rust-2024/missing-macro-fragment-specifiers.md
Original file line number Diff line number Diff line change
@@ -1,38 +1,91 @@
> **Rust Edition Guide は現在 Rust 2024 のアップデート作業に向けて翻訳作業中です。本ページはある時点での英語版をコピーしていますが、一部のリンクが動作しない場合や、最新情報が更新されていない場合があります。問題が発生した場合は、[原文(英語版)](https://doc.rust-lang.org/nightly/edition-guide/introduction.html)をご参照ください。**

<!--
# Missing macro fragment specifiers
-->

# 未使用のマクロフラグメント指定子

<!--
## Summary
-->

## 概要

<!--
- The [`missing_fragment_specifier`] lint is now a hard error.
-->

- [`missing_fragment_specifier`] リントがエラー扱いされるようになります。

<!--
[`missing_fragment_specifier`]: ../../rustc/lints/listing/deny-by-default.html#missing-fragment-specifier
-->

[`missing_fragment_specifier`]: https://doc.rust-lang.org/rustc/lints/listing/deny-by-default.html#missing-fragment-specifier

<!--
## Details
-->

## 詳細

<!--
The [`missing_fragment_specifier`] lint detects a situation when an **unused** pattern in a `macro_rules!` macro definition has a meta-variable (e.g. `$e`) that is not followed by a fragment specifier (e.g. `:expr`). This is now a hard error in the 2024 Edition.
-->

[`missing_fragment_specifier`] リントは、マクロ定義 (`macro_rules!`) 内の**未使用の**パターン中の(`$e` のような)メタ変数であって、(`:expr` といった)フラグメント指定子のないものを検出します。
2024 エディションからはこのリントはエラー扱いになります。

```rust,compile_fail
macro_rules! foo {
() => {};
($name) => { }; // ERROR: missing fragment specifier
// エラー: フラグメント指定子がありません
}

fn main() {
foo!();
}
```

<!--
Calling the macro with arguments that would match a rule with a missing specifier (e.g., `foo!($name)`) is a hard error in all editions. However, simply defining a macro with missing fragment specifiers is not, though we did add a lint in Rust 1.17.
-->

`foo!($name)` のように、フラグメント指定子のないメタ変数を含むルールにマッチするマクロ呼び出しは、全エディションでエラーです。
一方、そのようなマクロを定義すること自体はエラーではありません(これを検出するリントは 1.17 から追加されています)。

<!--
We'd like to make this a hard error in all editions, but there would be too much breakage right now. So we're starting by making this a hard error in Rust 2024.[^future-incompat]
-->

本当ならこのリントを全エディションでエラー扱いしたいところですが、あまりにも影響が大きすぎるため、ひとまず Rust 2024 以降でだけエラー扱いすることになりました。[^future-incompat]

<!--
[^future-incompat]: The lint is marked as a "future-incompatible" warning to indicate that it may become a hard error in all editions in a future release. See [#40107] for more information.
-->

[^future-incompat]: このリントは "future-incompatible" な(将来的に非互換になる)警告に分類されており、将来のリリースでは全エディションでエラー扱いになる予定であるとされています。
詳細は [#40107] をご参照ください。

[#40107]: https://github.com/rust-lang/rust/issues/40107

<!--
## Migration
-->

## 移行

<!--
To migrate your code to the 2024 Edition, remove the unused matcher rule from the macro. The [`missing_fragment_specifier`] lint is on by default in all editions, and should alert you to macros with this issue.
-->

コードを 2024 エディションに移行するには、上記のような未使用のルールを削除してください。
[`missing_fragment_specifier`] リントは全エディションで有効化されており、このようなマクロに対して警告が出ます。

<!--
There is no automatic migration for this change. We expect that this style of macro is extremely rare. The lint has been a future-incompatibility lint since Rust 1.17, a deny-by-default lint since Rust 1.20, and since Rust 1.82, it has warned about dependencies that are using this pattern.
-->

これに対する自動移行は提供されていません。
このようなマクロはまずないと思われます。
このリントは Rust 1.17 から future-incompatible なリントになり、Rust 1.20 からデフォルトで必ずエラーになり、1.82 からは依存ライブラリ中であっても警告が出るようになっています。