diff options
Diffstat (limited to 'public/posts/2021-10-02/rust-where-are-primitive-types-from')
| -rw-r--r-- | public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html | 138 |
1 files changed, 69 insertions, 69 deletions
diff --git a/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html b/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html index e86d724..bc40aaa 100644 --- a/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html +++ b/public/posts/2021-10-02/rust-where-are-primitive-types-from/index.html @@ -10,7 +10,7 @@ <link rel="icon" type="image/svg+xml" href="/favicon.svg"> <title>Rust のプリミティブ型はどこからやって来るか | REPL: Rest-Eat-Program Loop</title> <link rel="stylesheet" href="/style.css?h=17cf97a767ec5fb6e64967729f40f30a"> - <link rel="stylesheet" href="/hl.css?h=208c52e3b7c9db1cad782c5d30b4698f"> + <link rel="stylesheet" href="/hl.css?h=340e65ffd5c17713efc9107c06304f7b"> </head> <body class="single"> <header class="header"> @@ -65,26 +65,26 @@ Rust において、プリミティブ型の名前は予約語でない。したがって、次のコードは合法である。 </p> - <pre class="highlight" language="rust" linenumbering="unnumbered"><code>#![allow(non_camel_case_types)] -#![allow(dead_code)] + <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"><span class="hljs-meta">#![allow(non_camel_case_types)]</span> +<span class="hljs-meta">#![allow(dead_code)]</span> -struct bool; -struct char; -struct i8; -struct i16; -struct i32; -struct i64; -struct i128; -struct isize; -struct u8; -struct u16; -struct u32; -struct u64; -struct u128; -struct usize; -struct f32; -struct f64; -struct str;</code></pre> +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">bool</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">char</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i8</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i16</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i32</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i64</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">i128</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">isize</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u8</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u16</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u32</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u64</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">u128</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">usize</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">f32</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">f64</span>; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">str</span>;</code></pre> <p> では、普段単に<code>bool</code>と書いたとき、この<code>bool</code>は一体どこから来ているのか。rustc のソースを追ってみた。 @@ -119,20 +119,20 @@ struct str;</code></pre> <code>rustc</code>はセルフホストされている (=<code>rustc</code>自身が Rust で書かれている) ので、<code>bool</code>や<code>char</code>などで適当に検索をかけてもノイズが多すぎて話にならない。 しかし、お誂え向きなことに<code>i128</code>/<code>u128</code>というコンパイラ自身が使うことがなさそうな型が存在するのでこれを使って<code>git grep</code>してみる。 </p> - <pre class="monospaced highlight"><code>$ git grep "\bi128\b" | wc # i128 + <pre class="monospaced highlight"><code>$ git grep "\bi128\b" | wc # i128 165 1069 15790 -$ git grep "\bu128\b" | wc # u128 +$ git grep "\bu128\b" | wc # u128 293 2127 26667 -$ git grep "\bbool\b" | wc # cf. bool の結果 +$ git grep "\bbool\b" | wc # cf. bool の結果 3563 23577 294659</code></pre> <p> 165 程度であれば探すことができそうだ。今回は、クレート名を見ておおよその当たりをつけた。 </p> - <pre class="monospaced highlight"><code>$ git grep "\bi128\b" + <pre class="monospaced highlight"><code>$ git grep "\bi128\b" ... rustc_resolve/src/lib.rs: table.insert(sym::i128, Int(IntTy::I128)); ...</code></pre> @@ -141,36 +141,36 @@ rustc_resolve/src/lib.rs: table.insert(sym::i128, Int(IntTy::I128)); <code>rustc_resolve</code>というのはいかにも名前解決を担いそうなクレート名である。該当箇所を見てみる。 </p> - <pre class="highlight" language="rust" linenumbering="unnumbered"><code>/// Interns the names of the primitive types. -/// -/// All other types are defined somewhere and possibly imported, but the primitive ones need -/// special handling, since they have no place of origin. -struct PrimitiveTypeTable { + <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"><span class="hljs-comment">/// Interns the names of the primitive types.</span> +<span class="hljs-comment">///</span> +<span class="hljs-comment">/// All other types are defined somewhere and possibly imported, but the primitive ones need</span> +<span class="hljs-comment">/// special handling, since they have no place of origin.</span> +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">PrimitiveTypeTable</span> { primitive_types: FxHashMap<Symbol, PrimTy>, } -impl PrimitiveTypeTable { -fn new() -> PrimitiveTypeTable { -let mut table = FxHashMap::default(); +<span class="hljs-keyword">impl</span> <span class="hljs-title class_">PrimitiveTypeTable</span> { +<span class="hljs-keyword">fn</span> <span class="hljs-title function_">new</span>() <span class="hljs-punctuation">-></span> PrimitiveTypeTable { +<span class="hljs-keyword">let</span> <span class="hljs-keyword">mut </span><span class="hljs-variable">table</span> = FxHashMap::<span class="hljs-title function_ invoke__">default</span>(); -table.insert(sym::bool, Bool); -table.insert(sym::char, Char); -table.insert(sym::f32, Float(FloatTy::F32)); -table.insert(sym::f64, Float(FloatTy::F64)); -table.insert(sym::isize, Int(IntTy::Isize)); -table.insert(sym::i8, Int(IntTy::I8)); -table.insert(sym::i16, Int(IntTy::I16)); -table.insert(sym::i32, Int(IntTy::I32)); -table.insert(sym::i64, Int(IntTy::I64)); -table.insert(sym::i128, Int(IntTy::I128)); -table.insert(sym::str, Str); -table.insert(sym::usize, Uint(UintTy::Usize)); -table.insert(sym::u8, Uint(UintTy::U8)); -table.insert(sym::u16, Uint(UintTy::U16)); -table.insert(sym::u32, Uint(UintTy::U32)); -table.insert(sym::u64, Uint(UintTy::U64)); -table.insert(sym::u128, Uint(UintTy::U128)); -Self { primitive_types: table } +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">bool</span>, Bool); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">char</span>, Char); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">f32</span>, <span class="hljs-title function_ invoke__">Float</span>(FloatTy::F32)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">f64</span>, <span class="hljs-title function_ invoke__">Float</span>(FloatTy::F64)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">isize</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::Isize)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i8</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I8)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i16</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I16)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i32</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I32)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i64</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I64)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">i128</span>, <span class="hljs-title function_ invoke__">Int</span>(IntTy::I128)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">str</span>, Str); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">usize</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::Usize)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u8</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U8)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u16</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U16)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u32</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U32)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u64</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U64)); +table.<span class="hljs-title function_ invoke__">insert</span>(sym::<span class="hljs-type">u128</span>, <span class="hljs-title function_ invoke__">Uint</span>(UintTy::U128)); +<span class="hljs-keyword">Self</span> { primitive_types: table } } }</code></pre> @@ -188,26 +188,26 @@ Self { primitive_types: table } とある。次はこの struct の使用箇所を追う。追うと言っても使われている箇所は次の一箇所しかない。なお説明に不要な箇所は大きく削っている。 </p> - <pre class="highlight" language="rust" linenumbering="unnumbered"><code> /// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope. -/// (略) -fn resolve_ident_in_lexical_scope( -&mut self, -mut ident: Ident, + <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"> <span class="hljs-comment">/// This resolves the identifier `ident` in the namespace `ns` in the current lexical scope.</span> +<span class="hljs-comment">/// (略)</span> +<span class="hljs-keyword">fn</span> <span class="hljs-title function_">resolve_ident_in_lexical_scope</span>( +&<span class="hljs-keyword">mut</span> <span class="hljs-keyword">self</span>, +<span class="hljs-keyword">mut</span> ident: Ident, ns: Namespace, -// (略) -) -> Option<LexicalScopeBinding<'a>> { -// (略) +<span class="hljs-comment">// (略)</span> +) <span class="hljs-punctuation">-></span> <span class="hljs-type">Option</span><LexicalScopeBinding<<span class="hljs-symbol">'a</span>>> { +<span class="hljs-comment">// (略)</span> -if ns == TypeNS { -if let Some(prim_ty) = self.primitive_type_table.primitive_types.get(&ident.name) { -let binding = -(Res::PrimTy(*prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::root()) -.to_name_binding(self.arenas); -return Some(LexicalScopeBinding::Item(binding)); +<span class="hljs-keyword">if</span> ns == TypeNS { +<span class="hljs-keyword">if</span> <span class="hljs-keyword">let</span> <span class="hljs-variable">Some</span>(prim_ty) = <span class="hljs-keyword">self</span>.primitive_type_table.primitive_types.<span class="hljs-title function_ invoke__">get</span>(&ident.name) { +<span class="hljs-keyword">let</span> <span class="hljs-variable">binding</span> = +(Res::<span class="hljs-title function_ invoke__">PrimTy</span>(*prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::<span class="hljs-title function_ invoke__">root</span>()) +.<span class="hljs-title function_ invoke__">to_name_binding</span>(<span class="hljs-keyword">self</span>.arenas); +<span class="hljs-keyword">return</span> <span class="hljs-title function_ invoke__">Some</span>(LexicalScopeBinding::<span class="hljs-title function_ invoke__">Item</span>(binding)); } } -None +<span class="hljs-literal">None</span> }</code></pre> <p> @@ -226,12 +226,12 @@ None 動作がわかったところで、例として次のコードを考える。 </p> - <pre class="highlight" language="rust" linenumbering="unnumbered"><code>#![allow(non_camel_case_types)] + <pre class="highlight" language="rust" linenumbering="unnumbered"><code class="highlight"><span class="hljs-meta">#![allow(non_camel_case_types)]</span> -struct bool; +<span class="hljs-keyword">struct</span> <span class="hljs-title class_">bool</span>; -fn main() { -let _: bool = bool; +<span class="hljs-keyword">fn</span> <span class="hljs-title function_">main</span>() { +<span class="hljs-keyword">let</span> <span class="hljs-variable">_</span>: <span class="hljs-type">bool</span> = <span class="hljs-type">bool</span>; }</code></pre> <p> |
