Skip to content

Commit fba747a

Browse files
committed
Refactor out PrimitiveTypeTable
1 parent b81f581 commit fba747a

File tree

6 files changed

+67
-62
lines changed

6 files changed

+67
-62
lines changed

compiler/rustc_hir/src/hir.rs

+49
Original file line numberDiff line numberDiff line change
@@ -2057,6 +2057,28 @@ pub enum PrimTy {
20572057
}
20582058

20592059
impl PrimTy {
2060+
/// All of the primitive types
2061+
pub const ALL: [Self; 17] = [
2062+
// any changes here should also be reflected in `PrimTy::from_name`
2063+
Self::Int(IntTy::I8),
2064+
Self::Int(IntTy::I16),
2065+
Self::Int(IntTy::I32),
2066+
Self::Int(IntTy::I64),
2067+
Self::Int(IntTy::I128),
2068+
Self::Int(IntTy::Isize),
2069+
Self::Uint(UintTy::U8),
2070+
Self::Uint(UintTy::U16),
2071+
Self::Uint(UintTy::U32),
2072+
Self::Uint(UintTy::U64),
2073+
Self::Uint(UintTy::U128),
2074+
Self::Uint(UintTy::Usize),
2075+
Self::Float(FloatTy::F32),
2076+
Self::Float(FloatTy::F64),
2077+
Self::Bool,
2078+
Self::Char,
2079+
Self::Str,
2080+
];
2081+
20602082
pub fn name_str(self) -> &'static str {
20612083
match self {
20622084
PrimTy::Int(i) => i.name_str(),
@@ -2078,6 +2100,33 @@ impl PrimTy {
20782100
PrimTy::Char => sym::char,
20792101
}
20802102
}
2103+
2104+
/// Returns the matching `PrimTy` for a `Symbol` such as "str" or "i32".
2105+
/// Returns `None` if no matching type is found.
2106+
pub fn from_name(name: Symbol) -> Option<Self> {
2107+
let ty = match name {
2108+
// any changes here should also be reflected in `PrimTy::ALL`
2109+
sym::i8 => Self::Int(IntTy::I8),
2110+
sym::i16 => Self::Int(IntTy::I16),
2111+
sym::i32 => Self::Int(IntTy::I32),
2112+
sym::i64 => Self::Int(IntTy::I64),
2113+
sym::i128 => Self::Int(IntTy::I128),
2114+
sym::isize => Self::Int(IntTy::Isize),
2115+
sym::u8 => Self::Uint(UintTy::U8),
2116+
sym::u16 => Self::Uint(UintTy::U16),
2117+
sym::u32 => Self::Uint(UintTy::U32),
2118+
sym::u64 => Self::Uint(UintTy::U64),
2119+
sym::u128 => Self::Uint(UintTy::U128),
2120+
sym::usize => Self::Uint(UintTy::Usize),
2121+
sym::f32 => Self::Float(FloatTy::F32),
2122+
sym::f64 => Self::Float(FloatTy::F64),
2123+
sym::bool => Self::Bool,
2124+
sym::char => Self::Char,
2125+
sym::str => Self::Str,
2126+
_ => return None,
2127+
};
2128+
Some(ty)
2129+
}
20812130
}
20822131

20832132
#[derive(Debug, HashStable_Generic)]

