From 7afc765312f2811fd725b884c41c9725b66dba0f Mon Sep 17 00:00:00 2001 From: dyskinmel Date: Thu, 13 Feb 2025 10:16:20 -0600 Subject: [PATCH 1/6] =?UTF-8?q?Rust=202024=20=E9=83=A8=E5=88=86=E3=81=AE?= =?UTF-8?q?=E7=9B=AE=E6=AC=A1=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SUMMARY.md | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 5c12430..6256c3f 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -70,3 +70,73 @@ - [構文の予約](rust-2021/reserving-syntax.md) - [警告からエラーへの格上げ](rust-2021/warnings-promoted-to-error.md) - [マクロ規則における OR パターン](rust-2021/or-patterns-macro-rules.md) + +## Rust 2024 + + + +- [Rust 2024](rust-2024/index.md) + - [Language](rust-2024/language.md) + - [RPIT lifetime capture rules](rust-2024/rpit-lifetime-capture.md) + - [`if let` temporary scope](rust-2024/temporary-if-let-scope.md) + - [Tail expression temporary scope](rust-2024/temporary-tail-expr-scope.md) + - [Match ergonomics reservations](rust-2024/match-ergonomics.md) + - [Unsafe `extern` blocks](rust-2024/unsafe-extern.md) + - [Unsafe attributes](rust-2024/unsafe-attributes.md) + - [`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) + - [`gen` keyword](rust-2024/gen-keyword.md) + - [Reserved syntax](rust-2024/reserved-syntax.md) + - [Standard library](rust-2024/standard-library.md) + - [Changes to the prelude](rust-2024/prelude.md) + - [Add `IntoIterator` for `Box<[T]>`](rust-2024/intoiterator-box-slice.md) + - [Newly unsafe functions](rust-2024/newly-unsafe-functions.md) + - [Cargo](rust-2024/cargo.md) + - [Cargo: Rust-version aware resolver](rust-2024/cargo-resolver.md) + - [Cargo: Table and key name consistency](rust-2024/cargo-table-key-names.md) + - [Cargo: Reject unused inherited default-features](rust-2024/cargo-inherited-default-features.md) + - [Rustdoc](rust-2024/rustdoc.md) + - [Rustdoc combined tests](rust-2024/rustdoc-doctests.md) + - [Rustdoc nested `include!` change](rust-2024/rustdoc-nested-includes.md) + - [Rustfmt](rust-2024/rustfmt.md) + - [Rustfmt: Style edition](rust-2024/rustfmt-style-edition.md) + - [Rustfmt: Formatting fixes](rust-2024/rustfmt-formatting-fixes.md) + - [Rustfmt: Combine all delimited exprs as last argument](rust-2024/rustfmt-overflow-delimited-expr.md) + - [Rustfmt: Raw identifier sorting](rust-2024/rustfmt-raw-identifier-sorting.md) + - [Rustfmt: Version sorting](rust-2024/rustfmt-version-sorting.md) \ No newline at end of file From e399894b5cc65143efc4d834ba0fdf9546afe2bb Mon Sep 17 00:00:00 2001 From: dyskinmel Date: Tue, 18 Feb 2025 20:53:32 -0600 Subject: [PATCH 2/6] =?UTF-8?q?=E7=BF=BB=E8=A8=B3=E6=96=87=E3=81=8A?= =?UTF-8?q?=E3=82=88=E3=81=B3Summary=E3=81=AE=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SUMMARY.md | 2 +- src/rust-2024/newly-unsafe-functions.md | 163 ++++++++++++++++++++++++ 2 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 src/rust-2024/newly-unsafe-functions.md diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 6256c3f..821c77a 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -126,7 +126,7 @@ - [Standard library](rust-2024/standard-library.md) - [Changes to the prelude](rust-2024/prelude.md) - [Add `IntoIterator` for `Box<[T]>`](rust-2024/intoiterator-box-slice.md) - - [Newly unsafe functions](rust-2024/newly-unsafe-functions.md) + - [unsafe 関数](rust-2024/newly-unsafe-functions.md) - [Cargo](rust-2024/cargo.md) - [Cargo: Rust-version aware resolver](rust-2024/cargo-resolver.md) - [Cargo: Table and key name consistency](rust-2024/cargo-table-key-names.md) diff --git a/src/rust-2024/newly-unsafe-functions.md b/src/rust-2024/newly-unsafe-functions.md new file mode 100644 index 0000000..a2224fd --- /dev/null +++ b/src/rust-2024/newly-unsafe-functions.md @@ -0,0 +1,163 @@ + + +# unsafe 関数 + + + +## 概要 + + + +- 以下の関数が新たに [`unsafe`] としてマークされました: +- [`std::env::set_var`] +- [`std::env::remove_var`] +- [`std::os::unix::process::CommandExt::before_exec`] + + +[`unsafe`]: ../../reference/unsafe-keyword.html#unsafe-functions-unsafe-fn +[`std::env::set_var`]: ../../std/env/fn.set_var.html +[`std::env::remove_var`]: ../../std/env/fn.remove_var.html +[`std::os::unix::process::CommandExt::before_exec`]: ../../std/os/unix/process/trait.CommandExt.html#method.before_exec + + + + +## 詳細 + + + +標準ライブラリのいくつかの関数は、本来 `unsafe` であるべきであることが時間とともに明らかになってきました。しかし、関数を unsafe に変更すると、既存のコードでは `unsafe` ブロックで囲む必要が生じるため、破壊的変更となる可能性があります。そのため、これらの関数は 2024 Edition から `unsafe` にマークされますが、以前のエディションでは `unsafe` を要求しません。 + + + +### `std::env::{set_var, remove_var}` + + + +[`std::env::set_var`] や [`std::env::remove_var`] をマルチスレッド環境で呼び出すことは、一部のプラットフォームにおける プロセス環境の管理方法の制約により、安全でない可能性があります。標準ライブラリではこれらの関数をもともと安全な関数として定義していましたが、それが適切でなかったことが後に判明しました。 + + + +これらの関数を呼び出す際は 他のスレッドが動作していないことを確実に保証する必要があります。詳細は、各関数の [Safety] セクションを参照してください。 + +[Safety]: ../../std/env/fn.set_var.html#safety + + + +### `std::os::unix::process::CommandExt::before_exec` + + + +[`std::os::unix::process::CommandExt::before_exec`] は、Unix 固有の関数であり、`exec` を呼び出す前にクロージャを実行するための手段を提供します。この関数は Rust 1.37 で非推奨となり、代わりに [`pre_exec`] が導入されました。`pre_exec` は同じ機能を提供しますが、明示的に `unsafe` としてマークされています。 + + + +`before_exec` はすでに非推奨ですが、2024 Edition から `unsafe` として正しくマークされるようになりました。これにより、まだ `pre_exec` に移行していない レガシーコードに対しても `unsafe` ブロックが必要となるよう強制されます。 + + + +なお、`before_exec` のクロージャには 非常に厳格な安全性の要件があります。詳細は [Safety セクション][pre-exec-safety] を参照してください。 + +[`pre_exec`]: ../../std/os/unix/process/trait.CommandExt.html#tymethod.pre_exec +[pre-exec-safety]: ../../std/os/unix/process/trait.CommandExt.html#notes-and-safety + + + +## 移行 + + + +2021 Edition と 2024 Edition の両方でコードをコンパイル可能にするには、これらの関数を `unsafe` ブロック内でのみ呼び出すようにする必要があります。 + + + +**⚠ 注意:** これらの関数を使用している箇所を 手動で確認し、必要に応じてコードを書き換えることが重要です。特に、`set_var` や `remove_var` は 複数のスレッドが動作している可能性がある場合には呼び出すべきではありません。環境変数の操作が必要な場合は、別の方法を検討することを推奨します。 + + + +[`deprecated_safe_2024`] リントは、これらの関数の呼び出しを自動的に `unsafe` ブロックで囲むよう修正します。これは `rust-2024-compatibility` リントグループに含まれており、次のコマンドを実行することで適用できます: + +```sh +cargo fix --edition +``` + + + +たとえば、以下のコード: + +```rust +fn main() { + std::env::set_var("FOO", "123"); +} +``` + + + +は、自動修正により次のように変更されます: + +```rust +fn main() { + // TODO: Audit that the environment access only happens in single-threaded code. + // TODO: 環境変数の操作がシングルスレッドのコード内でのみ行われていることを確認する。 + unsafe { std::env::set_var("FOO", "123") }; +} +``` + + + +ただし、この自動移行では、これらの関数が正しく使用されているかどうかを検証することはできません。よって、最終的な責任は開発者にあり手動でコードを確認する必要があります。 + + + +または、リントを手動で有効にして、これらの関数が呼び出されている箇所を特定することもできます。 + +```rust +// Add this to the root of your crate to do a manual migration. +// クレートのルートに以下を追加して手動移行をサポート +#![warn(deprecated_safe_2024)] +``` + +[`deprecated_safe_2024`]: ../../rustc/lints/listing/allowed-by-default.html#deprecated-safe-2024 From be3daa43cf6cadf536e85904b1c864b472dfe50a Mon Sep 17 00:00:00 2001 From: dyskinmel Date: Thu, 20 Feb 2025 13:15:40 -0600 Subject: [PATCH 3/6] =?UTF-8?q?Summary=E3=81=AB=E3=82=BF=E3=82=A4=E3=83=88?= =?UTF-8?q?=E3=83=AB=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SUMMARY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 9d66515..430fc44 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -130,7 +130,7 @@ - [標準ライブラリ](rust-2024/standard-library.md) - [Changes to the prelude](rust-2024/prelude.md) - [Add `IntoIterator` for `Box<[T]>`](rust-2024/intoiterator-box-slice.md) - - [Newly unsafe functions](rust-2024/newly-unsafe-functions.md) + - [unsafe 関数](rust-2024/newly-unsafe-functions.md) - [Cargo](rust-2024/cargo.md) - [Cargo: Rust-version aware resolver](rust-2024/cargo-resolver.md) - [Cargo: Table and key name consistency](rust-2024/cargo-table-key-names.md) From 6096c54041993145dca63640557119563f20efcd Mon Sep 17 00:00:00 2001 From: dyskinmel Date: Thu, 20 Feb 2025 16:35:18 -0600 Subject: [PATCH 4/6] =?UTF-8?q?=E3=83=AA=E3=83=B3=E3=82=AF=E3=82=92?= =?UTF-8?q?=E7=9B=B8=E5=AF=BE=E3=83=91=E3=82=B9=E3=81=8B=E3=82=89=E5=A4=89?= =?UTF-8?q?=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rust-2024/unsafe-attributes.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/rust-2024/unsafe-attributes.md b/src/rust-2024/unsafe-attributes.md index 16edf5a..0f5b17f 100644 --- a/src/rust-2024/unsafe-attributes.md +++ b/src/rust-2024/unsafe-attributes.md @@ -9,9 +9,9 @@ - [`link_section`] - [`no_mangle`] -[`export_name`]: ../../reference/abi.html#the-export_name-attribute -[`link_section`]: ../../reference/abi.html#the-link_section-attribute -[`no_mangle`]: ../../reference/abi.html#the-no_mangle-attribute +[`export_name`]: https://doc.rust-lang.org/reference/abi.html#the-export_name-attribute +[`link_section`]: https://doc.rust-lang.org/reference/abi.html#the-link_section-attribute +[`no_mangle`]: https://doc.rust-lang.org/reference/abi.html#the-no_mangle-attribute ## Details @@ -56,8 +56,8 @@ fn arduino_loop() { } ``` -[symbol mangling]: ../../rustc/symbol-mangling/index.html -[`unsafe_attr_outside_unsafe`]: ../../rustc/lints/listing/allowed-by-default.html#unsafe-attr-outside-unsafe +[symbol mangling]: https://doc.rust-lang.org/rustc/symbol-mangling/index.html +[`unsafe_attr_outside_unsafe`]: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#unsafe-attr-outside-unsafe ## Migration From 0723cf4c91fd9cd7415176c007e42abe3d27e6b0 Mon Sep 17 00:00:00 2001 From: dyskinmel Date: Mon, 24 Feb 2025 11:34:52 -0600 Subject: [PATCH 5/6] =?UTF-8?q?Summary=E7=BF=BB=E8=A8=B3=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/SUMMARY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 1802315..5d2ed41 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -129,7 +129,7 @@ - [標準ライブラリ](rust-2024/standard-library.md) - [Changes to the prelude](rust-2024/prelude.md) - [Add `IntoIterator` for `Box<[T]>`](rust-2024/intoiterator-box-slice.md) - - [unsafe 関数](rust-2024/newly-unsafe-functions.md) + - [新しい unsafe 関数](rust-2024/newly-unsafe-functions.md) - [Cargo](rust-2024/cargo.md) - [Cargo: Rust-version aware resolver](rust-2024/cargo-resolver.md) - [Cargo: Table and key name consistency](rust-2024/cargo-table-key-names.md) From d3dc5211391faf4754ba3eb6a1f24a5e620c912a Mon Sep 17 00:00:00 2001 From: dyskinmel Date: Tue, 25 Feb 2025 16:22:54 -0600 Subject: [PATCH 6/6] =?UTF-8?q?=E7=BF=BB=E8=A8=B3=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/rust-2024/newly-unsafe-functions.md | 149 ++++++++++++++---------- 1 file changed, 90 insertions(+), 59 deletions(-) diff --git a/src/rust-2024/newly-unsafe-functions.md b/src/rust-2024/newly-unsafe-functions.md index c0ef8db..d208799 100644 --- a/src/rust-2024/newly-unsafe-functions.md +++ b/src/rust-2024/newly-unsafe-functions.md @@ -1,128 +1,141 @@ - # unsafe 関数 - ## 概要 - - 以下の関数が新たに [`unsafe`] としてマークされました: -- [`std::env::set_var`] -- [`std::env::remove_var`] -- [`std::os::unix::process::CommandExt::before_exec`] + - [`std::env::set_var`] + - [`std::env::remove_var`] + - [`std::os::unix::process::CommandExt::before_exec`] + -[`std::os::unix::process::CommandExt::before_exec`]: ../../std/os/unix/process/trait.CommandExt.html#method.before_exec - +[`unsafe`]: https://doc.rust-lang.org/reference/unsafe-keyword.html#unsafe-functions-unsafe-fn +[`std::env::set_var`]: https://doc.rust-lang.org/std/env/fn.set_var.html +[`std::env::remove_var`]: https://doc.rust-lang.org/std/env/fn.remove_var.html +[`std::os::unix::process::CommandExt::before_exec`]: https://doc.rust-lang.org/std/os/unix/process/trait.CommandExt.html#method.before_exec - ## 詳細 - -標準ライブラリのいくつかの関数は、本来 `unsafe` であるべきであることが時間とともに明らかになってきました。しかし、関数を unsafe に変更すると、既存のコードでは `unsafe` ブロックで囲む必要が生じるため、破壊的変更となる可能性があります。そのため、これらの関数は 2024 Edition から `unsafe` にマークされますが、以前のエディションでは `unsafe` を要求しません。 +標準ライブラリのいくつかの関数は、本来 `unsafe` であるべきであることが時間とともに明らかになってきました。しかし、関数を unsafe に変更すると、既存のコードでは `unsafe` ブロックで囲む必要が生じるため、破壊的変更となる可能性があります。この破壊的変更を避けるため、これらの関数は 2024 Edition から `unsafe` にマークされますが、以前のエディションでは `unsafe` を要求しません。 - ### `std::env::{set_var, remove_var}` - -[`std::env::set_var`] や [`std::env::remove_var`] をマルチスレッド環境で呼び出すことは、一部のプラットフォームにおける プロセス環境の管理方法の制約により、安全でない可能性があります。標準ライブラリではこれらの関数をもともと安全な関数として定義していましたが、それが適切でなかったことが後に判明しました。 +[`std::env::set_var`] や [`std::env::remove_var`] をマルチスレッド環境で呼び出すことは、一部のプラットフォームにおけるプロセス環境の管理方法の制約により、安全でない可能性があります。標準ライブラリではこれらの関数をもともと安全な関数として定義していましたが、それが適切でなかったことが後に判明しました。 - -これらの関数を呼び出す際は 他のスレッドが動作していないことを確実に保証する必要があります。詳細は、各関数の [Safety] セクションを参照してください。 +これらの関数を呼び出す際は、他のスレッドが動作していないことを保証する必要があります。詳細は、各関数の [Safety] セクションを参照してください。 + + +[Safety]: https://doc.rust-lang.org/std/env/fn.set_var.html#safety - ### `std::os::unix::process::CommandExt::before_exec` - -[`std::os::unix::process::CommandExt::before_exec`] は、Unix 固有の関数であり、`exec` を呼び出す前にクロージャを実行するための手段を提供します。この関数は Rust 1.37 で非推奨となり、代わりに [`pre_exec`] が導入されました。`pre_exec` は同じ機能を提供しますが、明示的に `unsafe` としてマークされています。 +[`std::os::unix::process::CommandExt::before_exec`] は、Unix 固有の関数であり、`exec` を呼び出す前にクロージャを実行するための手段を提供します。この関数は Rust 1.37 で非推奨となり、代わりに [`pre_exec`] が導入されました。`pre_exec` は同じ機能を提供しますが、明示的に `unsafe` としてマークされました。 - -`before_exec` はすでに非推奨ですが、2024 Edition から `unsafe` として正しくマークされるようになりました。これにより、まだ `pre_exec` に移行していない レガシーコードに対しても `unsafe` ブロックが必要となるよう強制されます。 +`before_exec` はすでに非推奨ですが、2024 Edition から `unsafe` として正しくマークされるようになりました。これにより、まだ `pre_exec` に移行していない レガシーコードに対しても `unsafe` ブロックが必要となります。 - -なお、`before_exec` のクロージャには 非常に厳格な安全性の要件があります。詳細は [Safety セクション][pre-exec-safety] を参照してください。 - +なお、`before_exec` のクロージャには非常に厳格な安全性の要件があります。詳細は [Safety セクション][pre-exec-safety]を参照してください。 + + +[`pre_exec`]: https://doc.rust-lang.org/std/os/unix/process/trait.CommandExt.html#tymethod.pre_exec +[pre-exec-safety]: https://doc.rust-lang.org/std/os/unix/process/trait.CommandExt.html#notes-and-safety - ## 移行 - 2021 Edition と 2024 Edition の両方でコードをコンパイル可能にするには、これらの関数を `unsafe` ブロック内でのみ呼び出すようにする必要があります。 - -**⚠ 注意:** これらの関数を使用している箇所を 手動で確認し、必要に応じてコードを書き換えることが重要です。特に、`set_var` や `remove_var` は 複数のスレッドが動作している可能性がある場合には呼び出すべきではありません。環境変数の操作が必要な場合は、別の方法を検討することを推奨します。 +**⚠ 注意:** これらの関数を使用している箇所を手動で確認し、必要に応じてコードを関数が前提とする条件を満たすよう書き換えることが重要です。特に、`set_var` や `remove_var` は複数のスレッドが動作している可能性がある場合には呼び出すべきではありません。ユースケースを管理するために、環境変数とは異なる仕組みを使用する必要があるかもしれません。 - -[`deprecated_safe_2024`] リントは、これらの関数の呼び出しを自動的に `unsafe` ブロックで囲むよう修正します。これは `rust-2024-compatibility` リントグループに含まれており、次のコマンドを実行することで適用できます: +[`deprecated_safe_2024`] リントは両方のエディションでコンパイルできるように、これらの関数の呼び出しを自動的に `unsafe` ブロックで囲むよう修正します。これは `rust-2024-compatibility` リントグループに含まれており、`cargo fix --edition` コマンドを実行することで適用できます。Rust 2024 エディションに対応するようコードを移行するには、次のコマンドを実行します: ```sh cargo fix --edition ``` - -たとえば、以下のコード: +たとえば、以下のコードは: ```rust fn main() { @@ -130,37 +143,55 @@ fn main() { } ``` - -は、自動修正により次のように変更されます: - +自動修正により次のように変更されます: + + + +```rust +fn main() { // TODO: 環境変数の操作がシングルスレッドのコード内でのみ行われていることを確認する。 unsafe { std::env::set_var("FOO", "123") }; } ``` - ただし、この自動移行では、これらの関数が正しく使用されているかどうかを検証することはできません。よって、最終的な責任は開発者にあり手動でコードを確認する必要があります。 - または、リントを手動で有効にして、これらの関数が呼び出されている箇所を特定することもできます。 + +```rust +// 手動で移行を行うには、クレートのルートに以下を追加してください。 +#![warn(deprecated_safe_2024)] +``` + + + +[`deprecated_safe_2024`]: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#deprecated-safe-2024 \ No newline at end of file