Skip to content
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

[2024] unsafe 関係の 3 ページを翻訳 #126

Open
wants to merge 6 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
6 changes: 3 additions & 3 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@
- [`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)
- [アンセーフな `extern` ブロック](rust-2024/unsafe-extern.md)
- [アンセーフなアトリビュート](rust-2024/unsafe-attributes.md)
- [`unsafe_op_in_unsafe_fn` 警告](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)
Expand Down
111 changes: 109 additions & 2 deletions src/rust-2024/unsafe-attributes.md
Original file line number Diff line number Diff line change
@@ -1,41 +1,103 @@
> **Rust Edition Guide は現在 Rust 2024 のアップデート作業に向けて翻訳作業中です。本ページはある時点での英語版をコピーしていますが、一部のリンクが動作しない場合や、最新情報が更新されていない場合があります。問題が発生した場合は、[原文(英語版)](https://doc.rust-lang.org/nightly/edition-guide/introduction.html)をご参照ください。**

<!--
# Unsafe attributes
-->

# アンセーフなアトリビュート

<!--
## Summary
-->

## 概要

<!--
- The following attributes must now be marked as `unsafe`:
-->
- 以下のアトリビュートに `unsafe` が必要になりました。
- [`export_name`]
- [`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
-->

## 詳細

<!--
Rust 1.82 added the ability in all editions to mark certain attributes as `unsafe` to indicate that they have soundness requirements that must be upheld.[^RFC3325] The syntax for an unsafe attribute looks like this:
-->

Rust 1.82 から全エディションにおいて、アトリビュートが健全性要件の遵守を要する場合に `unsafe` と書ける機能が追加されました。[^RFC3325]
アンセーフなアトリビュートは以下のように使用します。

<!--
```rust
// SAFETY: there is no other global function of this name
#[unsafe(no_mangle)]
pub fn example() {}
```
-->

```rust
// SAFETY: 同名のグローバル関数は他に存在しない
#[unsafe(no_mangle)]
pub fn example() {}
```

<!--
Marking the attribute with `unsafe` highlights that there are safety requirements that must be upheld that the compiler cannot verify on its own.
-->

アトリビュートが `unsafe` だと宣言することで、遵守されるべき安全性要件があって、それをコンパイラが保証できないことが明示できます。

<!--
Starting with the 2024 Edition, it is now required to mark these attributes as `unsafe`. The following section describes the safety requirements for these attributes.
-->

2024 エディションからは、上記のアトリビュートを `unsafe` とすることが必須となります。
実際に遵守すべき安全性要件を以下に説明します。

<!--
[^RFC3325]: See [RFC 3325](https://rust-lang.github.io/rfcs/3325-unsafe-attributes.html) for the original proposal.
-->

[^RFC3325]: 元の提案は [RFC 3325](https://rust-lang.github.io/rfcs/3325-unsafe-attributes.html) を参照のこと。

<!--
### Safety requirements
-->

### 安全性要件

<!--
The [`no_mangle`], [`export_name`], and [`link_section`] attributes influence the symbol names and linking behavior of items. Care must be taken to ensure that these attributes are used correctly.
-->

アトリビュート [`no_mangle`]、[`export_name`]、[`link_section`] はシンボル名とリンク動作に関わるため、細心の注意を払って使用しなくてはなりません。

<!--
Because the set of symbols across all linked libraries is a global namespace, there can be issues if there is a symbol name collision between libraries. Typically this isn't an issue for normally defined functions because [symbol mangling] helps ensure that the symbol name is unique. However, attributes like `export_name` can upset that assumption of uniqueness.
-->

リンクされるシンボルの名前空間は全ライブラリで共有なので、ライブラリ間でシンボル名が衝突すると問題が発生する可能性があります。
通常の方法で定義された関数は、[シンボルのマングリング]のおかげで基本的に名前の衝突を気にする必要はありませんが、`export_name` といったアトリビュートを使うとそうはいかなくなります。

<!--
For example, in previous editions the following crashes on most Unix-like platforms despite containing only safe code:
-->

例えば、以下のコードは何もアンセーフと書いていませんが、過去のエディションでは Unix 系の環境でクラッシュします。

```rust,no_run,edition2021
fn main() {
Expand All @@ -46,32 +108,77 @@ fn main() {
fn foo() -> usize { 1 }
```

<!--
In the 2024 Edition, it is now required to mark these attributes as unsafe to emphasize that it is required to ensure that the symbol is defined correctly:
-->

2024 エディションでは、上記のアトリビュートには `unsafe` をつける必要があり、シンボルが適切に定義される必要があることが明確になります。

<!--
```rust,edition2024
// SAFETY: There should only be a single definition of the loop symbol.
#[unsafe(export_name="loop")]
fn arduino_loop() {
// ...
}
```
-->

```rust,edition2024
// SAFETY: loop というシンボルの定義は以下のみ
#[unsafe(export_name="loop")]
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
-->

[シンボルのマングリング]: 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
-->

## 移行

<!--
The [`unsafe_attr_outside_unsafe`] lint can update these attributes to use the `unsafe(...)` format. 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:
-->

[`unsafe_attr_outside_unsafe`] リントで、上記のアトリビュートに `unsafe(...)` を自動付与できます。
このリントは、自動エディション移行に含まれる `rust-2024-compatibility` リントグループの一部です。
コードを Rust 2024 互換に移行するには、以下を実行します。

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

<!--
Just beware that this automatic migration will not be able to verify that these attributes are being used correctly. It is still your responsibility to manually review their usage.
-->

ただし、自動移行ツールはアトリビュートの使用方法が正しいことを検証するわけではありません。
それを保証するのは、変わらずプログラマの責任です。

<!--
Alternatively, you can manually enable the lint to find places where these attributes need to be updated.
-->

変更の必要のあるアトリビュートを検出するリントを手動で有効化することもできます。

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

```rust
// クレートのトップレベルに以下を追加すると手動移行できる
#![warn(unsafe_attr_outside_unsafe)]
```
94 changes: 92 additions & 2 deletions src/rust-2024/unsafe-extern.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,48 @@
> **Rust Edition Guide は現在 Rust 2024 のアップデート作業に向けて翻訳作業中です。本ページはある時点での英語版をコピーしていますが、一部のリンクが動作しない場合や、最新情報が更新されていない場合があります。問題が発生した場合は、[原文(英語版)](https://doc.rust-lang.org/nightly/edition-guide/introduction.html)をご参照ください。**

<!--
# Unsafe `extern` blocks
-->

# アンセーフな `extern` ブロック

<!--
## Summary
-->

## 概要

<!--
- [`extern` blocks] must now be marked with the `unsafe` keyword.
-->

- [`extern` ブロック]は `unsafe` として宣言される必要があります。

<!--
[`extern` blocks]: ../../reference/items/external-blocks.html
-->

[`extern` ブロック]: https://doc.rust-lang.org/reference/items/external-blocks.html

<!--
## Details
-->

## 詳細

<!--
Rust 1.82 added the ability in all editions to mark [`extern` blocks] with the `unsafe` keyword.[^RFC3484] Adding the `unsafe` keyword helps to emphasize that it is the responsibility of the author of the `extern` block to ensure that the signatures are correct. If the signatures are not correct, then it may result in undefined behavior.
-->

Rust 1.82 から、全エディションで [`extern` ブロック]を `unsafe` キーワードとともに宣言できるようになりました。[^RFC3484]
`unsafe` と書くことで、`extern` を書いた人にシグネチャが正しいことを保証する責任があることが明確になります。
シグネチャが正しくないと、未定義動作を引き起こしかねません。

<!--
The syntax for an unsafe `extern` block looks like this:
-->

以下に、アンセーフな `extern` ブロック構文の例を示します。

<!--
```rust
unsafe extern "C" {
// sqrt (from libm) may be called with any `f64`
Expand All @@ -29,28 +58,89 @@ unsafe extern "C" {
pub safe static IMPORTANT_BYTES: [u8; 256];
}
```
-->

```rust
unsafe extern "C" {
// (libm の) sqrt 関数は任意の `f64` 引数を取れる
pub safe fn sqrt(x: f64) -> f64;

// (libc の) strlen 関数は有効なポインタを要求するため、
// アンセーフな関数として宣言する
pub unsafe fn strlen(p: *const std::ffi::c_char) -> usize;

// safe も unsafe もない関数はデフォルトではアンセーフとなる
pub fn free(p: *mut core::ffi::c_void);

pub safe static IMPORTANT_BYTES: [u8; 256];
}
```

<!--
In addition to being able to mark an `extern` block as `unsafe`, you can also specify if individual items in the `extern` block are `safe` or `unsafe`. Items marked as `safe` can be used without an `unsafe` block.
-->

`extern` ブロックを `unsafe` とできるのに加え、`extern` ブロック内の個々のアイテムを `safe` か `unsafe` と明示できます。
`safe` のついたアイテムは、`unsafe` で囲まなくても使用できるようになります。

<!--
Starting with the 2024 Edition, it is now required to include the `unsafe` keyword on an `extern` block. This is intended to make it very clear that there are safety requirements that must be upheld by the extern definitions.
-->

2024 エディション以降、`extern` ブロックには必ず `unsafe` を付与する必要があります。
これにより、extern 定義が遵守すべき安全性要件が存在するということが明確になることが期待されます。

<!--
[^RFC3484]: See [RFC 3484](https://github.com/rust-lang/rfcs/blob/master/text/3484-unsafe-extern-blocks.md) for the original proposal.
-->

[^RFC3484]: 元の提案については [RFC 3484](https://github.com/rust-lang/rfcs/blob/master/text/3484-unsafe-extern-blocks.md) を参照のこと。

<!--
## Migration
-->

## 移行

<!--
The [`missing_unsafe_on_extern`] lint can update `extern` blocks to add the `unsafe` keyword. 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:
-->

[`missing_unsafe_on_extern`] リントで `extern` ブロックに `unsafe` を自動付与できます。
このリントは、自動エディション移行に含まれる `rust-2024-compatibility` リントグループの一部です。
コードを Rust 2024 互換に移行するには、以下を実行します。

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

<!--
Just beware that this automatic migration will not be able to verify that the signatures in the `extern` block are correct. It is still your responsibility to manually review their definition.
-->

ただし、自動移行ツールは、`extern` ブロックのシグネチャが正しいことを検証することはできません。
それを保証するのは、変わらずプログラマの責任です。

<!--
Alternatively, you can manually enable the lint to find places where there are `unsafe` blocks that need to be updated.
-->

変更の必要がある `unsafe` ブロックを検出するリントを手動で有効化することもできます。

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

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

<!--
[`missing_unsafe_on_extern`]: ../../rustc/lints/listing/allowed-by-default.html#missing-unsafe-on-extern
-->

[`missing_unsafe_on_extern`]: https://doc.rust-lang.org/rustc/lints/listing/allowed-by-default.html#missing-unsafe-on-extern
Loading