Skip to content

Commit e539dd6

Browse files
committed
Eliminate the SessionGlobals from librustc_ast.
By moving `{known,used}_attrs` from `SessionGlobals` to `Session`. This means they are accessed via the `Session`, rather than via TLS. A few `Attr` methods and `librustc_ast` functions are now methods of `Session`. All of this required passing a `Session` to lots of functions that didn't already have one. Some of these functions also had arguments removed, because those arguments could be accessed directly via the `Session` argument. `contains_feature_attr()` was dead, and is removed. Some functions were moved from `librustc_ast` elsewhere because they now need to access `Session`, which isn't available in that crate. - `entry_point_type()` --> `librustc_builtin_macros` - `global_allocator_spans()` --> `librustc_metadata` - `is_proc_macro_attr()` --> `Session`
1 parent d6fa011 commit e539dd6

File tree

120 files changed

+816
-805
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+816
-805
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3195,7 +3195,6 @@ dependencies = [
31953195
"rustc_macros",
31963196
"rustc_serialize",
31973197
"rustc_span",
3198-
"scoped-tls",
31993198
"smallvec 1.4.0",
32003199
"tracing",
32013200
]
@@ -3467,6 +3466,7 @@ dependencies = [
34673466
"rustc_index",
34683467
"rustc_macros",
34693468
"rustc_serialize",
3469+
"rustc_session",
34703470
"rustc_span",
34713471
"rustc_target",
34723472
"smallvec 1.4.0",

src/librustc_ast/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ doctest = false
1212
[dependencies]
1313
rustc_serialize = { path = "../librustc_serialize" }
1414
log = { package = "tracing", version = "0.1" }
15-
scoped-tls = "1.0"
1615
rustc_span = { path = "../librustc_span" }
1716
rustc_data_structures = { path = "../librustc_data_structures" }
1817
rustc_index = { path = "../librustc_index" }

src/librustc_ast/attr/mod.rs

+12-85
Original file line numberDiff line numberDiff line change
@@ -10,72 +10,30 @@ use crate::ptr::P;
1010
use crate::token::{self, CommentKind, Token};
1111
use crate::tokenstream::{DelimSpan, TokenStream, TokenTree, TreeAndJoint};
1212

13-
use rustc_data_structures::sync::Lock;
1413
use rustc_index::bit_set::GrowableBitSet;
15-
use rustc_span::edition::{Edition, DEFAULT_EDITION};
1614
use rustc_span::source_map::{BytePos, Spanned};
1715
use rustc_span::symbol::{sym, Ident, Symbol};
1816
use rustc_span::Span;
1917

20-
use log::debug;
2118
use std::iter;
2219
use std::ops::DerefMut;
2320

24-
// Per-session global variables: this struct is stored in thread-local storage
25-
// in such a way that it is accessible without any kind of handle to all
26-
// threads within the compilation session, but is not accessible outside the
27-
// session.
28-
pub struct SessionGlobals {
29-
used_attrs: Lock<GrowableBitSet<AttrId>>,
30-
known_attrs: Lock<GrowableBitSet<AttrId>>,
31-
span_session_globals: rustc_span::SessionGlobals,
32-
}
21+
pub struct MarkedAttrs(GrowableBitSet<AttrId>);
3322

34-
impl SessionGlobals {
35-
fn new(edition: Edition) -> SessionGlobals {
36-
SessionGlobals {
37-
// We have no idea how many attributes there will be, so just
38-
// initiate the vectors with 0 bits. We'll grow them as necessary.
39-
used_attrs: Lock::new(GrowableBitSet::new_empty()),
40-
known_attrs: Lock::new(GrowableBitSet::new_empty()),
41-
span_session_globals: rustc_span::SessionGlobals::new(edition),
42-
}
23+
impl MarkedAttrs {
24+
// We have no idea how many attributes there will be, so just
25+
// initiate the vectors with 0 bits. We'll grow them as necessary.
26+
pub fn new() -> Self {
27+
MarkedAttrs(GrowableBitSet::new_empty())
4328
}
44-
}
45-
46-
pub fn with_session_globals<R>(edition: Edition, f: impl FnOnce() -> R) -> R {
47-
let ast_session_globals = SessionGlobals::new(edition);
48-
SESSION_GLOBALS.set(&ast_session_globals, || {
49-
rustc_span::SESSION_GLOBALS.set(&ast_session_globals.span_session_globals, f)
50-
})
51-
}
52-
53-
pub fn with_default_session_globals<R>(f: impl FnOnce() -> R) -> R {
54-
with_session_globals(DEFAULT_EDITION, f)
55-
}
5629

57-
scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: SessionGlobals);
58-
59-
pub fn mark_used(attr: &Attribute) {
60-
debug!("marking {:?} as used", attr);
61-
SESSION_GLOBALS.with(|session_globals| {
62-
session_globals.used_attrs.lock().insert(attr.id);
63-
});
64-
}
65-
66-
pub fn is_used(attr: &Attribute) -> bool {
67-
SESSION_GLOBALS.with(|session_globals| session_globals.used_attrs.lock().contains(attr.id))
68-
}
69-
70-
pub fn mark_known(attr: &Attribute) {
71-
debug!("marking {:?} as known", attr);
72-
SESSION_GLOBALS.with(|session_globals| {
73-
session_globals.known_attrs.lock().insert(attr.id);
74-
});
75-
}
30+
pub fn mark(&mut self, attr: &Attribute) {
31+
self.0.insert(attr.id);
32+
}
7633

77-
pub fn is_known(attr: &Attribute) -> bool {
78-
SESSION_GLOBALS.with(|session_globals| session_globals.known_attrs.lock().contains(attr.id))
34+
pub fn is_marked(&self, attr: &Attribute) -> bool {
35+
self.0.contains(attr.id)
36+
}
7937
}
8038

8139
pub fn is_known_lint_tool(m_item: Ident) -> bool {
@@ -173,21 +131,6 @@ impl Attribute {
173131
}
174132
}
175133

176-
/// Returns `true` if the attribute's path matches the argument.
177-
/// If it matches, then the attribute is marked as used.
178-
/// Should only be used by rustc, other tools can use `has_name` instead,
179-
/// because only rustc is supposed to report the `unused_attributes` lint.
180-
/// `MetaItem` and `NestedMetaItem` are produced by "lowering" an `Attribute`
181-
/// and don't have identity, so they only has the `has_name` method,
182-
/// and you need to mark the original `Attribute` as used when necessary.
183-
pub fn check_name(&self, name: Symbol) -> bool {
184-
let matches = self.has_name(name);
185-
if matches {
186-
mark_used(self);
187-
}
188-
matches
189-
}
190-
191134
/// For a single-segment attribute, returns its name; otherwise, returns `None`.
192135
pub fn ident(&self) -> Option<Ident> {
193136
match self.kind {
@@ -418,22 +361,6 @@ pub fn list_contains_name(items: &[NestedMetaItem], name: Symbol) -> bool {
418361
items.iter().any(|item| item.has_name(name))
419362
}
420363

421-
pub fn contains_name(attrs: &[Attribute], name: Symbol) -> bool {
422-
attrs.iter().any(|item| item.check_name(name))
423-
}
424-
425-
pub fn find_by_name(attrs: &[Attribute], name: Symbol) -> Option<&Attribute> {
426-
attrs.iter().find(|attr| attr.check_name(name))
427-
}
428-
429-
pub fn filter_by_name(attrs: &[Attribute], name: Symbol) -> impl Iterator<Item = &Attribute> {
430-
attrs.iter().filter(move |attr| attr.check_name(name))
431-
}
432-
433-
pub fn first_attr_value_str_by_name(attrs: &[Attribute], name: Symbol) -> Option<Symbol> {
434-
attrs.iter().find(|at| at.check_name(name)).and_then(|at| at.value_str())
435-
}
436-
437364
impl MetaItem {
438365
fn token_trees_and_joints(&self) -> Vec<TreeAndJoint> {
439366
let mut idents = vec![];

src/librustc_ast/entry.rs

-28
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
1-
use crate::ast::{Item, ItemKind};
2-
use crate::attr;
3-
use rustc_span::symbol::sym;
4-
51
pub enum EntryPointType {
62
None,
73
MainNamed,
84
MainAttr,
95
Start,
106
OtherMain, // Not an entry point, but some other function named main
117
}
12-
13-
// Beware, this is duplicated in librustc_middle/middle/entry.rs, make sure to keep
14-
// them in sync.
15-
pub fn entry_point_type(item: &Item, depth: usize) -> EntryPointType {
16-
match item.kind {
17-
ItemKind::Fn(..) => {
18-
if attr::contains_name(&item.attrs, sym::start) {
19-
EntryPointType::Start
20-
} else if attr::contains_name(&item.attrs, sym::main) {
21-
EntryPointType::MainAttr
22-
} else if item.ident.name == sym::main {
23-
if depth == 1 {
24-
// This is a top-level function so can be 'main'
25-
EntryPointType::MainNamed
26-
} else {
27-
EntryPointType::OtherMain
28-
}
29-
} else {
30-
EntryPointType::None
31-
}
32-
}
33-
_ => EntryPointType::None,
34-
}
35-
}

src/librustc_ast/expand/allocator.rs

-24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use crate::{ast, attr, visit};
21
use rustc_span::symbol::{sym, Symbol};
3-
use rustc_span::Span;
42

53
#[derive(Clone, Copy)]
64
pub enum AllocatorKind {
@@ -53,25 +51,3 @@ pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
5351
output: AllocatorTy::ResultPtr,
5452
},
5553
];
56-
57-
pub fn global_allocator_spans(krate: &ast::Crate) -> Vec<Span> {
58-
struct Finder {
59-
name: Symbol,
60-
spans: Vec<Span>,
61-
}
62-
impl<'ast> visit::Visitor<'ast> for Finder {
63-
fn visit_item(&mut self, item: &'ast ast::Item) {
64-
if item.ident.name == self.name
65-
&& attr::contains_name(&item.attrs, sym::rustc_std_internal_symbol)
66-
{
67-
self.spans.push(item.span);
68-
}
69-
visit::walk_item(self, item)
70-
}
71-
}
72-
73-
let name = Symbol::intern(&AllocatorKind::Global.fn_name(sym::alloc));
74-
let mut f = Finder { name, spans: Vec::new() };
75-
visit::walk_crate(&mut f, krate);
76-
f.spans
77-
}

src/librustc_ast/expand/mod.rs

-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
11
//! Definitions shared by macros / syntax extensions and e.g. librustc_middle.
22
3-
use crate::ast::Attribute;
4-
use rustc_span::symbol::sym;
5-
63
pub mod allocator;
7-
8-
pub fn is_proc_macro_attr(attr: &Attribute) -> bool {
9-
[sym::proc_macro, sym::proc_macro_attribute, sym::proc_macro_derive]
10-
.iter()
11-
.any(|kind| attr.check_name(*kind))
12-
}

src/librustc_ast/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ pub mod util {
4242

4343
pub mod ast;
4444
pub mod attr;
45-
pub use attr::{with_default_session_globals, with_session_globals, SESSION_GLOBALS};
4645
pub mod crate_disambiguator;
4746
pub mod entry;
4847
pub mod expand;

src/librustc_ast/util/comments/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use super::*;
2-
use crate::with_default_session_globals;
2+
use rustc_span::with_default_session_globals;
33

44
#[test]
55
fn line_doc_comments() {

src/librustc_ast/util/lev_distance/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn test_lev_distance() {
2121

2222
#[test]
2323
fn test_find_best_match_for_name() {
24-
use crate::with_default_session_globals;
24+
use rustc_span::with_default_session_globals;
2525
with_default_session_globals(|| {
2626
let input = vec![Symbol::intern("aaab"), Symbol::intern("aaabc")];
2727
assert_eq!(

src/librustc_ast_lowering/item.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use super::{ImplTraitContext, ImplTraitPosition};
33
use crate::Arena;
44

55
use rustc_ast::ast::*;
6-
use rustc_ast::attr;
76
use rustc_ast::node_id::NodeMap;
87
use rustc_ast::ptr::P;
98
use rustc_ast::visit::{self, AssocCtxt, Visitor};
@@ -205,7 +204,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
205204
let attrs = self.lower_attrs(&i.attrs);
206205

207206
if let ItemKind::MacroDef(MacroDef { ref body, macro_rules }) = i.kind {
208-
if !macro_rules || attr::contains_name(&i.attrs, sym::macro_export) {
207+
if !macro_rules || self.sess.contains_name(&i.attrs, sym::macro_export) {
209208
let hir_id = self.lower_node_id(i.id);
210209
let body = P(self.lower_mac_args(body));
211210
self.exported_macros.push(hir::MacroDef {

src/librustc_ast_lowering/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737

3838
use rustc_ast::ast;
3939
use rustc_ast::ast::*;
40-
use rustc_ast::attr;
4140
use rustc_ast::node_id::NodeMap;
4241
use rustc_ast::token::{self, DelimToken, Nonterminal, Token};
4342
use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree};
@@ -2215,7 +2214,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22152214
synthetic: param
22162215
.attrs
22172216
.iter()
2218-
.filter(|attr| attr.check_name(sym::rustc_synthetic))
2217+
.filter(|attr| self.sess.check_name(attr, sym::rustc_synthetic))
22192218
.map(|_| hir::SyntheticTyParamKind::ImplTrait)
22202219
.next(),
22212220
};
@@ -2236,7 +2235,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22362235
hir_id: self.lower_node_id(param.id),
22372236
name,
22382237
span: param.ident.span,
2239-
pure_wrt_drop: attr::contains_name(&param.attrs, sym::may_dangle),
2238+
pure_wrt_drop: self.sess.contains_name(&param.attrs, sym::may_dangle),
22402239
attrs: self.lower_attrs(&param.attrs),
22412240
bounds: self.arena.alloc_from_iter(bounds),
22422241
kind,

src/librustc_ast_passes/ast_validation.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
use itertools::{Either, Itertools};
1010
use rustc_ast::ast::*;
11-
use rustc_ast::attr;
12-
use rustc_ast::expand::is_proc_macro_attr;
1311
use rustc_ast::ptr::P;
1412
use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
1513
use rustc_ast::walk_list;
@@ -891,11 +889,11 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
891889
}
892890

893891
fn visit_item(&mut self, item: &'a Item) {
894-
if item.attrs.iter().any(|attr| is_proc_macro_attr(attr)) {
892+
if item.attrs.iter().any(|attr| self.session.is_proc_macro_attr(attr)) {
895893
self.has_proc_macro_decls = true;
896894
}
897895

898-
if attr::contains_name(&item.attrs, sym::no_mangle) {
896+
if self.session.contains_name(&item.attrs, sym::no_mangle) {
899897
self.check_nomangle_item_asciionly(item.ident, item.span);
900898
}
901899

@@ -1027,7 +1025,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
10271025
}
10281026
ItemKind::Mod(Mod { inline, .. }) => {
10291027
// Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
1030-
if !inline && !attr::contains_name(&item.attrs, sym::path) {
1028+
if !inline && !self.session.contains_name(&item.attrs, sym::path) {
10311029
self.check_mod_file_item_asciionly(item.ident);
10321030
}
10331031
}

0 commit comments

Comments
 (0)