Skip to content

Commit 89c9e97

Browse files
authored
Rollup merge of #74079 - nnethercote:session-globals, r=nikomatsakis
Eliminate confusing "globals" terminology. There are some structures that are called "globals", but are they global to a compilation session, and not truly global. I have always found this highly confusing, so this commit renames them as "session globals" and adds a comment explaining things. Also, the commit fixes an unnecessary nesting of `set()` calls `src/librustc_errors/json/tests.rs` r? @Aaron1011
2 parents 07301e3 + 81c5bb6 commit 89c9e97

File tree

25 files changed

+136
-127
lines changed

25 files changed

+136
-127
lines changed

src/librustc_ast/attr/mod.rs

+24-18
Original file line numberDiff line numberDiff line change
@@ -21,55 +21,61 @@ use log::debug;
2121
use std::iter;
2222
use std::ops::DerefMut;
2323

24-
pub struct Globals {
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 {
2529
used_attrs: Lock<GrowableBitSet<AttrId>>,
2630
known_attrs: Lock<GrowableBitSet<AttrId>>,
27-
rustc_span_globals: rustc_span::Globals,
31+
span_session_globals: rustc_span::SessionGlobals,
2832
}
2933

30-
impl Globals {
31-
fn new(edition: Edition) -> Globals {
32-
Globals {
34+
impl SessionGlobals {
35+
fn new(edition: Edition) -> SessionGlobals {
36+
SessionGlobals {
3337
// We have no idea how many attributes there will be, so just
3438
// initiate the vectors with 0 bits. We'll grow them as necessary.
3539
used_attrs: Lock::new(GrowableBitSet::new_empty()),
3640
known_attrs: Lock::new(GrowableBitSet::new_empty()),
37-
rustc_span_globals: rustc_span::Globals::new(edition),
41+
span_session_globals: rustc_span::SessionGlobals::new(edition),
3842
}
3943
}
4044
}
4145

42-
pub fn with_globals<R>(edition: Edition, f: impl FnOnce() -> R) -> R {
43-
let globals = Globals::new(edition);
44-
GLOBALS.set(&globals, || rustc_span::GLOBALS.set(&globals.rustc_span_globals, f))
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+
})
4551
}
4652

47-
pub fn with_default_globals<R>(f: impl FnOnce() -> R) -> R {
48-
with_globals(DEFAULT_EDITION, f)
53+
pub fn with_default_session_globals<R>(f: impl FnOnce() -> R) -> R {
54+
with_session_globals(DEFAULT_EDITION, f)
4955
}
5056

51-
scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals);
57+
scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: SessionGlobals);
5258

5359
pub fn mark_used(attr: &Attribute) {
5460
debug!("marking {:?} as used", attr);
55-
GLOBALS.with(|globals| {
56-
globals.used_attrs.lock().insert(attr.id);
61+
SESSION_GLOBALS.with(|session_globals| {
62+
session_globals.used_attrs.lock().insert(attr.id);
5763
});
5864
}
5965

6066
pub fn is_used(attr: &Attribute) -> bool {
61-
GLOBALS.with(|globals| globals.used_attrs.lock().contains(attr.id))
67+
SESSION_GLOBALS.with(|session_globals| session_globals.used_attrs.lock().contains(attr.id))
6268
}
6369

6470
pub fn mark_known(attr: &Attribute) {
6571
debug!("marking {:?} as known", attr);
66-
GLOBALS.with(|globals| {
67-
globals.known_attrs.lock().insert(attr.id);
72+
SESSION_GLOBALS.with(|session_globals| {
73+
session_globals.known_attrs.lock().insert(attr.id);
6874
});
6975
}
7076

7177
pub fn is_known(attr: &Attribute) -> bool {
72-
GLOBALS.with(|globals| globals.known_attrs.lock().contains(attr.id))
78+
SESSION_GLOBALS.with(|session_globals| session_globals.known_attrs.lock().contains(attr.id))
7379
}
7480

7581
pub fn is_known_lint_tool(m_item: Ident) -> bool {

src/librustc_ast/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub mod util {
4343

4444
pub mod ast;
4545
pub mod attr;
46-
pub use attr::{with_default_globals, with_globals, GLOBALS};
46+
pub use attr::{with_default_session_globals, with_session_globals, SESSION_GLOBALS};
4747
pub mod crate_disambiguator;
4848
pub mod entry;
4949
pub mod expand;

src/librustc_ast/util/lev_distance/tests.rs

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

2222
#[test]
2323
fn test_find_best_match_for_name() {
24-
use crate::with_default_globals;
25-
with_default_globals(|| {
24+
use crate::with_default_session_globals;
25+
with_default_session_globals(|| {
2626
let input = vec![Symbol::intern("aaab"), Symbol::intern("aaabc")];
2727
assert_eq!(
2828
find_best_match_for_name(input.iter(), "aaaa", None),

src/librustc_ast_pretty/pprust/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use super::*;
22

33
use rustc_ast::ast;
4-
use rustc_ast::with_default_globals;
4+
use rustc_ast::with_default_session_globals;
55
use rustc_span::source_map::respan;
66
use rustc_span::symbol::Ident;
77

@@ -25,7 +25,7 @@ fn variant_to_string(var: &ast::Variant) -> String {
2525

2626
#[test]
2727
fn test_fun_to_string() {
28-
with_default_globals(|| {
28+
with_default_session_globals(|| {
2929
let abba_ident = Ident::from_str("abba");
3030

3131
let decl =
@@ -40,7 +40,7 @@ fn test_fun_to_string() {
4040

4141
#[test]
4242
fn test_variant_to_string() {
43-
with_default_globals(|| {
43+
with_default_session_globals(|| {
4444
let ident = Ident::from_str("principal_skinner");
4545

4646
let var = ast::Variant {

src/librustc_errors/json/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,16 @@ impl<T: Write> Write for Shared<T> {
3939
}
4040
}
4141

42-
fn with_default_globals(f: impl FnOnce()) {
43-
let globals = rustc_span::Globals::new(rustc_span::edition::DEFAULT_EDITION);
44-
rustc_span::GLOBALS.set(&globals, || rustc_span::GLOBALS.set(&globals, f))
42+
fn with_default_session_globals(f: impl FnOnce()) {
43+
let session_globals = rustc_span::SessionGlobals::new(rustc_span::edition::DEFAULT_EDITION);
44+
rustc_span::SESSION_GLOBALS.set(&session_globals, f);
4545
}
4646

4747
/// Test the span yields correct positions in JSON.
4848
fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) {
4949
let expected_output = TestData { spans: vec![expected_output] };
5050

51-
with_default_globals(|| {
51+
with_default_session_globals(|| {
5252
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
5353
sm.new_source_file(Path::new("test.rs").to_owned().into(), code.to_owned());
5454

src/librustc_expand/mut_visit/tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::tests::{matches_codepattern, string_to_crate};
22

33
use rustc_ast::ast;
44
use rustc_ast::mut_visit::{self, MutVisitor};
5-
use rustc_ast::with_default_globals;
5+
use rustc_ast::with_default_session_globals;
66
use rustc_ast_pretty::pprust;
77
use rustc_span::symbol::Ident;
88

@@ -38,7 +38,7 @@ macro_rules! assert_pred {
3838
// Make sure idents get transformed everywhere.
3939
#[test]
4040
fn ident_transformation() {
41-
with_default_globals(|| {
41+
with_default_session_globals(|| {
4242
let mut zz_visitor = ToZzIdentMutVisitor;
4343
let mut krate =
4444
string_to_crate("#[a] mod b {fn c (d : e, f : g) {h!(i,j,k);l;m}}".to_string());
@@ -55,7 +55,7 @@ fn ident_transformation() {
5555
// Make sure idents get transformed even inside macro defs.
5656
#[test]
5757
fn ident_transformation_in_defs() {
58-
with_default_globals(|| {
58+
with_default_session_globals(|| {
5959
let mut zz_visitor = ToZzIdentMutVisitor;
6060
let mut krate = string_to_crate(
6161
"macro_rules! a {(b $c:expr $(d $e:token)f+ => \

src/librustc_expand/parse/lexer/tests.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use rustc_ast::token::{self, Token, TokenKind};
22
use rustc_ast::util::comments::is_doc_comment;
3-
use rustc_ast::with_default_globals;
3+
use rustc_ast::with_default_session_globals;
44
use rustc_data_structures::sync::Lrc;
55
use rustc_errors::{emitter::EmitterWriter, Handler};
66
use rustc_parse::lexer::StringReader;
@@ -33,7 +33,7 @@ fn setup<'a>(sm: &SourceMap, sess: &'a ParseSess, teststr: String) -> StringRead
3333

3434
#[test]
3535
fn t1() {
36-
with_default_globals(|| {
36+
with_default_session_globals(|| {
3737
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
3838
let sh = mk_sess(sm.clone());
3939
let mut string_reader = setup(
@@ -79,7 +79,7 @@ fn mk_lit(kind: token::LitKind, symbol: &str, suffix: Option<&str>) -> TokenKind
7979

8080
#[test]
8181
fn doublecolon_parsing() {
82-
with_default_globals(|| {
82+
with_default_session_globals(|| {
8383
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
8484
let sh = mk_sess(sm.clone());
8585
check_tokenization(
@@ -91,7 +91,7 @@ fn doublecolon_parsing() {
9191

9292
#[test]
9393
fn doublecolon_parsing_2() {
94-
with_default_globals(|| {
94+
with_default_session_globals(|| {
9595
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
9696
let sh = mk_sess(sm.clone());
9797
check_tokenization(
@@ -103,7 +103,7 @@ fn doublecolon_parsing_2() {
103103

104104
#[test]
105105
fn doublecolon_parsing_3() {
106-
with_default_globals(|| {
106+
with_default_session_globals(|| {
107107
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
108108
let sh = mk_sess(sm.clone());
109109
check_tokenization(
@@ -115,7 +115,7 @@ fn doublecolon_parsing_3() {
115115

116116
#[test]
117117
fn doublecolon_parsing_4() {
118-
with_default_globals(|| {
118+
with_default_session_globals(|| {
119119
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
120120
let sh = mk_sess(sm.clone());
121121
check_tokenization(
@@ -127,7 +127,7 @@ fn doublecolon_parsing_4() {
127127

128128
#[test]
129129
fn character_a() {
130-
with_default_globals(|| {
130+
with_default_session_globals(|| {
131131
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
132132
let sh = mk_sess(sm.clone());
133133
assert_eq!(setup(&sm, &sh, "'a'".to_string()).next_token(), mk_lit(token::Char, "a", None),);
@@ -136,7 +136,7 @@ fn character_a() {
136136

137137
#[test]
138138
fn character_space() {
139-
with_default_globals(|| {
139+
with_default_session_globals(|| {
140140
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
141141
let sh = mk_sess(sm.clone());
142142
assert_eq!(setup(&sm, &sh, "' '".to_string()).next_token(), mk_lit(token::Char, " ", None),);
@@ -145,7 +145,7 @@ fn character_space() {
145145

146146
#[test]
147147
fn character_escaped() {
148-
with_default_globals(|| {
148+
with_default_session_globals(|| {
149149
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
150150
let sh = mk_sess(sm.clone());
151151
assert_eq!(
@@ -157,7 +157,7 @@ fn character_escaped() {
157157

158158
#[test]
159159
fn lifetime_name() {
160-
with_default_globals(|| {
160+
with_default_session_globals(|| {
161161
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
162162
let sh = mk_sess(sm.clone());
163163
assert_eq!(
@@ -169,7 +169,7 @@ fn lifetime_name() {
169169

170170
#[test]
171171
fn raw_string() {
172-
with_default_globals(|| {
172+
with_default_session_globals(|| {
173173
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
174174
let sh = mk_sess(sm.clone());
175175
assert_eq!(
@@ -181,7 +181,7 @@ fn raw_string() {
181181

182182
#[test]
183183
fn literal_suffixes() {
184-
with_default_globals(|| {
184+
with_default_session_globals(|| {
185185
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
186186
let sh = mk_sess(sm.clone());
187187
macro_rules! test {
@@ -232,7 +232,7 @@ fn line_doc_comments() {
232232

233233
#[test]
234234
fn nested_block_comments() {
235-
with_default_globals(|| {
235+
with_default_session_globals(|| {
236236
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
237237
let sh = mk_sess(sm.clone());
238238
let mut lexer = setup(&sm, &sh, "/* /* */ */'a'".to_string());
@@ -243,7 +243,7 @@ fn nested_block_comments() {
243243

244244
#[test]
245245
fn crlf_comments() {
246-
with_default_globals(|| {
246+
with_default_session_globals(|| {
247247
let sm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
248248
let sh = mk_sess(sm.clone());
249249
let mut lexer = setup(&sm, &sh, "// test\r\n/// test\r\n".to_string());

0 commit comments

Comments
 (0)