about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNicholas Nethercote <nnethercote@mozilla.com>2020-08-06 12:48:53 +1000
committerNicholas Nethercote <nnethercote@mozilla.com>2020-08-10 07:12:59 +1000
commit0a597bd98f91b7f0e87985b8dcac0fd9820c6d47 (patch)
treec743ffb9380a285605cb60e94a891da53009e278
parent75b67c2d5e12d98b70323bd7874886dd650c5499 (diff)
downloadrust-0a597bd98f91b7f0e87985b8dcac0fd9820c6d47.tar.gz
rust-0a597bd98f91b7f0e87985b8dcac0fd9820c6d47.zip
Remove `CowBoxSymStr`.
`CowBoxSymStr` is a type that either holds a `SymbolStr` (which is much
the same as a `Symbol`), or an owned string. When computing skeletons,
a `SymbolStr` is stored if the skeleton is the same as the original
string, otherwise an owned string is stored.

So, basically, `CowBoxSymStr` is a type for string interning. But we
already have one of those: `Symbol` itself. This PR removes
`CowBoxSymStr`, using `Symbol` instead. A good thing about this is that
it avoids storing `SymbolStr` values in `skeleton_map`, something that
is discouraged.

The PR also inlines and removes the `calc_skeleton()` function because
that simplifies the code.
-rw-r--r--src/librustc_lint/non_ascii_idents.rs84
1 files changed, 23 insertions, 61 deletions
diff --git a/src/librustc_lint/non_ascii_idents.rs b/src/librustc_lint/non_ascii_idents.rs
index 7c45d826473..ab1658b2229 100644
--- a/src/librustc_lint/non_ascii_idents.rs
+++ b/src/librustc_lint/non_ascii_idents.rs
@@ -1,7 +1,7 @@
 use crate::{EarlyContext, EarlyLintPass, LintContext};
 use rustc_ast::ast;
 use rustc_data_structures::fx::FxHashMap;
-use rustc_span::symbol::SymbolStr;
+use rustc_span::symbol::Symbol;
 
 declare_lint! {
     pub NON_ASCII_IDENTS,
@@ -39,7 +39,6 @@ impl EarlyLintPass for NonAsciiIdents {
         use rustc_span::Span;
         use std::collections::BTreeMap;
         use unicode_security::GeneralSecurityProfile;
-        use utils::CowBoxSymStr;
 
         let check_non_ascii_idents = cx.builder.lint_level(NON_ASCII_IDENTS).0 != Level::Allow;
         let check_uncommon_codepoints =
@@ -83,33 +82,34 @@ impl EarlyLintPass for NonAsciiIdents {
         }
 
         if has_non_ascii_idents && check_confusable_idents {
-            let mut skeleton_map: FxHashMap<CowBoxSymStr, (SymbolStr, Span, bool)> =
+            let mut skeleton_map: FxHashMap<Symbol, (Symbol, Span, bool)> =
                 FxHashMap::with_capacity_and_hasher(symbols.len(), Default::default());
-            let mut str_buf = String::new();
-            for (symbol, &sp) in symbols.iter() {
-                fn calc_skeleton(symbol_str: &SymbolStr, buffer: &mut String) -> CowBoxSymStr {
-                    use std::mem::replace;
-                    use unicode_security::confusable_detection::skeleton;
-                    buffer.clear();
-                    buffer.extend(skeleton(symbol_str));
-                    if *symbol_str == *buffer {
-                        CowBoxSymStr::Interned(symbol_str.clone())
-                    } else {
-                        let owned = replace(buffer, String::new());
-                        CowBoxSymStr::Owned(owned.into_boxed_str())
-                    }
-                }
+            let mut skeleton_buf = String::new();
+
+            for (&symbol, &sp) in symbols.iter() {
+                use unicode_security::confusable_detection::skeleton;
+
                 let symbol_str = symbol.as_str();
                 let is_ascii = symbol_str.is_ascii();
-                let skeleton = calc_skeleton(&symbol_str, &mut str_buf);
+
+                // Get the skeleton as a `Symbol`.
+                skeleton_buf.clear();
+                skeleton_buf.extend(skeleton(&symbol_str));
+                let skeleton_sym = if *symbol_str == *skeleton_buf {
+                    symbol
+                } else {
+                    Symbol::intern(&skeleton_buf)
+                };
+
                 skeleton_map
-                    .entry(skeleton)
-                    .and_modify(|(existing_symbolstr, existing_span, existing_is_ascii)| {
+                    .entry(skeleton_sym)
+                    .and_modify(|(existing_symbol, existing_span, existing_is_ascii)| {
                         if !*existing_is_ascii || !is_ascii {
                             cx.struct_span_lint(CONFUSABLE_IDENTS, sp, |lint| {
                                 lint.build(&format!(
                                     "identifier pair considered confusable between `{}` and `{}`",
-                                    existing_symbolstr, symbol_str
+                                    existing_symbol.as_str(),
+                                    symbol.as_str()
                                 ))
                                 .span_label(
                                     *existing_span,
@@ -119,12 +119,12 @@ impl EarlyLintPass for NonAsciiIdents {
                             });
                         }
                         if *existing_is_ascii && !is_ascii {
-                            *existing_symbolstr = symbol_str.clone();
+                            *existing_symbol = symbol;
                             *existing_span = sp;
                             *existing_is_ascii = is_ascii;
                         }
                     })
-                    .or_insert((symbol_str, sp, is_ascii));
+                    .or_insert((symbol, sp, is_ascii));
             }
         }
 
@@ -238,41 +238,3 @@ impl EarlyLintPass for NonAsciiIdents {
         }
     }
 }
-
-mod utils {
-    use rustc_span::symbol::SymbolStr;
-    use std::hash::{Hash, Hasher};
-    use std::ops::Deref;
-
-    pub(super) enum CowBoxSymStr {
-        Interned(SymbolStr),
-        Owned(Box<str>),
-    }
-
-    impl Deref for CowBoxSymStr {
-        type Target = str;
-
-        fn deref(&self) -> &str {
-            match self {
-                CowBoxSymStr::Interned(interned) => interned,
-                CowBoxSymStr::Owned(ref owned) => owned,
-            }
-        }
-    }
-
-    impl Hash for CowBoxSymStr {
-        #[inline]
-        fn hash<H: Hasher>(&self, state: &mut H) {
-            Hash::hash(&**self, state)
-        }
-    }
-
-    impl PartialEq<CowBoxSymStr> for CowBoxSymStr {
-        #[inline]
-        fn eq(&self, other: &CowBoxSymStr) -> bool {
-            PartialEq::eq(&**self, &**other)
-        }
-    }
-
-    impl Eq for CowBoxSymStr {}
-}