Skip to content

Commit

Permalink
Rollup merge of rust-lang#64141 - nnethercote:minimize-LocalInternedS…
Browse files Browse the repository at this point in the history
…tring, r=petrochenkov

Minimize uses of `LocalInternedString`

`LocalInternedString` is described as "An alternative to `Symbol` and `InternedString`, useful when the chars within the symbol need to be accessed. It is best used for temporary values."

This PR makes the code match that comment, by removing all non-local uses of `LocalInternedString`. This allows the removal of a number of operations on `LocalInternedString` and a couple of uses of `unsafe`.
  • Loading branch information
Centril committed Sep 5, 2019
2 parents 57ffc83 + cc17b1b commit 4dfbaba
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 133 deletions.
2 changes: 1 addition & 1 deletion src/librustc/hir/map/collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ 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_str();
let name = cstore.crate_name_untracked(cnum).as_interned_str();
let disambiguator = cstore.crate_disambiguator_untracked(cnum).to_fingerprint();
let hash = cstore.crate_hash_untracked(cnum);
(name, disambiguator, hash)
Expand Down
23 changes: 1 addition & 22 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, LocalInternedString};
use syntax::symbol::InternedString;
use syntax::tokenstream;
use syntax_pos::SourceFile;

Expand Down Expand Up @@ -39,27 +39,6 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for InternedString {
}
}

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

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

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