compiler/rustc_resolve/src/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_feature::BUILTIN_ATTRIBUTES;
99
use rustc_hir::def::Namespace::{self, *};
1010
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind};
1111
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
12+
use rustc_hir::PrimTy;
1213
use rustc_middle::bug;
1314
use rustc_middle::ty::{self, DefIdTree};
1415
use rustc_session::Session;
@@ -718,10 +719,9 @@ impl<'a> Resolver<'a> {
718719
}
719720
}
720721
Scope::BuiltinTypes => {
721-
let primitive_types = &this.primitive_type_table.primitive_types;
722-
suggestions.extend(primitive_types.iter().flat_map(|(name, prim_ty)| {
722+
suggestions.extend(PrimTy::ALL.iter().filter_map(|prim_ty| {
723723
let res = Res::PrimTy(*prim_ty);
724-
filter_fn(res).then_some(TypoSuggestion::from_res(*name, res))
724+
filter_fn(res).then_some(TypoSuggestion::from_res(prim_ty.name(), res))
725725
}))
726726
}
727727
}

compiler/rustc_resolve/src/late.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_errors::DiagnosticId;
2020
use rustc_hir::def::Namespace::{self, *};
2121
use rustc_hir::def::{self, CtorKind, DefKind, PartialRes, PerNS};
2222
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
23-
use rustc_hir::TraitCandidate;
23+
use rustc_hir::{PrimTy, TraitCandidate};
2424
use rustc_middle::{bug, span_bug};
2525
use rustc_session::lint;
2626
use rustc_span::symbol::{kw, sym, Ident, Symbol};
@@ -1921,7 +1921,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19211921
self.r.trait_map.insert(id, traits);
19221922
}
19231923

1924-
if self.r.primitive_type_table.primitive_types.contains_key(&path[0].ident.name) {
1924+
if PrimTy::from_name(path[0].ident.name).is_some() {
19251925
let mut std_path = Vec::with_capacity(1 + path.len());
19261926

19271927
std_path.push(Segment::from_ident(Ident::with_dummy_span(sym::std)));
@@ -2115,13 +2115,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
21152115
// The same fallback is used when `a` resolves to nothing.
21162116
PathResult::Module(ModuleOrUniformRoot::Module(_)) | PathResult::Failed { .. }
21172117
if (ns == TypeNS || path.len() > 1)
2118-
&& self
2119-
.r
2120-
.primitive_type_table
2121-
.primitive_types
2122-
.contains_key(&path[0].ident.name) =>
2118+
&& PrimTy::from_name(path[0].ident.name).is_some() =>
21232119
{
2124-
let prim = self.r.primitive_type_table.primitive_types[&path[0].ident.name];
2120+
let prim = PrimTy::from_name(path[0].ident.name).unwrap();
21252121
PartialRes::with_unresolved_segments(Res::PrimTy(prim), path.len() - 1)
21262122
}
21272123
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {

compiler/rustc_resolve/src/late/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1210,8 +1210,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
12101210
// Add primitive types to the mix
12111211
if filter_fn(Res::PrimTy(PrimTy::Bool)) {
12121212
names.extend(
1213-
self.r.primitive_type_table.primitive_types.iter().map(|(name, prim_ty)| {
1214-
TypoSuggestion::from_res(*name, Res::PrimTy(*prim_ty))
1213+
PrimTy::ALL.iter().map(|prim_ty| {
1214+
TypoSuggestion::from_res(prim_ty.name(), Res::PrimTy(*prim_ty))
12151215
}),
12161216
)
12171217
}

compiler/rustc_resolve/src/lib.rs

+4-43
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_arena::{DroplessArena, TypedArena};
2424
use rustc_ast::node_id::NodeMap;
2525
use rustc_ast::unwrap_or;
2626
use rustc_ast::visit::{self, Visitor};
27-
use rustc_ast::{self as ast, FloatTy, IntTy, NodeId, UintTy};
27+
use rustc_ast::{self as ast, NodeId};
2828
use rustc_ast::{Crate, CRATE_NODE_ID};
2929
use rustc_ast::{ItemKind, Path};
3030
use rustc_ast_lowering::ResolverAstLowering;
@@ -38,8 +38,7 @@ use rustc_hir::def::Namespace::*;
3838
use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
3939
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
4040
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
41-
use rustc_hir::PrimTy::{self, Bool, Char, Float, Int, Str, Uint};
42-
use rustc_hir::TraitCandidate;
41+
use rustc_hir::{PrimTy, TraitCandidate};
4342
use rustc_index::vec::IndexVec;
4443
use rustc_metadata::creader::{CStore, CrateLoader};
4544
use rustc_middle::hir::exports::ExportMap;
@@ -833,39 +832,6 @@ impl<'a> NameBinding<'a> {
833832
}
834833
}
835834

836-
/// Interns the names of the primitive types.
837-
///
838-
/// All other types are defined somewhere and possibly imported, but the primitive ones need
839-
/// special handling, since they have no place of origin.
840-
struct PrimitiveTypeTable {
841-
primitive_types: FxHashMap<Symbol, PrimTy>,
842-
}
843-
844-
impl PrimitiveTypeTable {
845-
fn new() -> PrimitiveTypeTable {
846-
let mut table = FxHashMap::default();
847-
848-
table.insert(sym::bool, Bool);
849-
table.insert(sym::char, Char);
850-
table.insert(sym::f32, Float(FloatTy::F32));
851-
table.insert(sym::f64, Float(FloatTy::F64));
852-
table.insert(sym::isize, Int(IntTy::Isize));
853-
table.insert(sym::i8, Int(IntTy::I8));
854-
table.insert(sym::i16, Int(IntTy::I16));
855-
table.insert(sym::i32, Int(IntTy::I32));
856-
table.insert(sym::i64, Int(IntTy::I64));
857-
table.insert(sym::i128, Int(IntTy::I128));
858-
table.insert(sym::str, Str);
859-
table.insert(sym::usize, Uint(UintTy::Usize));
860-
table.insert(sym::u8, Uint(UintTy::U8));
861-
table.insert(sym::u16, Uint(UintTy::U16));
862-
table.insert(sym::u32, Uint(UintTy::U32));
863-
table.insert(sym::u64, Uint(UintTy::U64));
864-
table.insert(sym::u128, Uint(UintTy::U128));
865-
Self { primitive_types: table }
866-
}
867-
}
868-
869835
#[derive(Debug, Default, Clone)]
870836
pub struct ExternPreludeEntry<'a> {
871837
extern_crate_item: Option<&'a NameBinding<'a>>,
@@ -911,9 +877,6 @@ pub struct Resolver<'a> {
911877
/// "self-confirming" import resolutions during import validation.
912878
unusable_binding: Option<&'a NameBinding<'a>>,
913879

914-
/// The idents for the primitive types.
915-
primitive_type_table: PrimitiveTypeTable,
916-
917880
/// Resolutions for nodes that have a single resolution.
918881
partial_res_map: NodeMap<PartialRes>,
919882
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
@@ -1283,8 +1246,6 @@ impl<'a> Resolver<'a> {
12831246
last_import_segment: false,
12841247
unusable_binding: None,
12851248

1286-
primitive_type_table: PrimitiveTypeTable::new(),
1287-
12881249
partial_res_map: Default::default(),
12891250
import_res_map: Default::default(),
12901251
label_res_map: Default::default(),
@@ -1993,9 +1954,9 @@ impl<'a> Resolver<'a> {
19931954
}
19941955

19951956
if ns == TypeNS {
1996-
if let Some(prim_ty) = self.primitive_type_table.primitive_types.get(&ident.name) {
1957+
if let Some(prim_ty) = PrimTy::from_name(ident.name) {
19971958
let binding =
1998-
(Res::PrimTy(*prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::root())
1959+
(Res::PrimTy(prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::root())
19991960
.to_name_binding(self.arenas);
20001961
return Some(LexicalScopeBinding::Item(binding));
20011962
}

compiler/rustc_resolve/src/macros.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use rustc_expand::expand::{AstFragment, Invocation, InvocationKind};
2121
use rustc_feature::is_builtin_attr_name;
2222
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
2323
use rustc_hir::def_id;
24+
use rustc_hir::PrimTy;
2425
use rustc_middle::middle::stability;
2526
use rustc_middle::ty;
2627
use rustc_session::lint::builtin::{SOFT_UNSTABLE, UNUSED_MACROS};
@@ -796,12 +797,10 @@ impl<'a> Resolver<'a> {
796797
}
797798
result
798799
}
799-
Scope::BuiltinTypes => {
800-
match this.primitive_type_table.primitive_types.get(&ident.name).cloned() {
801-
Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
802-
None => Err(Determinacy::Determined),
803-
}
804-
}
800+
Scope::BuiltinTypes => match PrimTy::from_name(ident.name) {
801+
Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
802+
None => Err(Determinacy::Determined),
803+
},
805804
};
806805

807806
match result {

0 commit comments

Comments
 (0)