Skip to content

Commit

Permalink
Remove InternedString.
Browse files Browse the repository at this point in the history
By using `LocalInternedString` instead for the few remaining uses.
  • Loading branch information
nnethercote committed Oct 21, 2019
1 parent 2da7a9c commit 08e2f05
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 127 deletions.
4 changes: 2 additions & 2 deletions src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,13 +186,13 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {
});

let mut upstream_crates: Vec<_> = cstore.crates_untracked().iter().map(|&cnum| {
let name = cstore.crate_name_untracked(cnum).as_interned_str();
let name = cstore.crate_name_untracked(cnum);
let disambiguator = cstore.crate_disambiguator_untracked(cnum).to_fingerprint();
let hash = cstore.crate_hash_untracked(cnum);
(name, disambiguator, hash)
}).collect();

upstream_crates.sort_unstable_by_key(|&(name, dis, _)| (name, dis));
upstream_crates.sort_unstable_by_key(|&(name, dis, _)| (name.as_str(), dis));

// We hash the final, remapped names of all local source files so we
// don't have to include the path prefix remapping commandline args.
Expand Down
19 changes: 10 additions & 9 deletions src/librustc/ich/impls_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::mem;
use syntax::ast;
use syntax::feature_gate;
use syntax::parse::token;
use syntax::symbol::InternedString;
use syntax::symbol::LocalInternedString;
use syntax::tokenstream;
use syntax_pos::SourceFile;

Expand All @@ -18,20 +18,21 @@ use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX};
use smallvec::SmallVec;
use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey, StableHasher};

impl<'a> HashStable<StableHashingContext<'a>> for InternedString {
impl<'a> HashStable<StableHashingContext<'a>> for LocalInternedString {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
self.with(|s| s.hash_stable(hcx, hasher))
let str = self as &str;
str.hash_stable(hcx, hasher)
}
}

