Skip to content

Commit 5930081

Browse files
committed
Remove lots of Symbol::as_str() calls.
In various ways, such as changing functions to take a `Symbol` instead of a `&str`.
1 parent f04e866 commit 5930081

File tree

30 files changed

+121
-96
lines changed

30 files changed

+121
-96
lines changed

src/librustc_ast/util/comments.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub use CommentStyle::*;
22

33
use crate::ast;
44
use rustc_span::source_map::SourceMap;
5-
use rustc_span::{BytePos, CharPos, FileName, Pos};
5+
use rustc_span::{BytePos, CharPos, FileName, Pos, Symbol};
66

77
use log::debug;
88

@@ -52,7 +52,8 @@ pub fn is_doc_comment(s: &str) -> bool {
5252
|| s.starts_with("/*!")
5353
}
5454

55-
pub fn doc_comment_style(comment: &str) -> ast::AttrStyle {
55+
pub fn doc_comment_style(comment: Symbol) -> ast::AttrStyle {
56+
let comment = &comment.as_str();
5657
assert!(is_doc_comment(comment));
5758
if comment.starts_with("//!") || comment.starts_with("/*!") {
5859
ast::AttrStyle::Inner
@@ -61,7 +62,9 @@ pub fn doc_comment_style(comment: &str) -> ast::AttrStyle {
6162
}
6263
}
6364

64-
pub fn strip_doc_comment_decoration(comment: &str) -> String {
65+
pub fn strip_doc_comment_decoration(comment: Symbol) -> String {
66+
let comment = &comment.as_str();
67+
6568
/// remove whitespace-only lines from the start/end of lines
6669
fn vertical_trim(lines: Vec<String>) -> Vec<String> {
6770
let mut i = 0;
+37-26
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,58 @@
11
use super::*;
2+
use crate::with_default_session_globals;
23

34
#[test]
45
fn test_block_doc_comment_1() {
5-
let comment = "/**\n * Test \n ** Test\n * Test\n*/";
6-
let stripped = strip_doc_comment_decoration(comment);
7-
assert_eq!(stripped, " Test \n* Test\n Test");
6+
with_default_session_globals(|| {
7+
let comment = "/**\n * Test \n ** Test\n * Test\n*/";
8+
let stripped = strip_doc_comment_decoration(Symbol::intern(comment));
9+
assert_eq!(stripped, " Test \n* Test\n Test");
10+
})
811
}
912

1013
#[test]
1114
fn test_block_doc_comment_2() {
12-
let comment = "/**\n * Test\n * Test\n*/";
13-
let stripped = strip_doc_comment_decoration(comment);
14-
assert_eq!(stripped, " Test\n Test");
15+
with_default_session_globals(|| {
16+
let comment = "/**\n * Test\n * Test\n*/";
17+
let stripped = strip_doc_comment_decoration(Symbol::intern(comment));
18+
assert_eq!(stripped, " Test\n Test");
19+
})
1520
}
1621

1722
#[test]
1823
fn test_block_doc_comment_3() {
19-
let comment = "/**\n let a: *i32;\n *a = 5;\n*/";
20-
let stripped = strip_doc_comment_decoration(comment);
21-
assert_eq!(stripped, " let a: *i32;\n *a = 5;");
24+
with_default_session_globals(|| {
25+
let comment = "/**\n let a: *i32;\n *a = 5;\n*/";
26+
let stripped = strip_doc_comment_decoration(Symbol::intern(comment));
27+
assert_eq!(stripped, " let a: *i32;\n *a = 5;");
28+
})
2229
}
2330

2431
#[test]
2532
fn test_block_doc_comment_4() {
26-
let comment = "/*******************\n test\n *********************/";
27-
let stripped = strip_doc_comment_decoration(comment);
28-
assert_eq!(stripped, " test");
33+
with_default_session_globals(|| {
34+
let comment = "/*******************\n test\n *********************/";
35+
let stripped = strip_doc_comment_decoration(Symbol::intern(comment));
36+
assert_eq!(stripped, " test");
37+
})
2938
}
3039

3140
#[test]
3241
fn test_line_doc_comment() {
33-
let stripped = strip_doc_comment_decoration("/// test");
34-
assert_eq!(stripped, " test");
35-
let stripped = strip_doc_comment_decoration("///! test");
36-
assert_eq!(stripped, " test");
37-
let stripped = strip_doc_comment_decoration("// test");
38-
assert_eq!(stripped, " test");
39-
let stripped = strip_doc_comment_decoration("// test");
40-
assert_eq!(stripped, " test");
41-
let stripped = strip_doc_comment_decoration("///test");
42-
assert_eq!(stripped, "test");
43-
let stripped = strip_doc_comment_decoration("///!test");
44-
assert_eq!(stripped, "test");
45-
let stripped = strip_doc_comment_decoration("//test");
46-
assert_eq!(stripped, "test");
42+
with_default_session_globals(|| {
43+
let stripped = strip_doc_comment_decoration(Symbol::intern("/// test"));
44+
assert_eq!(stripped, " test");
45+
let stripped = strip_doc_comment_decoration(Symbol::intern("///! test"));
46+
assert_eq!(stripped, " test");
47+
let stripped = strip_doc_comment_decoration(Symbol::intern("// test"));
48+
assert_eq!(stripped, " test");
49+
let stripped = strip_doc_comment_decoration(Symbol::intern("// test"));
50+
assert_eq!(stripped, " test");
51+
let stripped = strip_doc_comment_decoration(Symbol::intern("///test"));
52+
assert_eq!(stripped, "test");
53+
let stripped = strip_doc_comment_decoration(Symbol::intern("///!test"));
54+
assert_eq!(stripped, "test");
55+
let stripped = strip_doc_comment_decoration(Symbol::intern("//test"));
56+
assert_eq!(stripped, "test");
57+
})
4758
}

src/librustc_ast/util/lev_distance.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ pub fn lev_distance(a: &str, b: &str) -> usize {
4747
/// a lower(upper)case letters mismatch.
4848
pub fn find_best_match_for_name<'a, T>(
4949
iter_names: T,
50-
lookup: &str,
50+
lookup: Symbol,
5151
dist: Option<usize>,
5252
) -> Option<Symbol>
5353
where
5454
T: Iterator<Item = &'a Symbol>,
5555
{
56+
let lookup = &lookup.as_str();
5657
let max_dist = dist.map_or_else(|| cmp::max(lookup.len(), 3) / 3, |d| d);
5758
let name_vec: Vec<&Symbol> = iter_names.collect();
5859

src/librustc_ast/util/lev_distance/tests.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,34 @@ fn test_find_best_match_for_name() {
2525
with_default_session_globals(|| {
2626
let input = vec![Symbol::intern("aaab"), Symbol::intern("aaabc")];
2727
assert_eq!(
28-
find_best_match_for_name(input.iter(), "aaaa", None),
28+
find_best_match_for_name(input.iter(), Symbol::intern("aaaa"), None),
2929
Some(Symbol::intern("aaab"))
3030
);
3131

32-
assert_eq!(find_best_match_for_name(input.iter(), "1111111111", None), None);
32+
assert_eq!(
33+
find_best_match_for_name(input.iter(), Symbol::intern("1111111111"), None),
34+
None
35+
);
3336

3437
let input = vec![Symbol::intern("aAAA")];
3538
assert_eq!(
36-
find_best_match_for_name(input.iter(), "AAAA", None),
39+
find_best_match_for_name(input.iter(), Symbol::intern("AAAA"), None),
3740
Some(Symbol::intern("aAAA"))
3841
);
3942

4043
let input = vec![Symbol::intern("AAAA")];
4144
// Returns None because `lev_distance > max_dist / 3`
42-
assert_eq!(find_best_match_for_name(input.iter(), "aaaa", None), None);
45+
assert_eq!(find_best_match_for_name(input.iter(), Symbol::intern("aaaa"), None), None);
4346

4447
let input = vec![Symbol::intern("AAAA")];
4548
assert_eq!(
46-
find_best_match_for_name(input.iter(), "aaaa", Some(4)),
49+
find_best_match_for_name(input.iter(), Symbol::intern("aaaa"), Some(4)),
4750
Some(Symbol::intern("AAAA"))
4851
);
4952

5053
let input = vec![Symbol::intern("a_longer_variable_name")];
5154
assert_eq!(
52-
find_best_match_for_name(input.iter(), "a_variable_longer_name", None),
55+
find_best_match_for_name(input.iter(), Symbol::intern("a_variable_longer_name"), None),
5356
Some(Symbol::intern("a_longer_variable_name"))
5457
);
5558
})

src/librustc_ast_pretty/pprust.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
522522
self.word(st)
523523
}
524524

525+
fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
526+
self.print_string(&sym.as_str(), style);
527+
}
528+
525529
fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) {
526530
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
527531
}
@@ -2050,7 +2054,7 @@ impl<'a> State<'a> {
20502054
let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r
20512055
{
20522056
InlineAsmRegOrRegClass::Reg(r) => {
2053-
s.print_string(&r.as_str(), ast::StrStyle::Cooked)
2057+
s.print_symbol(*r, ast::StrStyle::Cooked)
20542058
}
20552059
InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()),
20562060
};
@@ -2144,7 +2148,7 @@ impl<'a> State<'a> {
21442148
ast::ExprKind::LlvmInlineAsm(ref a) => {
21452149
self.s.word("llvm_asm!");
21462150
self.popen();
2147-
self.print_string(&a.asm.as_str(), a.asm_str_style);
2151+
self.print_symbol(a.asm, a.asm_str_style);
21482152
self.word_space(":");
21492153

21502154
self.commasep(Inconsistent, &a.outputs, |s, out| {
@@ -2164,16 +2168,16 @@ impl<'a> State<'a> {
21642168
self.word_space(":");
21652169

21662170
self.commasep(Inconsistent, &a.inputs, |s, &(co, ref o)| {
2167-
s.print_string(&co.as_str(), ast::StrStyle::Cooked);
2171+
s.print_symbol(co, ast::StrStyle::Cooked);
21682172
s.popen();
21692173
s.print_expr(o);
21702174
s.pclose();
21712175
});
21722176
self.s.space();
21732177
self.word_space(":");
21742178

2175-
self.commasep(Inconsistent, &a.clobbers, |s, co| {
2176-
s.print_string(&co.as_str(), ast::StrStyle::Cooked);
2179+
self.commasep(Inconsistent, &a.clobbers, |s, &co| {
2180+
s.print_symbol(co, ast::StrStyle::Cooked);
21772181
});
21782182

21792183
let mut options = vec![];

src/librustc_expand/proc_macro_server.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ impl FromInternal<(TreeAndJoint, &'_ ParseSess, &'_ mut Vec<Self>)>
149149
}
150150
Literal(lit) => tt!(Literal { lit }),
151151
DocComment(c) => {
152-
let style = comments::doc_comment_style(&c.as_str());
153-
let stripped = comments::strip_doc_comment_decoration(&c.as_str());
152+
let style = comments::doc_comment_style(c);
153+
let stripped = comments::strip_doc_comment_decoration(c);
154154
let mut escaped = String::new();
155155
for ch in stripped.chars() {
156156
escaped.extend(ch.escape_debug());

src/librustc_hir_pretty/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1557,7 +1557,7 @@ impl<'a> State<'a> {
15571557
let i = &a.inner;
15581558
self.s.word("llvm_asm!");
15591559
self.popen();
1560-
self.print_string(&i.asm.as_str(), i.asm_str_style);
1560+
self.print_symbol(i.asm, i.asm_str_style);
15611561
self.word_space(":");
15621562

15631563
let mut out_idx = 0;
@@ -1579,8 +1579,8 @@ impl<'a> State<'a> {
15791579
self.word_space(":");
15801580

15811581
let mut in_idx = 0;
1582-
self.commasep(Inconsistent, &i.inputs, |s, co| {
1583-
s.print_string(&co.as_str(), ast::StrStyle::Cooked);
1582+
self.commasep(Inconsistent, &i.inputs, |s, &co| {
1583+
s.print_symbol(co, ast::StrStyle::Cooked);
15841584
s.popen();
15851585
s.print_expr(&a.inputs_exprs[in_idx]);
15861586
s.pclose();
@@ -1589,8 +1589,8 @@ impl<'a> State<'a> {
15891589
self.s.space();
15901590
self.word_space(":");
15911591

1592-
self.commasep(Inconsistent, &i.clobbers, |s, co| {
1593-
s.print_string(&co.as_str(), ast::StrStyle::Cooked);
1592+
self.commasep(Inconsistent, &i.clobbers, |s, &co| {
1593+
s.print_symbol(co, ast::StrStyle::Cooked);
15941594
});
15951595

15961596
let mut options = vec![];

src/librustc_incremental/assert_module_sources.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ impl AssertModuleSource<'tcx> {
139139
}
140140

141141
self.tcx.sess.cgu_reuse_tracker.set_expectation(
142-
&cgu_name.as_str(),
142+
cgu_name,
143143
&user_path,
144144
attr.span,
145145
expected_reuse,

src/librustc_incremental/persist/dirty_clean.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl DirtyCleanVisitor<'tcx> {
234234
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
235235
if item.check_name(LABEL) {
236236
let value = expect_associated_value(self.tcx, &item);
237-
return Some(self.resolve_labels(&item, &value.as_str()));
237+
return Some(self.resolve_labels(&item, value));
238238
}
239239
}
240240
None
@@ -245,7 +245,7 @@ impl DirtyCleanVisitor<'tcx> {
245245
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
246246
if item.check_name(EXCEPT) {
247247
let value = expect_associated_value(self.tcx, &item);
248-
return self.resolve_labels(&item, &value.as_str());
248+
return self.resolve_labels(&item, value);
249249
}
250250
}
251251
// if no `label` or `except` is given, only the node's group are asserted
@@ -347,9 +347,9 @@ impl DirtyCleanVisitor<'tcx> {
347347
(name, labels)
348348
}
349349

350-
fn resolve_labels(&self, item: &NestedMetaItem, value: &str) -> Labels {
350+
fn resolve_labels(&self, item: &NestedMetaItem, value: Symbol) -> Labels {
351351
let mut out = Labels::default();
352-
for label in value.split(',') {
352+
for label in value.as_str().split(',') {
353353
let label = label.trim();
354354
if DepNode::has_label_string(label) {
355355
if out.contains(label) {

src/librustc_interface/util.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,8 @@ pub(crate) fn check_attr_crate_type(attrs: &[ast::Attribute], lint_buffer: &mut
427427

428428
if let ast::MetaItemKind::NameValue(spanned) = a.meta().unwrap().kind {
429429
let span = spanned.span;
430-
let lev_candidate = find_best_match_for_name(
431-
CRATE_TYPES.iter().map(|(k, _)| k),
432-
&n.as_str(),
433-
None,
434-
);
430+
let lev_candidate =
431+
find_best_match_for_name(CRATE_TYPES.iter().map(|(k, _)| k), n, None);
435432
if let Some(candidate) = lev_candidate {
436433
lint_buffer.buffer_lint_with_diagnostic(
437434
lint::builtin::UNKNOWN_CRATE_TYPES,

src/librustc_lint/context.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,11 @@ impl LintStore {
394394
let symbols =
395395
self.by_name.keys().map(|name| Symbol::intern(&name)).collect::<Vec<_>>();
396396

397-
let suggestion =
398-
find_best_match_for_name(symbols.iter(), &lint_name.to_lowercase(), None);
397+
let suggestion = find_best_match_for_name(
398+
symbols.iter(),
399+
Symbol::intern(&lint_name.to_lowercase()),
400+
None,
401+
);
399402

400403
CheckLintNameResult::NoLint(suggestion)
401404
}

src/librustc_metadata/link_args.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_hir as hir;
22
use rustc_hir::itemlikevisit::ItemLikeVisitor;
33
use rustc_middle::ty::TyCtxt;
4-
use rustc_span::symbol::sym;
4+
use rustc_span::symbol::{sym, Symbol};
55
use rustc_target::spec::abi::Abi;
66

77
crate fn collect(tcx: TyCtxt<'_>) -> Vec<String> {
@@ -11,7 +11,7 @@ crate fn collect(tcx: TyCtxt<'_>) -> Vec<String> {
1111
for attr in tcx.hir().krate().item.attrs.iter() {
1212
if attr.has_name(sym::link_args) {
1313
if let Some(linkarg) = attr.value_str() {
14-
collector.add_link_args(&linkarg.as_str());
14+
collector.add_link_args(linkarg);
1515
}
1616
}
1717
}
@@ -36,7 +36,7 @@ impl<'tcx> ItemLikeVisitor<'tcx> for Collector {
3636
// First, add all of the custom #[link_args] attributes
3737
for m in it.attrs.iter().filter(|a| a.check_name(sym::link_args)) {
3838
if let Some(linkarg) = m.value_str() {
39-
self.add_link_args(&linkarg.as_str());
39+
self.add_link_args(linkarg);
4040
}
4141
}
4242
}
@@ -46,7 +46,7 @@ impl<'tcx> ItemLikeVisitor<'tcx> for Collector {
4646
}
4747

4848
impl Collector {
49-
fn add_link_args(&mut self, args: &str) {
50-
self.args.extend(args.split(' ').filter(|s| !s.is_empty()).map(|s| s.to_string()))
49+
fn add_link_args(&mut self, args: Symbol) {
50+
self.args.extend(args.as_str().split(' ').filter(|s| !s.is_empty()).map(|s| s.to_string()))
5151
}
5252
}

src/librustc_middle/mir/mono.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,7 @@ impl CodegenUnitNameBuilder<'tcx> {
448448
if self.tcx.sess.opts.debugging_opts.human_readable_cgu_names {
449449
cgu_name
450450
} else {
451-
let cgu_name = &cgu_name.as_str();
452-
Symbol::intern(&CodegenUnit::mangle_name(cgu_name))
451+
Symbol::intern(&CodegenUnit::mangle_name(&cgu_name.as_str()))
453452
}
454453
}
455454

src/librustc_parse/parser/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl<'a> Parser<'a> {
7474
}
7575

7676
fn mk_doc_comment(&self, s: Symbol) -> ast::Attribute {
77-
attr::mk_doc_comment(comments::doc_comment_style(&s.as_str()), s, self.token.span)
77+
attr::mk_doc_comment(comments::doc_comment_style(s), s, self.token.span)
7878
}
7979

8080
/// Matches `attribute = # ! [ meta_item ]`.

0 commit comments

Comments
 (0)