Skip to content

Commit 04c7d30

Browse files
committed
Store a Symbol in InternedString
1 parent e687205 commit 04c7d30

File tree

102 files changed

+687
-569
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+687
-569
lines changed

src/libproc_macro/lib.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,11 @@ impl TokenTree {
678678
},
679679
TokenNode::Term(symbol) => {
680680
let ident = ast::Ident { name: symbol.0, ctxt: self.span.0.ctxt() };
681-
let token =
682-
if symbol.0.as_str().starts_with("'") { Lifetime(ident) } else { Ident(ident) };
681+
let token = if symbol.0.with_str(|str| str.starts_with("'")) {
682+
Lifetime(ident)
683+
} else {
684+
Ident(ident)
685+
};
683686
return TokenTree::Token(self.span.0, token).into();
684687
}
685688
TokenNode::Literal(token) => return TokenTree::Token(self.span.0, token.0).into(),

src/libproc_macro/quote.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl Quote for usize {
162162

163163
impl Quote for Term {
164164
fn quote(self) -> TokenStream {
165-
quote!(::Term::intern((quote self.as_str())))
165+
self.with_str(|str| quote!(::Term::intern((quote str))))
166166
}
167167
}
168168

src/librustc/hir/check_attr.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ impl<'a> CheckAttrVisitor<'a> {
4949
/// Check any attribute.
5050
fn check_attribute(&self, attr: &ast::Attribute, item: &ast::Item, target: Target) {
5151
if let Some(name) = attr.name() {
52-
match &*name.as_str() {
52+
name.with_str(|str| match str {
5353
"inline" => self.check_inline(attr, item, target),
5454
"repr" => self.check_repr(attr, item, target),
5555
_ => (),
56-
}
56+
})
5757
}
5858
}
5959

@@ -86,7 +86,7 @@ impl<'a> CheckAttrVisitor<'a> {
8686
None => continue,
8787
};
8888

89-
let (message, label) = match &*name.as_str() {
89+
let (message, label) = match &*name.to_string() {
9090
"C" => {
9191
is_c = true;
9292
if target != Target::Struct &&

src/librustc/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1844,7 +1844,7 @@ pub struct StructField {
18441844
impl StructField {
18451845
// Still necessary in couple of places
18461846
pub fn is_positional(&self) -> bool {
1847-
let first = self.name.as_str().as_bytes()[0];
1847+
let first = self.name.with_str(|str| str.as_bytes()[0]);
18481848
first >= b'0' && first <= b'9'
18491849
}
18501850
}

src/librustc/hir/print.rs

+22-19
Original file line numberDiff line numberDiff line change
@@ -528,11 +528,13 @@ impl<'a> State<'a> {
528528
self.head(&visibility_qualified(&item.vis, "extern crate"))?;
529529
if let Some(p) = *optional_path {
530530
let val = p.as_str();
531-
if val.contains("-") {
532-
self.print_string(&val, ast::StrStyle::Cooked)?;
533-
} else {
534-
self.print_name(p)?;
535-
}
531+
val.with(|str| {
532+
if str.contains("-") {
533+
self.print_string(str, ast::StrStyle::Cooked)
534+
} else {
535+
self.print_name(p)
536+
}
537+
})?;
536538
self.s.space()?;
537539
self.s.word("as")?;
538540
self.s.space()?;
@@ -623,7 +625,7 @@ impl<'a> State<'a> {
623625
}
624626
hir::ItemGlobalAsm(ref ga) => {
625627
self.head(&visibility_qualified(&item.vis, "global asm"))?;
626-
self.s.word(&ga.asm.as_str())?;
628+
ga.asm.with_str(|str| self.s.word(str))?;
627629
self.end()?
628630
}
629631
hir::ItemTy(ref ty, ref generics) => {
@@ -1469,20 +1471,21 @@ impl<'a> State<'a> {
14691471
hir::ExprInlineAsm(ref a, ref outputs, ref inputs) => {
14701472
self.s.word("asm!")?;
14711473
self.popen()?;
1472-
self.print_string(&a.asm.as_str(), a.asm_str_style)?;
1474+
a.asm.with_str(|str| self.print_string(str, a.asm_str_style))?;
14731475
self.word_space(":")?;
14741476

14751477
let mut out_idx = 0;
14761478
self.commasep(Inconsistent, &a.outputs, |s, out| {
1477-
let constraint = out.constraint.as_str();
1478-
let mut ch = constraint.chars();
1479-
match ch.next() {
1480-
Some('=') if out.is_rw => {
1481-
s.print_string(&format!("+{}", ch.as_str()),
1482-
ast::StrStyle::Cooked)?
1479+
out.constraint.with_str(|constraint| {
1480+
let mut ch = constraint.chars();
1481+
match ch.next() {
1482+
Some('=') if out.is_rw => {
1483+
s.print_string(&format!("+{}", ch.as_str()),
1484+
ast::StrStyle::Cooked)
1485+
}
1486+
_ => s.print_string(&constraint, ast::StrStyle::Cooked)
14831487
}
1484-
_ => s.print_string(&constraint, ast::StrStyle::Cooked)?,
1485-
}
1488+
})?;
14861489
s.popen()?;
14871490
s.print_expr(&outputs[out_idx])?;
14881491
s.pclose()?;
@@ -1494,7 +1497,7 @@ impl<'a> State<'a> {
14941497

14951498
let mut in_idx = 0;
14961499
self.commasep(Inconsistent, &a.inputs, |s, co| {
1497-
s.print_string(&co.as_str(), ast::StrStyle::Cooked)?;
1500+
co.with_str(|str| s.print_string(str, ast::StrStyle::Cooked))?;
14981501
s.popen()?;
14991502
s.print_expr(&inputs[in_idx])?;
15001503
s.pclose()?;
@@ -1505,7 +1508,7 @@ impl<'a> State<'a> {
15051508
self.word_space(":")?;
15061509

15071510
self.commasep(Inconsistent, &a.clobbers, |s, co| {
1508-
s.print_string(&co.as_str(), ast::StrStyle::Cooked)?;
1511+
co.with_str(|str| s.print_string(str, ast::StrStyle::Cooked))?;
15091512
Ok(())
15101513
})?;
15111514

@@ -1578,7 +1581,7 @@ impl<'a> State<'a> {
15781581
}
15791582

15801583
pub fn print_name(&mut self, name: ast::Name) -> io::Result<()> {
1581-
self.s.word(&name.as_str())?;
1584+
name.with_str(|str| self.s.word(str))?;
15821585
self.ann.post(self, NodeName(&name))
15831586
}
15841587

@@ -1936,7 +1939,7 @@ impl<'a> State<'a> {
19361939
self.commasep(Inconsistent, &decl.inputs, |s, ty| {
19371940
s.ibox(indent_unit)?;
19381941
if let Some(name) = arg_names.get(i) {
1939-
s.s.word(&name.node.as_str())?;
1942+
name.node.with_str(|str| s.s.word(str))?;
19401943
s.s.word(":")?;
19411944
s.s.space()?;
19421945
} else if let Some(body_id) = body_id {

src/librustc/ich/impls_syntax.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ impl<'gcx> HashStable<StableHashingContext<'gcx>> for InternedString {
3333
fn hash_stable<W: StableHasherResult>(&self,
3434
hcx: &mut StableHashingContext<'gcx>,
3535
hasher: &mut StableHasher<W>) {
36-
let s: &str = &**self;
37-
s.hash_stable(hcx, hasher);
36+
self.with(|str| str.hash_stable(hcx, hasher));
3837
}
3938
}
4039

src/librustc/lint/levels.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ impl<'a> LintLevelsBuilder<'a> {
197197
"malformed lint attribute");
198198
};
199199
for attr in attrs {
200-
let level = match attr.name().and_then(|name| Level::from_str(&name.as_str())) {
200+
let level = match attr.name()
201+
.and_then(|name| name.with_str(|str| Level::from_str(str))) {
201202
None => continue,
202203
Some(lvl) => lvl,
203204
};
@@ -221,7 +222,7 @@ impl<'a> LintLevelsBuilder<'a> {
221222
}
222223
};
223224
let name = word.name();
224-
match store.check_lint_name(&name.as_str()) {
225+
match name.with_str(|str| store.check_lint_name(str)) {
225226
CheckLintNameResult::Ok(ids) => {
226227
let src = LintSource::Node(name, li.span);
227228
for id in ids {
@@ -256,8 +257,8 @@ impl<'a> LintLevelsBuilder<'a> {
256257
src,
257258
Some(li.span.into()),
258259
&msg);
259-
if name.as_str().chars().any(|c| c.is_uppercase()) {
260-
let name_lower = name.as_str().to_lowercase();
260+
if name.with_str(|str| str.chars().any(|c| c.is_uppercase())) {
261+
let name_lower = name.with_str(|str| str.to_lowercase());
261262
if let CheckLintNameResult::NoLint =
262263
store.check_lint_name(&name_lower) {
263264
db.emit();

src/librustc/lint/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
441441
&format!("requested on the command line with `{} {}`",
442442
flag, hyphen_case_lint_name));
443443
} else {
444-
let hyphen_case_flag_val = lint_flag_val.as_str().replace("_", "-");
444+
let hyphen_case_flag_val = lint_flag_val.with_str(|str| str.replace("_", "-"));
445445
sess.diag_note_once(
446446
&mut err,
447447
DiagnosticMessageId::from(lint),

src/librustc/middle/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
533533
name: ast::Name,
534534
node_type: &str,
535535
participle: &str) {
536-
if !name.as_str().starts_with("_") {
536+
if !name.with_str(|str| str.starts_with("_")) {
537537
self.tcx
538538
.lint_node(lint::builtin::DEAD_CODE,
539539
id,

src/librustc/middle/lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct LanguageItemCollector<'a, 'tcx: 'a> {
105105
impl<'a, 'v, 'tcx> ItemLikeVisitor<'v> for LanguageItemCollector<'a, 'tcx> {
106106
fn visit_item(&mut self, item: &hir::Item) {
107107
if let Some(value) = extract(&item.attrs) {
108-
let item_index = self.item_refs.get(&*value.as_str()).cloned();
108+
let item_index = value.with_str(|str| self.item_refs.get(str).cloned());
109109

110110
if let Some(item_index) = item_index {
111111
let def_id = self.tcx.hir.local_def_id(item.id);

src/librustc/middle/recursion_limit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ fn update_limit(sess: &Session, krate: &ast::Crate, limit: &Cell<usize>,
3535
}
3636

3737
if let Some(s) = attr.value_str() {
38-
if let Some(n) = s.as_str().parse().ok() {
38+
if let Some(n) = s.with_str(|str| str.parse()).ok() {
3939
limit.set(n);
4040
return;
4141
}

src/librustc/middle/stability.rs

+28-20
Original file line numberDiff line numberDiff line change
@@ -164,26 +164,29 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
164164
if let (&Some(attr::RustcDeprecation {since: dep_since, ..}),
165165
&attr::Stable {since: stab_since}) = (&stab.rustc_depr, &stab.level) {
166166
// Explicit version of iter::order::lt to handle parse errors properly
167-
for (dep_v, stab_v) in
168-
dep_since.as_str().split(".").zip(stab_since.as_str().split(".")) {
169-
if let (Ok(dep_v), Ok(stab_v)) = (dep_v.parse::<u64>(), stab_v.parse()) {
170-
match dep_v.cmp(&stab_v) {
171-
Ordering::Less => {
172-
self.tcx.sess.span_err(item_sp, "An API can't be stabilized \
173-
after it is deprecated");
174-
break
167+
dep_since.with_str(|dep_since| stab_since.with_str(|stab_since| {
168+
for (dep_v, stab_v) in dep_since.split(".").zip(stab_since.split(".")) {
169+
if let (Ok(dep_v), Ok(stab_v)) = (dep_v.parse::<u64>(),
170+
stab_v.parse()) {
171+
match dep_v.cmp(&stab_v) {
172+
Ordering::Less => {
173+
self.tcx.sess.span_err(item_sp,
174+
"An API can't be stabilized \
175+
after it is deprecated");
176+
break
177+
}
178+
Ordering::Equal => continue,
179+
Ordering::Greater => break,
175180
}
176-
Ordering::Equal => continue,
177-
Ordering::Greater => break,
181+
} else {
182+
// Act like it isn't less because the question is now nonsensical,
183+
// and this makes us not do anything else interesting.
184+
self.tcx.sess.span_err(item_sp, "Invalid stability or deprecation \
185+
version found");
186+
break
178187
}
179-
} else {
180-
// Act like it isn't less because the question is now nonsensical,
181-
// and this makes us not do anything else interesting.
182-
self.tcx.sess.span_err(item_sp, "Invalid stability or deprecation \
183-
version found");
184-
break
185188
}
186-
}
189+
}))
187190
}
188191

189192
let hir_id = self.tcx.hir.node_to_hir_id(id);
@@ -620,8 +623,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
620623
let error_id = (DiagnosticMessageId::StabilityId(issue), span_key, msg.clone());
621624
let fresh = self.sess.one_time_diagnostics.borrow_mut().insert(error_id);
622625
if fresh {
623-
emit_feature_err(&self.sess.parse_sess, &feature.as_str(), span,
624-
GateIssue::Library(Some(issue)), &msg);
626+
feature.with_str(|str| {
627+
emit_feature_err(&self.sess.parse_sess,
628+
str,
629+
span,
630+
GateIssue::Library(Some(issue)),
631+
&msg)
632+
});
625633
}
626634
}
627635
Some(_) => {
@@ -742,7 +750,7 @@ pub fn check_unused_or_stable_features<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
742750
remaining_lib_features.remove(&Symbol::intern("proc_macro"));
743751

744752
for &(ref stable_lang_feature, span) in &sess.features.borrow().declared_stable_lang_features {
745-
let version = find_lang_feature_accepted_version(&stable_lang_feature.as_str())
753+
let version = stable_lang_feature.with_str(|str| find_lang_feature_accepted_version(str))
746754
.expect("unexpectedly couldn't find version feature was stabilized");
747755
tcx.lint_node(lint::builtin::STABLE_FEATURES,
748756
ast::CRATE_NODE_ID,

src/librustc/middle/weak_lang_items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
130130

131131
fn visit_foreign_item(&mut self, i: &hir::ForeignItem) {
132132
if let Some(lang_item) = lang_items::extract(&i.attrs) {
133-
self.register(&lang_item.as_str(), i.span);
133+
lang_item.with_str(|str| self.register(str, i.span));
134134
}
135135
intravisit::walk_foreign_item(self, i)
136136
}

src/librustc/mir/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1652,7 +1652,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
16521652
CtorKind::Fictive => {
16531653
let mut struct_fmt = fmt.debug_struct("");
16541654
for (field, place) in variant_def.fields.iter().zip(places) {
1655-
struct_fmt.field(&field.name.as_str(), place);
1655+
field.name.with_str(|str| struct_fmt.field(str, place));
16561656
}
16571657
struct_fmt.finish()
16581658
}
@@ -1671,7 +1671,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
16711671
tcx.with_freevars(node_id, |freevars| {
16721672
for (freevar, place) in freevars.iter().zip(places) {
16731673
let var_name = tcx.hir.name(freevar.var_id());
1674-
struct_fmt.field(&var_name.as_str(), place);
1674+
var_name.with_str(|str| struct_fmt.field(str, place));
16751675
}
16761676
});
16771677

@@ -1689,7 +1689,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
16891689
tcx.with_freevars(node_id, |freevars| {
16901690
for (freevar, place) in freevars.iter().zip(places) {
16911691
let var_name = tcx.hir.name(freevar.var_id());
1692-
struct_fmt.field(&var_name.as_str(), place);
1692+
var_name.with_str(|str| struct_fmt.field(str, place));
16931693
}
16941694
struct_fmt.field("$state", &places[freevars.len()]);
16951695
for i in (freevars.len() + 1)..places.len() {

src/librustc/traits/error_reporting.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -357,14 +357,14 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
357357
//
358358
// Currently I'm leaving it for what I need for `try`.
359359
if self.tcx.trait_of_item(item) == Some(trait_ref.def_id) {
360-
method = self.tcx.item_name(item);
360+
method = self.tcx.item_name(item).to_string();
361361
flags.push(("from_method", None));
362362
flags.push(("from_method", Some(&*method)));
363363
}
364364
}
365365

366366
if let Some(k) = obligation.cause.span.compiler_desugaring_kind() {
367-
desugaring = k.as_symbol().as_str();
367+
desugaring = k.as_symbol().to_string();
368368
flags.push(("from_desugaring", None));
369369
flags.push(("from_desugaring", Some(&*desugaring)));
370370
}

0 commit comments

Comments
 (0)