Skip to content

Commit 85fb5cd

Browse files
Rollup merge of #81680 - camsteffen:primty, r=oli-obk
Refactor `PrimitiveTypeTable` for Clippy I removed `PrimitiveTypeTable` and added `PrimTy::ALL` and `PrimTy::from_name` in its place. This allows Clippy to use `PrimTy::from_name` for the `builtin_type_shadow` lint, and a `const` list of primitive types is deleted from Clippy code (the goal). All changes should be a little faster, if anything.
2 parents 747abb8 + f66115d commit 85fb5cd

File tree

9 files changed

+71
-80
lines changed

9 files changed

+71
-80
lines changed

compiler/rustc_hir/src/hir.rs

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

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

20842133
#[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};
@@ -1927,7 +1927,7 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19271927
self.r.trait_map.insert(id, traits);
19281928
}
19291929

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

19331933
std_path.push(Segment::from_ident(Ident::with_dummy_span(sym::std)));
@@ -2121,13 +2121,9 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
21212121
// The same fallback is used when `a` resolves to nothing.
21222122
PathResult::Module(ModuleOrUniformRoot::Module(_)) | PathResult::Failed { .. }
21232123
if (ns == TypeNS || path.len() > 1)
2124-
&& self
2125-
.r
2126-
.primitive_type_table
2127-
.primitive_types
2128-
.contains_key(&path[0].ident.name) =>
2124+
&& PrimTy::from_name(path[0].ident.name).is_some() =>
21292125
{
2130-
let prim = self.r.primitive_type_table.primitive_types[&path[0].ident.name];
2126+
let prim = PrimTy::from_name(path[0].ident.name).unwrap();
21312127
PartialRes::with_unresolved_segments(Res::PrimTy(prim), path.len() - 1)
21322128
}
21332129
PathResult::Module(ModuleOrUniformRoot::Module(module)) => {

compiler/rustc_resolve/src/late/diagnostics.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1212,8 +1212,8 @@ impl<'a: 'ast, 'ast> LateResolutionVisitor<'a, '_, 'ast> {
12121212
// Add primitive types to the mix
12131213
if filter_fn(Res::PrimTy(PrimTy::Bool)) {
12141214
names.extend(
1215-
self.r.primitive_type_table.primitive_types.iter().map(|(name, prim_ty)| {
1216-
TypoSuggestion::from_res(*name, Res::PrimTy(*prim_ty))
1215+
PrimTy::ALL.iter().map(|prim_ty| {
1216+
TypoSuggestion::from_res(prim_ty.name(), Res::PrimTy(*prim_ty))
12171217
}),
12181218
)
12191219
}

compiler/rustc_resolve/src/lib.rs

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

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

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

1287-
primitive_type_table: PrimitiveTypeTable::new(),
1288-
12891250
partial_res_map: Default::default(),
12901251
import_res_map: Default::default(),
12911252
label_res_map: Default::default(),
@@ -1994,9 +1955,9 @@ impl<'a> Resolver<'a> {
19941955
}
19951956

19961957
if ns == TypeNS {
1997-
if let Some(prim_ty) = self.primitive_type_table.primitive_types.get(&ident.name) {
1958+
if let Some(prim_ty) = PrimTy::from_name(ident.name) {
19981959
let binding =
1999-
(Res::PrimTy(*prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::root())
1960+
(Res::PrimTy(prim_ty), ty::Visibility::Public, DUMMY_SP, ExpnId::root())
20001961
.to_name_binding(self.arenas);
20011962
return Some(LexicalScopeBinding::Item(binding));
20021963
}

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 {

src/tools/clippy/clippy_lints/src/misc_early.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
use crate::utils::{constants, snippet_opt, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
1+
use crate::utils::{snippet_opt, span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then};
22
use rustc_ast::ast::{
33
BindingMode, Expr, ExprKind, GenericParamKind, Generics, Lit, LitFloatType, LitIntType, LitKind, Mutability,
44
NodeId, Pat, PatKind, UnOp,
55
};
66
use rustc_ast::visit::FnKind;
77
use rustc_data_structures::fx::FxHashMap;
88
use rustc_errors::Applicability;
9+
use rustc_hir::PrimTy;
910
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
1011
use rustc_middle::lint::in_external_macro;
1112
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -264,13 +265,12 @@ impl EarlyLintPass for MiscEarlyLints {
264265
fn check_generics(&mut self, cx: &EarlyContext<'_>, gen: &Generics) {
265266
for param in &gen.params {
266267
if let GenericParamKind::Type { .. } = param.kind {
267-
let name = param.ident.as_str();
268-
if constants::BUILTIN_TYPES.contains(&&*name) {
268+
if let Some(prim_ty) = PrimTy::from_name(param.ident.name) {
269269
span_lint(
270270
cx,
271271
BUILTIN_TYPE_SHADOW,
272272
param.ident.span,
273-
&format!("this generic shadows the built-in type `{}`", name),
273+
&format!("this generic shadows the built-in type `{}`", prim_ty.name()),
274274
);
275275
}
276276
}

src/tools/clippy/clippy_lints/src/utils/constants.rs

-13
This file was deleted.

src/tools/clippy/clippy_lints/src/utils/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub mod author;
88
pub mod camel_case;
99
pub mod comparisons;
1010
pub mod conf;
11-
pub mod constants;
1211
mod diagnostics;
1312
pub mod eager_or_lazy;
1413
pub mod higher;

0 commit comments

Comments
 (0)