Skip to content

Preintern symbols #4105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ pub mod zero_div_zero;
// end lints modules, do not remove this comment, it’s used in `update_lints`

pub use crate::utils::conf::Conf;
pub use crate::utils::sym::PREINTERNED_SYMBOLS;

mod reexport {
crate use syntax::ast::Name;
Expand Down
158 changes: 79 additions & 79 deletions clippy_lints/src/utils/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,116 @@

use super::sym::{self, *};
use lazy_static::lazy_static;
use syntax::symbol::Symbol;
use syntax::symbol::{keywords, Symbol};

lazy_static! {
pub static ref ANY_TRAIT: [Symbol; 3] = [*std, *any, *Any];
pub static ref ANY_TRAIT: [Symbol; 3] = [std, any, *Any];
pub static ref ARC: [Symbol; 3] = [*alloc, *sync, *Arc];
pub static ref ASMUT_TRAIT: [Symbol; 3] = [*core, *convert, *sym::AsMut];
pub static ref ASREF_TRAIT: [Symbol; 3] = [*core, *convert, *sym::AsRef];
pub static ref BEGIN_PANIC: [Symbol; 3] = [*std, *panicking, *begin_panic];
pub static ref BEGIN_PANIC_FMT: [Symbol; 3] = [*std, *panicking, *begin_panic_fmt];
pub static ref ASMUT_TRAIT: [Symbol; 3] = [core, convert, *sym::AsMut];
pub static ref ASREF_TRAIT: [Symbol; 3] = [core, convert, *sym::AsRef];
pub static ref BEGIN_PANIC: [Symbol; 3] = [std, *panicking, *begin_panic];
pub static ref BEGIN_PANIC_FMT: [Symbol; 3] = [std, *panicking, *begin_panic_fmt];
pub static ref BINARY_HEAP: [Symbol; 4] = [*alloc, *collections, *binary_heap, *BinaryHeap];
pub static ref BORROW_TRAIT: [Symbol; 3] = [*core, *borrow, *Borrow];
pub static ref BORROW_TRAIT: [Symbol; 3] = [core, *borrow, *Borrow];
pub static ref BTREEMAP: [Symbol; 5] = [*alloc, *collections, *btree, *map, *BTreeMap];
pub static ref BTREEMAP_ENTRY: [Symbol; 5] = [*alloc, *collections, *btree, *map, *Entry];
pub static ref BTREESET: [Symbol; 5] = [*alloc, *collections, *btree, *set, *BTreeSet];
pub static ref CLONE_TRAIT: [Symbol; 3] = [*core, *clone, *sym::Clone];
pub static ref CLONE_TRAIT_METHOD: [Symbol; 4] = [*core, *clone, *sym::Clone, *clone];
pub static ref CMP_MAX: [Symbol; 3] = [*core, *cmp, *max];
pub static ref CMP_MIN: [Symbol; 3] = [*core, *cmp, *min];
pub static ref CLONE_TRAIT: [Symbol; 3] = [core, clone, *sym::Clone];
pub static ref CLONE_TRAIT_METHOD: [Symbol; 4] = [core, clone, *sym::Clone, clone];
pub static ref CMP_MAX: [Symbol; 3] = [core, *cmp, *max];
pub static ref CMP_MIN: [Symbol; 3] = [core, *cmp, *min];
pub static ref COW: [Symbol; 3] = [*alloc, *borrow, *Cow];
pub static ref CSTRING_NEW: [Symbol; 5] = [*std, *ffi, *c_str, *CString, *new];
pub static ref DEFAULT_TRAIT: [Symbol; 3] = [*core, *default, *sym::Default];
pub static ref DEFAULT_TRAIT_METHOD: [Symbol; 4] = [*core, *default, *sym::Default, *default];
pub static ref DEREF_TRAIT_METHOD: [Symbol; 5] = [*core, *ops, *deref, *Deref, *deref];
pub static ref DISPLAY_FMT_METHOD: [Symbol; 4] = [*core, *fmt, *Display, *fmt];
pub static ref DOUBLE_ENDED_ITERATOR: [Symbol; 4] = [*core, *iter, *traits, *sym::DoubleEndedIterator];
pub static ref DROP: [Symbol; 3] = [*core, *mem, *drop];
pub static ref DROP_TRAIT: [Symbol; 4] = [*core, *ops, *drop, *sym::Drop];
pub static ref DURATION: [Symbol; 3] = [*core, *time, *Duration];
pub static ref CSTRING_NEW: [Symbol; 5] = [std, *ffi, *c_str, *CString, *new];
pub static ref DEFAULT_TRAIT: [Symbol; 3] = [core, *default, *sym::Default];
pub static ref DEFAULT_TRAIT_METHOD: [Symbol; 4] = [core, *default, *sym::Default, *default];
pub static ref DEREF_TRAIT_METHOD: [Symbol; 5] = [core, ops, *deref, *Deref, *deref];
pub static ref DISPLAY_FMT_METHOD: [Symbol; 4] = [core, *fmt, *Display, *fmt];
pub static ref DOUBLE_ENDED_ITERATOR: [Symbol; 4] = [core, iter, *traits, *sym::DoubleEndedIterator];
pub static ref DROP: [Symbol; 3] = [core, *mem, *drop];
pub static ref DROP_TRAIT: [Symbol; 4] = [core, ops, *drop, *sym::Drop];
pub static ref DURATION: [Symbol; 3] = [core, *time, *Duration];
pub static ref EARLY_CONTEXT: [Symbol; 4] = [*rustc, *lint, *context, *EarlyContext];
pub static ref FMT_ARGUMENTS_NEWV1: [Symbol; 4] = [*core, *fmt, *Arguments, *new_v1];
pub static ref FMT_ARGUMENTS_NEWV1FORMATTED: [Symbol; 4] = [*core, *fmt, *Arguments, *new_v1_formatted];
pub static ref FROM_FROM: [Symbol; 4] = [*core, *convert, *sym::From, *from];
pub static ref FROM_TRAIT: [Symbol; 3] = [*core, *convert, *sym::From];
pub static ref FMT_ARGUMENTS_NEWV1: [Symbol; 4] = [core, *fmt, *Arguments, *new_v1];
pub static ref FMT_ARGUMENTS_NEWV1FORMATTED: [Symbol; 4] = [core, *fmt, *Arguments, *new_v1_formatted];
pub static ref FROM_FROM: [Symbol; 4] = [core, convert, *sym::From, from];
pub static ref FROM_TRAIT: [Symbol; 3] = [core, convert, *sym::From];
pub static ref HASH: [Symbol; 2] = [*hash, *Hash];
pub static ref HASHMAP: [Symbol; 5] = [*std, *collections, *hash, *map, *HashMap];
pub static ref HASHMAP_ENTRY: [Symbol; 5] = [*std, *collections, *hash, *map, *Entry];
pub static ref HASHSET: [Symbol; 5] = [*std, *collections, *hash, *set, *HashSet];
pub static ref INDEX: [Symbol; 3] = [*core, *ops, *Index];
pub static ref INDEX_MUT: [Symbol; 3] = [*core, *ops, *IndexMut];
pub static ref INIT: [Symbol; 4] = [*core, *intrinsics, *empty_symbol, *init];
pub static ref INTO: [Symbol; 3] = [*core, *convert, *sym::Into];
pub static ref INTO_ITERATOR: [Symbol; 5] = [*core, *iter, *traits, *collect, *sym::IntoIterator];
pub static ref IO_READ: [Symbol; 3] = [*std, *io, *Read];
pub static ref IO_WRITE: [Symbol; 3] = [*std, *io, *Write];
pub static ref ITERATOR: [Symbol; 5] = [*core, *iter, *traits, *iterator, *sym::Iterator];
pub static ref HASHMAP: [Symbol; 5] = [std, *collections, *hash, *map, *HashMap];
pub static ref HASHMAP_ENTRY: [Symbol; 5] = [std, *collections, *hash, *map, *Entry];
pub static ref HASHSET: [Symbol; 5] = [std, *collections, *hash, *set, *HashSet];
pub static ref INDEX: [Symbol; 3] = [core, ops, *Index];
pub static ref INDEX_MUT: [Symbol; 3] = [core, ops, *IndexMut];
pub static ref INIT: [Symbol; 4] = [core, intrinsics, keywords::Invalid.name(), *init];
pub static ref INTO: [Symbol; 3] = [core, convert, *sym::Into];
pub static ref INTO_ITERATOR: [Symbol; 5] = [core, iter, *traits, *collect, *sym::IntoIterator];
pub static ref IO_READ: [Symbol; 3] = [std, *io, *Read];
pub static ref IO_WRITE: [Symbol; 3] = [std, *io, *Write];
pub static ref ITERATOR: [Symbol; 5] = [core, iter, *traits, *iterator, *sym::Iterator];
pub static ref LATE_CONTEXT: [Symbol; 4] = [*rustc, *lint, *context, *LateContext];
pub static ref LINKED_LIST: [Symbol; 4] = [*alloc, *collections, *linked_list, *LinkedList];
pub static ref LINT: [Symbol; 3] = [*rustc, *lint, *Lint];
pub static ref LINT_PASS: [Symbol; 3] = [*rustc, *lint, *LintPass];
pub static ref MEM_DISCRIMINANT: [Symbol; 3] = [*core, *mem, *discriminant];
pub static ref MEM_FORGET: [Symbol; 3] = [*core, *mem, *forget];
pub static ref MEM_REPLACE: [Symbol; 3] = [*core, *mem, *replace];
pub static ref MEM_UNINIT: [Symbol; 3] = [*core, *mem, *uninitialized];
pub static ref MEM_ZEROED: [Symbol; 3] = [*core, *mem, *zeroed];
pub static ref MUTEX: [Symbol; 4] = [*std, *sync, *mutex, *Mutex];
pub static ref OPEN_OPTIONS: [Symbol; 3] = [*std, *fs, *OpenOptions];
pub static ref OPS_MODULE: [Symbol; 2] = [*core, *ops];
pub static ref OPTION: [Symbol; 3] = [*core, *option, *sym::Option];
pub static ref OPTION_NONE: [Symbol; 4] = [*core, *option, *sym::Option, *sym::None];
pub static ref OPTION_SOME: [Symbol; 4] = [*core, *option, *sym::Option, *sym::Some];
pub static ref ORD: [Symbol; 3] = [*core, *cmp, *sym::Ord];
pub static ref OS_STRING: [Symbol; 4] = [*std, *ffi, *os_str, *OsString];
pub static ref OS_STR_TO_OS_STRING: [Symbol; 5] = [*std, *ffi, *os_str, *OsStr, *to_os_string];
pub static ref PARTIAL_ORD: [Symbol; 3] = [*core, *cmp, *sym::PartialOrd];
pub static ref PATH: [Symbol; 3] = [*std, *path, *Path];
pub static ref PATH_BUF: [Symbol; 3] = [*std, *path, *PathBuf];
pub static ref PATH_TO_PATH_BUF: [Symbol; 4] = [*std, *path, *Path, *to_path_buf];
pub static ref MEM_DISCRIMINANT: [Symbol; 3] = [core, *mem, *discriminant];
pub static ref MEM_FORGET: [Symbol; 3] = [core, *mem, *forget];
pub static ref MEM_REPLACE: [Symbol; 3] = [core, *mem, *replace];
pub static ref MEM_UNINIT: [Symbol; 3] = [core, *mem, *uninitialized];
pub static ref MEM_ZEROED: [Symbol; 3] = [core, *mem, *zeroed];
pub static ref MUTEX: [Symbol; 4] = [std, *sync, *mutex, *Mutex];
pub static ref OPEN_OPTIONS: [Symbol; 3] = [std, *fs, *OpenOptions];
pub static ref OPS_MODULE: [Symbol; 2] = [core, ops];
pub static ref OPTION: [Symbol; 3] = [core, option, *sym::Option];
pub static ref OPTION_NONE: [Symbol; 4] = [core, option, *sym::Option, *sym::None];
pub static ref OPTION_SOME: [Symbol; 4] = [core, option, *sym::Option, *sym::Some];
pub static ref ORD: [Symbol; 3] = [core, *cmp, *sym::Ord];
pub static ref OS_STRING: [Symbol; 4] = [std, *ffi, *os_str, *OsString];
pub static ref OS_STR_TO_OS_STRING: [Symbol; 5] = [std, *ffi, *os_str, *OsStr, *to_os_string];
pub static ref PARTIAL_ORD: [Symbol; 3] = [core, *cmp, *sym::PartialOrd];
pub static ref PATH: [Symbol; 3] = [std, path, *Path];
pub static ref PATH_BUF: [Symbol; 3] = [std, path, *PathBuf];
pub static ref PATH_TO_PATH_BUF: [Symbol; 4] = [std, path, *Path, *to_path_buf];
pub static ref PTR_NULL: [Symbol; 2] = [*ptr, *null];
pub static ref PTR_NULL_MUT: [Symbol; 2] = [*ptr, *null_mut];
pub static ref RANGE: [Symbol; 3] = [*core, *ops, *Range];
pub static ref RANGE_ARGUMENT_TRAIT: [Symbol; 3] = [*core, *ops, *RangeBounds];
pub static ref RANGE_FROM: [Symbol; 3] = [*core, *ops, *RangeFrom];
pub static ref RANGE_FROM_STD: [Symbol; 3] = [*std, *ops, *RangeFrom];
pub static ref RANGE_FULL: [Symbol; 3] = [*core, *ops, *RangeFull];
pub static ref RANGE_FULL_STD: [Symbol; 3] = [*std, *ops, *RangeFull];
pub static ref RANGE_INCLUSIVE_NEW: [Symbol; 4] = [*core, *ops, *RangeInclusive, *new];
pub static ref RANGE_INCLUSIVE_STD_NEW: [Symbol; 4] = [*std, *ops, *RangeInclusive, *new];
pub static ref RANGE_STD: [Symbol; 3] = [*std, *ops, *Range];
pub static ref RANGE_TO: [Symbol; 3] = [*core, *ops, *RangeTo];
pub static ref RANGE_TO_INCLUSIVE: [Symbol; 3] = [*core, *ops, *RangeToInclusive];
pub static ref RANGE_TO_INCLUSIVE_STD: [Symbol; 3] = [*std, *ops, *RangeToInclusive];
pub static ref RANGE_TO_STD: [Symbol; 3] = [*std, *ops, *RangeTo];
pub static ref RANGE: [Symbol; 3] = [core, ops, Range];
pub static ref RANGE_ARGUMENT_TRAIT: [Symbol; 3] = [core, ops, *RangeBounds];
pub static ref RANGE_FROM: [Symbol; 3] = [core, ops, RangeFrom];
pub static ref RANGE_FROM_STD: [Symbol; 3] = [std, ops, RangeFrom];
pub static ref RANGE_FULL: [Symbol; 3] = [core, ops, RangeFull];
pub static ref RANGE_FULL_STD: [Symbol; 3] = [std, ops, RangeFull];
pub static ref RANGE_INCLUSIVE_NEW: [Symbol; 4] = [core, ops, RangeInclusive, *new];
pub static ref RANGE_INCLUSIVE_STD_NEW: [Symbol; 4] = [std, ops, RangeInclusive, *new];
pub static ref RANGE_STD: [Symbol; 3] = [std, ops, Range];
pub static ref RANGE_TO: [Symbol; 3] = [core, ops, RangeTo];
pub static ref RANGE_TO_INCLUSIVE: [Symbol; 3] = [core, ops, RangeToInclusive];
pub static ref RANGE_TO_INCLUSIVE_STD: [Symbol; 3] = [std, ops, RangeToInclusive];
pub static ref RANGE_TO_STD: [Symbol; 3] = [std, ops, RangeTo];
pub static ref RC: [Symbol; 3] = [*alloc, *rc, *Rc];
pub static ref RECEIVER: [Symbol; 4] = [*std, *sync, *mpsc, *Receiver];
pub static ref RECEIVER: [Symbol; 4] = [std, *sync, *mpsc, *Receiver];
pub static ref REGEX: [Symbol; 3] = [*regex, *re_unicode, *Regex];
pub static ref REGEX_BUILDER_NEW: [Symbol; 5] = [*regex, *re_builder, *unicode, *RegexBuilder, *new];
pub static ref REGEX_BYTES_BUILDER_NEW: [Symbol; 5] = [*regex, *re_builder, *bytes, *RegexBuilder, *new];
pub static ref REGEX_BYTES_NEW: [Symbol; 4] = [*regex, *re_bytes, *Regex, *new];
pub static ref REGEX_BYTES_SET_NEW: [Symbol; 5] = [*regex, *re_set, *bytes, *RegexSet, *new];
pub static ref REGEX_NEW: [Symbol; 4] = [*regex, *re_unicode, *Regex, *new];
pub static ref REGEX_SET_NEW: [Symbol; 5] = [*regex, *re_set, *unicode, *RegexSet, *new];
pub static ref REPEAT: [Symbol; 3] = [*core, *iter, *repeat];
pub static ref RESULT: [Symbol; 3] = [*core, *result, *sym::Result];
pub static ref RESULT_ERR: [Symbol; 4] = [*core, *result, *sym::Result, *sym::Err];
pub static ref RESULT_OK: [Symbol; 4] = [*core, *result, *sym::Result, *sym::Ok];
pub static ref REPEAT: [Symbol; 3] = [core, iter, *repeat];
pub static ref RESULT: [Symbol; 3] = [core, result, *sym::Result];
pub static ref RESULT_ERR: [Symbol; 4] = [core, result, *sym::Result, *sym::Err];
pub static ref RESULT_OK: [Symbol; 4] = [core, result, *sym::Result, *sym::Ok];
pub static ref SERDE_DE_VISITOR: [Symbol; 3] = [*serde, *de, *Visitor];
pub static ref SLICE_INTO_VEC: [Symbol; 4] = [*alloc, *slice, *impl_slice_t, *into_vec];
pub static ref SLICE_ITER: [Symbol; 3] = [*core, *slice, *Iter];
pub static ref STD_MEM_TRANSMUTE: [Symbol; 3] = [*std, *mem, *transmute];
pub static ref STD_PTR_NULL: [Symbol; 3] = [*std, *ptr, *null];
pub static ref STDERR: [Symbol; 4] = [*std, *io, *stdio, *stderr];
pub static ref STDOUT: [Symbol; 4] = [*std, *io, *stdio, *stdout];
pub static ref SLICE_ITER: [Symbol; 3] = [core, *slice, *Iter];
pub static ref STD_MEM_TRANSMUTE: [Symbol; 3] = [std, *mem, transmute];
pub static ref STD_PTR_NULL: [Symbol; 3] = [std, *ptr, *null];
pub static ref STDERR: [Symbol; 4] = [std, *io, *stdio, *stderr];
pub static ref STDOUT: [Symbol; 4] = [std, *io, *stdio, *stdout];
pub static ref STRING: [Symbol; 3] = [*alloc, *string, *sym::String];
pub static ref TO_OWNED: [Symbol; 3] = [*alloc, *borrow, *sym::ToOwned];
pub static ref TO_OWNED_METHOD: [Symbol; 4] = [*alloc, *borrow, *sym::ToOwned, *to_owned];
pub static ref TO_STRING: [Symbol; 3] = [*alloc, *string, *sym::ToString];
pub static ref TO_STRING_METHOD: [Symbol; 4] = [*alloc, *string, *sym::ToString, *to_string];
pub static ref TRANSMUTE: [Symbol; 4] = [*core, *intrinsics, *empty_symbol, *transmute];
pub static ref TRY_INTO_RESULT: [Symbol; 4] = [*std, *ops, *Try, *into_result];
pub static ref UNINIT: [Symbol; 4] = [*core, *intrinsics, *empty_symbol, *uninit];
pub static ref TRANSMUTE: [Symbol; 4] = [core, intrinsics, keywords::Invalid.name(), transmute];
pub static ref TRY_INTO_RESULT: [Symbol; 4] = [std, ops, Try, into_result];
pub static ref UNINIT: [Symbol; 4] = [core, intrinsics, keywords::Invalid.name(), *uninit];
pub static ref VEC: [Symbol; 3] = [*alloc, *vec, *sym::Vec];
pub static ref VEC_DEQUE: [Symbol; 4] = [*alloc, *collections, *vec_deque, *VecDeque];
pub static ref VEC_FROM_ELEM: [Symbol; 3] = [*alloc, *vec, *from_elem];
Expand Down
35 changes: 19 additions & 16 deletions clippy_lints/src/utils/sym.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
#![allow(default_hash_types, non_upper_case_globals)]

use lazy_static::lazy_static;
use syntax::symbol::Symbol;
use syntax::symbol::{Interner, Symbol};
// this import is very important! otherwise we may redefine symbols that already exist in rustc
// and thus cause two equal symbols to differ
pub use syntax::symbol::sym::*;

macro_rules! symbols_simple {
($($ident:ident,)*) => {
$(
lazy_static! {
pub(crate) static ref $ident: Symbol = Symbol::intern(stringify!($ident));
}
)*
};
}
pub const PREINTERNED_SYMBOLS: &[&str] = &[
"<impl [T]>",
$(stringify!($ident)),*
];

macro_rules! symbols_init {
($($ident:ident: $expr:expr,)*) => {
lazy_static! {
pub(crate) static ref impl_slice_t: Symbol = {
let sym = Symbol::intern("<impl [T]>");
assert_eq!(sym.as_u32(), Interner::FIRST_DRIVER_INDEX);
sym
};
}
$(
lazy_static! {
pub(crate) static ref $ident: Symbol = Symbol::intern($expr);
// we can keep using `from_str` here, because it'll fetch the preinterned symbol id
// and that will be stable across reruns of clippy even if the global table gets
// nuked.
pub(crate) static ref $ident: Symbol = Symbol::intern(stringify!($ident));
}
)*
};
Expand Down Expand Up @@ -386,8 +394,3 @@ symbols_simple! {
zeroed,
zip,
}

symbols_init! {
impl_slice_t: "<impl [T]>",
empty_symbol: "",
}
2 changes: 1 addition & 1 deletion src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub fn main() {
let callbacks: &mut (dyn rustc_driver::Callbacks + Send) =
if clippy_enabled { &mut clippy } else { &mut default };
let args = args;
rustc_driver::run_compiler(&args, callbacks, None, None)
rustc_driver::run_compiler(&args, clippy_lints::PREINTERNED_SYMBOLS, callbacks, None, None)
})
.and_then(|result| result)
.is_err() as i32,
Expand Down