Eliminate librustc_hir's dependency on librustc_session.

This commit is contained in:
Nicholas Nethercote 2020-08-07 11:11:44 +10:00
parent e539dd65f8
commit 3dc8a36958
7 changed files with 25 additions and 13 deletions

View File

@ -3466,7 +3466,6 @@ dependencies = [
"rustc_index",
"rustc_macros",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
"smallvec 1.4.0",

View File

@ -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" }

View File

@ -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<CTX> HashStable<CTX> 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,
})
})

View File

@ -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<Symbol> {
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<Symbol>
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)* {

View File

@ -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 => {

View File

@ -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)

View File

@ -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);
}