impl<'a> HashStable<StableHashingContext<'a>> for ast::Name {
#[inline]
fn hash_stable<W: StableHasherResult>(&self,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/lint/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::util::common::time;
use std::default::Default as StdDefault;
use syntax::ast;
use syntax::edition;
use syntax_pos::{MultiSpan, Span, symbol::{LocalInternedString, Symbol}};
use syntax_pos::{MultiSpan, Span, symbol::Symbol};
use errors::DiagnosticBuilder;
use crate::hir;
use crate::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
Expand Down Expand Up @@ -405,7 +405,7 @@ impl LintStore {
pub fn check_lint_name(
&self,
lint_name: &str,
tool_name: Option<LocalInternedString>,
tool_name: Option<Symbol>,
) -> CheckLintNameResult<'_> {
let complete_name = if let Some(tool_name) = tool_name {
format!("{}::{}", tool_name, lint_name)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ impl<'a> LintLevelsBuilder<'a> {
continue;
}

Some(tool_ident.as_str())
Some(tool_ident.name)
} else {
None
};
Expand Down
19 changes: 10 additions & 9 deletions src/librustc/traits/on_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ use syntax::ast::{MetaItem, NestedMetaItem};
use syntax::attr;
use syntax::symbol::{Symbol, kw, sym};
use syntax_pos::Span;
use syntax_pos::symbol::LocalInternedString;

#[derive(Clone, Debug)]
pub struct OnUnimplementedFormatString(LocalInternedString);
pub struct OnUnimplementedFormatString(Symbol);

#[derive(Debug)]
pub struct OnUnimplementedDirective {
Expand Down Expand Up @@ -89,19 +88,19 @@ impl<'tcx> OnUnimplementedDirective {
if item.check_name(sym::message) && message.is_none() {
if let Some(message_) = item.value_str() {
message = Some(OnUnimplementedFormatString::try_parse(
tcx, trait_def_id, message_.as_str(), span)?);
tcx, trait_def_id, message_, span)?);
continue;
}
} else if item.check_name(sym::label) && label.is_none() {
if let Some(label_) = item.value_str() {
label = Some(OnUnimplementedFormatString::try_parse(
tcx, trait_def_id, label_.as_str(), span)?);
tcx, trait_def_id, label_, span)?);
continue;
}
} else if item.check_name(sym::note) && note.is_none() {
if let Some(note_) = item.value_str() {
note = Some(OnUnimplementedFormatString::try_parse(
tcx, trait_def_id, note_.as_str(), span)?);
tcx, trait_def_id, note_, span)?);
continue;
}
} else if item.check_name(sym::on) && is_root &&
Expand Down Expand Up @@ -154,7 +153,7 @@ impl<'tcx> OnUnimplementedDirective {
message: None,
subcommands: vec![],
label: Some(OnUnimplementedFormatString::try_parse(
tcx, trait_def_id, value.as_str(), attr.span)?),
tcx, trait_def_id, value, attr.span)?),
note: None,
}))
} else {
Expand Down Expand Up @@ -218,7 +217,7 @@ impl<'tcx> OnUnimplementedFormatString {
fn try_parse(
tcx: TyCtxt<'tcx>,
trait_def_id: DefId,
from: LocalInternedString,
from: Symbol,
err_sp: Span,
) -> Result<Self, ErrorReported> {
let result = OnUnimplementedFormatString(from);
Expand All @@ -234,7 +233,8 @@ impl<'tcx> OnUnimplementedFormatString {
) -> Result<(), ErrorReported> {
let name = tcx.item_name(trait_def_id);
let generics = tcx.generics_of(trait_def_id);
let parser = Parser::new(&self.0, None, vec![], false);
let s = self.0.as_str();
let parser = Parser::new(&s, None, vec![], false);
let mut result = Ok(());
for token in parser {
match token {
Expand Down Expand Up @@ -294,7 +294,8 @@ impl<'tcx> OnUnimplementedFormatString {
}).collect::<FxHashMap<Symbol, String>>();
let empty_string = String::new();

let parser = Parser::new(&self.0, None, vec![], false);
let s = self.0.as_str();
let parser = Parser::new(&s, None, vec![], false);
parser.map(|p|
match p {
Piece::String(s) => s,
Expand Down
6 changes: 1 addition & 5 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ use std::ops::Range;
use syntax::ast::{self, Name, Ident, NodeId};
use syntax::attr;
use syntax::ext::hygiene::ExpnId;
use syntax::symbol::{kw, sym, Symbol, LocalInternedString, InternedString};
use syntax::symbol::{kw, sym, Symbol, InternedString};
use syntax_pos::Span;

use smallvec;
Expand Down Expand Up @@ -3386,10 +3386,6 @@ impl SymbolName {
name: InternedString::intern(name)
}
}

pub fn as_str(&self) -> LocalInternedString {
self.name.as_str()
}
}

impl fmt::Display for SymbolName {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_codegen_llvm/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::context::CodegenCx;
use crate::type_::Type;
use crate::type_of::LayoutLlvmExt;
use crate::value::Value;
use syntax::symbol::LocalInternedString;
use rustc_codegen_ssa::common::{IntPredicate, TypeKind, RealPredicate};
use rustc_codegen_ssa::MemFlags;
use libc::{c_uint, c_char};
Expand All @@ -24,6 +23,7 @@ use std::ffi::CStr;
use std::ops::{Deref, Range};
use std::ptr;
use std::iter::TrustedLen;
use syntax::symbol::Symbol;

// All Builders must have an llfn associated with them
#[must_use]
Expand Down Expand Up @@ -1082,8 +1082,8 @@ impl StaticBuilderMethods for Builder<'a, 'll, 'tcx> {

fn static_panic_msg(
&mut self,
msg: Option<LocalInternedString>,
filename: LocalInternedString,
msg: Option<Symbol>,
filename: Symbol,
line: Self::Value,
col: Self::Value,
kind: &str,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn get_fn(
return llfn;
}

let sym = tcx.symbol_name(instance).as_str();
let sym = tcx.symbol_name(instance).name.as_str();
debug!("get_fn({:?}: {:?}) => {}", instance, sig, sym);

// Create a fn pointer with the substituted signature.
Expand Down
13 changes: 7 additions & 6 deletions src/librustc_codegen_llvm/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_codegen_ssa::mir::place::PlaceRef;

use libc::{c_uint, c_char};

use syntax::symbol::LocalInternedString;
use syntax::symbol::Symbol;
use syntax::ast::Mutability;

pub use crate::context::CodegenCx;
Expand Down Expand Up @@ -122,17 +122,18 @@ impl CodegenCx<'ll, 'tcx> {

fn const_cstr(
&self,
s: LocalInternedString,
s: Symbol,
null_terminated: bool,
) -> &'ll Value {
unsafe {
if let Some(&llval) = self.const_cstr_cache.borrow().get(&s) {
return llval;
}

let s_str = s.as_str();
let sc = llvm::LLVMConstStringInContext(self.llcx,
s.as_ptr() as *const c_char,
s.len() as c_uint,
s_str.as_ptr() as *const c_char,
s_str.len() as c_uint,
!null_terminated as Bool);
let sym = self.generate_local_symbol_name("str");
let g = self.define_global(&sym[..], self.val_ty(sc)).unwrap_or_else(||{
Expand All @@ -147,8 +148,8 @@ impl CodegenCx<'ll, 'tcx> {
}
}

pub fn const_str_slice(&self, s: LocalInternedString) -> &'ll Value {
let len = s.len();
pub fn const_str_slice(&self, s: Symbol) -> &'ll Value {
let len = s.as_str().len();
let cs = consts::ptrcast(self.const_cstr(s, false),
self.type_ptr_to(self.layout_of(self.tcx.mk_str()).llvm_type(self)));
self.const_fat_ptr(cs, self.const_usize(len as u64))
Expand Down
15 changes: 8 additions & 7 deletions src/librustc_codegen_llvm/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ use rustc::mir::interpret::{ConstValue, Allocation, read_target_uint,
Pointer, ErrorHandled, GlobalId};
use rustc::mir::mono::MonoItem;
use rustc::hir::Node;
use syntax_pos::Span;
use rustc_target::abi::HasDataLayout;
use syntax::symbol::sym;
use syntax_pos::symbol::LocalInternedString;
use rustc::ty::{self, Ty, Instance};
use rustc_codegen_ssa::traits::*;
use syntax::symbol::{Symbol, sym};
use syntax_pos::Span;

use rustc::ty::layout::{self, Size, Align, LayoutOf};

Expand Down Expand Up @@ -122,10 +121,11 @@ fn check_and_apply_linkage(
cx: &CodegenCx<'ll, 'tcx>,
attrs: &CodegenFnAttrs,
ty: Ty<'tcx>,
sym: LocalInternedString,
sym: Symbol,
span: Span
) -> &'ll Value {
let llty = cx.layout_of(ty).llvm_type(cx);
let sym = sym.as_str();
if let Some(linkage) = attrs.linkage {
debug!("get_static: sym={} linkage={:?}", sym, linkage);

Expand Down Expand Up @@ -221,7 +221,7 @@ impl CodegenCx<'ll, 'tcx> {
def_id);

let ty = instance.ty(self.tcx);
let sym = self.tcx.symbol_name(instance).as_str();
let sym = self.tcx.symbol_name(instance).name.as_symbol();

debug!("get_static: sym={} instance={:?}", sym, instance);

Expand All @@ -232,11 +232,12 @@ impl CodegenCx<'ll, 'tcx> {
Node::Item(&hir::Item {
ref attrs, span, node: hir::ItemKind::Static(..), ..
}) => {
if self.get_declared_value(&sym[..]).is_some() {
let sym_str = sym.as_str();
if self.get_declared_value(&sym_str).is_some() {
span_bug!(span, "Conflicting symbol names for static?");
}

let g = self.define_global(&sym[..], llty).unwrap();
let g = self.define_global(&sym_str, llty).unwrap();

if !self.tcx.is_reachable_non_generic(def_id) {
unsafe {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::cell::{Cell, RefCell};
use std::iter;
use std::str;
use std::sync::Arc;
use syntax::symbol::LocalInternedString;
use syntax::symbol::Symbol;
use syntax::source_map::{DUMMY_SP, Span};
use crate::abi::Abi;

Expand All @@ -52,7 +52,7 @@ pub struct CodegenCx<'ll, 'tcx> {
pub vtables:
RefCell<FxHashMap<(Ty<'tcx>, Option<ty::PolyExistentialTraitRef<'tcx>>), &'ll Value>>,
/// Cache of constant strings,
pub const_cstr_cache: RefCell<FxHashMap<LocalInternedString, &'ll Value>>,
pub const_cstr_cache: RefCell<FxHashMap<Symbol, &'ll Value>>,

/// Reverse-direction for const ptrs cast from globals.
/// Key is a Value holding a *T,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/debuginfo/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2251,7 +2251,7 @@ pub fn create_global_var_metadata(
None
} else {
let linkage_name = mangled_name_of_instance(cx, Instance::mono(tcx, def_id));
Some(SmallCStr::new(&linkage_name.as_str()))
Some(SmallCStr::new(&linkage_name.name.as_str()))
};

let global_align = cx.align_of(variable_type);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_llvm/debuginfo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
let scope_line = span_start(self, span).line;

let function_name = CString::new(name).unwrap();
let linkage_name = SmallCStr::new(&linkage_name.as_str());
let linkage_name = SmallCStr::new(&linkage_name.name.as_str());

let mut flags = DIFlags::FlagPrototyped;

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ fn reachable_non_generics_provider(
})
.map(|def_id| {
let export_level = if special_runtime_crate {
let name = tcx.symbol_name(Instance::mono(tcx, def_id)).as_str();
let name = tcx.symbol_name(Instance::mono(tcx, def_id)).name.as_str();
// We can probably do better here by just ensuring that
// it has hidden visibility rather than public
// visibility, as this is primarily here to ensure it's
Expand Down
11 changes: 5 additions & 6 deletions src/librustc_codegen_ssa/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::traits::*;

use std::borrow::Cow;

use syntax::symbol::LocalInternedString;
use syntax::symbol::Symbol;
use syntax_pos::Pos;

use super::{FunctionCx, LocalRef};
Expand Down Expand Up @@ -397,7 +397,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {

// Get the location information.
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
let filename = LocalInternedString::intern(&loc.file.name.to_string());
let filename = Symbol::intern(&loc.file.name.to_string());
let line = bx.const_u32(loc.line as u32);
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);

Expand All @@ -418,8 +418,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
vec![file_line_col, index, len])
}
_ => {
let str = msg.description();
let msg_str = LocalInternedString::intern(str);
let msg_str = Symbol::intern(msg.description());
let msg_file_line_col = bx.static_panic_msg(
Some(msg_str),
filename,
Expand Down Expand Up @@ -531,15 +530,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let layout = bx.layout_of(ty);
if layout.abi.is_uninhabited() {
let loc = bx.sess().source_map().lookup_char_pos(span.lo());
let filename = LocalInternedString::intern(&loc.file.name.to_string());
let filename = Symbol::intern(&loc.file.name.to_string());
let line = bx.const_u32(loc.line as u32);
let col = bx.const_u32(loc.col.to_usize() as u32 + 1);

let str = format!(
"Attempted to instantiate uninhabited type {}",
ty
);
let msg_str = LocalInternedString::intern(&str);
let msg_str = Symbol::intern(&str);
let msg_file_line_col = bx.static_panic_msg(
Some(msg_str),
filename,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/mono_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
self.to_raw_string(),
cx.codegen_unit().name());

let symbol_name = self.symbol_name(cx.tcx()).as_str();
let symbol_name = self.symbol_name(cx.tcx()).name.as_str();

debug!("symbol {}", &symbol_name);

Expand Down
Loading

0 comments on commit 4dfbaba

Please sign in to comment.