Skip to content

Commit 262fce4

Browse files
authored
Rollup merge of #75043 - petrochenkov:hasname, r=nnethercote
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 #74932 r? @nnethercote
2 parents cc0ac7e + 05f414b commit 262fce4

File tree

32 files changed

+94
-89
lines changed

32 files changed

+94
-89
lines changed

src/librustc_ast/attr/mod.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ impl NestedMetaItem {
100100
}
101101

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

107107
/// For a single-segment meta item, returns its name; otherwise, returns `None`.
@@ -173,8 +173,13 @@ impl Attribute {
173173
}
174174
}
175175

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

281-
pub fn check_name(&self, name: Symbol) -> bool {
286+
pub fn has_name(&self, name: Symbol) -> bool {
282287
self.path == name
283288
}
284289

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

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

411416
pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {

src/librustc_ast_passes/feature_gate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
243243
if attr.check_name(sym::doc) {
244244
for nested_meta in attr.meta_item_list().unwrap_or_default() {
245245
macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => {
246-
$(if nested_meta.check_name(sym::$name) {
246+
$(if nested_meta.has_name(sym::$name) {
247247
let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental");
248248
gate_feature_post!(self, $feature, attr.span, msg);
249249
})*
@@ -314,7 +314,7 @@ impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
314314
ast::ItemKind::Struct(..) => {
315315
for attr in attr::filter_by_name(&i.attrs[..], sym::repr) {
316316
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
317-
if item.check_name(sym::simd) {
317+
if item.has_name(sym::simd) {
318318
gate_feature_post!(
319319
&self,
320320
repr_simd,

src/librustc_attr/builtin.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ pub fn find_unwind_attr(diagnostic: Option<&Handler>, attrs: &[Attribute]) -> Op
9292
if let Some(meta) = attr.meta() {
9393
if let MetaItemKind::List(items) = meta.kind {
9494
if items.len() == 1 {
95-
if items[0].check_name(sym::allowed) {
95+
if items[0].has_name(sym::allowed) {
9696
return Some(UnwindAttr::Allowed);
97-
} else if items[0].check_name(sym::aborts) {
97+
} else if items[0].has_name(sym::aborts) {
9898
return Some(UnwindAttr::Aborts);
9999
}
100100
}
@@ -168,7 +168,7 @@ pub fn contains_feature_attr(attrs: &[Attribute], feature_name: Symbol) -> bool
168168
item.check_name(sym::feature)
169169
&& item
170170
.meta_item_list()
171-
.map(|list| list.iter().any(|mi| mi.is_word() && mi.check_name(feature_name)))
171+
.map(|list| list.iter().any(|mi| mi.is_word() && mi.has_name(feature_name)))
172172
.unwrap_or(false)
173173
})
174174
}
@@ -505,7 +505,7 @@ pub fn cfg_matches(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Feat
505505
}
506506

507507
fn try_gate_cfg(cfg: &ast::MetaItem, sess: &ParseSess, features: Option<&Features>) {
508-
let gate = find_gated_cfg(|sym| cfg.check_name(sym));
508+
let gate = find_gated_cfg(|sym| cfg.has_name(sym));
509509
if let (Some(feats), Some(gated_cfg)) = (features, gate) {
510510
gate_cfg(&gated_cfg, cfg.span, sess, feats);
511511
}
@@ -898,7 +898,7 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
898898
}
899899
} else {
900900
if let Some(meta_item) = item.meta_item() {
901-
if meta_item.check_name(sym::align) {
901+
if meta_item.has_name(sym::align) {
902902
if let MetaItemKind::NameValue(ref value) = meta_item.kind {
903903
recognised = true;
904904
let mut err = struct_span_err!(

src/librustc_builtin_macros/proc_macro_harness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'a> CollectProcMacros<'a> {
143143

144144
let attributes_attr = list.get(1);
145145
let proc_attrs: Vec<_> = if let Some(attr) = attributes_attr {
146-
if !attr.check_name(sym::attributes) {
146+
if !attr.has_name(sym::attributes) {
147147
self.handler.span_err(attr.span(), "second argument must be `attributes`")
148148
}
149149
attr.meta_item_list()

src/librustc_builtin_macros/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
336336
Some(list) => {
337337
let msg = list
338338
.iter()
339-
.find(|mi| mi.check_name(sym::expected))
339+
.find(|mi| mi.has_name(sym::expected))
340340
.and_then(|mi| mi.meta_item())
341341
.and_then(|mi| mi.value_str());
342342
if list.len() != 1 || msg.is_none() {

src/librustc_expand/expand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,14 +1644,14 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
16441644
}
16451645

16461646
if let Some(list) = at.meta_item_list() {
1647-
if !list.iter().any(|it| it.check_name(sym::include)) {
1647+
if !list.iter().any(|it| it.has_name(sym::include)) {
16481648
return noop_visit_attribute(at, self);
16491649
}
16501650

16511651
let mut items = vec![];
16521652

16531653
for mut it in list {
1654-
if !it.check_name(sym::include) {
1654+
if !it.has_name(sym::include) {
16551655
items.push({
16561656
noop_visit_meta_list_item(&mut it, self);
16571657
it

src/librustc_incremental/assert_module_sources.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl AssertModuleSource<'tcx> {
149149

150150
fn field(&self, attr: &ast::Attribute, name: Symbol) -> Symbol {
151151
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
152-
if item.check_name(name) {
152+
if item.has_name(name) {
153153
if let Some(value) = item.value_str() {
154154
return value;
155155
} else {

src/librustc_incremental/persist/dirty_clean.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl DirtyCleanVisitor<'tcx> {
231231

232232
fn labels(&self, attr: &Attribute) -> Option<Labels> {
233233
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
234-
if item.check_name(LABEL) {
234+
if item.has_name(LABEL) {
235235
let value = expect_associated_value(self.tcx, &item);
236236
return Some(self.resolve_labels(&item, value));
237237
}
@@ -242,7 +242,7 @@ impl DirtyCleanVisitor<'tcx> {
242242
/// `except=` attribute value
243243
fn except(&self, attr: &Attribute) -> Labels {
244244
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
245-
if item.check_name(EXCEPT) {
245+
if item.has_name(EXCEPT) {
246246
let value = expect_associated_value(self.tcx, &item);
247247
return self.resolve_labels(&item, value);
248248
}
@@ -474,15 +474,15 @@ fn check_config(tcx: TyCtxt<'_>, attr: &Attribute) -> bool {
474474
debug!("check_config: config={:?}", config);
475475
let (mut cfg, mut except, mut label) = (None, false, false);
476476
for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
477-
if item.check_name(CFG) {
477+
if item.has_name(CFG) {
478478
let value = expect_associated_value(tcx, &item);
479479
debug!("check_config: searching for cfg {:?}", value);
480480
cfg = Some(config.contains(&(value, None)));
481481
}
482-
if item.check_name(LABEL) {
482+
if item.has_name(LABEL) {
483483
label = true;
484484
}
485-
if item.check_name(EXCEPT) {
485+
if item.has_name(EXCEPT) {
486486
except = true;
487487
}
488488
}

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ fn has_doc(attr: &ast::Attribute) -> bool {
330330

331331
if let Some(list) = attr.meta_item_list() {
332332
for meta in list {
333-
if meta.check_name(sym::include) || meta.check_name(sym::hidden) {
333+
if meta.has_name(sym::include) || meta.has_name(sym::hidden) {
334334
return true;
335335
}
336336
}

src/librustc_metadata/native_libs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
5858
let mut kind_specified = false;
5959

6060
for item in items.iter() {
61-
if item.check_name(sym::kind) {
61+
if item.has_name(sym::kind) {
6262
kind_specified = true;
6363
let kind = match item.value_str() {
6464
Some(name) => name,
@@ -84,9 +84,9 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
8484
NativeLibKind::Unspecified
8585
}
8686
};
87-
} else if item.check_name(sym::name) {
87+
} else if item.has_name(sym::name) {
8888
lib.name = item.value_str();
89-
} else if item.check_name(sym::cfg) {
89+
} else if item.has_name(sym::cfg) {
9090
let cfg = match item.meta_item_list() {
9191
Some(list) => list,
9292
None => continue, // skip like historical compilers
@@ -98,7 +98,7 @@ impl ItemLikeVisitor<'tcx> for Collector<'tcx> {
9898
} else {
9999
self.tcx.sess.span_err(cfg[0].span(), "invalid argument for `cfg(..)`");
100100
}
101-
} else if item.check_name(sym::wasm_import_module) {
101+
} else if item.has_name(sym::wasm_import_module) {
102102
match item.value_str() {
103103
Some(s) => lib.wasm_import_module = Some(s),
104104
None => {

0 commit comments

Comments
 (0)