Skip to content

Commit d6ca34c

Browse files
committed
Use Symbol more in lint APIs
1 parent ab7cf71 commit d6ca34c

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

src/librustc/lint/context.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -759,35 +759,35 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
759759
/// # Examples
760760
///
761761
/// ```rust,ignore (no context or def id available)
762-
/// if cx.match_def_path(def_id, &["core", "option", "Option"]) {
762+
/// if cx.match_def_path(def_id, &[sym::core, sym::option, sym::Option]) {
763763
/// // The given `def_id` is that of an `Option` type
764764
/// }
765765
/// ```
766-
pub fn match_def_path(&self, def_id: DefId, path: &[&str]) -> bool {
766+
pub fn match_def_path(&self, def_id: DefId, path: &[Symbol]) -> bool {
767767
let names = self.get_def_path(def_id);
768768

769-
names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b)
769+
names.len() == path.len() && names.into_iter().zip(path.iter()).all(|(a, &b)| a == b)
770770
}
771771

772-
/// Gets the absolute path of `def_id` as a vector of `&str`.
772+
/// Gets the absolute path of `def_id` as a vector of `Symbol`.
773773
///
774774
/// # Examples
775775
///
776776
/// ```rust,ignore (no context or def id available)
777777
/// let def_path = cx.get_def_path(def_id);
778-
/// if let &["core", "option", "Option"] = &def_path[..] {
778+
/// if let &[sym::core, sym::option, sym::Option] = &def_path[..] {
779779
/// // The given `def_id` is that of an `Option` type
780780
/// }
781781
/// ```
782-
pub fn get_def_path(&self, def_id: DefId) -> Vec<LocalInternedString> {
782+
pub fn get_def_path(&self, def_id: DefId) -> Vec<Symbol> {
783783
pub struct AbsolutePathPrinter<'a, 'tcx> {
784784
pub tcx: TyCtxt<'a, 'tcx, 'tcx>,
785785
}
786786

787787
impl<'tcx> Printer<'tcx, 'tcx> for AbsolutePathPrinter<'_, 'tcx> {
788788
type Error = !;
789789

790-
type Path = Vec<LocalInternedString>;
790+
type Path = Vec<Symbol>;
791791
type Region = ();
792792
type Type = ();
793793
type DynExistential = ();
@@ -820,14 +820,14 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
820820
}
821821

822822
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
823-
Ok(vec![self.tcx.original_crate_name(cnum).as_str()])
823+
Ok(vec![self.tcx.original_crate_name(cnum)])
824824
}
825825

826826
fn path_qualified(
827827
self,
828828
self_ty: Ty<'tcx>,
829829
trait_ref: Option<ty::TraitRef<'tcx>>,
830-
) -> Result<Self::Path, Self::Error> {
830+
) -> Result<Self::Path, Self::Error> {
831831
if trait_ref.is_none() {
832832
if let ty::Adt(def, substs) = self_ty.sty {
833833
return self.print_def_path(def.did, substs);
@@ -836,8 +836,8 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
836836

837837
// This shouldn't ever be needed, but just in case:
838838
Ok(vec![match trait_ref {
839-
Some(trait_ref) => LocalInternedString::intern(&format!("{:?}", trait_ref)),
840-
None => LocalInternedString::intern(&format!("<{}>", self_ty)),
839+
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)),
840+
None => Symbol::intern(&format!("<{}>", self_ty)),
841841
}])
842842
}
843843

