Skip to content

Commit

Permalink
Rollup merge of rust-lang#75043 - petrochenkov:hasname, r=nnethercote
Browse files Browse the repository at this point in the history
rustc_ast: `(Nested)MetaItem::check_name` -> `has_name`

For consistency with `Attribute::has_name` which doesn't mark the attribute as used either.

Replace all uses of `check_name` with `has_name` outside of rustc, only rustc needs to mark attributes as used.

cc rust-lang#74932
r? @nnethercote
  • Loading branch information
JohnTitor authored Aug 3, 2020
2 parents 7abacbe + 05f414b commit c9dff69
Show file tree
Hide file tree
Showing 32 changed files with 94 additions and 89 deletions.
17 changes: 11 additions & 6 deletions src/librustc_ast/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ impl NestedMetaItem {
}

/// Returns `true` if this list item is a MetaItem with a name of `name`.
pub fn check_name(&self, name: Symbol) -> bool {
self.meta_item().map_or(false, |meta_item| meta_item.check_name(name))
pub fn has_name(&self, name: Symbol) -> bool {
self.meta_item().map_or(false, |meta_item| meta_item.has_name(name))
}

/// For a single-segment meta item, returns its name; otherwise, returns `None`.
Expand Down Expand Up @@ -173,8 +173,13 @@ impl Attribute {
}
}

/// Returns `true` if the attribute's path matches the argument. If it matches, then the
/// attribute is marked as used.
/// Returns `true` if the attribute's path matches the argument.
/// If it matches, then the attribute is marked as used.
/// Should only be used by rustc, other tools can use `has_name` instead,
/// because only rustc is supposed to report the `unused_attributes` lint.
/// `MetaItem` and `NestedMetaItem` are produced by "lowering" an `Attribute`
/// and don't have identity, so they only has the `has_name` method,
/// and you need to mark the original `Attribute` as used when necessary.
pub fn check_name(&self, name: Symbol) -> bool {
let matches = self.has_name(name);
if matches {
Expand Down Expand Up @@ -278,7 +283,7 @@ impl MetaItem {
}
}

pub fn check_name(&self, name: Symbol) -> bool {
pub fn has_name(&self, name: Symbol) -> bool {
self.path == name
}

Expand Down Expand Up @@ -405,7 +410,7 @@ pub fn mk_doc_comment(style: AttrStyle, comment: Symbol, span: Span) -> Attribut
}

pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
items.iter().any(|item| item.check_name(name))
items.iter().any(|item| item.has_name(name))
}

pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_ast_passes/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
if attr.check_name(sym::doc) {
for nested_meta in attr.meta_item_list().unwrap_or_default() {
macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => {
$(if nested_meta.check_name(sym::$name) {
$(if nested_meta.has_name(sym::$name) {
let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental");
gate_feature_post!(self, $feature, attr.span, msg);
})*
Expand Down Expand Up @@ -314,7 +314,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
ast::ItemKind::Struct(..) => {
for attr in attr::filter_by_name(&i.attrs[..], sym::repr) {
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
if item.check_name(sym::simd) {
if item.has_name(sym::simd) {
gate_feature_post!(
&self,
repr_simd,
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_attr/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op
if let Some(meta) = attr.meta() {
if let MetaItemKind::List(items) = meta.kind {
if items.len() == 1 {
if items[0].check_name(sym::allowed) {
if items[0].has_name(sym::allowed) {
return Some(UnwindAttr::Allowed);
} else if items[0].check_name(sym::aborts) {
} else if items[0].has_name(sym::aborts) {
return Some(UnwindAttr::Aborts);
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@ pub fn contains_feature_attr(attrs: &[Attribute], feature_name: Symbol) -> bool
item.check_name(sym::feature)
&& item
.meta_item_list()
.map(|list| list.iter().any(|mi| mi.is_word() && mi.check_name(feature_name)))
.map(|list| list.iter().any(|mi| mi.is_word() && mi.has_name(feature_name)))
.unwrap_or(false)
})
}
Expand Down Expand Up @@ -505,7 +505,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
}

fn try_gate_cfg(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) {
let gate = find_gated_cfg(|sym| cfg.check_name(sym));
let gate = find_gated_cfg(|sym| cfg.has_name(sym));
if let (Some(feats), Some(gated_cfg)) = (features, gate) {
gate_cfg(&gated_cfg, cfg.span, sess, feats);
}
Expand Down Expand Up @@ -898,7 +898,7 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
}
} else {
if let Some(meta_item) = item.meta_item() {
if meta_item.check_name(sym::align) {
if meta_item.has_name(sym::align) {
if let MetaItemKind::NameValue(ref value) = meta_item.kind {
recognised = true;
let mut err = struct_span_err!(
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/proc_macro_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl<'a> CollectProcMacros<'a> {

let attributes_attr = list.get(1);
let proc_attrs: Vec<_> = if let Some(attr) = attributes_attr {
if !attr.check_name(sym::attributes) {
if !attr.has_name(sym::attributes) {
self.handler.span_err(attr.span(), "second argument must be `attributes`")
}
attr.meta_item_list()
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_builtin_macros/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
Some(list) => {
let msg = list
.iter()
.find(|mi| mi.check_name(sym::expected))
.find(|mi| mi.has_name(sym::expected))
.and_then(|mi| mi.meta_item())
.and_then(|mi| mi.value_str());
if list.len() != 1 || msg.is_none() {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_expand/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1644,14 +1644,14 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
}

if let Some(list) = at.meta_item_list() {
if !list.iter().any(|it| it.check_name(sym::include)) {
if !list.iter().any(|it| it.has_name(sym::include)) {
return noop_visit_attribute(at, self);
}

let mut items = vec![];

for mut it in list {
if !it.check_name(sym::include) {
if !it.has_name(sym::include) {
items.push({
noop_visit_meta_list_item(&mut it, self);
it
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_incremental/assert_module_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl AssertModuleSource<'tcx> {

fn field(&self, attr: &ast::Attribute, name: Symbol) -> Symbol {
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
if item.check_name(name) {
if item.has_name(name) {
if let Some(value) = item.value_str() {
return value;
} else {
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_incremental/persist/dirty_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl DirtyCleanVisitor<'tcx> {

fn labels(&self, attr: &Attribute) -> Option<Labels> {
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
if item.check_name(LABEL) {
if item.has_name(LABEL) {
let value = expect_associated_value(self.tcx, &item);
return Some(self.resolve_labels(&item, value));
}
Expand All @@ -242,7 +242,7 @@ impl DirtyCleanVisitor<'tcx> {
/// `except=` attribute value
fn except(&self, attr: &Attribute) -> Labels {
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
if item.check_name(EXCEPT) {
if item.has_name(EXCEPT) {
let value = expect_associated_value(self.tcx, &item);
return self.resolve_labels(&item, value);
}
Expand Down Expand Up @@ -474,15 +474,15 @@ fn check_config(tcx: TyCtxt<'_>, attr: &Attribute) -> bool {
debug!("check_config: config={:?}", config);
let (mut cfg, mut except, mut label) = (None, false, false);
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
if item.check_name(CFG) {
if item.has_name(CFG) {
let value = expect_associated_value(tcx, &item);
debug!("check_config: searching for cfg {:?}", value);
cfg = Some(config.contains(&(value, None)));
}
if item.check_name(LABEL) {
if item.has_name(LABEL) {
label = true;
}
if item.check_name(EXCEPT) {
if item.has_name(EXCEPT) {
except = true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ fn has_doc(attr: &ast::Attribute) -> bool {

if let Some(list) = attr.meta_item_list() {
for meta in list {
if meta.check_name(sym::include) || meta.check_name(sym::hidden) {
if meta.has_name(sym::include) || meta.has_name(sym::hidden) {
return true;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_metadata/native_libs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
let mut kind_specified = false;

for item in items.iter() {
if item.check_name(sym::kind) {
if item.has_name(sym::kind) {
kind_specified = true;
let kind = match item.value_str() {
Some(name) => name,
Expand All @@ -84,9 +84,9 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
NativeLibKind::Unspecified
}
};
} else if item.check_name(sym::name) {
} else if item.has_name(sym::name) {
lib.name = item.value_str();
} else if item.check_name(sym::cfg) {
} else if item.has_name(sym::cfg) {
let cfg = match item.meta_item_list() {
Some(list) => list,
None => continue, // skip like historical compilers
Expand All @@ -98,7 +98,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
} else {
self.tcx.sess.span_err(cfg[0].span(), "invalid argument for `cfg(..)`");
}
} else if item.check_name(sym::wasm_import_module) {
} else if item.has_name(sym::wasm_import_module) {
match item.value_str() {
Some(s) => lib.wasm_import_module = Some(s),
None => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/dataflow/framework/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl RustcMirAttrs {
.flat_map(|attr| attr.meta_item_list().into_iter().flat_map(|v| v.into_iter()));

for attr in rustc_mir_attrs {
let attr_result = if attr.check_name(sym::borrowck_graphviz_postflow) {
let attr_result = if attr.has_name(sym::borrowck_graphviz_postflow) {
Self::set_field(&mut ret.basename_and_suffix, tcx, &attr, |s| {
let path = PathBuf::from(s.to_string());
match path.file_name() {
Expand All @@ -350,7 +350,7 @@ impl RustcMirAttrs {
}
}
})
} else if attr.check_name(sym::borrowck_graphviz_format) {
} else if attr.has_name(sym::borrowck_graphviz_format) {
Self::set_field(&mut ret.formatter, tcx, &attr, |s| match s {
sym::gen_kill | sym::two_phase => Ok(s),
_ => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/dataflow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub(crate) fn has_rustc_mir_with(attrs: &[ast::Attribute], name: Symbol) -> Opti
let items = attr.meta_item_list();
for item in items.iter().flat_map(|l| l.iter()) {
match item.meta_item() {
Some(mi) if mi.check_name(name) => return Some(mi.clone()),
Some(mi) if mi.has_name(name) => return Some(mi.clone()),
_ => continue,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_passes/check_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl CheckAttrVisitor<'tcx> {
if let Some(mi) = attr.meta() {
if let Some(list) = mi.meta_item_list() {
for meta in list {
if meta.check_name(sym::alias) {
if meta.has_name(sym::alias) {
if !meta.is_value_str()
|| meta
.value_str()
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,10 +832,10 @@ impl<'tcx> SaveContext<'tcx> {
if let Some(meta_list) = attr.meta_item_list() {
meta_list
.into_iter()
.filter(|it| it.check_name(sym::include))
.filter(|it| it.has_name(sym::include))
.filter_map(|it| it.meta_item_list().map(|l| l.to_owned()))
.flat_map(|it| it)
.filter(|meta| meta.check_name(sym::contents))
.filter(|meta| meta.has_name(sym::contents))
.filter_map(|meta| meta.value_str())
.for_each(|val| {
result.push_str(&val.as_str());
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_trait_selection/traits/on_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,27 +95,27 @@ impl<'tcx> OnUnimplementedDirective {
};

for item in item_iter {
if item.check_name(sym::message) && message.is_none() {
if item.has_name(sym::message) && message.is_none() {
if let Some(message_) = item.value_str() {
message = parse_value(message_)?;
continue;
}
} else if item.check_name(sym::label) && label.is_none() {
} else if item.has_name(sym::label) && label.is_none() {
if let Some(label_) = item.value_str() {
label = parse_value(label_)?;
continue;
}
} else if item.check_name(sym::note) && note.is_none() {
} else if item.has_name(sym::note) && note.is_none() {
if let Some(note_) = item.value_str() {
note = parse_value(note_)?;
continue;
}
} else if item.check_name(sym::enclosing_scope) && enclosing_scope.is_none() {
} else if item.has_name(sym::enclosing_scope) && enclosing_scope.is_none() {
if let Some(enclosing_scope_) = item.value_str() {
enclosing_scope = parse_value(enclosing_scope_)?;
continue;
}
} else if item.check_name(sym::on)
} else if item.has_name(sym::on)
&& is_root
&& message.is_none()
&& label.is_none()
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_typeck/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,7 @@ fn from_target_feature(
let rust_features = tcx.features();
for item in list {
// Only `enable = ...` is accepted in the meta-item list.
if !item.check_name(sym::enable) {
if !item.has_name(sym::enable) {
bad_item(item.span());
continue;
}
Expand Down Expand Up @@ -2483,11 +2483,11 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
no_sanitize_span = Some(attr.span);
if let Some(list) = attr.meta_item_list() {
for item in list.iter() {
if item.check_name(sym::address) {
if item.has_name(sym::address) {
codegen_fn_attrs.no_sanitize |= SanitizerSet::ADDRESS;
} else if item.check_name(sym::memory) {
} else if item.has_name(sym::memory) {
codegen_fn_attrs.no_sanitize |= SanitizerSet::MEMORY;
} else if item.check_name(sym::thread) {
} else if item.has_name(sym::thread) {
codegen_fn_attrs.no_sanitize |= SanitizerSet::THREAD;
} else {
tcx.sess
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl Clean<ExternalCrate> for CrateNum {
let mut prim = None;
for attr in attrs.lists(sym::doc) {
if let Some(v) = attr.value_str() {
if attr.check_name(sym::primitive) {
if attr.has_name(sym::primitive) {
prim = PrimitiveType::from_symbol(v);
if prim.is_some() {
break;
Expand Down Expand Up @@ -168,7 +168,7 @@ impl Clean<ExternalCrate> for CrateNum {
let mut keyword = None;
for attr in attrs.lists(sym::doc) {
if let Some(v) = attr.value_str() {
if attr.check_name(sym::keyword) {
if attr.has_name(sym::keyword) {
if v.is_doc_keyword() {
keyword = Some(v.to_string());
break;
Expand Down Expand Up @@ -2157,7 +2157,7 @@ impl Clean<Vec<Item>> for doctree::ExternCrate<'_> {
fn clean(&self, cx: &DocContext<'_>) -> Vec<Item> {
let please_inline = self.vis.node.is_pub()
&& self.attrs.iter().any(|a| {
a.check_name(sym::doc)
a.has_name(sym::doc)
&& match a.meta_item_list() {
Some(l) => attr::list_contains_name(&l, sym::inline),
None => false,
Expand Down Expand Up @@ -2197,7 +2197,7 @@ impl Clean<Vec<Item>> for doctree::Import<'_> {
// Don't inline doc(hidden) imports so they can be stripped at a later stage.
let mut denied = !self.vis.node.is_pub()
|| self.attrs.iter().any(|a| {
a.check_name(sym::doc)
a.has_name(sym::doc)
&& match a.meta_item_list() {
Some(l) => {
attr::list_contains_name(&l, sym::no_inline)
Expand Down
Loading

0 comments on commit c9dff69

Please sign in to comment.