Skip to content

[WIP] Investigate mark_used(attr) #74952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 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,9 +173,14 @@ 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.
pub fn check_name(&self, name: Symbol) -> bool {
self.has_name(name)
}

pub fn check_name2(&self, name: Symbol) -> bool {
let matches = self.has_name(name);
if matches {
mark_used(self);
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,25 +410,41 @@ 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 {
attrs.iter().any(|item| item.check_name(name))
}

pub fn contains_name2(attrs: &[Attribute], name: Symbol) -> bool {
attrs.iter().any(|item| item.check_name2(name))
}

pub fn find_by_name(attrs: &[Attribute], name: Symbol) -> Option<&Attribute> {
attrs.iter().find(|attr| attr.check_name(name))
}

pub fn find_by_name2(attrs: &[Attribute], name: Symbol) -> Option<&Attribute> {
attrs.iter().find(|attr| attr.check_name2(name))
}

pub fn filter_by_name(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> {
attrs.iter().filter(move |attr| attr.check_name(name))
}

pub fn filter_by_name2(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> {
attrs.iter().filter(move |attr| attr.check_name2(name))
}

pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option<Symbol> {
attrs.iter().find(|at| at.check_name(name)).and_then(|at| at.value_str())
}

pub fn first_attr_value_str_by_name2(attrs: &[Attribute], name: Symbol) -> Option<Symbol> {
attrs.iter().find(|at| at.check_name2(name)).and_then(|at| at.value_str())
}

impl MetaItem {
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
let mut idents = vec![];
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast_lowering/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2236,7 +2236,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
hir_id: self.lower_node_id(param.id),
name,
span: param.ident.span,
pure_wrt_drop: attr::contains_name(&param.attrs, sym::may_dangle),
pure_wrt_drop: attr::contains_name2(&param.attrs, sym::may_dangle),
attrs: self.lower_attrs(&param.attrs),
bounds: self.arena.alloc_from_iter(bounds),
kind,
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
21 changes: 8 additions & 13 deletions src/librustc_attr/builtin.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Parsing and validation of builtin attributes

use super::{find_by_name, mark_used};
use super::find_by_name;

use rustc_ast::ast::{self, Attribute, Lit, LitKind, MetaItem, MetaItemKind, NestedMetaItem};
use rustc_ast_pretty::pprust;
Expand Down 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 @@ -214,8 +214,6 @@ where
continue; // not a stability level
}

mark_used(attr);

let meta = attr.meta();

if attr.has_name(sym::rustc_promotable) {
Expand Down Expand Up @@ -505,7 +503,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 @@ -650,7 +648,7 @@ where
let diagnostic = &sess.span_diagnostic;

'outer: for attr in attrs_iter {
if !(attr.check_name(sym::deprecated) || attr.check_name(sym::rustc_deprecated)) {
if !(attr.check_name2(sym::deprecated) || attr.check_name(sym::rustc_deprecated)) {
continue;
}

Expand Down Expand Up @@ -773,8 +771,6 @@ where
}
}

mark_used(&attr);

let is_since_rustc_version = attr.check_name(sym::rustc_deprecated);
depr = Some(Deprecation { since, note, suggestion, is_since_rustc_version });
}
Expand Down Expand Up @@ -823,9 +819,8 @@ pub fn find_repr_attrs(sess: &ParseSess, attr: &Attribute) -> Vec<ReprAttr> {

let mut acc = Vec::new();
let diagnostic = &sess.span_diagnostic;
if attr.has_name(sym::repr) {
if attr.check_name2(sym::repr) {
if let Some(items) = attr.meta_item_list() {
mark_used(attr);
for item in items {
if !item.is_meta_item() {
handle_errors(
Expand Down Expand Up @@ -898,7 +893,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: 0 additions & 2 deletions src/librustc_builtin_macros/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,6 @@ impl<'a> TraitDef<'a> {
let self_type = cx.ty_path(path);

let attr = cx.attribute(cx.meta_word(self.span, sym::automatically_derived));
// Just mark it now since we know that it'll end up used downstream
attr::mark_used(&attr);
let opt_trait_ref = Some(trait_ref);
let unused_qual = {
let word = rustc_ast::attr::mk_nested_word_item(Ident::new(
Expand Down
3 changes: 2 additions & 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 Expand Up @@ -264,6 +264,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {

for attr in &item.attrs {
if is_proc_macro_attr(&attr) {
attr::mark_used(attr);
if let Some(prev_attr) = found_attr {
let prev_item = prev_attr.get_normal_item();
let item = attr.get_normal_item();
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_builtin_macros/standard_library_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ pub fn inject(
let rust_2018 = sess.edition >= Edition::Edition2018;

// the first name in this list is the crate name of the crate with the prelude
let names: &[Symbol] = if attr::contains_name(&krate.attrs, sym::no_core) {
let names: &[Symbol] = if attr::contains_name2(&krate.attrs, sym::no_core) {
return (krate, None);
} else if attr::contains_name(&krate.attrs, sym::no_std) {
} else if attr::contains_name2(&krate.attrs, sym::no_std) {
if attr::contains_name(&krate.attrs, sym::compiler_builtins) {
&[sym::core]
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_builtin_macros/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,15 +319,15 @@ enum ShouldPanic {
}

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

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

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

Expand All @@ -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_builtin_macros/test_harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub fn inject(
// unconditional, so that the attribute is still marked as used in
// non-test builds.
let reexport_test_harness_main =
attr::first_attr_value_str_by_name(&krate.attrs, sym::reexport_test_harness_main);
attr::first_attr_value_str_by_name2(&krate.attrs, sym::reexport_test_harness_main);

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

fn get_test_runner(sd: &rustc_errors::Handler, krate: &ast::Crate) -> Option<ast::Path> {
let test_attr = attr::find_by_name(&krate.attrs, sym::test_runner)?;
let test_attr = attr::find_by_name2(&krate.attrs, sym::test_runner)?;
let meta_list = test_attr.meta_item_list()?;
let span = test_attr.span;
match &*meta_list {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_expand/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ impl SyntaxExtension {
kind,
span,
allow_internal_unstable,
allow_internal_unsafe: attr::contains_name(attrs, sym::allow_internal_unsafe),
allow_internal_unsafe: attr::contains_name2(attrs, sym::allow_internal_unsafe),
local_inner_macros,
stability,
deprecation: attr::find_deprecation(&sess, attrs, span),
Expand Down
7 changes: 2 additions & 5 deletions src/librustc_expand/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn get_features(
// Process the edition umbrella feature-gates first, to ensure
// `edition_enabled_features` is completed before it's queried.
for attr in krate_attrs {
if !attr.check_name(sym::feature) {
if !attr.check_name2(sym::feature) {
continue;
}

Expand Down Expand Up @@ -280,9 +280,6 @@ impl<'a> StripUnconfigured<'a> {
return vec![attr];
}

// At this point we know the attribute is considered used.
attr::mark_used(&attr);

if !attr::cfg_matches(&cfg_predicate, self.sess, self.features) {
return vec![];
}
Expand Down Expand Up @@ -528,5 +525,5 @@ impl<'a> MutVisitor for StripUnconfigured<'a> {
}

fn is_cfg(attr: &Attribute) -> bool {
attr.check_name(sym::cfg)
attr.check_name2(sym::cfg)
}
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
4 changes: 2 additions & 2 deletions src/librustc_expand/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ crate fn push_directory(
attrs: &[Attribute],
Directory { mut ownership, mut path }: Directory,
) -> Directory {
if let Some(filename) = attr::first_attr_value_str_by_name(attrs, sym::path) {
if let Some(filename) = attr::first_attr_value_str_by_name2(attrs, sym::path) {
path.push(&*filename.as_str());
ownership = DirectoryOwnership::Owned { relative: None };
} else {
Expand Down Expand Up @@ -220,7 +220,7 @@ fn error_cannot_declare_mod_here<'a, T>(
// Public for rustfmt usage.
pub fn submod_path_from_attr(attrs: &[Attribute], dir_path: &Path) -> Option<PathBuf> {
// Extract path string from first `#[path = "path_string"]` attribute.
let path_string = attr::first_attr_value_str_by_name(attrs, sym::path)?;
let path_string = attr::first_attr_value_str_by_name2(attrs, sym::path)?;
let path_string = path_string.as_str();

// On windows, the base path might have the form
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_hir/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ impl<CTX> HashStable<CTX> for LangItem {
pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
attrs.iter().find_map(|attr| {
Some(match attr {
_ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span),
_ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span),
_ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span),
_ if attr.check_name2(sym::lang) => (attr.value_str()?, attr.span),
_ if attr.check_name2(sym::panic_handler) => (sym::panic_impl, attr.span),
_ if attr.check_name2(sym::alloc_error_handler) => (sym::oom, attr.span),
_ => return None,
})
})
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
Loading