Skip to content

Commit 8c52267

Browse files
committed
Auto merge of #46972 - Zoxc:internedstring-no-deref, r=<try>
Store a Symbol in InternedString
2 parents 6c06bfa + c3ab779 commit 8c52267

File tree

102 files changed

+689
-571
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

+689
-571
lines changed

src/libproc_macro/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,8 @@ impl Term {
407407

408408
/// Get a reference to the interned string.
409409
#[unstable(feature = "proc_macro", issue = "38356")]
410-
pub fn as_str(&self) -> &str {
411-
unsafe { &*(&*self.0.as_str() as *const str) }
410+
pub fn with_str<F: FnOnce(&str) -> R, R>(self, f: F) -> R {
411+
self.0.with_str(f)
412412
}
413413
}
414414

@@ -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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 22 additions & 19 deletions
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

Lines changed: 1 addition & 2 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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);

0 commit comments

Comments
 (0)