Skip to content

Commit 567ad74

Browse files
committed
Auto merge of #74175 - nnethercote:more-static-symbols, r=oli-obk
More static symbols These commits add some more static symbols and convert lots of places to use them. r? @oli-obk
2 parents 23744c8 + 5930081 commit 567ad74

File tree

67 files changed

+991
-745
lines changed

Some content is hidden

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

67 files changed

+991
-745
lines changed

src/librustc_ast/expand/allocator.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub enum AllocatorKind {
99
}
1010

1111
impl AllocatorKind {
12-
pub fn fn_name(&self, base: &str) -> String {
12+
pub fn fn_name(&self, base: Symbol) -> String {
1313
match *self {
1414
AllocatorKind::Global => format!("__rg_{}", base),
1515
AllocatorKind::Default => format!("__rdl_{}", base),
@@ -26,29 +26,29 @@ pub enum AllocatorTy {
2626
}
2727

2828
pub struct AllocatorMethod {
29-
pub name: &'static str,
29+
pub name: Symbol,
3030
pub inputs: &'static [AllocatorTy],
3131
pub output: AllocatorTy,
3232
}
3333

3434
pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
3535
AllocatorMethod {
36-
name: "alloc",
36+
name: sym::alloc,
3737
inputs: &[AllocatorTy::Layout],
3838
output: AllocatorTy::ResultPtr,
3939
},
4040
AllocatorMethod {
41-
name: "dealloc",
41+
name: sym::dealloc,
4242
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],
4343
output: AllocatorTy::Unit,
4444
},
4545
AllocatorMethod {
46-
name: "realloc",
46+
name: sym::realloc,
4747
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout, AllocatorTy::Usize],
4848
output: AllocatorTy::ResultPtr,
4949
},
5050
AllocatorMethod {
51-
name: "alloc_zeroed",
51+
name: sym::alloc_zeroed,
5252
inputs: &[AllocatorTy::Layout],
5353
output: AllocatorTy::ResultPtr,
5454
},
@@ -70,7 +70,7 @@ pub fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> {
7070
}
7171
}
7272

73-
let name = Symbol::intern(&AllocatorKind::Global.fn_name("alloc"));
73+
let name = Symbol::intern(&AllocatorKind::Global.fn_name(sym::alloc));
7474
let mut f = Finder { name, spans: Vec::new() };
7575
visit::walk_crate(&mut f, krate);
7676
f.spans

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
@@ -533,6 +533,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
533533
self.word(st)
534534
}
535535

