Skip to content

Commit fe5f42c

Browse files
committed
Auto merge of #60630 - nnethercote:use-Symbol-more, r=petrochenkov
Use `Symbol` more A `Symbol` can be equated with a string (e.g. `&str`). This involves a TLS lookup to get the chars (and a Mutex lock in a parallel compiler) and then a char-by-char comparison. This functionality is convenient but avoids one of the main benefits of `Symbol`s, which is fast equality comparisons. This PR removes the `Symbol`/string equality operations, forcing a lot of existing string occurrences to become `Symbol`s. Fortunately, these are almost all static strings (many are attribute names) and we can add static `Symbol`s as necessary, and very little extra interning occurs. The benefits are (a) a slight speedup (possibly greater in a parallel compiler), and (b) the code is a lot more principled about `Symbol` use. The main downside is verbosity, particularly with more `use syntax::symbol::symbols` items. r? @Zoxc
2 parents 4443957 + ea9fac5 commit fe5f42c

File tree

135 files changed

+1507
-1041
lines changed

Some content is hidden

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

135 files changed

+1507
-1041
lines changed

src/librustc/hir/check_attr.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::hir;
1212
use crate::hir::def_id::DefId;
1313
use crate::hir::intravisit::{self, Visitor, NestedVisitorMap};
1414
use std::fmt::{self, Display};
15+
use syntax::symbol::sym;
1516
use syntax_pos::Span;
1617

1718
#[derive(Copy, Clone, PartialEq)]
@@ -95,18 +96,18 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
9596
fn check_attributes(&self, item: &hir::Item, target: Target) {
9697
if target == Target::Fn || target == Target::Const {
9798
self.tcx.codegen_fn_attrs(self.tcx.hir().local_def_id_from_hir_id(item.hir_id));
98-
} else if let Some(a) = item.attrs.iter().find(|a| a.check_name("target_feature")) {
99+
} else if let Some(a) = item.attrs.iter().find(|a| a.check_name(sym::target_feature)) {
99100
self.tcx.sess.struct_span_err(a.span, "attribute should be applied to a function")
100101
.span_label(item.span, "not a function")
101102
.emit();
102103
}
103104

104105
for attr in &item.attrs {
105-
if attr.check_name("inline") {
106+
if attr.check_name(sym::inline) {
106107
self.check_inline(attr, &item.span, target)
107-
} else if attr.check_name("non_exhaustive") {
108+
} else if attr.check_name(sym::non_exhaustive) {
108109
self.check_non_exhaustive(attr, item, target)
109-
} else if attr.check_name("marker") {
110+
} else if attr.check_name(sym::marker) {
110111
self.check_marker(attr, item, target)
111112
}
112113
}
@@ -166,7 +167,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
166167
// ```
167168
let hints: Vec<_> = item.attrs
168169
.iter()
169-
.filter(|attr| attr.check_name("repr"))
170+
.filter(|attr| attr.check_name(sym::repr))
170171
.filter_map(|attr| attr.meta_item_list())
171172
.flatten()
172173
.collect();
@@ -177,9 +178,9 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
177178
let mut is_transparent = false;
178179

179180
for hint in &hints {
180-
let (article, allowed_targets) = match hint.name_or_empty().get() {
181-
name @ "C" | name @ "align" => {
182-
is_c |= name == "C";
181+
let (article, allowed_targets) = match hint.name_or_empty() {
182+
name @ sym::C | name @ sym::align => {
183+
is_c |= name == sym::C;
183184
if target != Target::Struct &&
184185
target != Target::Union &&
185186
target != Target::Enum {
@@ -188,33 +189,33 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
188189
continue
189190
}
190191
}
191-
"packed" => {
192+
sym::packed => {
192193
if target != Target::Struct &&
193194
target != Target::Union {
194195
("a", "struct or union")
195196
} else {
196197
continue
197198
}
198199
}
199-
"simd" => {
200+
sym::simd => {
200201
is_simd = true;
201202
if target != Target::Struct {
202203
("a", "struct")
203204
} else {
204205
continue
205206
}
206207
}
207-
"transparent" => {
208+
sym::transparent => {
208209
is_transparent = true;
209210
if target != Target::Struct {
210211
("a", "struct")
211212
} else {
212213
continue
213214
}
214215
}
215-
"i8" | "u8" | "i16" | "u16" |
216-
"i32" | "u32" | "i64" | "u64" |
217-
"isize" | "usize" => {
216+
sym::i8 | sym::u8 | sym::i16 | sym::u16 |
217+
sym::i32 | sym::u32 | sym::i64 | sym::u64 |
218+
sym::isize | sym::usize => {
218219
int_reprs += 1;
219220
if target != Target::Enum {
220221
("an", "enum")
@@ -268,10 +269,10 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
268269
// When checking statements ignore expressions, they will be checked later
269270
if let hir::StmtKind::Local(ref l) = stmt.node {
270271
for attr in l.attrs.iter() {
271-
if attr.check_name("inline") {
272+
if attr.check_name(sym::inline) {
272273
self.check_inline(attr, &stmt.span, Target::Statement);
273274
}
274-
if attr.check_name("repr") {
275+
if attr.check_name(sym::repr) {
275276
self.emit_repr_error(
276277
attr.span,
277278
stmt.span,
@@ -289,10 +290,10 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
289290
_ => Target::Expression,
290291
};
291292
for attr in expr.attrs.iter() {
292-
if attr.check_name("inline") {
293+
if attr.check_name(sym::inline) {
293294
self.check_inline(attr, &expr.span, target);
294295
}
295-
if attr.check_name("repr") {
296+
if attr.check_name(sym::repr) {
296297
self.emit_repr_error(
297298
attr.span,
298299
expr.span,
@@ -305,7 +306,7 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
305306

306307
fn check_used(&self, item: &hir::Item, target: Target) {
307308
for attr in &item.attrs {
308-
if attr.check_name("used") && target != Target::Static {
309+
if attr.check_name(sym::used) && target != Target::Static {
309310
self.tcx.sess
310311
.span_err(attr.span, "attribute must be applied to a `static` variable");
311312
}

0 commit comments

Comments
 (0)