Skip to content

Commit e40f14a

Browse files
authored
Rollup merge of rust-lang#60827 - oli-obk:late_symbol, r=nnethercote
Use `Symbol` more in lint APIs r? @nnethercote This will cause clippy breakage, but super trivial to fix since we can then remove the hacky `match_def_path` function that I added and go back to calling rustc's `match_def_path` method.
2 parents e43aff7 + 1107158 commit e40f14a

File tree

3 files changed

+44
-28
lines changed

3 files changed

+44
-28
lines changed

src/librustc/lint/context.rs

+18-18
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 = ();
@@ -807,19 +807,19 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
807807
fn print_dyn_existential(
808808
self,
809809
_predicates: &'tcx ty::List<ty::ExistentialPredicate<'tcx>>,
810-
) -> Result<Self::DynExistential, Self::Error> {
810+
) -> Result<Self::DynExistential, Self::Error> {
811811
Ok(())
812812
}
813813

814814
fn path_crate(self, cnum: CrateNum) -> Result<Self::Path, Self::Error> {
815-
Ok(vec![self.tcx.original_crate_name(cnum).as_str()])
815+
Ok(vec![self.tcx.original_crate_name(cnum)])
816816
}
817817

818818
fn path_qualified(
819819
self,
820820
self_ty: Ty<'tcx>,
821821
trait_ref: Option<ty::TraitRef<'tcx>>,
822-
) -> Result<Self::Path, Self::Error> {
822+
) -> Result<Self::Path, Self::Error> {
823823
if trait_ref.is_none() {
824824
if let ty::Adt(def, substs) = self_ty.sty {
825825
return self.print_def_path(def.did, substs);
@@ -828,8 +828,8 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
828828

829829
// This shouldn't ever be needed, but just in case:
830830
Ok(vec![match trait_ref {
831-
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)).as_str(),
832-
None => Symbol::intern(&format!("<{}>", self_ty)).as_str(),
831+
Some(trait_ref) => Symbol::intern(&format!("{:?}", trait_ref)),
832+
None => Symbol::intern(&format!("<{}>", self_ty)),
833833
}])
834834
}
835835

@@ -839,15 +839,15 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
839839
_disambiguated_data: &DisambiguatedDefPathData,
840840
self_ty: Ty<'tcx>,
841841
trait_ref: Option<ty::TraitRef<'tcx>>,
842-
) -> Result<Self::Path, Self::Error> {
842+
) -> Result<Self::Path, Self::Error> {
843843
let mut path = print_prefix(self)?;
844844

845845
// This shouldn't ever be needed, but just in case:
846846
path.push(match trait_ref {
847847
Some(trait_ref) => {
848-
Symbol::intern(&format!("<impl {} for {}>", trait_ref, self_ty)).as_str()
848+
Symbol::intern(&format!("<impl {} for {}>", trait_ref, self_ty))
849849
},
850-
None => Symbol::intern(&format!("<impl {}>", self_ty)).as_str(),
850+
None => Symbol::intern(&format!("<impl {}>", self_ty)),
851851
});
852852

853853
Ok(path)
@@ -857,7 +857,7 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
857857
self,
858858
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
859859
disambiguated_data: &DisambiguatedDefPathData,
860-
) -> Result<Self::Path, Self::Error> {
860+
) -> Result<Self::Path, Self::Error> {
861861
let mut path = print_prefix(self)?;
862862

863863
// Skip `::{{constructor}}` on tuple/unit structs.
@@ -866,15 +866,15 @@ impl<'a, 'tcx> LateContext<'a, 'tcx> {
866866
_ => {}
867867
}
868868

869-
path.push(disambiguated_data.data.as_interned_str().as_str());
869+
path.push(disambiguated_data.data.as_interned_str().as_symbol());
870870
Ok(path)
871871
}
872872

873873
fn path_generic_args(
874874
self,
875875
print_prefix: impl FnOnce(Self) -> Result<Self::Path, Self::Error>,
876876
_args: &[Kind<'tcx>],
877-
) -> Result<Self::Path, Self::Error> {
877+
) -> Result<Self::Path, Self::Error> {
878878
print_prefix(self)
879879
}
880880
}

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
@@ -184,6 +184,7 @@ symbols! {
184184
const_raw_ptr_to_usize_cast,
185185
const_transmute,
186186
contents,
187+
context,
187188
convert,
188189
copy_closures,
189190
core,
@@ -253,12 +254,16 @@ symbols! {
253254
fundamental,
254255
future,
255256
Future,
257+
FxHashSet,
258+
FxHashMap,
256259
generators,
257260
generic_associated_types,
258261
generic_param_attrs,
259262
global_allocator,
260263
global_asm,
261264
globs,
265+
HashSet,
266+
HashMap,
262267
hexagon_target_feature,
263268
hidden,
264269
homogeneous_aggregate,
@@ -454,6 +459,7 @@ symbols! {
454459
rust_2015_preview,
455460
rust_2018_preview,
456461
rust_begin_unwind,
462+
rustc,
457463
rustc_allocator_nounwind,
458464
rustc_allow_const_fn_ptr,
459465
rustc_args_required_const,
@@ -536,6 +542,7 @@ symbols! {
536542
struct_inherit,
537543
structural_match,
538544
struct_variant,
545+
sty,
539546
suggestion,
540547
target_feature,
541548
target_has_atomic,
@@ -560,7 +567,10 @@ symbols! {
560567
Try,
561568
try_blocks,
562569
tuple_indexing,
570+
Ty,
563571
ty,
572+
TyCtxt,
573+
TyKind,
564574
type_alias_enum_variants,
565575
type_ascription,
566576
type_length_limit,

0 commit comments

Comments
 (0)