impl<'a> ToStableHashKey<StableHashingContext<'a>> for InternedString {
type KeyType = InternedString;
impl<'a> ToStableHashKey<StableHashingContext<'a>> for LocalInternedString {
type KeyType = LocalInternedString;

#[inline]
fn to_stable_hash_key(&self,
_: &StableHashingContext<'a>)
-> InternedString {
-> LocalInternedString {
self.clone()
}
}
Expand All @@ -44,13 +45,13 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Name {
}

impl<'a> ToStableHashKey<StableHashingContext<'a>> for ast::Name {
type KeyType = InternedString;
type KeyType = LocalInternedString;

#[inline]
fn to_stable_hash_key(&self,
_: &StableHashingContext<'a>)
-> InternedString {
self.as_interned_str()
-> LocalInternedString {
self.as_str()
}
}

Expand Down
120 changes: 5 additions & 115 deletions src/libsyntax_pos/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_macros::symbols;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
use rustc_serialize::{UseSpecializedDecodable, UseSpecializedEncodable};

use std::cmp::{PartialEq, Ordering, PartialOrd, Ord};
use std::cmp::{PartialEq, PartialOrd, Ord};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::str;
Expand Down Expand Up @@ -766,11 +766,6 @@ impl Ident {
Ident::with_dummy_span(kw::Invalid)
}

/// Maps an interned string to an identifier with an empty syntax context.
pub fn from_interned_str(string: InternedString) -> Ident {
Ident::with_dummy_span(string.as_symbol())
}

/// Maps a string to an identifier with a dummy span.
pub fn from_str(string: &str) -> Ident {
Ident::with_dummy_span(Symbol::intern(string))
Expand Down Expand Up @@ -813,11 +808,6 @@ impl Ident {
pub fn as_str(self) -> LocalInternedString {
self.name.as_str()
}

/// Convert the name to an `InternedString`.
pub fn as_interned_str(self) -> InternedString {
self.name.as_interned_str()
}
}

impl PartialEq for Ident {
Expand Down Expand Up @@ -903,15 +893,6 @@ impl Symbol {
})
}

/// Access two symbols' chars. This is a slowish operation because it
/// requires locking the symbol interner, but it is faster than calling
/// `with()` twice.
fn with2<F: FnOnce(&str, &str) -> R, R>(self, other: Symbol, f: F) -> R {
with_interner(|interner| {
f(interner.get(self), interner.get(other))
})
}

/// Convert to a `LocalInternedString`. This is a slowish operation because
/// it requires locking the symbol interner.
pub fn as_str(self) -> LocalInternedString {
Expand All @@ -922,11 +903,6 @@ impl Symbol {
})
}

/// Convert to an `InternedString`.
pub fn as_interned_str(self) -> InternedString {
InternedString { symbol: self }
}

pub fn as_u32(self) -> u32 {
self.0.as_u32()
}
Expand Down Expand Up @@ -1105,9 +1081,9 @@ fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T {
GLOBALS.with(|globals| f(&mut *globals.symbol_interner.lock()))
}

/// An alternative to `Symbol` and `InternedString`, useful when the chars
/// within the symbol need to be accessed. It deliberately has limited
/// functionality and should only be used for temporary values.
/// An alternative to `Symbol`, useful when the chars within the symbol need to
/// be accessed. It deliberately has limited functionality and should only be
/// used for temporary values.
///
/// Because the interner outlives any thread which uses this type, we can
/// safely treat `string` which points to interner data, as an immortal string,
Expand All @@ -1116,7 +1092,7 @@ fn with_interner<T, F: FnOnce(&mut Interner) -> T>(f: F) -> T {
// FIXME: ensure that the interner outlives any thread which uses
// `LocalInternedString`, by creating a new thread right after constructing the
// interner.
#[derive(Eq, PartialOrd, Ord)]
#[derive(Clone, Eq, PartialOrd, Ord)]
pub struct LocalInternedString {
string: &'static str,
}
Expand Down Expand Up @@ -1157,89 +1133,3 @@ impl fmt::Display for LocalInternedString {
fmt::Display::fmt(self.string, f)
}
}

/// An alternative to `Symbol` that is focused on string contents.
///
/// Its implementations of `Hash`, `PartialOrd` and `Ord` work with the
/// string chars rather than the symbol integer. This is useful when hash
/// stability is required across compile sessions, or a guaranteed sort
/// ordering is required.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct InternedString {
symbol: Symbol,
}

impl InternedString {
/// Maps a string to its interned representation.
pub fn intern(string: &str) -> Self {
InternedString {
symbol: Symbol::intern(string)
}
}

pub fn with<F: FnOnce(&str) -> R, R>(self, f: F) -> R {
self.symbol.with(f)
}

fn with2<F: FnOnce(&str, &str) -> R, R>(self, other: &InternedString, f: F) -> R {
self.symbol.with2(other.symbol, f)
}

pub fn as_symbol(self) -> Symbol {
self.symbol
}

/// Convert to a `LocalInternedString`. This is a slowish operation because it
/// requires locking the symbol interner.
pub fn as_str(self) -> LocalInternedString {
self.symbol.as_str()
}
}

impl Hash for InternedString {
fn hash<H: Hasher>(&self, state: &mut H) {
self.with(|str| str.hash(state))
}
}

impl PartialOrd<InternedString> for InternedString {
fn partial_cmp(&self, other: &InternedString) -> Option<Ordering> {
if self.symbol == other.symbol {
return Some(Ordering::Equal);
}
self.with2(other, |self_str, other_str| self_str.partial_cmp(other_str))
}
}

impl Ord for InternedString {
fn cmp(&self, other: &InternedString) -> Ordering {
if self.symbol == other.symbol {
return Ordering::Equal;
}
self.with2(other, |self_str, other_str| self_str.cmp(other_str))
}
}

impl fmt::Debug for InternedString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.with(|str| fmt::Debug::fmt(&str, f))
}
}

impl fmt::Display for InternedString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.with(|str| fmt::Display::fmt(&str, f))
}
}

impl Decodable for InternedString {
fn decode<D: Decoder>(d: &mut D) -> Result<InternedString, D::Error> {
Ok(InternedString::intern(&d.read_str()?))
}
}

impl Encodable for InternedString {
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
self.with(|string| s.emit_str(string))
}
}
1 change: 0 additions & 1 deletion src/tools/linkchecker/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ fn check(cache: &mut Cache,
// FIXME(#32129)
if file.ends_with("std/string/struct.String.html") ||
file.ends_with("interpret/struct.ImmTy.html") ||
file.ends_with("symbol/struct.InternedString.html") ||
file.ends_with("ast/struct.ThinVec.html") ||
file.ends_with("util/struct.ThinVec.html") ||
file.ends_with("layout/struct.TyLayout.html") ||
Expand Down

0 comments on commit 08e2f05

Please sign in to comment.