@@ -847,16 +847,16 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
847847
_disambiguated_data: &DisambiguatedDefPathData,
848848
self_ty: Ty<'tcx>,
849849
trait_ref: Option<ty::TraitRef<'tcx>>,
850-
) -> Result<Self::Path, Self::Error> {
850+
) -> Result<Self::Path, Self::Error> {
851851
let mut path = print_prefix(self)?;
852852

853853
// This shouldn't ever be needed, but just in case:
854854
path.push(match trait_ref {
855855
Some(trait_ref) => {
856-
LocalInternedString::intern(&format!("<impl {} for {}>", trait_ref,
856+
Symbol::intern(&format!("<impl {} for {}>", trait_ref,
857857
self_ty))
858858
},
859-
None => LocalInternedString::intern(&format!("<impl {}>", self_ty)),
859+
None => Symbol::intern(&format!("<impl {}>", self_ty)),
860860
});
861861

862862
Ok(path)
@@ -866,7 +866,7 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
866866
self,
867867
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
868868
disambiguated_data: &DisambiguatedDefPathData,
869-
) -> Result<Self::Path, Self::Error> {
869+
) -> Result<Self::Path, Self::Error> {
870870
let mut path = print_prefix(self)?;
871871

872872
// Skip `::{{constructor}}` on tuple/unit structs.
@@ -875,15 +875,15 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
875875
_ => {}
876876
}
877877

878-
path.push(disambiguated_data.data.as_interned_str().as_str());
878+
path.push(disambiguated_data.data.as_interned_str().as_symbol());
879879
Ok(path)
880880
}
881881

882882
fn path_generic_args(
883883
self,
884884
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
885885
_args: &[Kind<'tcx>],
886-
) -> Result<Self::Path, Self::Error> {
886+
) -> Result<Self::Path, Self::Error> {
887887
print_prefix(self)
888888
}
889889
}

src/librustc/lint/internal.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::lint::{
88
use errors::Applicability;
99
use rustc_data_structures::fx::FxHashMap;
1010
use syntax::ast::Ident;
11+
use syntax::symbol::{sym, Symbol};
1112

1213
declare_lint! {
1314
pub DEFAULT_HASH_TYPES,
@@ -16,14 +17,16 @@ declare_lint! {
1617
}
1718

1819
pub struct DefaultHashTypes {
19-
map: FxHashMap<String, String>,
20+
map: FxHashMap<Symbol, Symbol>,
2021
}
2122

2223
impl DefaultHashTypes {
24+
// we are allowed to use `HashMap` and `HashSet` as identifiers for implementing the lint itself
25+
#[allow(internal)]
2326
pub fn new() -> Self {
2427
let mut map = FxHashMap::default();
25-
map.insert("HashMap".to_string(), "FxHashMap".to_string());
26-
map.insert("HashSet".to_string(), "FxHashSet".to_string());
28+
map.insert(sym::HashMap, sym::FxHashMap);
29+
map.insert(sym::HashSet, sym::FxHashSet);
2730
Self { map }
2831
}
2932
}
@@ -32,11 +35,10 @@ impl_lint_pass!(DefaultHashTypes => [DEFAULT_HASH_TYPES]);
3235

3336
impl EarlyLintPass for DefaultHashTypes {
3437
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) {
35-
let ident_string = ident.to_string();
36-
if let Some(replace) = self.map.get(&ident_string) {
38+
if let Some(replace) = self.map.get(&ident.name) {
3739
let msg = format!(
3840
"Prefer {} over {}, it has better performance",
39-
replace, ident_string
41+
replace, ident
4042
);
4143
let mut db = cx.struct_span_lint(DEFAULT_HASH_TYPES, ident.span, &msg);
4244
db.span_suggestion(
@@ -169,25 +171,29 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TyTyKind {
169171
}
170172

171173
fn lint_ty_kind_usage(cx: &LateContext<'_, '_>, segment: &PathSegment) -> bool {
172-
if segment.ident.as_str() == "TyKind" {
174+
if segment.ident.name == sym::TyKind {
173175
if let Some(res) = segment.res {
174176
if let Some(did) = res.opt_def_id() {
175-
return cx.match_def_path(did, &["rustc", "ty", "sty", "TyKind"]);
177+
return cx.match_def_path(did, TYKIND_PATH);
176178
}
177179
}
178180
}
179181

180182
false
181183
}
182184

185+
const TYKIND_PATH: &[Symbol] = &[sym::rustc, sym::ty, sym::sty, sym::TyKind];
186+
const TY_PATH: &[Symbol] = &[sym::rustc, sym::ty, sym::Ty];
187+
const TYCTXT_PATH: &[Symbol] = &[sym::rustc, sym::ty, sym::context, sym::TyCtxt];
188+
183189
fn is_ty_or_ty_ctxt(cx: &LateContext<'_, '_>, ty: &Ty) -> Option<String> {
184190
match &ty.node {
185191
TyKind::Path(qpath) => {
186192
if let QPath::Resolved(_, path) = qpath {
187193
let did = path.res.opt_def_id()?;
188-
if cx.match_def_path(did, &["rustc", "ty", "Ty"]) {
194+
if cx.match_def_path(did, TY_PATH) {
189195
return Some(format!("Ty{}", gen_args(path.segments.last().unwrap())));
190-
} else if cx.match_def_path(did, &["rustc", "ty", "context", "TyCtxt"]) {
196+
} else if cx.match_def_path(did, TYCTXT_PATH) {
191197
return Some(format!("TyCtxt{}", gen_args(path.segments.last().unwrap())));
192198
}
193199
}

src/libsyntax_pos/symbol.rs

+10
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ symbols! {
194194
const_raw_ptr_to_usize_cast,
195195
const_transmute,
196196
contents,
197+
context,
197198
convert,
198199
copy_closures,
199200
core,
@@ -282,6 +283,8 @@ symbols! {
282283
fundamental,
283284
future,
284285
Future,
286+
FxHashSet,
287+
FxHashMap,
285288
gen_future,
286289
generators,
287290
generic_associated_types,
@@ -291,6 +294,8 @@ symbols! {
291294
globs,
292295
hash,
293296
Hash,
297+
HashSet,
298+
HashMap,
294299
hexagon_target_feature,
295300
hidden,
296301
homogeneous_aggregate,
@@ -505,6 +510,7 @@ symbols! {
505510
rust_2015_preview,
506511
rust_2018_preview,
507512
rust_begin_unwind,
513+
rustc,
508514
rustc_allocator_nounwind,
509515
rustc_allow_const_fn_ptr,
510516
rustc_args_required_const,
@@ -590,6 +596,7 @@ symbols! {
590596
struct_inherit,
591597
structural_match,
592598
struct_variant,
599+
sty,
593600
suggestion,
594601
target_feature,
595602
target_has_atomic,
@@ -618,7 +625,10 @@ symbols! {
618625
try_trait,
619626
tt,
620627
tuple_indexing,
628+
Ty,
621629
ty,
630+
TyCtxt,
631+
TyKind,
622632
type_alias_enum_variants,
623633
type_ascription,
624634
type_length_limit,

0 commit comments

Comments
 (0)