Skip to content

Commit

Permalink
Eliminate librustc_hir's dependency on librustc_session.
Browse files Browse the repository at this point in the history
  • Loading branch information
nnethercote committed Aug 8, 2020
1 parent e539dd6 commit 3dc8a36
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 13 deletions.
1 change: 0 additions & 1 deletion Cargo.lock
Expand Up @@ -3466,7 +3466,6 @@ dependencies = [
"rustc_index",
"rustc_macros",
"rustc_serialize",
"rustc_session",
"rustc_span",
"rustc_target",
"smallvec 1.4.0",
Expand Down
1 change: 0 additions & 1 deletion src/librustc_hir/Cargo.toml
Expand Up @@ -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" }
Expand Down
17 changes: 12 additions & 5 deletions src/librustc_hir/lang_items.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
})
})
Expand Down
10 changes: 7 additions & 3 deletions src/librustc_hir/weak_lang_items.rs
Expand Up @@ -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;
Expand All @@ -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)* {
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_passes/lang_items.rs
Expand Up @@ -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 => {
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_passes/weak_lang_items.rs
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/collect.rs
Expand Up @@ -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);
}
Expand Down

0 comments on commit 3dc8a36

Please sign in to comment.