Refactor out PrimitiveTypeTable
This commit is contained in:
parent
b81f5811f9
commit
fba747a06e
6 changed files with 67 additions and 62 deletions
|
@ -2057,6 +2057,28 @@ pub enum PrimTy {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrimTy {
|
impl PrimTy {
|
||||||
|
/// All of the primitive types
|
||||||
|
pub const ALL: [Self; 17] = [
|
||||||
|
// any changes here should also be reflected in `PrimTy::from_name`
|
||||||
|
Self::Int(IntTy::I8),
|
||||||
|
Self::Int(IntTy::I16),
|
||||||
|
Self::Int(IntTy::I32),
|
||||||
|
Self::Int(IntTy::I64),
|
||||||
|
Self::Int(IntTy::I128),
|
||||||
|
Self::Int(IntTy::Isize),
|
||||||
|
Self::Uint(UintTy::U8),
|
||||||
|
Self::Uint(UintTy::U16),
|
||||||
|
Self::Uint(UintTy::U32),
|
||||||
|
Self::Uint(UintTy::U64),
|
||||||
|
Self::Uint(UintTy::U128),
|
||||||
|
Self::Uint(UintTy::Usize),
|
||||||
|
Self::Float(FloatTy::F32),
|
||||||
|
Self::Float(FloatTy::F64),
|
||||||
|
Self::Bool,
|
||||||
|
Self::Char,
|
||||||
|
Self::Str,
|
||||||
|
];
|
||||||
|
|
||||||
pub fn name_str(self) -> &'static str {
|
pub fn name_str(self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
PrimTy::Int(i) => i.name_str(),
|
PrimTy::Int(i) => i.name_str(),
|
||||||
|
@ -2078,6 +2100,33 @@ impl PrimTy {
|
||||||
PrimTy::Char => sym::char,
|
PrimTy::Char => sym::char,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Returns the matching `PrimTy` for a `Symbol` such as "str" or "i32".
|
||||||
|
/// Returns `None` if no matching type is found.
|
||||||
|
pub fn from_name(name: Symbol) -> Option<Self> {
|
||||||
|
let ty = match name {
|
||||||
|
// any changes here should also be reflected in `PrimTy::ALL`
|
||||||
|
sym::i8 => Self::Int(IntTy::I8),
|
||||||
|
sym::i16 => Self::Int(IntTy::I16),
|
||||||
|
sym::i32 => Self::Int(IntTy::I32),
|
||||||
|
sym::i64 => Self::Int(IntTy::I64),
|
||||||
|
sym::i128 => Self::Int(IntTy::I128),
|
||||||
|
sym::isize => Self::Int(IntTy::Isize),
|
||||||
|
sym::u8 => Self::Uint(UintTy::U8),
|
||||||
|
sym::u16 => Self::Uint(UintTy::U16),
|
||||||
|
sym::u32 => Self::Uint(UintTy::U32),
|
||||||
|
sym::u64 => Self::Uint(UintTy::U64),
|
||||||
|
sym::u128 => Self::Uint(UintTy::U128),
|
||||||
|
sym::usize => Self::Uint(UintTy::Usize),
|
||||||
|
sym::f32 => Self::Float(FloatTy::F32),
|
||||||
|
sym::f64 => Self::Float(FloatTy::F64),
|
||||||
|
sym::bool => Self::Bool,
|
||||||
|
sym::char => Self::Char,
|
||||||
|
sym::str => Self::Str,
|
||||||
|
_ => return None,
|
||||||
|
};
|
||||||
|
Some(ty)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, HashStable_Generic)]
|
#[derive(Debug, HashStable_Generic)]
|
||||||
|
|
|
@ -9,6 +9,7 @@ use rustc_feature::BUILTIN_ATTRIBUTES;
|
||||||
use rustc_hir::def::Namespace::{self, *};
|
use rustc_hir::def::Namespace::{self, *};
|
||||||
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind};
|
use rustc_hir::def::{self, CtorKind, CtorOf, DefKind, NonMacroAttrKind};
|
||||||
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
|
||||||
|
use rustc_hir::PrimTy;
|
||||||
use rustc_middle::bug;
|
use rustc_middle::bug;
|
||||||
use rustc_middle::ty::{self, DefIdTree};
|
use rustc_middle::ty::{self, DefIdTree};
|
||||||
use rustc_session::Session;
|
use rustc_session::Session;
|
||||||
|
@ -718,10 +719,9 @@ impl<'a> Resolver<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Scope::BuiltinTypes => {
|
Scope::BuiltinTypes => {
|
||||||
let primitive_types = &this.primitive_type_table.primitive_types;
|
suggestions.extend(PrimTy::ALL.iter().filter_map(|prim_ty| {
|
||||||
suggestions.extend(primitive_types.iter().flat_map(|(name, prim_ty)| {
|
|
||||||
let res = Res::PrimTy(*prim_ty);
|
let res = Res::PrimTy(*prim_ty);
|
||||||
filter_fn(res).then_some(TypoSuggestion::from_res(*name, res))
|
filter_fn(res).then_some(TypoSuggestion::from_res(prim_ty.name(), res))
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ use rustc_errors::DiagnosticId;
|
||||||
use rustc_hir::def::Namespace::{self, *};
|
use rustc_hir::def::Namespace::{self, *};
|
||||||
use rustc_hir::def::{self, CtorKind, DefKind, PartialRes, PerNS};
|
use rustc_hir::def::{self, CtorKind, DefKind, PartialRes, PerNS};
|
||||||
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
|
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
|
||||||
use rustc_hir::TraitCandidate;
|
use rustc_hir::{PrimTy, TraitCandidate};
|
||||||
use rustc_middle::{bug, span_bug};
|
use rustc_middle::{bug, span_bug};
|
||||||
use rustc_session::lint;
|
use rustc_session::lint;
|
||||||
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
||||||
|
@ -1921,7 +1921,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
||||||
self.r.trait_map.insert(id, traits);
|
self.r.trait_map.insert(id, traits);
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.r.primitive_type_table.primitive_types.contains_key(&path[0].ident.name) {
|
if PrimTy::from_name(path[0].ident.name).is_some() {
|
||||||
let mut std_path = Vec::with_capacity(1 + path.len());
|
let mut std_path = Vec::with_capacity(1 + path.len());
|
||||||
|
|
||||||
std_path.push(Segment::from_ident(Ident::with_dummy_span(sym::std)));
|
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> {
|
||||||
// The same fallback is used when `a` resolves to nothing.
|
// The same fallback is used when `a` resolves to nothing.
|
||||||
PathResult::Module(ModuleOrUniformRoot::Module(_)) | PathResult::Failed { .. }
|
PathResult::Module(ModuleOrUniformRoot::Module(_)) | PathResult::Failed { .. }
|
||||||
if (ns == TypeNS || path.len() > 1)
|
if (ns == TypeNS || path.len() > 1)
|
||||||
&& self
|
&& PrimTy::from_name(path[0].ident.name).is_some() =>
|
||||||
.r
|
|
||||||
.primitive_type_table
|
|
||||||
.primitive_types
|
|
||||||
.contains_key(&path[0].ident.name) =>
|
|
||||||
{
|
{
|
||||||
let prim = self.r.primitive_type_table.primitive_types[&path[0].ident.name];
|
let prim = PrimTy::from_name(path[0].ident.name).unwrap();
|
||||||
PartialRes::with_unresolved_segments(Res::PrimTy(prim), path.len() - 1)
|
PartialRes::with_unresolved_segments(Res::PrimTy(prim), path.len() - 1)
|
||||||
}
|
}
|
||||||
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
|
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {
|
||||||
|
|
|
@ -1210,8 +1210,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
|
||||||
// Add primitive types to the mix
|
// Add primitive types to the mix
|
||||||
if filter_fn(Res::PrimTy(PrimTy::Bool)) {
|
if filter_fn(Res::PrimTy(PrimTy::Bool)) {
|
||||||
names.extend(
|
names.extend(
|
||||||
self.r.primitive_type_table.primitive_types.iter().map(|(name, prim_ty)| {
|
PrimTy::ALL.iter().map(|prim_ty| {
|
||||||
TypoSuggestion::from_res(*name, Res::PrimTy(*prim_ty))
|
TypoSuggestion::from_res(prim_ty.name(), Res::PrimTy(*prim_ty))
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ use rustc_arena::{DroplessArena, TypedArena};
|
||||||
use rustc_ast::node_id::NodeMap;
|
use rustc_ast::node_id::NodeMap;
|
||||||
use rustc_ast::unwrap_or;
|
use rustc_ast::unwrap_or;
|
||||||
use rustc_ast::visit::{self, Visitor};
|
use rustc_ast::visit::{self, Visitor};
|
||||||
use rustc_ast::{self as ast, FloatTy, IntTy, NodeId, UintTy};
|
use rustc_ast::{self as ast, NodeId};
|
||||||
use rustc_ast::{Crate, CRATE_NODE_ID};
|
use rustc_ast::{Crate, CRATE_NODE_ID};
|
||||||
use rustc_ast::{ItemKind, Path};
|
use rustc_ast::{ItemKind, Path};
|
||||||
use rustc_ast_lowering::ResolverAstLowering;
|
use rustc_ast_lowering::ResolverAstLowering;
|
||||||
|
@ -38,8 +38,7 @@ use rustc_hir::def::Namespace::*;
|
||||||
use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
|
use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
|
||||||
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
|
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
|
||||||
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
|
use rustc_hir::definitions::{DefKey, DefPathData, Definitions};
|
||||||
use rustc_hir::PrimTy::{self, Bool, Char, Float, Int, Str, Uint};
|
use rustc_hir::{PrimTy, TraitCandidate};
|
||||||
use rustc_hir::TraitCandidate;
|
|
||||||
use rustc_index::vec::IndexVec;
|
use rustc_index::vec::IndexVec;
|
||||||
use rustc_metadata::creader::{CStore, CrateLoader};
|
use rustc_metadata::creader::{CStore, CrateLoader};
|
||||||
use rustc_middle::hir::exports::ExportMap;
|
use rustc_middle::hir::exports::ExportMap;
|
||||||
|
@ -833,39 +832,6 @@ impl<'a> NameBinding<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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();
|
|
||||||
|
|
||||||
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 }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct ExternPreludeEntry<'a> {
|
pub struct ExternPreludeEntry<'a> {
|
||||||
extern_crate_item: Option<&'a NameBinding<'a>>,
|
extern_crate_item: Option<&'a NameBinding<'a>>,
|
||||||
|
@ -911,9 +877,6 @@ pub struct Resolver<'a> {
|
||||||
/// "self-confirming" import resolutions during import validation.
|
/// "self-confirming" import resolutions during import validation.
|
||||||
unusable_binding: Option<&'a NameBinding<'a>>,
|
unusable_binding: Option<&'a NameBinding<'a>>,
|
||||||
|
|
||||||
/// The idents for the primitive types.
|
|
||||||
primitive_type_table: PrimitiveTypeTable,
|
|
||||||
|
|
||||||
/// Resolutions for nodes that have a single resolution.
|
/// Resolutions for nodes that have a single resolution.
|
||||||
partial_res_map: NodeMap<PartialRes>,
|
partial_res_map: NodeMap<PartialRes>,
|
||||||
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
|
/// Resolutions for import nodes, which have multiple resolutions in different namespaces.
|
||||||
|
@ -1283,8 +1246,6 @@ impl<'a> Resolver<'a> {
|
||||||
last_import_segment: false,
|
last_import_segment: false,
|
||||||
unusable_binding: None,
|
unusable_binding: None,
|
||||||
|
|
||||||
primitive_type_table: PrimitiveTypeTable::new(),
|
|
||||||
|
|
||||||
partial_res_map: Default::default(),
|
partial_res_map: Default::default(),
|
||||||
import_res_map: Default::default(),
|
import_res_map: Default::default(),
|
||||||
label_res_map: Default::default(),
|
label_res_map: Default::default(),
|
||||||
|
@ -1993,9 +1954,9 @@ impl<'a> Resolver<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ns == TypeNS {
|
if ns == TypeNS {
|
||||||
if let Some(prim_ty) = self.primitive_type_table.primitive_types.get(&ident.name) {
|
if let Some(prim_ty) = PrimTy::from_name(ident.name) {
|
||||||
let binding =
|
let binding =
|
||||||
(Res::PrimTy(*prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::root())
|
(Res::PrimTy(prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::root())
|
||||||
.to_name_binding(self.arenas);
|
.to_name_binding(self.arenas);
|
||||||
return Some(LexicalScopeBinding::Item(binding));
|
return Some(LexicalScopeBinding::Item(binding));
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,7 @@ use rustc_expand::expand::{AstFragment, Invocation, InvocationKind};
|
||||||
use rustc_feature::is_builtin_attr_name;
|
use rustc_feature::is_builtin_attr_name;
|
||||||
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
|
use rustc_hir::def::{self, DefKind, NonMacroAttrKind};
|
||||||
use rustc_hir::def_id;
|
use rustc_hir::def_id;
|
||||||
|
use rustc_hir::PrimTy;
|
||||||
use rustc_middle::middle::stability;
|
use rustc_middle::middle::stability;
|
||||||
use rustc_middle::ty;
|
use rustc_middle::ty;
|
||||||
use rustc_session::lint::builtin::{SOFT_UNSTABLE, UNUSED_MACROS};
|
use rustc_session::lint::builtin::{SOFT_UNSTABLE, UNUSED_MACROS};
|
||||||
|
@ -796,12 +797,10 @@ impl<'a> Resolver<'a> {
|
||||||
}
|
}
|
||||||
result
|
result
|
||||||
}
|
}
|
||||||
Scope::BuiltinTypes => {
|
Scope::BuiltinTypes => match PrimTy::from_name(ident.name) {
|
||||||
match this.primitive_type_table.primitive_types.get(&ident.name).cloned() {
|
|
||||||
Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
|
Some(prim_ty) => ok(Res::PrimTy(prim_ty), DUMMY_SP, this.arenas),
|
||||||
None => Err(Determinacy::Determined),
|
None => Err(Determinacy::Determined),
|
||||||
}
|
},
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match result {
|
match result {
|
||||||
|
|
Loading…
Add table
Reference in a new issue