From 3dc8a3695825a8f7c5375d2560bc58573326699d Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Fri, 7 Aug 2020 11:11:44 +1000 Subject: [PATCH] Eliminate `librustc_hir`'s dependency on `librustc_session`. --- Cargo.lock | 1 - src/librustc_hir/Cargo.toml | 1 - src/librustc_hir/lang_items.rs | 17 ++++++++++++----- src/librustc_hir/weak_lang_items.rs | 10 +++++++--- src/librustc_passes/lang_items.rs | 3 ++- src/librustc_passes/weak_lang_items.rs | 3 ++- src/librustc_typeck/collect.rs | 3 ++- 7 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de297f51536..a3b37465f1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3466,7 +3466,6 @@ dependencies = [ "rustc_index", "rustc_macros", "rustc_serialize", - "rustc_session", "rustc_span", "rustc_target", "smallvec 1.4.0", diff --git a/src/librustc_hir/Cargo.toml b/src/librustc_hir/Cargo.toml index 7acaa01764d..4a404e176e1 100644 --- a/src/librustc_hir/Cargo.toml +++ b/src/librustc_hir/Cargo.toml @@ -16,7 +16,6 @@ rustc_data_structures = { path = "../librustc_data_structures" } rustc_index = { path = "../librustc_index" } rustc_span = { path = "../librustc_span" } rustc_serialize = { path = "../librustc_serialize" } -rustc_session = { path = "../librustc_session" } rustc_ast = { path = "../librustc_ast" } lazy_static = "1" log = { package = "tracing", version = "0.1" } diff --git a/src/librustc_hir/lang_items.rs b/src/librustc_hir/lang_items.rs index bf088473e15..7f473a45848 100644 --- a/src/librustc_hir/lang_items.rs +++ b/src/librustc_hir/lang_items.rs @@ -16,7 +16,6 @@ use rustc_ast::ast; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_macros::HashStable_Generic; -use rustc_session::Session; use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::Span; @@ -142,12 +141,20 @@ impl HashStable for LangItem { /// 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. -pub fn extract(sess: &Session, attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> { +/// +/// 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 `librustc_hir` to depend on `librustc_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, +{ attrs.iter().find_map(|attr| { Some(match attr { - _ if sess.check_name(attr, sym::lang) => (attr.value_str()?, attr.span), - _ if sess.check_name(attr, sym::panic_handler) => (sym::panic_impl, attr.span), - _ if sess.check_name(attr, sym::alloc_error_handler) => (sym::oom, attr.span), + _ 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), _ => return None, }) }) diff --git a/src/librustc_hir/weak_lang_items.rs b/src/librustc_hir/weak_lang_items.rs index ddec2e5bc49..fd64361cb35 100644 --- a/src/librustc_hir/weak_lang_items.rs +++ b/src/librustc_hir/weak_lang_items.rs @@ -5,7 +5,6 @@ use crate::{lang_items, LangItem, LanguageItems}; use rustc_ast::ast; use rustc_data_structures::fx::FxHashMap; -use rustc_session::Session; use rustc_span::symbol::{sym, Symbol}; use lazy_static::lazy_static; @@ -21,8 +20,13 @@ lazy_static! { }; } -pub fn link_name(sess: &Session, attrs: &[ast::Attribute]) -> Option { - lang_items::extract(sess, attrs).and_then(|(name, _)| { +/// The `check_name` argument avoids the need for `librustc_hir` to depend on +/// `librustc_session`. +pub fn link_name<'a, F>(check_name: F, attrs: &'a [ast::Attribute]) -> Option +where + F: Fn(&'a ast::Attribute, Symbol) -> bool +{ + lang_items::extract(check_name, attrs).and_then(|(name, _)| { $(if name == sym::$name { Some(sym::$sym) } else)* { diff --git a/src/librustc_passes/lang_items.rs b/src/librustc_passes/lang_items.rs index f97ac26f4b7..07415870549 100644 --- a/src/librustc_passes/lang_items.rs +++ b/src/librustc_passes/lang_items.rs @@ -56,7 +56,8 @@ impl LanguageItemCollector<'tcx> { } fn check_for_lang(&mut self, actual_target: Target, hir_id: HirId, attrs: &[Attribute]) { - if let Some((value, span)) = extract(&self.tcx.sess, &attrs) { + let check_name = |attr, sym| self.tcx.sess.check_name(attr, sym); + if let Some((value, span)) = extract(check_name, &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/src/librustc_passes/weak_lang_items.rs b/src/librustc_passes/weak_lang_items.rs index 0a7b195a823..2749b96bc85 100644 --- a/src/librustc_passes/weak_lang_items.rs +++ b/src/librustc_passes/weak_lang_items.rs @@ -100,7 +100,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> { } fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) { - if let Some((lang_item, _)) = hir::lang_items::extract(&self.tcx.sess, &i.attrs) { + let check_name = |attr, sym| self.tcx.sess.check_name(attr, sym); + if let Some((lang_item, _)) = hir::lang_items::extract(check_name, &i.attrs) { self.register(lang_item, i.span, i.hir_id); } intravisit::walk_foreign_item(self, i) diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index d293c9671a2..34ce7ca31c0 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -2614,7 +2614,8 @@ 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; } - if let Some(name) = weak_lang_items::link_name(&tcx.sess, &attrs) { + let check_name = |attr, sym| tcx.sess.check_name(attr, sym); + if let Some(name) = weak_lang_items::link_name(check_name, &attrs) { codegen_fn_attrs.export_name = Some(name); codegen_fn_attrs.link_name = Some(name); }