Skip to content

Commit 2b75720

Browse files
committed
Clean up lang_items::extract
Noted in #87739 (review), lang_items::extract no longer needs to take a closure.
1 parent f7bb8e3 commit 2b75720

File tree

5 files changed

+9
-27
lines changed

5 files changed

+9
-27
lines changed

compiler/rustc_hir/src/lang_items.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,12 @@ impl<CTX> HashStable<CTX> for LangItem {
151151
/// Extracts the first `lang = "$name"` out of a list of attributes.
152152
/// The attributes `#[panic_handler]` and `#[alloc_error_handler]`
153153
/// are also extracted out when found.
154-
///
155-
/// About the `check_name` argument: passing in a `Session` would be simpler,
156-
/// because then we could call `Session::check_name` directly. But we want to
157-
/// avoid the need for `rustc_hir` to depend on `rustc_session`, so we
158-
/// use a closure instead.
159-
pub fn extract<'a, F>(check_name: F, attrs: &'a [ast::Attribute]) -> Option<(Symbol, Span)>
160-
where
161-
F: Fn(&'a ast::Attribute, Symbol) -> bool,
162-
{
154+
pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
163155
attrs.iter().find_map(|attr| {
164156
Some(match attr {
165-
_ if check_name(attr, sym::lang) => (attr.value_str()?, attr.span),
166-
_ if check_name(attr, sym::panic_handler) => (sym::panic_impl, attr.span),
167-
_ if check_name(attr, sym::alloc_error_handler) => (sym::oom, attr.span),
157+
_ if attr.has_name(sym::lang) => (attr.value_str()?, attr.span),
158+
_ if attr.has_name(sym::panic_handler) => (sym::panic_impl, attr.span),
159+
_ if attr.has_name(sym::alloc_error_handler) => (sym::oom, attr.span),
168160
_ => return None,
169161
})
170162
})

compiler/rustc_hir/src/weak_lang_items.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,9 @@ pub static WEAK_ITEMS_REFS: SyncLazy<StableMap<Symbol, LangItem>> = SyncLazy::ne
1818
map
1919
});
2020

21-
/// The `check_name` argument avoids the need for `rustc_hir` to depend on
22-
/// `rustc_session`.
23-
pub fn link_name<'a, F>(check_name: F, attrs: &'a [ast::Attribute]) -> Option<Symbol>
24-
where
25-
F: Fn(&'a ast::Attribute, Symbol) -> bool
21+
pub fn link_name(attrs: &[ast::Attribute]) -> Option<Symbol>
2622
{
27-
lang_items::extract(check_name, attrs).and_then(|(name, _)| {
23+
lang_items::extract(attrs).and_then(|(name, _)| {
2824
$(if name == sym::$name {
2925
Some(sym::$sym)
3026
} else)* {

compiler/rustc_passes/src/lang_items.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use crate::check_attr::target_from_impl_item;
1111
use crate::weak_lang_items;
1212

13-
use rustc_ast::Attribute;
1413
use rustc_errors::{pluralize, struct_span_err};
1514
use rustc_hir as hir;
1615
use rustc_hir::def_id::DefId;
@@ -57,8 +56,7 @@ impl<'tcx> LanguageItemCollector<'tcx> {
5756

5857
fn check_for_lang(&mut self, actual_target: Target, hir_id: HirId) {
5958
let attrs = self.tcx.hir().attrs(hir_id);
60-
let check_name = |attr: &Attribute, sym| attr.has_name(sym);
61-
if let Some((value, span)) = extract(check_name, &attrs) {
59+
if let Some((value, span)) = extract(&attrs) {
6260
match ITEM_REFS.get(&value).cloned() {
6361
// Known lang item with attribute on correct target.
6462
Some((item_index, expected_target)) if actual_target == expected_target => {

compiler/rustc_passes/src/weak_lang_items.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Validity checking for weak lang items
22
3-
use rustc_ast::Attribute;
43
use rustc_data_structures::fx::FxHashSet;
54
use rustc_errors::struct_span_err;
65
use rustc_hir as hir;
@@ -103,9 +102,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
103102
}
104103

105104
fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) {
106-
let check_name = |attr: &Attribute, sym| attr.has_name(sym);
107105
let attrs = self.tcx.hir().attrs(i.hir_id());
108-
if let Some((lang_item, _)) = lang_items::extract(check_name, attrs) {
106+
if let Some((lang_item, _)) = lang_items::extract(attrs) {
109107
self.register(lang_item, i.span);
110108
}
111109
intravisit::walk_foreign_item(self, i)

compiler/rustc_typeck/src/collect.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use crate::constrained_generic_params as cgp;
2121
use crate::errors;
2222
use crate::middle::resolve_lifetime as rl;
2323
use rustc_ast as ast;
24-
use rustc_ast::Attribute;
2524
use rustc_ast::{MetaItemKind, NestedMetaItem};
2625
use rustc_attr::{list_contains_name, InlineAttr, InstructionSetAttr, OptimizeAttr};
2726
use rustc_data_structures::captures::Captures;
@@ -3120,8 +3119,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
31203119
if tcx.is_weak_lang_item(id) {
31213120
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
31223121
}
3123-
let check_name = |attr: &Attribute, sym| attr.has_name(sym);
3124-
if let Some(name) = weak_lang_items::link_name(check_name, attrs) {
3122+
if let Some(name) = weak_lang_items::link_name(attrs) {
31253123
codegen_fn_attrs.export_name = Some(name);
31263124
codegen_fn_attrs.link_name = Some(name);
31273125
}

0 commit comments

Comments
 (0)