Skip to content

Commit ea9fac5

Browse files
committed
Return a Symbol from name_or_empty functions.
1 parent 999c1fc commit ea9fac5

File tree

15 files changed

+112
-105
lines changed

15 files changed

+112
-105
lines changed

Diff for: src/librustc/hir/check_attr.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
178178
let mut is_transparent = false;
179179

180180
for hint in &hints {
181-
let (article, allowed_targets) = match hint.name_or_empty().get() {
182-
name @ "C" | name @ "align" => {
183-
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;
184184
if target != Target::Struct &&
185185
target != Target::Union &&
186186
target != Target::Enum {
@@ -189,33 +189,33 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
189189
continue
190190
}
191191
}
192-
"packed" => {
192+
sym::packed => {
193193
if target != Target::Struct &&
194194
target != Target::Union {
195195
("a", "struct or union")
196196
} else {
197197
continue
198198
}
199199
}
200-
"simd" => {
200+
sym::simd => {
201201
is_simd = true;
202202
if target != Target::Struct {
203203
("a", "struct")
204204
} else {
205205
continue
206206
}
207207
}
208-
"transparent" => {
208+
sym::transparent => {
209209
is_transparent = true;
210210
if target != Target::Struct {
211211
("a", "struct")
212212
} else {
213213
continue
214214
}
215215
}
216-
"i8" | "u8" | "i16" | "u16" |
217-
"i32" | "u32" | "i64" | "u64" |
218-
"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 => {
219219
int_reprs += 1;
220220
if target != Target::Enum {
221221
("an", "enum")

Diff for: src/librustc/lint/levels.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<'a> LintLevelsBuilder<'a> {
194194
struct_span_err!(sess, span, E0452, "malformed lint attribute")
195195
};
196196
for attr in attrs {
197-
let level = match Level::from_str(&attr.name_or_empty()) {
197+
let level = match Level::from_symbol(attr.name_or_empty()) {
198198
None => continue,
199199
Some(lvl) => lvl,
200200
};

Diff for: src/librustc/lint/mod.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use syntax::ast;
3838
use syntax::source_map::{MultiSpan, ExpnFormat};
3939
use syntax::early_buffered_lints::BufferedEarlyLintId;
4040
use syntax::edition::Edition;
41-
use syntax::symbol::Symbol;
41+
use syntax::symbol::{Symbol, sym};
4242
use syntax_pos::Span;
4343

4444
pub use crate::lint::context::{LateContext, EarlyContext, LintContext, LintStore,
@@ -570,6 +570,17 @@ impl Level {
570570
_ => None,
571571
}
572572
}
573+
574+
/// Converts a symbol to a level.
575+
pub fn from_symbol(x: Symbol) -> Option<Level> {
576+
match x {
577+
sym::allow => Some(Allow),
578+
sym::warn => Some(Warn),
579+
sym::deny => Some(Deny),
580+
sym::forbid => Some(Forbid),
581+
_ => None,
582+
}
583+
}
573584
}
574585

575586
/// How a lint level was set.
@@ -752,7 +763,7 @@ pub fn struct_lint_level<'a>(sess: &'a Session,
752763

753764
pub fn maybe_lint_level_root(tcx: TyCtxt<'_, '_, '_>, id: hir::HirId) -> bool {
754765
let attrs = tcx.hir().attrs_by_hir_id(id);
755-
attrs.iter().any(|attr| Level::from_str(&attr.name_or_empty()).is_some())
766+
attrs.iter().any(|attr| Level::from_symbol(attr.name_or_empty()).is_some())
756767
}
757768

758769
fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)

Diff for: src/librustc/middle/lib_features.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
6565
for meta in metas {
6666
if let Some(mi) = meta.meta_item() {
6767
// Find the `feature = ".."` meta-item.
68-
match (mi.name_or_empty().get(), mi.value_str()) {
69-
("feature", val) => feature = val,
70-
("since", val) => since = val,
68+
match (mi.name_or_empty(), mi.value_str()) {
69+
(sym::feature, val) => feature = val,
70+
(sym::since, val) => since = val,
7171
_ => {}
7272
}
7373
}

Diff for: src/librustc/middle/stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
195195
// Emit errors for non-staged-api crates.
196196
for attr in attrs {
197197
let name = attr.name_or_empty();
198-
if ["unstable", "stable", "rustc_deprecated"].contains(&name.get()) {
198+
if [sym::unstable, sym::stable, sym::rustc_deprecated].contains(&name) {
199199
attr::mark_used(attr);
200200
self.tcx.sess.span_err(attr.span, "stability attributes may not be used \
201201
outside of the standard library");

Diff for: src/librustc_lint/unused.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
262262
// Has a plugin registered this attribute as one that must be used at
263263
// the crate level?
264264
let plugin_crate = plugin_attributes.iter()
265-
.find(|&&(ref x, t)| name == x.as_str() && AttributeType::CrateLevel == t)
265+
.find(|&&(x, t)| name == x && AttributeType::CrateLevel == t)
266266
.is_some();
267267
if known_crate || plugin_crate {
268268
let msg = match attr.style {

Diff for: src/librustc_metadata/creader.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,8 @@ impl<'a> CrateLoader<'a> {
650650
/// SVH and DefIndex of the registrar function.
651651
pub fn find_plugin_registrar(&mut self,
652652
span: Span,
653-
name: &str)
653+
name: Symbol)
654654
-> Option<(PathBuf, CrateDisambiguator)> {
655-
let name = Symbol::intern(name);
656655
let ekrate = self.read_extension_crate(span, name, name);
657656

658657
if ekrate.target_only {

Diff for: src/librustc_passes/layout_test.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,26 @@ impl<'a, 'tcx> VarianceTest<'a, 'tcx> {
5555
// The `..` are the names of fields to dump.
5656
let meta_items = attr.meta_item_list().unwrap_or_default();
5757
for meta_item in meta_items {
58-
match meta_item.name_or_empty().get() {
59-
"abi" => {
58+
match meta_item.name_or_empty() {
59+
sym::abi => {
6060
self.tcx
6161
.sess
6262
.span_err(item.span, &format!("abi: {:?}", ty_layout.abi));
6363
}
6464

65-
"align" => {
65+
sym::align => {
6666
self.tcx
6767
.sess
6868
.span_err(item.span, &format!("align: {:?}", ty_layout.align));
6969
}
7070

71-
"size" => {
71+
sym::size => {
7272
self.tcx
7373
.sess
7474
.span_err(item.span, &format!("size: {:?}", ty_layout.size));
7575
}
7676

77-
"homogeneous_aggregate" => {
77+
sym::homogeneous_aggregate => {
7878
self.tcx.sess.span_err(
7979
item.span,
8080
&format!(

Diff for: src/librustc_plugin/load.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::mem;
1111
use std::path::PathBuf;
1212
use syntax::ast;
1313
use syntax::span_err;
14-
use syntax::symbol::sym;
14+
use syntax::symbol::{Symbol, keywords, sym};
1515
use syntax_pos::{Span, DUMMY_SP};
1616

1717
/// Pointer to a registrar function.
@@ -58,9 +58,9 @@ pub fn load_plugins(sess: &Session,
5858
for plugin in plugins {
5959
// plugins must have a name and can't be key = value
6060
let name = plugin.name_or_empty();
61-
if !name.is_empty() && !plugin.is_value_str() {
61+
if name != keywords::Invalid.name() && !plugin.is_value_str() {
6262
let args = plugin.meta_item_list().map(ToOwned::to_owned);
63-
loader.load_plugin(plugin.span(), &name, args.unwrap_or_default());
63+
loader.load_plugin(plugin.span(), name, args.unwrap_or_default());
6464
} else {
6565
call_malformed_plugin_attribute(sess, attr.span);
6666
}
@@ -70,7 +70,7 @@ pub fn load_plugins(sess: &Session,
7070

7171
if let Some(plugins) = addl_plugins {
7272
for plugin in plugins {
73-
loader.load_plugin(DUMMY_SP, &plugin, vec![]);
73+
loader.load_plugin(DUMMY_SP, Symbol::intern(&plugin), vec![]);
7474
}
7575
}
7676

@@ -86,7 +86,7 @@ impl<'a> PluginLoader<'a> {
8686
}
8787
}
8888

89-
fn load_plugin(&mut self, span: Span, name: &str, args: Vec<ast::NestedMetaItem>) {
89+
fn load_plugin(&mut self, span: Span, name: Symbol, args: Vec<ast::NestedMetaItem>) {
9090
let registrar = self.reader.find_plugin_registrar(span, name);
9191

9292
if let Some((lib, disambiguator)) = registrar {

Diff for: src/librustdoc/core.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -421,19 +421,19 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
421421

422422
let name = attr.name_or_empty();
423423
if attr.is_word() {
424-
if name == "no_default_passes" {
424+
if name == sym::no_default_passes {
425425
report_deprecated_attr("no_default_passes", diag);
426426
if default_passes == passes::DefaultPassOption::Default {
427427
default_passes = passes::DefaultPassOption::None;
428428
}
429429
}
430430
} else if let Some(value) = attr.value_str() {
431-
let sink = match name.get() {
432-
"passes" => {
431+
let sink = match name {
432+
sym::passes => {
433433
report_deprecated_attr("passes = \"...\"", diag);
434434
&mut manual_passes
435435
},
436-
"plugins" => {
436+
sym::plugins => {
437437
report_deprecated_attr("plugins = \"...\"", diag);
438438
eprintln!("WARNING: #![doc(plugins = \"...\")] no longer functions; \
439439
see CVE-2018-1000622");
@@ -446,7 +446,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
446446
}
447447
}
448448

449-
if attr.is_word() && name == "document_private_items" {
449+
if attr.is_word() && name == sym::document_private_items {
450450
if default_passes == passes::DefaultPassOption::Default {
451451
default_passes = passes::DefaultPassOption::Private;
452452
}

Diff for: src/librustdoc/html/render.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ use syntax::ast;
5050
use syntax::ext::base::MacroKind;
5151
use syntax::source_map::FileName;
5252
use syntax::feature_gate::UnstableFeatures;
53-
use syntax::symbol::sym;
53+
use syntax::symbol::{Symbol, sym};
5454
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId};
5555
use rustc::middle::privacy::AccessLevels;
5656
use rustc::middle::stability;
@@ -573,23 +573,23 @@ pub fn run(mut krate: clean::Crate,
573573
// going to emit HTML
574574
if let Some(attrs) = krate.module.as_ref().map(|m| &m.attrs) {
575575
for attr in attrs.lists(sym::doc) {
576-
match (attr.name_or_empty().get(), attr.value_str()) {
577-
("html_favicon_url", Some(s)) => {
576+
match (attr.name_or_empty(), attr.value_str()) {
577+
(sym::html_favicon_url, Some(s)) => {
578578
scx.layout.favicon = s.to_string();
579579
}
580-
("html_logo_url", Some(s)) => {
580+
(sym::html_logo_url, Some(s)) => {
581581
scx.layout.logo = s.to_string();
582582
}
583-
("html_playground_url", Some(s)) => {
583+
(sym::html_playground_url, Some(s)) => {
584584
markdown::PLAYGROUND.with(|slot| {
585585
let name = krate.name.clone();
586586
*slot.borrow_mut() = Some((Some(name), s.to_string()));
587587
});
588588
}
589-
("issue_tracker_base_url", Some(s)) => {
589+
(sym::issue_tracker_base_url, Some(s)) => {
590590
scx.issue_tracker_base_url = Some(s.to_string());
591591
}
592-
("html_no_source", None) if attr.is_word() => {
592+
(sym::html_no_source, None) if attr.is_word() => {
593593
scx.include_sources = false;
594594
}
595595
_ => {}
@@ -3762,22 +3762,22 @@ fn render_attribute(attr: &ast::MetaItem) -> Option<String> {
37623762
}
37633763
}
37643764

3765-
const ATTRIBUTE_WHITELIST: &'static [&'static str] = &[
3766-
"export_name",
3767-
"lang",
3768-
"link_section",
3769-
"must_use",
3770-
"no_mangle",
3771-
"repr",
3772-
"unsafe_destructor_blind_to_params",
3773-
"non_exhaustive"
3765+
const ATTRIBUTE_WHITELIST: &'static [Symbol] = &[
3766+
sym::export_name,
3767+
sym::lang,
3768+
sym::link_section,
3769+
sym::must_use,
3770+
sym::no_mangle,
3771+
sym::repr,
3772+
sym::unsafe_destructor_blind_to_params,
3773+
sym::non_exhaustive
37743774
];
37753775

37763776
fn render_attributes(w: &mut dyn fmt::Write, it: &clean::Item) -> fmt::Result {
37773777
let mut attrs = String::new();
37783778

37793779
for attr in &it.attrs.other_attrs {
3780-
if !ATTRIBUTE_WHITELIST.contains(&attr.name_or_empty().get()) {
3780+
if !ATTRIBUTE_WHITELIST.contains(&attr.name_or_empty()) {
37813781
continue;
37823782
}
37833783
if let Some(s) = render_attribute(&attr.meta().unwrap()) {

0 commit comments

Comments
 (0)