Skip to content

Commit

Permalink
Return a Symbol from name_or_empty functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed May 12, 2019
1 parent 999c1fc commit ea9fac5
Show file tree
Hide file tree
Showing 15 changed files with 112 additions and 105 deletions.
18 changes: 9 additions & 9 deletions src/librustc/hir/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,9 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
let mut is_transparent = false;

for hint in &hints {
let (article, allowed_targets) = match hint.name_or_empty().get() {
name @ "C" | name @ "align" => {
is_c |= name == "C";
let (article, allowed_targets) = match hint.name_or_empty() {
name @ sym::C | name @ sym::align => {
is_c |= name == sym::C;
if target != Target::Struct &&
target != Target::Union &&
target != Target::Enum {
Expand All @@ -189,33 +189,33 @@ impl<'a, 'tcx> CheckAttrVisitor<'a, 'tcx> {
continue
}
}
"packed" => {
sym::packed => {
if target != Target::Struct &&
target != Target::Union {
("a", "struct or union")
} else {
continue
}
}
"simd" => {
sym::simd => {
is_simd = true;
if target != Target::Struct {
("a", "struct")
} else {
continue
}
}
"transparent" => {
sym::transparent => {
is_transparent = true;
if target != Target::Struct {
("a", "struct")
} else {
continue
}
}
"i8" | "u8" | "i16" | "u16" |
"i32" | "u32" | "i64" | "u64" |
"isize" | "usize" => {
sym::i8 | sym::u8 | sym::i16 | sym::u16 |
sym::i32 | sym::u32 | sym::i64 | sym::u64 |
sym::isize | sym::usize => {
int_reprs += 1;
if target != Target::Enum {
("an", "enum")
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/lint/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ impl<'a> LintLevelsBuilder<'a> {
struct_span_err!(sess, span, E0452, "malformed lint attribute")
};
for attr in attrs {
let level = match Level::from_str(&attr.name_or_empty()) {
let level = match Level::from_symbol(attr.name_or_empty()) {
None => continue,
Some(lvl) => lvl,
};
Expand Down
15 changes: 13 additions & 2 deletions src/librustc/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use syntax::ast;
use syntax::source_map::{MultiSpan, ExpnFormat};
use syntax::early_buffered_lints::BufferedEarlyLintId;
use syntax::edition::Edition;
use syntax::symbol::Symbol;
use syntax::symbol::{Symbol, sym};
use syntax_pos::Span;

pub use crate::lint::context::{LateContext, EarlyContext, LintContext, LintStore,
Expand Down Expand Up @@ -570,6 +570,17 @@ impl Level {
_ => None,
}
}

/// Converts a symbol to a level.
pub fn from_symbol(x: Symbol) -> Option<Level> {
match x {
sym::allow => Some(Allow),
sym::warn => Some(Warn),
sym::deny => Some(Deny),
sym::forbid => Some(Forbid),
_ => None,
}
}
}

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

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

fn lint_levels<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, cnum: CrateNum)
Expand Down
6 changes: 3 additions & 3 deletions src/librustc/middle/lib_features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ impl<'a, 'tcx> LibFeatureCollector<'a, 'tcx> {
for meta in metas {
if let Some(mi) = meta.meta_item() {
// Find the `feature = ".."` meta-item.
match (mi.name_or_empty().get(), mi.value_str()) {
("feature", val) => feature = val,
("since", val) => since = val,
match (mi.name_or_empty(), mi.value_str()) {
(sym::feature, val) => feature = val,
(sym::since, val) => since = val,
_ => {}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<'a, 'tcx: 'a> Annotator<'a, 'tcx> {
// Emit errors for non-staged-api crates.
for attr in attrs {
let name = attr.name_or_empty();
if ["unstable", "stable", "rustc_deprecated"].contains(&name.get()) {
if [sym::unstable, sym::stable, sym::rustc_deprecated].contains(&name) {
attr::mark_used(attr);
self.tcx.sess.span_err(attr.span, "stability attributes may not be used \
outside of the standard library");
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/unused.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
// Has a plugin registered this attribute as one that must be used at
// the crate level?
let plugin_crate = plugin_attributes.iter()
.find(|&&(ref x, t)| name == x.as_str() && AttributeType::CrateLevel == t)
.find(|&&(x, t)| name == x && AttributeType::CrateLevel == t)
.is_some();
if known_crate || plugin_crate {
let msg = match attr.style {
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,9 +650,8 @@ impl<'a> CrateLoader<'a> {
/// SVH and DefIndex of the registrar function.
pub fn find_plugin_registrar(&mut self,
span: Span,
name: &str)
name: Symbol)
-> Option<(PathBuf, CrateDisambiguator)> {
let name = Symbol::intern(name);
let ekrate = self.read_extension_crate(span, name, name);

if ekrate.target_only {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_passes/layout_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,26 +55,26 @@ impl<'a, 'tcx> VarianceTest<'a, 'tcx> {
// The `..` are the names of fields to dump.
let meta_items = attr.meta_item_list().unwrap_or_default();
for meta_item in meta_items {
match meta_item.name_or_empty().get() {
"abi" => {
match meta_item.name_or_empty() {
sym::abi => {
self.tcx
.sess
.span_err(item.span, &format!("abi: {:?}", ty_layout.abi));
}

"align" => {
sym::align => {
self.tcx
.sess
.span_err(item.span, &format!("align: {:?}", ty_layout.align));
}

"size" => {
sym::size => {
self.tcx
.sess
.span_err(item.span, &format!("size: {:?}", ty_layout.size));
}

"homogeneous_aggregate" => {
sym::homogeneous_aggregate => {
self.tcx.sess.span_err(
item.span,
&format!(
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_plugin/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::mem;
use std::path::PathBuf;
use syntax::ast;
use syntax::span_err;
use syntax::symbol::sym;
use syntax::symbol::{Symbol, keywords, sym};
use syntax_pos::{Span, DUMMY_SP};

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

if let Some(plugins) = addl_plugins {
for plugin in plugins {
loader.load_plugin(DUMMY_SP, &plugin, vec![]);
loader.load_plugin(DUMMY_SP, Symbol::intern(&plugin), vec![]);
}
}

Expand All @@ -86,7 +86,7 @@ impl<'a> PluginLoader<'a> {
}
}

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

if let Some((lib, disambiguator)) = registrar {
Expand Down
10 changes: 5 additions & 5 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,19 +421,19 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt

let name = attr.name_or_empty();
if attr.is_word() {
if name == "no_default_passes" {
if name == sym::no_default_passes {
report_deprecated_attr("no_default_passes", diag);
if default_passes == passes::DefaultPassOption::Default {
default_passes = passes::DefaultPassOption::None;
}
}
} else if let Some(value) = attr.value_str() {
let sink = match name.get() {
"passes" => {
let sink = match name {
sym::passes => {
report_deprecated_attr("passes = \"...\"", diag);
&mut manual_passes
},
"plugins" => {
sym::plugins => {
report_deprecated_attr("plugins = \"...\"", diag);
eprintln!("WARNING: #![doc(plugins = \"...\")] no longer functions; \
see CVE-2018-1000622");
Expand All @@ -446,7 +446,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
}
}

if attr.is_word() && name == "document_private_items" {
if attr.is_word() && name == sym::document_private_items {
if default_passes == passes::DefaultPassOption::Default {
default_passes = passes::DefaultPassOption::Private;
}
Expand Down
34 changes: 17 additions & 17 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use syntax::ast;
use syntax::ext::base::MacroKind;
use syntax::source_map::FileName;
use syntax::feature_gate::UnstableFeatures;
use syntax::symbol::sym;
use syntax::symbol::{Symbol, sym};
use rustc::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId};
use rustc::middle::privacy::AccessLevels;
use rustc::middle::stability;
Expand Down Expand Up @@ -573,23 +573,23 @@ pub fn run(mut krate: clean::Crate,
// going to emit HTML
if let Some(attrs) = krate.module.as_ref().map(|m| &m.attrs) {
for attr in attrs.lists(sym::doc) {
match (attr.name_or_empty().get(), attr.value_str()) {
("html_favicon_url", Some(s)) => {
match (attr.name_or_empty(), attr.value_str()) {
(sym::html_favicon_url, Some(s)) => {
scx.layout.favicon = s.to_string();
}
("html_logo_url", Some(s)) => {
(sym::html_logo_url, Some(s)) => {
scx.layout.logo = s.to_string();
}
("html_playground_url", Some(s)) => {
(sym::html_playground_url, Some(s)) => {
markdown::PLAYGROUND.with(|slot| {
let name = krate.name.clone();
*slot.borrow_mut() = Some((Some(name), s.to_string()));
});
}
("issue_tracker_base_url", Some(s)) => {
(sym::issue_tracker_base_url, Some(s)) => {
scx.issue_tracker_base_url = Some(s.to_string());
}
("html_no_source", None) if attr.is_word() => {
(sym::html_no_source, None) if attr.is_word() => {
scx.include_sources = false;
}
_ => {}
Expand Down Expand Up @@ -3762,22 +3762,22 @@ fn render_attribute(attr: &ast::MetaItem) -> Option<String> {
}
}

const ATTRIBUTE_WHITELIST: &'static [&'static str] = &[
"export_name",
"lang",
"link_section",
"must_use",
"no_mangle",
"repr",
"unsafe_destructor_blind_to_params",
"non_exhaustive"
const ATTRIBUTE_WHITELIST: &'static [Symbol] = &[
sym::export_name,
sym::lang,
sym::link_section,
sym::must_use,
sym::no_mangle,
sym::repr,
sym::unsafe_destructor_blind_to_params,
sym::non_exhaustive
];

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

for attr in &it.attrs.other_attrs {
if !ATTRIBUTE_WHITELIST.contains(&attr.name_or_empty().get()) {
if !ATTRIBUTE_WHITELIST.contains(&attr.name_or_empty()) {
continue;
}
if let Some(s) = render_attribute(&attr.meta().unwrap()) {
Expand Down
Loading

0 comments on commit ea9fac5

Please sign in to comment.