From 0766039bd9e6b9f5e6334e84666f5be698d41fc3 Mon Sep 17 00:00:00 2001
From: nsfisis
#![allow(non_camel_case_types)]
-#![allow(dead_code)]
+ #![allow(non_camel_case_types)]
+#![allow(dead_code)]
-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;
+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;
では、普段単にboolと書いたとき、このboolは一体どこから来ているのか。rustc のソースを追ってみた。
@@ -119,20 +119,20 @@ struct str;
rustcはセルフホストされている (=rustc自身が Rust で書かれている) ので、boolやcharなどで適当に検索をかけてもノイズが多すぎて話にならない。 しかし、お誂え向きなことにi128/u128というコンパイラ自身が使うことがなさそうな型が存在するのでこれを使ってgit grepしてみる。
$ git grep "\bi128\b" | wc # i128
+ $ 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
165 程度であれば探すことができそうだ。今回は、クレート名を見ておおよその当たりをつけた。
- $ git grep "\bi128\b"
+ $ git grep "\bi128\b"
...
rustc_resolve/src/lib.rs: table.insert(sym::i128, Int(IntTy::I128));
...
@@ -141,36 +141,36 @@ rustc_resolve/src/lib.rs: table.insert(sym::i128, Int(IntTy::I128));
rustc_resolveというのはいかにも名前解決を担いそうなクレート名である。該当箇所を見てみる。
- /// 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 {
+ /// 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 {
primitive_types: FxHashMap<Symbol, PrimTy>,
}
-impl PrimitiveTypeTable {
-fn new() -> PrimitiveTypeTable {
-let mut table = FxHashMap::default();
+impl PrimitiveTypeTable {
+fn new() -> PrimitiveTypeTable {
+let mut table = FxHashMap::default();
-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.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 }
}
}
@@ -188,26 +188,26 @@ Self { primitive_types: table }
とある。次はこの struct の使用箇所を追う。追うと言っても使われている箇所は次の一箇所しかない。なお説明に不要な箇所は大きく削っている。
- /// 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,
+ /// 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,
ns: Namespace,
-// (略)
-) -> Option<LexicalScopeBinding<'a>> {
-// (略)
+// (略)
+) -> Option<LexicalScopeBinding<'a>> {
+// (略)
-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));
+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));
}
}
-None
+None
}
@@ -226,12 +226,12 @@ None
動作がわかったところで、例として次のコードを考える。
- #![allow(non_camel_case_types)]
+ #![allow(non_camel_case_types)]
-struct bool;
+struct bool;
-fn main() {
-let _: bool = bool;
+fn main() {
+let _: bool = bool;
}
--
cgit v1.2.3-70-g09d2