Skip to content

Commit 6b870d5

Browse files
committed
WIP
1 parent 0efc743 commit 6b870d5

File tree

29 files changed

+74
-66
lines changed

29 files changed

+74
-66
lines changed

src/librustc_ast/attr/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ impl Attribute {
177177
/// If it matches, then the attribute is marked as used.
178178
/// Should only be used by rustc, other tools can use `has_name` instead.
179179
pub fn check_name(&self, name: Symbol) -> bool {
180+
self.has_name(name)
181+
}
182+
183+
pub fn check_name2(&self, name: Symbol) -> bool {
180184
let matches = self.has_name(name);
181185
if matches {
182186
mark_used(self);
@@ -413,18 +417,34 @@ pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {
413417
attrs.iter().any(|item| item.check_name(name))
414418
}
415419

420+
pub fn contains_name2(attrs: &[Attribute], name: Symbol) -> bool {
421+
attrs.iter().any(|item| item.check_name2(name))
422+
}
423+
416424
pub fn find_by_name(attrs: &[Attribute], name: Symbol) -> Option<&Attribute> {
417425
attrs.iter().find(|attr| attr.check_name(name))
418426
}
419427

428+
pub fn find_by_name2(attrs: &[Attribute], name: Symbol) -> Option<&Attribute> {
429+
attrs.iter().find(|attr| attr.check_name2(name))
430+
}
431+
420432
pub fn filter_by_name(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> {
421433
attrs.iter().filter(move |attr| attr.check_name(name))
422434
}
423435

436+
pub fn filter_by_name2(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> {
437+
attrs.iter().filter(move |attr| attr.check_name2(name))
438+
}
439+
424440
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option<Symbol> {
425441
attrs.iter().find(|at| at.check_name(name)).and_then(|at| at.value_str())
426442
}
427443

444+
pub fn first_attr_value_str_by_name2(attrs: &[Attribute], name: Symbol) -> Option<Symbol> {
445+
attrs.iter().find(|at| at.check_name2(name)).and_then(|at| at.value_str())
446+
}
447+
428448
impl MetaItem {
429449
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
430450
let mut idents = vec![];

src/librustc_ast_lowering/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2236,7 +2236,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22362236
hir_id: self.lower_node_id(param.id),
22372237
name,
22382238
span: param.ident.span,
2239-
pure_wrt_drop: attr::contains_name(&param.attrs, sym::may_dangle),
2239+
pure_wrt_drop: attr::contains_name2(&param.attrs, sym::may_dangle),
22402240
attrs: self.lower_attrs(&param.attrs),
22412241
bounds: self.arena.alloc_from_iter(bounds),
22422242
kind,

src/librustc_attr/builtin.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Parsing and validation of builtin attributes
22
3-
use super::{find_by_name, mark_used};
3+
use super::find_by_name;
44

55
use rustc_ast::ast::{self, Attribute, Lit, LitKind, MetaItem, MetaItemKind, NestedMetaItem};
66
use rustc_ast_pretty::pprust;
@@ -214,8 +214,6 @@ where
214214
continue; // not a stability level
215215
}
216216

217-
mark_used(attr);
218-
219217
let meta = attr.meta();
220218

221219
if attr.has_name(sym::rustc_promotable) {
@@ -650,7 +648,7 @@ where
650648
let diagnostic = &sess.span_diagnostic;
651649

652650
'outer: for attr in attrs_iter {
653-
if !(attr.check_name(sym::deprecated) || attr.check_name(sym::rustc_deprecated)) {
651+
if !(attr.check_name2(sym::deprecated) || attr.check_name(sym::rustc_deprecated)) {
654652
continue;
655653
}
656654

@@ -773,8 +771,6 @@ where
773771
}
774772
}
775773

776-
mark_used(&attr);
777-
778774
let is_since_rustc_version = attr.check_name(sym::rustc_deprecated);
779775
depr = Some(Deprecation { since, note, suggestion, is_since_rustc_version });
780776
}
@@ -823,9 +819,8 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {
823819

824820
let mut acc = Vec::new();
825821
let diagnostic = &sess.span_diagnostic;
826-
if attr.has_name(sym::repr) {
822+
if attr.check_name2(sym::repr) {
827823
if let Some(items) = attr.meta_item_list() {
828-
mark_used(attr);
829824
for item in items {
830825
if !item.is_meta_item() {
831826
handle_errors(

src/librustc_builtin_macros/deriving/generic/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -676,8 +676,6 @@ impl<'a> TraitDef<'a> {
676676
let self_type = cx.ty_path(path);
677677

678678
let attr = cx.attribute(cx.meta_word(self.span, sym::automatically_derived));
679-
// Just mark it now since we know that it'll end up used downstream
680-
attr::mark_used(&attr);
681679
let opt_trait_ref = Some(trait_ref);
682680
let unused_qual = {
683681
let word = rustc_ast::attr::mk_nested_word_item(Ident::new(

src/librustc_builtin_macros/proc_macro_harness.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
264264

265265
for attr in &item.attrs {
266266
if is_proc_macro_attr(&attr) {
267+
attr::mark_used(attr);
267268
if let Some(prev_attr) = found_attr {
268269
let prev_item = prev_attr.get_normal_item();
269270
let item = attr.get_normal_item();

src/librustc_builtin_macros/standard_library_imports.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ pub fn inject(
1717
let rust_2018 = sess.edition >= Edition::Edition2018;
1818

1919
// the first name in this list is the crate name of the crate with the prelude
20-
let names: &[Symbol] = if attr::contains_name(&krate.attrs, sym::no_core) {
20+
let names: &[Symbol] = if attr::contains_name2(&krate.attrs, sym::no_core) {
2121
return (krate, None);
22-
} else if attr::contains_name(&krate.attrs, sym::no_std) {
22+
} else if attr::contains_name2(&krate.attrs, sym::no_std) {
2323
if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
2424
&[sym::core]
2525
} else {

src/librustc_builtin_macros/test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,15 @@ enum ShouldPanic {
319319
}
320320

321321
fn should_ignore(i: &ast::Item) -> bool {
322-
attr::contains_name(&i.attrs, sym::ignore)
322+
attr::contains_name2(&i.attrs, sym::ignore)
323323
}
324324

325325
fn should_fail(i: &ast::Item) -> bool {
326-
attr::contains_name(&i.attrs, sym::allow_fail)
326+
attr::contains_name2(&i.attrs, sym::allow_fail)
327327
}
328328

329329
fn should_panic(cx: &ExtCtxt<'_>, i: &ast::Item) -> ShouldPanic {
330-
match attr::find_by_name(&i.attrs, sym::should_panic) {
330+
match attr::find_by_name2(&i.attrs, sym::should_panic) {
331331
Some(attr) => {
332332
let sd = &cx.parse_sess.span_diagnostic;
333333

src/librustc_builtin_macros/test_harness.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub fn inject(
5151
// unconditional, so that the attribute is still marked as used in
5252
// non-test builds.
5353
let reexport_test_harness_main =
54-
attr::first_attr_value_str_by_name(&krate.attrs, sym::reexport_test_harness_main);
54+
attr::first_attr_value_str_by_name2(&krate.attrs, sym::reexport_test_harness_main);
5555

5656
// Do this here so that the test_runner crate attribute gets marked as used
5757
// even in non-test builds
@@ -344,7 +344,7 @@ fn is_test_case(i: &ast::Item) -> bool {
344344
}
345345

346346
fn get_test_runner(sd: &rustc_errors::Handler, krate: &ast::Crate) -> Option<ast::Path> {
347-
let test_attr = attr::find_by_name(&krate.attrs, sym::test_runner)?;
347+
let test_attr = attr::find_by_name2(&krate.attrs, sym::test_runner)?;
348348
let meta_list = test_attr.meta_item_list()?;
349349
let span = test_attr.span;
350350
match &*meta_list {

src/librustc_expand/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ impl SyntaxExtension {
818818
kind,
819819
span,
820820
allow_internal_unstable,
821-
allow_internal_unsafe: attr::contains_name(attrs, sym::allow_internal_unsafe),
821+
allow_internal_unsafe: attr::contains_name2(attrs, sym::allow_internal_unsafe),
822822
local_inner_macros,
823823
stability,
824824
deprecation: attr::find_deprecation(&sess, attrs, span),

src/librustc_expand/config.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ fn get_features(
7070
// Process the edition umbrella feature-gates first, to ensure
7171
// `edition_enabled_features` is completed before it's queried.
7272
for attr in krate_attrs {
73-
if !attr.check_name(sym::feature) {
73+
if !attr.check_name2(sym::feature) {
7474
continue;
7575
}
7676

@@ -280,9 +280,6 @@ impl<'a> StripUnconfigured<'a> {
280280
return vec![attr];
281281
}
282282

283-
// At this point we know the attribute is considered used.
284-
attr::mark_used(&attr);
285-
286283
if !attr::cfg_matches(&cfg_predicate, self.sess, self.features) {
287284
return vec![];
288285
}
@@ -528,5 +525,5 @@ impl<'a> MutVisitor for StripUnconfigured<'a> {
528525
}
529526

530527
fn is_cfg(attr: &Attribute) -> bool {
531-
attr.check_name(sym::cfg)
528+
attr.check_name2(sym::cfg)
532529
}

src/librustc_expand/module.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ crate fn push_directory(
102102
attrs: &[Attribute],
103103
Directory { mut ownership, mut path }: Directory,
104104
) -> Directory {
105-
if let Some(filename) = attr::first_attr_value_str_by_name(attrs, sym::path) {
105+
if let Some(filename) = attr::first_attr_value_str_by_name2(attrs, sym::path) {
106106
path.push(&*filename.as_str());
107107
ownership = DirectoryOwnership::Owned { relative: None };
108108
} else {
@@ -220,7 +220,7 @@ fn error_cannot_declare_mod_here<'a, T>(
220220
// Public for rustfmt usage.
221221
pub fn submod_path_from_attr(attrs: &[Attribute], dir_path: &Path) -> Option<PathBuf> {
222222
// Extract path string from first `#[path = "path_string"]` attribute.
223-
let path_string = attr::first_attr_value_str_by_name(attrs, sym::path)?;
223+
let path_string = attr::first_attr_value_str_by_name2(attrs, sym::path)?;
224224
let path_string = path_string.as_str();
225225

226226
// On windows, the base path might have the form

src/librustc_hir/lang_items.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,9 @@ impl<CTX> HashStable<CTX> for LangItem {
144144
pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
145145
attrs.iter().find_map(|attr| {
146146
Some(match attr {
147-
_ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span),
148-
_ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span),
149-
_ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span),
147+
_ if attr.check_name2(sym::lang) => (attr.value_str()?, attr.span),
148+
_ if attr.check_name2(sym::panic_handler) => (sym::panic_impl, attr.span),
149+
_ if attr.check_name2(sym::alloc_error_handler) => (sym::oom, attr.span),
150150
_ => return None,
151151
})
152152
})

src/librustc_interface/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ pub fn collect_crate_types(session: &Session, attrs: &[ast::Attribute]) -> Vec<C
469469
let attr_types: Vec<CrateType> = attrs
470470
.iter()
471471
.filter_map(|a| {
472-
if a.check_name(sym::crate_type) {
472+
if a.check_name2(sym::crate_type) {
473473
match a.value_str() {
474474
Some(s) => categorize_crate_type(s),
475475
_ => None,

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ impl EarlyLintPass for DeprecatedAttr {
737737
return;
738738
}
739739
}
740-
if attr.check_name(sym::no_start) || attr.check_name(sym::crate_id) {
740+
if attr.check_name2(sym::no_start) || attr.check_name2(sym::crate_id) {
741741
let path_str = pprust::path_to_string(&attr.get_normal_item().path);
742742
let msg = format!("use of deprecated attribute `{}`: no longer used.", path_str);
743743
lint_deprecated_attr(cx, attr, &msg, None);

src/librustc_lint/levels.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,13 @@ impl<'s> LintLevelsBuilder<'s> {
121121
for attr in attrs {
122122
let level = match Level::from_symbol(attr.name_or_empty()) {
123123
None => continue,
124-
Some(lvl) => lvl,
124+
Some(lvl) => {
125+
attr::mark_used(attr);
126+
lvl
127+
}
125128
};
126129

127130
let meta = unwrap_or!(attr.meta(), continue);
128-
attr::mark_used(attr);
129-
130131
let mut metas = unwrap_or!(meta.meta_item_list(), continue);
131132

132133
if metas.is_empty() {

src/librustc_metadata/creader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ impl<'a> CrateLoader<'a> {
900900
}
901901
None => item.ident.name,
902902
};
903-
let dep_kind = if attr::contains_name(&item.attrs, sym::no_link) {
903+
let dep_kind = if attr::contains_name2(&item.attrs, sym::no_link) {
904904
DepKind::MacrosOnly
905905
} else {
906906
DepKind::Explicit

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::native_libs;
55
use crate::rmeta::{self, encoder};
66

77
use rustc_ast::ast;
8-
use rustc_ast::attr;
98
use rustc_ast::expand::allocator::AllocatorKind;
109
use rustc_data_structures::svh::Svh;
1110
use rustc_hir as hir;
@@ -414,10 +413,6 @@ impl CStore {
414413

415414
// Mark the attrs as used
416415
let attrs = data.get_item_attrs(id.index, sess);
417-
for attr in attrs.iter() {
418-
attr::mark_used(attr);
419-
}
420-
421416
let ident = data.item_ident(id.index, sess);
422417

423418
LoadedMacro::MacroDef(

src/librustc_middle/middle/limits.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn update_limit(
2727
default: usize,
2828
) {
2929
for attr in &krate.attrs {
30-
if !attr.check_name(name) {
30+
if !attr.check_name2(name) {
3131
continue;
3232
}
3333

src/librustc_middle/ty/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3020,6 +3020,10 @@ impl<'tcx> TyCtxt<'tcx> {
30203020
attr::contains_name(&self.get_attrs(did), attr)
30213021
}
30223022

3023+
pub fn has_attr2(self, did: DefId, attr: Symbol) -> bool {
3024+
attr::contains_name2(&self.get_attrs(did), attr)
3025+
}
3026+
30233027
/// Returns `true` if this is an `auto trait`.
30243028
pub fn trait_is_auto(self, trait_def_id: DefId) -> bool {
30253029
self.trait_def(trait_def_id).has_auto_impl

src/librustc_parse/validate_attr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::parse_in;
44

55
use rustc_ast::ast::{self, Attribute, MacArgs, MacDelimiter, MetaItem, MetaItemKind};
6+
use rustc_ast::attr;
67
use rustc_ast::tokenstream::DelimSpan;
78
use rustc_errors::{Applicability, PResult};
89
use rustc_feature::{AttributeTemplate, BUILTIN_ATTRIBUTE_MAP};
@@ -154,6 +155,8 @@ pub fn check_builtin_attribute(
154155
)
155156
.emit();
156157
}
158+
// Avoid additional noise from the unused lint.
159+
attr::mark_used(attr);
157160
}
158161
}
159162
Err(mut err) => {

src/librustc_passes/diagnostic_items.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fn collect_item(
8888
/// Extract the first `rustc_diagnostic_item = "$name"` out of a list of attributes.
8989
fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
9090
attrs.iter().find_map(|attr| {
91-
if attr.check_name(sym::rustc_diagnostic_item) { attr.value_str() } else { None }
91+
if attr.check_name2(sym::rustc_diagnostic_item) { attr.value_str() } else { None }
9292
})
9393
}
9494

src/librustc_passes/entry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ fn entry_fn(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option<(LocalDefId, EntryFnType)
8181
fn entry_point_type(item: &Item<'_>, at_root: bool) -> EntryPointType {
8282
match item.kind {
8383
ItemKind::Fn(..) => {
84-
if attr::contains_name(&item.attrs, sym::start) {
84+
if attr::contains_name2(&item.attrs, sym::start) {
8585
EntryPointType::Start
86-
} else if attr::contains_name(&item.attrs, sym::main) {
86+
} else if attr::contains_name2(&item.attrs, sym::main) {
8787
EntryPointType::MainAttr
8888
} else if item.ident.name == sym::main {
8989
if at_root {

src/librustc_passes/stability.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ impl<'a, 'tcx> Annotator<'a, 'tcx> {
252252
for attr in attrs {
253253
let name = attr.name_or_empty();
254254
if unstable_attrs.contains(&name) {
255-
attr::mark_used(attr);
256255
struct_span_err!(
257256
self.tcx.sess,
258257
attr.span,

src/librustc_plugin_impl/load.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn load_plugins(
3232
let mut plugins = Vec::new();
3333

3434
for attr in &krate.attrs {
35-
if !attr.check_name(sym::plugin) {
35+
if !attr.check_name2(sym::plugin) {
3636
continue;
3737
}
3838

0 commit comments

Comments
 (0)