Skip to content

Commit 3dc8a36

Browse files
committed
Eliminate librustc_hir's dependency on librustc_session.
1 parent e539dd6 commit 3dc8a36

File tree

7 files changed

+25
-13
lines changed

7 files changed

+25
-13
lines changed

Cargo.lock

-1
Original file line numberDiff line numberDiff line change
@@ -3466,7 +3466,6 @@ dependencies = [
34663466
"rustc_index",
34673467
"rustc_macros",
34683468
"rustc_serialize",
3469-
"rustc_session",
34703469
"rustc_span",
34713470
"rustc_target",
34723471
"smallvec 1.4.0",

src/librustc_hir/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ rustc_data_structures = { path = "../librustc_data_structures" }
1616
rustc_index = { path = "../librustc_index" }
1717
rustc_span = { path = "../librustc_span" }
1818
rustc_serialize = { path = "../librustc_serialize" }
19-
rustc_session = { path = "../librustc_session" }
2019
rustc_ast = { path = "../librustc_ast" }
2120
lazy_static = "1"
2221
log = { package = "tracing", version = "0.1" }

src/librustc_hir/lang_items.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_ast::ast;
1616
use rustc_data_structures::fx::FxHashMap;
1717
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1818
use rustc_macros::HashStable_Generic;
19-
use rustc_session::Session;
2019
use rustc_span::symbol::{kw, sym, Symbol};
2120
use rustc_span::Span;
2221

@@ -142,12 +141,20 @@ impl<CTX> HashStable<CTX> for LangItem {
142141
/// Extracts the first `lang = "$name"` out of a list of attributes.
143142
/// The attributes `#[panic_handler]` and `#[alloc_error_handler]`
144143
/// are also extracted out when found.
145-
pub fn extract(sess: &Session, attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
144+
///
145+
/// About the `check_name` argument: passing in a `Session` would be simpler,
146+
/// because then we could call `Session::check_name` directly. But we want to
147+
/// avoid the need for `librustc_hir` to depend on `librustc_session`, so we
148+
/// use a closure instead.
149+
pub fn extract<'a, F>(check_name: F, attrs: &'a [ast::Attribute]) -> Option<(Symbol, Span)>
150+
where
151+
F: Fn(&'a ast::Attribute, Symbol) -> bool,
152+
{
146153
attrs.iter().find_map(|attr| {
147154
Some(match attr {
148-
_ if sess.check_name(attr, sym::lang) => (attr.value_str()?, attr.span),
149-
_ if sess.check_name(attr, sym::panic_handler) => (sym::panic_impl, attr.span),
150-
_ if sess.check_name(attr, sym::alloc_error_handler) => (sym::oom, attr.span),
155+
_ if check_name(attr, sym::lang) => (attr.value_str()?, attr.span),
156+
_ if check_name(attr, sym::panic_handler) => (sym::panic_impl, attr.span),
157+
_ if check_name(attr, sym::alloc_error_handler) => (sym::oom, attr.span),
151158
_ => return None,
152159
})
153160
})

src/librustc_hir/weak_lang_items.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use crate::{lang_items, LangItem, LanguageItems};
55

66
use rustc_ast::ast;
77
use rustc_data_structures::fx::FxHashMap;
8-
use rustc_session::Session;
98
use rustc_span::symbol::{sym, Symbol};
109

1110
use lazy_static::lazy_static;
@@ -21,8 +20,13 @@ lazy_static! {
2120
};
2221
}
2322

24-
pub fn link_name(sess: &Session, attrs: &[ast::Attribute]) -> Option<Symbol> {
25-
lang_items::extract(sess, attrs).and_then(|(name, _)| {
23+
/// The `check_name` argument avoids the need for `librustc_hir` to depend on
24+
/// `librustc_session`.
25+
pub fn link_name<'a, F>(check_name: F, attrs: &'a [ast::Attribute]) -> Option<Symbol>
26+
where
27+
F: Fn(&'a ast::Attribute, Symbol) -> bool
28+
{
29+
lang_items::extract(check_name, attrs).and_then(|(name, _)| {
2630
$(if name == sym::$name {
2731
Some(sym::$sym)
2832
} else)* {

src/librustc_passes/lang_items.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ impl LanguageItemCollector<'tcx> {
5656
}
5757

5858
fn check_for_lang(&mut self, actual_target: Target, hir_id: HirId, attrs: &[Attribute]) {
59-
if let Some((value, span)) = extract(&self.tcx.sess, &attrs) {
59+
let check_name = |attr, sym| self.tcx.sess.check_name(attr, sym);
60+
if let Some((value, span)) = extract(check_name, &attrs) {
6061
match ITEM_REFS.get(&value).cloned() {
6162
// Known lang item with attribute on correct target.
6263
Some((item_index, expected_target)) if actual_target == expected_target => {

src/librustc_passes/weak_lang_items.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
100100
}
101101

102102
fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) {
103-
if let Some((lang_item, _)) = hir::lang_items::extract(&self.tcx.sess, &i.attrs) {
103+
let check_name = |attr, sym| self.tcx.sess.check_name(attr, sym);
104+
if let Some((lang_item, _)) = hir::lang_items::extract(check_name, &i.attrs) {
104105
self.register(lang_item, i.span, i.hir_id);
105106
}
106107
intravisit::walk_foreign_item(self, i)

src/librustc_typeck/collect.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2614,7 +2614,8 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs {
26142614
if tcx.is_weak_lang_item(id) {
26152615
codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL;
26162616
}
2617-
if let Some(name) = weak_lang_items::link_name(&tcx.sess, &attrs) {
2617+
let check_name = |attr, sym| tcx.sess.check_name(attr, sym);
2618+
if let Some(name) = weak_lang_items::link_name(check_name, &attrs) {
26182619
codegen_fn_attrs.export_name = Some(name);
26192620
codegen_fn_attrs.link_name = Some(name);
26202621
}

0 commit comments

Comments
 (0)