From 2b7572092c9953cd82d99768732e624bd6da56e9 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Sun, 9 Jan 2022 13:41:04 -0800 Subject: [PATCH] Clean up lang_items::extract Noted in https://github.com/rust-lang/rust/pull/87739#pullrequestreview-740497194, lang_items::extract no longer needs to take a closure. --- compiler/rustc_hir/src/lang_items.rs | 16 ++++------------ compiler/rustc_hir/src/weak_lang_items.rs | 8 ++------ compiler/rustc_passes/src/lang_items.rs | 4 +--- compiler/rustc_passes/src/weak_lang_items.rs | 4 +--- compiler/rustc_typeck/src/collect.rs | 4 +--- 5 files changed, 9 insertions(+), 27 deletions(-) diff --git a/compiler/rustc_hir/src/lang_items.rs b/compiler/rustc_hir/src/lang_items.rs index a03c561861e..def0c1d0687 100644 --- a/compiler/rustc_hir/src/lang_items.rs +++ b/compiler/rustc_hir/src/lang_items.rs @@ -151,20 +151,12 @@ fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) { /// Extracts the first `lang = "$name"` out of a list of attributes. /// The attributes `#[panic_handler]` and `#[alloc_error_handler]` /// are also extracted out when found. -/// -/// About the `check_name` argument: passing in a `Session` would be simpler, -/// because then we could call `Session::check_name` directly. But we want to -/// avoid the need for `rustc_hir` to depend on `rustc_session`, so we -/// use a closure instead. -pub fn extract<'a, F>(check_name: F, attrs: &'a [ast::Attribute]) -> Option<(Symbol, Span)> -where - F: Fn(&'a ast::Attribute, Symbol) -> bool, -{ +pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> { attrs.iter().find_map(|attr| { Some(match attr { - _ if check_name(attr, sym::lang) => (attr.value_str()?, attr.span), - _ if check_name(attr, sym::panic_handler) => (sym::panic_impl, attr.span), - _ if check_name(attr, sym::alloc_error_handler) => (sym::oom, attr.span), + _ if attr.has_name(sym::lang) => (attr.value_str()?, attr.span), + _ if attr.has_name(sym::panic_handler) => (sym::panic_impl, attr.span), + _ if attr.has_name(sym::alloc_error_handler) => (sym::oom, attr.span), _ => return None, }) }) diff --git a/compiler/rustc_hir/src/weak_lang_items.rs b/compiler/rustc_hir/src/weak_lang_items.rs index 58c3065240c..78748209d1a 100644 --- a/compiler/rustc_hir/src/weak_lang_items.rs +++ b/compiler/rustc_hir/src/weak_lang_items.rs @@ -18,13 +18,9 @@ macro_rules! weak_lang_items { map }); -/// The `check_name` argument avoids the need for `rustc_hir` to depend on -/// `rustc_session`. -pub fn link_name<'a, F>(check_name: F, attrs: &'a [ast::Attribute]) -> Option -where - F: Fn(&'a ast::Attribute, Symbol) -> bool +pub fn link_name(attrs: &[ast::Attribute]) -> Option { - lang_items::extract(check_name, attrs).and_then(|(name, _)| { + lang_items::extract(attrs).and_then(|(name, _)| { $(if name == sym::$name { Some(sym::$sym) } else)* { diff --git a/compiler/rustc_passes/src/lang_items.rs b/compiler/rustc_passes/src/lang_items.rs index a808d6c8348..0c934ecc913 100644 --- a/compiler/rustc_passes/src/lang_items.rs +++ b/compiler/rustc_passes/src/lang_items.rs @@ -10,7 +10,6 @@ use crate::check_attr::target_from_impl_item; use crate::weak_lang_items; -use rustc_ast::Attribute; use rustc_errors::{pluralize, struct_span_err}; use rustc_hir as hir; use rustc_hir::def_id::DefId; @@ -57,8 +56,7 @@ fn new(tcx: TyCtxt<'tcx>) -> LanguageItemCollector<'tcx> { fn check_for_lang(&mut self, actual_target: Target, hir_id: HirId) { let attrs = self.tcx.hir().attrs(hir_id); - let check_name = |attr: &Attribute, sym| attr.has_name(sym); - if let Some((value, span)) = extract(check_name, &attrs) { + if let Some((value, span)) = extract(&attrs) { match ITEM_REFS.get(&value).cloned() { // Known lang item with attribute on correct target. Some((item_index, expected_target)) if actual_target == expected_target => { diff --git a/compiler/rustc_passes/src/weak_lang_items.rs b/compiler/rustc_passes/src/weak_lang_items.rs index 61c82f031dd..21514d19f6a 100644 --- a/compiler/rustc_passes/src/weak_lang_items.rs +++ b/compiler/rustc_passes/src/weak_lang_items.rs @@ -1,6 +1,5 @@ //! Validity checking for weak lang items -use rustc_ast::Attribute; use rustc_data_structures::fx::FxHashSet; use rustc_errors::struct_span_err; use rustc_hir as hir; @@ -103,9 +102,8 @@ fn nested_visit_map(&mut self) -> NestedVisitorMap { } fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) { - let check_name = |attr: &Attribute, sym| attr.has_name(sym); let attrs = self.tcx.hir().attrs(i.hir_id()); - if let Some((lang_item, _)) = lang_items::extract(check_name, attrs) { + if let Some((lang_item, _)) = lang_items::extract(attrs) { self.register(lang_item, i.span); } intravisit::walk_foreign_item(self, i) diff --git a/compiler/rustc_typeck/src/collect.rs b/compiler/rustc_typeck/src/collect.rs index d4d4baa3f71..ccf8d1d9cea 100644 --- a/compiler/rustc_typeck/src/collect.rs +++ b/compiler/rustc_typeck/src/collect.rs @@ -21,7 +21,6 @@ use crate::errors; use crate::middle::resolve_lifetime as rl; use rustc_ast as ast; -use rustc_ast::Attribute; use rustc_ast::{MetaItemKind, NestedMetaItem}; use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr}; use rustc_data_structures::captures::Captures; @@ -3120,8 +3119,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { if tcx.is_weak_lang_item(id) { codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL; } - let check_name = |attr: &Attribute, sym| attr.has_name(sym); - if let Some(name) = weak_lang_items::link_name(check_name, attrs) { + if let Some(name) = weak_lang_items::link_name(attrs) { codegen_fn_attrs.export_name = Some(name); codegen_fn_attrs.link_name = Some(name); }