From e8d2f629245f956ec63da04ca672f4cab3a928ef Mon Sep 17 00:00:00 2001 From: Matthew Jasper Date: Sat, 14 Sep 2019 21:10:12 +0100 Subject: [PATCH] Prefer `Symbol` to `Ident` when there's no sensible `Span` --- src/librustc_lint/unused.rs | 17 +++++-------- src/librustc_resolve/lib.rs | 34 ++++++++++++------------- src/librustc_resolve/resolve_imports.rs | 10 +++++--- 3 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index 561bf202dfeff..21ea4766d8e52 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -618,24 +618,19 @@ impl UnusedImportBraces { } // Trigger the lint if the nested item is a non-self single item - let node_ident; - match items[0].0.kind { + let node_name = match items[0].0.kind { ast::UseTreeKind::Simple(rename, ..) => { let orig_ident = items[0].0.prefix.segments.last().unwrap().ident; if orig_ident.name == kw::SelfLower { return; } - node_ident = rename.unwrap_or(orig_ident); + rename.unwrap_or(orig_ident).name } - ast::UseTreeKind::Glob => { - node_ident = ast::Ident::from_str("*"); - } - ast::UseTreeKind::Nested(_) => { - return; - } - } + ast::UseTreeKind::Glob => Symbol::intern("*"), + ast::UseTreeKind::Nested(_) => return, + }; - let msg = format!("braces around {} is unnecessary", node_ident.name); + let msg = format!("braces around {} is unnecessary", node_name); cx.span_lint(UNUSED_IMPORT_BRACES, item.span, &msg); } } diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index f97fcb0a035a2..74f68e5147126 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -40,7 +40,7 @@ use rustc_metadata::cstore::CStore; use syntax::ext::hygiene::{ExpnId, Transparency, SyntaxContext}; use syntax::ast::{self, Name, NodeId, Ident, FloatTy, IntTy, UintTy}; use syntax::ext::base::{SyntaxExtension, MacroKind, SpecialDerives}; -use syntax::symbol::{Symbol, kw, sym}; +use syntax::symbol::{kw, sym}; use syntax::visit::{self, Visitor}; use syntax::attr; @@ -241,7 +241,7 @@ impl Segment { fn names_to_string(segments: &[Segment]) -> String { names_to_string(&segments.iter() - .map(|seg| seg.ident) + .map(|seg| seg.ident.name) .collect::>()) } } @@ -951,7 +951,7 @@ pub struct Resolver<'a> { struct_constructors: DefIdMap<(Res, ty::Visibility)>, /// Features enabled for this crate. - active_features: FxHashSet, + active_features: FxHashSet, /// Stores enum visibilities to properly build a reduced graph /// when visiting the correspondent variants. @@ -1018,8 +1018,8 @@ impl<'a> hir::lowering::Resolver for Resolver<'a> { fn resolve_str_path( &mut self, span: Span, - crate_root: Option, - components: &[Symbol], + crate_root: Option, + components: &[Name], ns: Namespace, ) -> (ast::Path, Res) { let root = if crate_root.is_some() { @@ -2555,7 +2555,7 @@ impl<'a> Resolver<'a> { fn add_suggestion_for_rename_of_use( &self, err: &mut DiagnosticBuilder<'_>, - name: Symbol, + name: Name, directive: &ImportDirective<'_>, binding_span: Span, ) { @@ -2770,22 +2770,22 @@ impl<'a> Resolver<'a> { } } -fn names_to_string(idents: &[Ident]) -> String { +fn names_to_string(names: &[Name]) -> String { let mut result = String::new(); - for (i, ident) in idents.iter() - .filter(|ident| ident.name != kw::PathRoot) + for (i, name) in names.iter() + .filter(|name| **name != kw::PathRoot) .enumerate() { if i > 0 { result.push_str("::"); } - result.push_str(&ident.as_str()); + result.push_str(&name.as_str()); } result } fn path_names_to_string(path: &Path) -> String { names_to_string(&path.segments.iter() - .map(|seg| seg.ident) + .map(|seg| seg.ident.name) .collect::>()) } @@ -2793,15 +2793,14 @@ fn path_names_to_string(path: &Path) -> String { fn module_to_string(module: Module<'_>) -> Option { let mut names = Vec::new(); - fn collect_mod(names: &mut Vec, module: Module<'_>) { + fn collect_mod(names: &mut Vec, module: Module<'_>) { if let ModuleKind::Def(.., name) = module.kind { if let Some(parent) = module.parent { - names.push(Ident::with_dummy_span(name)); + names.push(name); collect_mod(names, parent); } } else { - // danger, shouldn't be ident? - names.push(Ident::from_str("")); + names.push(Name::intern("")); collect_mod(names, module.parent.unwrap()); } } @@ -2810,9 +2809,8 @@ fn module_to_string(module: Module<'_>) -> Option { if names.is_empty() { return None; } - Some(names_to_string(&names.into_iter() - .rev() - .collect::>())) + names.reverse(); + Some(names_to_string(&names)) } #[derive(Copy, Clone, Debug)] diff --git a/src/librustc_resolve/resolve_imports.rs b/src/librustc_resolve/resolve_imports.rs index eb509f1a01d67..e77e8290f1faa 100644 --- a/src/librustc_resolve/resolve_imports.rs +++ b/src/librustc_resolve/resolve_imports.rs @@ -1433,15 +1433,17 @@ fn import_path_to_string(names: &[Ident], let global = !names.is_empty() && names[0].name == kw::PathRoot; if let Some(pos) = pos { let names = if global { &names[1..pos + 1] } else { &names[..pos + 1] }; - names_to_string(names) + names_to_string(&names.iter().map(|ident| ident.name).collect::>()) } else { let names = if global { &names[1..] } else { names }; if names.is_empty() { import_directive_subclass_to_string(subclass) } else { - format!("{}::{}", - names_to_string(names), - import_directive_subclass_to_string(subclass)) + format!( + "{}::{}", + names_to_string(&names.iter().map(|ident| ident.name).collect::>()), + import_directive_subclass_to_string(subclass), + ) } } }