536+
fn print_symbol(&mut self, sym: Symbol, style: ast::StrStyle) {
537+
self.print_string(&sym.as_str(), style);
538+
}
539+
536540
fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) {
537541
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
538542
}
@@ -2061,7 +2065,7 @@ impl<'a> State<'a> {
20612065
let print_reg_or_class = |s: &mut Self, r: &InlineAsmRegOrRegClass| match r
20622066
{
20632067
InlineAsmRegOrRegClass::Reg(r) => {
2064-
s.print_string(&r.as_str(), ast::StrStyle::Cooked)
2068+
s.print_symbol(*r, ast::StrStyle::Cooked)
20652069
}
20662070
InlineAsmRegOrRegClass::RegClass(r) => s.word(r.to_string()),
20672071
};
@@ -2155,7 +2159,7 @@ impl<'a> State<'a> {
21552159
ast::ExprKind::LlvmInlineAsm(ref a) => {
21562160
self.s.word("llvm_asm!");
21572161
self.popen();
2158-
self.print_string(&a.asm.as_str(), a.asm_str_style);
2162+
self.print_symbol(a.asm, a.asm_str_style);
21592163
self.word_space(":");
21602164

21612165
self.commasep(Inconsistent, &a.outputs, |s, out| {
@@ -2175,16 +2179,16 @@ impl<'a> State<'a> {
21752179
self.word_space(":");
21762180

21772181
self.commasep(Inconsistent, &a.inputs, |s, &(co, ref o)| {
2178-
s.print_string(&co.as_str(), ast::StrStyle::Cooked);
2182+
s.print_symbol(co, ast::StrStyle::Cooked);
21792183
s.popen();
21802184
s.print_expr(o);
21812185
s.pclose();
21822186
});
21832187
self.s.space();
21842188
self.word_space(":");
21852189

2186-
self.commasep(Inconsistent, &a.clobbers, |s, co| {
2187-
s.print_string(&co.as_str(), ast::StrStyle::Cooked);
2190+
self.commasep(Inconsistent, &a.clobbers, |s, &co| {
2191+
s.print_symbol(co, ast::StrStyle::Cooked);
21882192
});
21892193

21902194
let mut options = vec![];

src/librustc_attr/builtin.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1041,10 +1041,10 @@ pub fn find_transparency(
10411041
break;
10421042
} else if let Some(value) = attr.value_str() {
10431043
transparency = Some((
1044-
match &*value.as_str() {
1045-
"transparent" => Transparency::Transparent,
1046-
"semitransparent" => Transparency::SemiTransparent,
1047-
"opaque" => Transparency::Opaque,
1044+
match value {
1045+
sym::transparent => Transparency::Transparent,
1046+
sym::semitransparent => Transparency::SemiTransparent,
1047+
sym::opaque => Transparency::Opaque,
10481048
_ => {
10491049
error = Some(TransparencyError::UnknownTransparency(value, attr.span));
10501050
continue;

src/librustc_builtin_macros/deriving/clone.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn expand_deriving_clone(
8484
is_unsafe: false,
8585
supports_unions: true,
8686
methods: vec![MethodDef {
87-
name: "clone",
87+
name: sym::clone,
8888
generics: LifetimeBounds::empty(),
8989
explicit_self: borrowed_explicit_self(),
9090
args: Vec::new(),

src/librustc_builtin_macros/deriving/cmp/eq.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub fn expand_deriving_eq(
2828
is_unsafe: false,
2929
supports_unions: true,
3030
methods: vec![MethodDef {
31-
name: "assert_receiver_is_total_eq",
31+
name: sym::assert_receiver_is_total_eq,
3232
generics: LifetimeBounds::empty(),
3333
explicit_self: borrowed_explicit_self(),
3434
args: vec![],

src/librustc_builtin_macros/deriving/cmp/ord.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub fn expand_deriving_ord(
2626
is_unsafe: false,
2727
supports_unions: false,
2828
methods: vec![MethodDef {
29-
name: "cmp",
29+
name: sym::cmp,
3030
generics: LifetimeBounds::empty(),
3131
explicit_self: borrowed_explicit_self(),
3232
args: vec![(borrowed_self(), "other")],

src/librustc_builtin_macros/deriving/cmp/partial_eq.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ pub fn expand_deriving_partial_eq(
9292
// avoid defining `ne` if we can
9393
// c-like enums, enums without any fields and structs without fields
9494
// can safely define only `eq`.
95-
let mut methods = vec![md!("eq", cs_eq)];
95+
let mut methods = vec![md!(sym::eq, cs_eq)];
9696
if !is_type_without_fields(item) {
97-
methods.push(md!("ne", cs_ne));
97+
methods.push(md!(sym::ne, cs_ne));
9898
}
9999

100100
let trait_def = TraitDef {

src/librustc_builtin_macros/deriving/cmp/partial_ord.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ pub fn expand_deriving_partial_ord(
4949
let attrs = vec![cx.attribute(inline)];
5050

5151
let partial_cmp_def = MethodDef {
52-
name: "partial_cmp",
52+
name: sym::partial_cmp,
5353
generics: LifetimeBounds::empty(),
5454
explicit_self: borrowed_explicit_self(),
5555
args: vec![(borrowed_self(), "other")],
@@ -70,10 +70,10 @@ pub fn expand_deriving_partial_ord(
7070
} else {
7171
vec![
7272
partial_cmp_def,
73-
md!("lt", true, false),
74-
md!("le", true, true),
75-
md!("gt", false, false),
76-
md!("ge", false, true),
73+
md!(sym::lt, true, false),
74+
md!(sym::le, true, true),
75+
md!(sym::gt, false, false),
76+
md!(sym::ge, false, true),
7777
]
7878
};
7979

@@ -108,14 +108,14 @@ pub fn some_ordering_collapsed(
108108
) -> P<ast::Expr> {
109109
let lft = cx.expr_ident(span, self_arg_tags[0]);
110110
let rgt = cx.expr_addr_of(span, cx.expr_ident(span, self_arg_tags[1]));
111-
let op_str = match op {
112-
PartialCmpOp => "partial_cmp",
113-
LtOp => "lt",
114-
LeOp => "le",
115-
GtOp => "gt",
116-
GeOp => "ge",
111+
let op_sym = match op {
112+
PartialCmpOp => sym::partial_cmp,
113+
LtOp => sym::lt,
114+
LeOp => sym::le,
115+
GtOp => sym::gt,
116+
GeOp => sym::ge,
117117
};
118-
cx.expr_method_call(span, lft, cx.ident_of(op_str, span), vec![rgt])
118+
cx.expr_method_call(span, lft, Ident::new(op_sym, span), vec![rgt])
119119
}
120120

121121
pub fn cs_partial_cmp(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> P<Expr> {

src/librustc_builtin_macros/deriving/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn expand_deriving_debug(
2929
is_unsafe: false,
3030
supports_unions: false,
3131
methods: vec![MethodDef {
32-
name: "fmt",
32+
name: sym::fmt,
3333
generics: LifetimeBounds::empty(),
3434
explicit_self: borrowed_explicit_self(),
3535
args: vec![(fmtr, "f")],

src/librustc_builtin_macros/deriving/decodable.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_ast::ast;
88
use rustc_ast::ast::{Expr, MetaItem, Mutability};
99
use rustc_ast::ptr::P;
1010
use rustc_expand::base::{Annotatable, ExtCtxt};
11-
use rustc_span::symbol::Symbol;
11+
use rustc_span::symbol::{sym, Symbol};
1212
use rustc_span::Span;
1313

1414
pub fn expand_deriving_rustc_decodable(
@@ -30,7 +30,7 @@ pub fn expand_deriving_rustc_decodable(
3030
is_unsafe: false,
3131
supports_unions: false,
3232
methods: vec![MethodDef {
33-
name: "decode",
33+
name: sym::decode,
3434
generics: LifetimeBounds {
3535
lifetimes: Vec::new(),
3636
bounds: vec![(

0 commit comments

Comments
 (0)