Skip to content

Commit 7bec46a

Browse files
authored
Merge pull request #67 from TonalidadeHidrica/tyvar-behind-raw-pointer
「推論変数への生ポインタに対するメソッドのディスパッチ」を翻訳
2 parents d43afc8 + b4a4adc commit 7bec46a

File tree

3 files changed

+102
-2
lines changed

3 files changed

+102
-2
lines changed

TranslationTable.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
| growable | 伸張可能
109109
| guard | ガード
110110
| handle | ハンドル
111+
| hard error | ハードエラー
111112
| hash | ハッシュ
112113
| identifier | 識別子
113114
| immutable | イミュータブル

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
- [Path and module system changes](rust-2018/path-changes.md)
3636
- [トレイト関数の匿名パラメータの非推奨化](rust-2018/trait-fn-parameters.md)
3737
- [新しいキーワード](rust-2018/new-keywords.md)
38-
- [Method dispatch for raw pointers to inference variables](rust-2018/tyvar-behind-raw-pointer.md)
38+
- [推論変数への生ポインタに対するメソッドのディスパッチ](rust-2018/tyvar-behind-raw-pointer.md)
3939
- [Cargo changes](rust-2018/cargo.md)
4040

4141
## Rust 2021
Lines changed: 100 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,110 @@
1+
<!--
12
# Method dispatch for raw pointers to inference variables
3+
-->
24

5+
# 推論変数への生ポインタに対するメソッドのディスパッチ
6+
7+
<!--
38
## Summary
9+
-->
10+
11+
## 概要
412

13+
<!--
514
- The [`tyvar_behind_raw_pointer`][#46906] lint is now a hard error.
15+
-->
16+
17+
- リント [`tyvar_behind_raw_pointer`][#46906] はハードエラー[^1]になりました。
618

719
[#46906]: https://github.com/rust-lang/rust/issues/46906
820

21+
[^1]: (訳注) ハードエラーとは、`#[allow(...)]` などを使って、ユーザーが無効化することができないエラーのことです。
22+
例えば、ライフタイムに不整合があったとします。ユーザーが実際はそれが安全であると信じていても、そのようなコードをコンパイルすることは許されていません。 `#[allow(...)]` による上書きも不可能となっています。これがハードエラーです。
23+
一方、到達不能コード(たとえば関数の途中で `return` をしており、それ以降のコードは決して実行されないような状況)は、コンパイル自体は可能で、時には役に立つこともあるので、ユーザーが `#[allow(dead_code)]` と書くことで警告を抑制できます。これはハードエラーではありません。
24+
詳細は [rustc book の説明(英語)](https://rustc-dev-guide.rust-lang.org/diagnostics.html#lints-versus-fixed-diagnostics) もご参照ください。
25+
26+
<!--
927
## Details
28+
-->
29+
30+
## 詳細
31+
32+
<!--
33+
See Rust issue [#46906] for details.
34+
-->
35+
36+
詳細は Rust のイシュー [#46906] を参照してください。
37+
38+
> *訳注*:
39+
> 詳しく解説します。以下のプログラムをご覧ください。
40+
>
41+
> ```rust,ignore
42+
> let s = libc::getenv(k.as_ptr()) as *const _;
43+
> s.is_null()
44+
> ```
45+
>
46+
> 1行目の [`libc::getenv`] は、`*mut c_char` 型の生ポインタを返す関数です。
47+
> このポインタは `as` を使って `*const` ポインタに変換できますが、その際これが「何の型のポインタであるか」を `_` にて省略しています。
48+
>
49+
> 2行目では、`*const _` である `s` に対して `.is_null()` を呼び出しています。
50+
> 任意の型 `T` について、プリミティブ型 `*const T` は [`is_null`] という固有メソッドを持つので、ここで呼び出されるのはこの固有メソッドです。
51+
>
52+
> 問題はこの後です。
53+
> 現在はメソッド[^2]を呼べる型は[一部の型に限られて][methods]いますが、
54+
> 将来それを任意の型に拡張しようという[提案][arbitrary_self_types-tracking]が出ています。
55+
> この新機能は "arbitrary self types" (self の型の任意化)と呼ばれます。
56+
> しかし、これが導入されると困ったことが起きます。
57+
>
58+
> 次のような構造体があったとしましょう:
59+
> ```rust,ignore
60+
> #![feature(arbitrary_self_types)]
61+
>
62+
> struct MyType;
63+
>
64+
> impl MyType {
65+
> // この関数が問題
66+
> fn is_null(self: *mut Self) -> bool {
67+
> println!("?");
68+
> true
69+
> }
70+
> }
71+
> ```
72+
>
73+
> すると、最初のプログラムの2行目 `s.is_null` はどうなるでしょうか?
74+
> 変数 `s` はキャストによって `*const _` 、つまり「何かの型への生定数ポインタ」を意味していました。
75+
> そして今や、 `is_null` として呼び出せる関数は2つあります。
76+
> 1つは先程の `*const T` に対して実装された [`is_null`]、
77+
> もう一つは今 `*const MyType` に対して実装された `is_null` です。
78+
> つまり、メソッドの呼び出しに曖昧性が生じています。
79+
>
80+
> この問題の解決策は簡単です。キャスト後の型がどの定数ポインタになるのか明示すればよいです:
81+
>
82+
> ```rust,ignore
83+
> let s = libc::getenv(k.as_ptr()) as *const libc::c_char;
84+
> s.is_null()
85+
> ```
86+
>
87+
> こうすることで、`is_null` の候補は `*const T` だけになります。
88+
> `libc::c_char` は他のクレートで定義された型ですので、
89+
> この型に対して新しくメソッドが実装されることはなく、恒久的に曖昧性がなくなります。
90+
>
91+
> こうした理由から、 `*const _` や `*mut _` など、「未知の型への生ポインタ」に対してメソッドを呼び出すと、コンパイラがそれを検知するようになりました。
92+
> 最初は警告リントとして導入されましたが、Rust 2018 エディションでハードエラーに格上げされました。これが、本ページで説明されている変更点です。
93+
94+
[^2]: 関連関数のうち、第一引数が `self` であるものは、メソッド呼び出し演算子(`.`)を用いて呼び出すことができます。
95+
このような関連関数をメソッドと呼びます。
96+
`s.is_null()` と書くと、これは `s` に対してメソッド `is_null(...)` を呼び出していることになります。
97+
([参考](https://doc.rust-lang.org/reference/items/associated-items.html#methods))
98+
99+
[`libc::getenv`]: https://docs.rs/libc/0.2.107/i686-pc-windows-msvc/libc/fn.getenv.html
100+
[`is_null`]: https://doc.rust-lang.org/std/primitive.pointer.html#method.is_null-1
101+
[methods]: https://doc.rust-lang.org/reference/items/associated-items.html#methods
102+
[arbitrary_self_types-tracking]: https://github.com/rust-lang/rust/issues/44874
10103
11-
See Rust issue [#46906] for details.
104+
> *訳注*:
105+
> タイトルにある「推論変数」とは、英語では inference variable または existencial variable と呼ばれます。
106+
> Rust には型をある程度明示しなくても自動的に決定する機能(型推論)があります。
107+
> 型推論とは、非常に単純に説明すると、未知の型を変数、プログラム中から得られる手がかりを条件とみなした「型の連立方程式」を解く事に当たります。
108+
> 推論変数とは、この方程式における変数のことです。
109+
> 今回は `*const _`、つまり「未知の型 `_` に対する定数生ポインタ」が出てきています。この `_` が「推論変数」にあたります。
110+
> 詳しくは、[rustc book の型推論の説明 (英語)](https://rustc-dev-guide.rust-lang.org/type-inference.html#a-note-on-terminology)もご参照ください。

0 commit comments

Comments
 (0)