From 81c5bb6a3fb02d1c1c3a4698e9e0e031ef2f4e4e Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Mon, 6 Jul 2020 10:53:14 +1000 Subject: [PATCH] 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` --- src/librustc_ast/attr/mod.rs | 42 +++++++++++-------- src/librustc_ast/lib.rs | 2 +- src/librustc_ast/util/lev_distance/tests.rs | 4 +- src/librustc_ast_pretty/pprust/tests.rs | 6 +-- src/librustc_errors/json/tests.rs | 8 ++-- src/librustc_expand/mut_visit/tests.rs | 6 +-- src/librustc_expand/parse/lexer/tests.rs | 28 ++++++------- src/librustc_expand/parse/tests.rs | 24 +++++------ src/librustc_expand/tests.rs | 4 +- src/librustc_expand/tokenstream/tests.rs | 20 ++++----- src/librustc_interface/interface.rs | 2 +- src/librustc_interface/tests.rs | 10 ++--- src/librustc_interface/util.rs | 21 +++++----- src/librustc_middle/ty/query/job.rs | 15 ++++--- src/librustc_parse_format/tests.rs | 4 +- src/librustc_span/hygiene.rs | 4 +- src/librustc_span/lib.rs | 26 +++++++----- src/librustc_span/span_encoding.rs | 4 +- src/librustc_span/symbol.rs | 4 +- src/librustc_span/symbol/tests.rs | 4 +- src/librustdoc/clean/cfg/tests.rs | 16 +++---- src/librustdoc/test.rs | 3 +- .../ui-fulldeps/mod_dir_path_canonicalized.rs | 2 +- src/test/ui-fulldeps/pprust-expr-roundtrip.rs | 2 +- src/tools/error_index_generator/main.rs | 2 +- 25 files changed, 136 insertions(+), 127 deletions(-) diff --git a/src/librustc_ast/attr/mod.rs b/src/librustc_ast/attr/mod.rs index 76139209c91..9d4b6dbed98 100644 --- a/src/librustc_ast/attr/mod.rs +++ b/src/librustc_ast/attr/mod.rs @@ -21,55 +21,61 @@ use std::iter; use std::ops::DerefMut; -pub struct Globals { +// Per-session global variables: this struct is stored in thread-local storage +// in such a way that it is accessible without any kind of handle to all +// threads within the compilation session, but is not accessible outside the +// session. +pub struct SessionGlobals { used_attrs: Lock>, known_attrs: Lock>, - rustc_span_globals: rustc_span::Globals, + span_session_globals: rustc_span::SessionGlobals, } -impl Globals { - fn new(edition: Edition) -> Globals { - Globals { +impl SessionGlobals { + fn new(edition: Edition) -> SessionGlobals { + SessionGlobals { // We have no idea how many attributes there will be, so just // initiate the vectors with 0 bits. We'll grow them as necessary. used_attrs: Lock::new(GrowableBitSet::new_empty()), known_attrs: Lock::new(GrowableBitSet::new_empty()), - rustc_span_globals: rustc_span::Globals::new(edition), + span_session_globals: rustc_span::SessionGlobals::new(edition), } } } -pub fn with_globals(edition: Edition, f: impl FnOnce() -> R) -> R { - let globals = Globals::new(edition); - GLOBALS.set(&globals, || rustc_span::GLOBALS.set(&globals.rustc_span_globals, f)) +pub fn with_session_globals(edition: Edition, f: impl FnOnce() -> R) -> R { + let ast_session_globals = SessionGlobals::new(edition); + SESSION_GLOBALS.set(&ast_session_globals, || { + rustc_span::SESSION_GLOBALS.set(&ast_session_globals.span_session_globals, f) + }) } -pub fn with_default_globals(f: impl FnOnce() -> R) -> R { - with_globals(DEFAULT_EDITION, f) +pub fn with_default_session_globals(f: impl FnOnce() -> R) -> R { + with_session_globals(DEFAULT_EDITION, f) } -scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals); +scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: SessionGlobals); pub fn mark_used(attr: &Attribute) { debug!("marking {:?} as used", attr); - GLOBALS.with(|globals| { - globals.used_attrs.lock().insert(attr.id); + SESSION_GLOBALS.with(|session_globals| { + session_globals.used_attrs.lock().insert(attr.id); }); } pub fn is_used(attr: &Attribute) -> bool { - GLOBALS.with(|globals| globals.used_attrs.lock().contains(attr.id)) + SESSION_GLOBALS.with(|session_globals| session_globals.used_attrs.lock().contains(attr.id)) } pub fn mark_known(attr: &Attribute) { debug!("marking {:?} as known", attr); - GLOBALS.with(|globals| { - globals.known_attrs.lock().insert(attr.id); + SESSION_GLOBALS.with(|session_globals| { + session_globals.known_attrs.lock().insert(attr.id); }); } pub fn is_known(attr: &Attribute) -> bool { - GLOBALS.with(|globals| globals.known_attrs.lock().contains(attr.id)) + SESSION_GLOBALS.with(|session_globals| session_globals.known_attrs.lock().contains(attr.id)) } pub fn is_known_lint_tool(m_item: Ident) -> bool { diff --git a/src/librustc_ast/lib.rs b/src/librustc_ast/lib.rs index 5c1c9d6ab90..ffd2aa61f28 100644 --- a/src/librustc_ast/lib.rs +++ b/src/librustc_ast/lib.rs @@ -43,7 +43,7 @@ pub mod util { pub mod ast; pub mod attr; -pub use attr::{with_default_globals, with_globals, GLOBALS}; +pub use attr::{with_default_session_globals, with_session_globals, SESSION_GLOBALS}; pub mod crate_disambiguator; pub mod entry; pub mod expand; diff --git a/src/librustc_ast/util/lev_distance/tests.rs b/src/librustc_ast/util/lev_distance/tests.rs index 222661687c1..e9b6c9759b6 100644 --- a/src/librustc_ast/util/lev_distance/tests.rs +++ b/src/librustc_ast/util/lev_distance/tests.rs @@ -21,8 +21,8 @@ fn test_lev_distance() { #[test] fn test_find_best_match_for_name() { - use crate::with_default_globals; - with_default_globals(|| { + use crate::with_default_session_globals; + with_default_session_globals(|| { let input = vec![Symbol::intern("aaab"), Symbol::intern("aaabc")]; assert_eq!( find_best_match_for_name(input.iter(), "aaaa", None), diff --git a/src/librustc_ast_pretty/pprust/tests.rs b/src/librustc_ast_pretty/pprust/tests.rs index f92e40ed6ff..96377a4ae02 100644 --- a/src/librustc_ast_pretty/pprust/tests.rs +++ b/src/librustc_ast_pretty/pprust/tests.rs @@ -1,7 +1,7 @@ use super::*; use rustc_ast::ast; -use rustc_ast::with_default_globals; +use rustc_ast::with_default_session_globals; use rustc_span::source_map::respan; use rustc_span::symbol::Ident; @@ -25,7 +25,7 @@ fn variant_to_string(var: &ast::Variant) -> String { #[test] fn test_fun_to_string() { - with_default_globals(|| { + with_default_session_globals(|| { let abba_ident = Ident::from_str("abba"); let decl = @@ -40,7 +40,7 @@ fn test_fun_to_string() { #[test] fn test_variant_to_string() { - with_default_globals(|| { + with_default_session_globals(|| { let ident = Ident::from_str("principal_skinner"); let var = ast::Variant { diff --git a/src/librustc_errors/json/tests.rs b/src/librustc_errors/json/tests.rs index 35912901d68..3a501dd5254 100644 --- a/src/librustc_errors/json/tests.rs +++ b/src/librustc_errors/json/tests.rs @@ -39,16 +39,16 @@ fn flush(&mut self) -> io::Result<()> { } } -fn with_default_globals(f: impl FnOnce()) { - let globals = rustc_span::Globals::new(rustc_span::edition::DEFAULT_EDITION); - rustc_span::GLOBALS.set(&globals, || rustc_span::GLOBALS.set(&globals, f)) +fn with_default_session_globals(f: impl FnOnce()) { + let session_globals = rustc_span::SessionGlobals::new(rustc_span::edition::DEFAULT_EDITION); + rustc_span::SESSION_GLOBALS.set(&session_globals, f); } /// Test the span yields correct positions in JSON. fn test_positions(code: &str, span: (u32, u32), expected_output: SpanTestData) { let expected_output = TestData { spans: vec![expected_output] }; - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); sm.new_source_file(Path::new("test.rs").to_owned().into(), code.to_owned()); diff --git a/src/librustc_expand/mut_visit/tests.rs b/src/librustc_expand/mut_visit/tests.rs index 48da1a3ccc4..c22d2a100c3 100644 --- a/src/librustc_expand/mut_visit/tests.rs +++ b/src/librustc_expand/mut_visit/tests.rs @@ -2,7 +2,7 @@ use rustc_ast::ast; use rustc_ast::mut_visit::{self, MutVisitor}; -use rustc_ast::with_default_globals; +use rustc_ast::with_default_session_globals; use rustc_ast_pretty::pprust; use rustc_span::symbol::Ident; @@ -38,7 +38,7 @@ macro_rules! assert_pred { // Make sure idents get transformed everywhere. #[test] fn ident_transformation() { - with_default_globals(|| { + with_default_session_globals(|| { let mut zz_visitor = ToZzIdentMutVisitor; let mut krate = 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() { // Make sure idents get transformed even inside macro defs. #[test] fn ident_transformation_in_defs() { - with_default_globals(|| { + with_default_session_globals(|| { let mut zz_visitor = ToZzIdentMutVisitor; let mut krate = string_to_crate( "macro_rules! a {(b $c:expr $(d $e:token)f+ => \ diff --git a/src/librustc_expand/parse/lexer/tests.rs b/src/librustc_expand/parse/lexer/tests.rs index 2932475430b..b3775c78e73 100644 --- a/src/librustc_expand/parse/lexer/tests.rs +++ b/src/librustc_expand/parse/lexer/tests.rs @@ -1,6 +1,6 @@ use rustc_ast::token::{self, Token, TokenKind}; use rustc_ast::util::comments::is_doc_comment; -use rustc_ast::with_default_globals; +use rustc_ast::with_default_session_globals; use rustc_data_structures::sync::Lrc; use rustc_errors::{emitter::EmitterWriter, Handler}; use rustc_parse::lexer::StringReader; @@ -33,7 +33,7 @@ fn setup<'a>(sm: &SourceMap, sess: &'a ParseSess, teststr: String) -> StringRead #[test] fn t1() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); let mut string_reader = setup( @@ -79,7 +79,7 @@ fn mk_lit(kind: token::LitKind, symbol: &str, suffix: Option<&str>) -> TokenKind #[test] fn doublecolon_parsing() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); check_tokenization( @@ -91,7 +91,7 @@ fn doublecolon_parsing() { #[test] fn doublecolon_parsing_2() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); check_tokenization( @@ -103,7 +103,7 @@ fn doublecolon_parsing_2() { #[test] fn doublecolon_parsing_3() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); check_tokenization( @@ -115,7 +115,7 @@ fn doublecolon_parsing_3() { #[test] fn doublecolon_parsing_4() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); check_tokenization( @@ -127,7 +127,7 @@ fn doublecolon_parsing_4() { #[test] fn character_a() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); assert_eq!(setup(&sm, &sh, "'a'".to_string()).next_token(), mk_lit(token::Char, "a", None),); @@ -136,7 +136,7 @@ fn character_a() { #[test] fn character_space() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); assert_eq!(setup(&sm, &sh, "' '".to_string()).next_token(), mk_lit(token::Char, " ", None),); @@ -145,7 +145,7 @@ fn character_space() { #[test] fn character_escaped() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); assert_eq!( @@ -157,7 +157,7 @@ fn character_escaped() { #[test] fn lifetime_name() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); assert_eq!( @@ -169,7 +169,7 @@ fn lifetime_name() { #[test] fn raw_string() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); assert_eq!( @@ -181,7 +181,7 @@ fn raw_string() { #[test] fn literal_suffixes() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); macro_rules! test { @@ -232,7 +232,7 @@ fn line_doc_comments() { #[test] fn nested_block_comments() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); let mut lexer = setup(&sm, &sh, "/* /* */ */'a'".to_string()); @@ -243,7 +243,7 @@ fn nested_block_comments() { #[test] fn crlf_comments() { - with_default_globals(|| { + with_default_session_globals(|| { let sm = Lrc::new(SourceMap::new(FilePathMapping::empty())); let sh = mk_sess(sm.clone()); let mut lexer = setup(&sm, &sh, "// test\r\n/// test\r\n".to_string()); diff --git a/src/librustc_expand/parse/tests.rs b/src/librustc_expand/parse/tests.rs index 437f6e62d7d..fc9b9f2dab0 100644 --- a/src/librustc_expand/parse/tests.rs +++ b/src/librustc_expand/parse/tests.rs @@ -5,7 +5,7 @@ use rustc_ast::token::{self, Token}; use rustc_ast::tokenstream::{DelimSpan, TokenStream, TokenTree}; use rustc_ast::visit; -use rustc_ast::with_default_globals; +use rustc_ast::with_default_session_globals; use rustc_ast_pretty::pprust::item_to_string; use rustc_errors::PResult; use rustc_parse::new_parser_from_source_str; @@ -50,7 +50,7 @@ fn string_to_item(source_str: String) -> Option> { #[should_panic] #[test] fn bad_path_expr_1() { - with_default_globals(|| { + with_default_session_globals(|| { string_to_expr("::abc::def::return".to_string()); }) } @@ -58,7 +58,7 @@ fn bad_path_expr_1() { // Checks the token-tree-ization of macros. #[test] fn string_to_tts_macro() { - with_default_globals(|| { + with_default_session_globals(|| { let tts: Vec<_> = string_to_stream("macro_rules! zip (($a)=>($a))".to_string()).trees().collect(); let tts: &[TokenTree] = &tts[..]; @@ -95,7 +95,7 @@ fn string_to_tts_macro() { #[test] fn string_to_tts_1() { - with_default_globals(|| { + with_default_session_globals(|| { let tts = string_to_stream("fn a (b : i32) { b; }".to_string()); let expected = TokenStream::new(vec![ @@ -130,7 +130,7 @@ fn string_to_tts_1() { #[test] fn parse_use() { - with_default_globals(|| { + with_default_session_globals(|| { let use_s = "use foo::bar::baz;"; let vitem = string_to_item(use_s.to_string()).unwrap(); let vitem_s = item_to_string(&vitem); @@ -145,7 +145,7 @@ fn parse_use() { #[test] fn parse_extern_crate() { - with_default_globals(|| { + with_default_session_globals(|| { let ex_s = "extern crate foo;"; let vitem = string_to_item(ex_s.to_string()).unwrap(); let vitem_s = item_to_string(&vitem); @@ -183,7 +183,7 @@ fn visit_pat(&mut self, p: &'a ast::Pat) { #[test] fn span_of_self_arg_pat_idents_are_correct() { - with_default_globals(|| { + with_default_session_globals(|| { let srcs = [ "impl z { fn a (&self, &myarg: i32) {} }", "impl z { fn a (&mut self, &myarg: i32) {} }", @@ -207,7 +207,7 @@ fn span_of_self_arg_pat_idents_are_correct() { #[test] fn parse_exprs() { - with_default_globals(|| { + with_default_session_globals(|| { // just make sure that they parse.... string_to_expr("3 + 4".to_string()); string_to_expr("a::z.froob(b,&(987+3))".to_string()); @@ -216,7 +216,7 @@ fn parse_exprs() { #[test] fn attrs_fix_bug() { - with_default_globals(|| { + with_default_session_globals(|| { string_to_item( "pub fn mk_file_writer(path: &Path, flags: &[FileFlag]) -> Result, String> { @@ -237,7 +237,7 @@ fn wb() -> c_int { O_WRONLY as c_int } #[test] fn crlf_doc_comments() { - with_default_globals(|| { + with_default_session_globals(|| { let sess = sess(); let name_1 = FileName::Custom("crlf_source_1".to_string()); @@ -271,7 +271,7 @@ fn parse_expr_from_source_str( new_parser_from_source_str(sess, name, source).parse_expr() } - with_default_globals(|| { + with_default_session_globals(|| { let sess = sess(); let expr = parse_expr_from_source_str( PathBuf::from("foo").into(), @@ -299,7 +299,7 @@ fn parse_expr_from_source_str( // See `recurse_into_file_modules` in the parser. #[test] fn out_of_line_mod() { - with_default_globals(|| { + with_default_session_globals(|| { let item = parse_item_from_source_str( PathBuf::from("foo").into(), "mod foo { struct S; mod this_does_not_exist; }".to_owned(), diff --git a/src/librustc_expand/tests.rs b/src/librustc_expand/tests.rs index fbc49ddd562..283ea0f68d9 100644 --- a/src/librustc_expand/tests.rs +++ b/src/librustc_expand/tests.rs @@ -1,6 +1,6 @@ use rustc_ast::ast; use rustc_ast::tokenstream::TokenStream; -use rustc_ast::with_default_globals; +use rustc_ast::with_default_session_globals; use rustc_parse::{new_parser_from_source_str, parser::Parser, source_file_to_stream}; use rustc_session::parse::ParseSess; use rustc_span::source_map::{FilePathMapping, SourceMap}; @@ -124,7 +124,7 @@ fn flush(&mut self) -> io::Result<()> { } fn test_harness(file_text: &str, span_labels: Vec, expected_output: &str) { - with_default_globals(|| { + with_default_session_globals(|| { let output = Arc::new(Mutex::new(Vec::new())); let source_map = Lrc::new(SourceMap::new(FilePathMapping::empty())); diff --git a/src/librustc_expand/tokenstream/tests.rs b/src/librustc_expand/tokenstream/tests.rs index caaa08df499..bc171bec6ff 100644 --- a/src/librustc_expand/tokenstream/tests.rs +++ b/src/librustc_expand/tokenstream/tests.rs @@ -2,7 +2,7 @@ use rustc_ast::token; use rustc_ast::tokenstream::{TokenStream, TokenStreamBuilder, TokenTree}; -use rustc_ast::with_default_globals; +use rustc_ast::with_default_session_globals; use rustc_span::{BytePos, Span, Symbol}; use smallvec::smallvec; @@ -16,7 +16,7 @@ fn sp(a: u32, b: u32) -> Span { #[test] fn test_concat() { - with_default_globals(|| { + with_default_session_globals(|| { let test_res = string_to_ts("foo::bar::baz"); let test_fst = string_to_ts("foo::bar"); let test_snd = string_to_ts("::baz"); @@ -29,7 +29,7 @@ fn test_concat() { #[test] fn test_to_from_bijection() { - with_default_globals(|| { + with_default_session_globals(|| { let test_start = string_to_ts("foo::bar(baz)"); let test_end = test_start.trees().collect(); assert_eq!(test_start, test_end) @@ -38,7 +38,7 @@ fn test_to_from_bijection() { #[test] fn test_eq_0() { - with_default_globals(|| { + with_default_session_globals(|| { let test_res = string_to_ts("foo"); let test_eqs = string_to_ts("foo"); assert_eq!(test_res, test_eqs) @@ -47,7 +47,7 @@ fn test_eq_0() { #[test] fn test_eq_1() { - with_default_globals(|| { + with_default_session_globals(|| { let test_res = string_to_ts("::bar::baz"); let test_eqs = string_to_ts("::bar::baz"); assert_eq!(test_res, test_eqs) @@ -56,7 +56,7 @@ fn test_eq_1() { #[test] fn test_eq_3() { - with_default_globals(|| { + with_default_session_globals(|| { let test_res = string_to_ts(""); let test_eqs = string_to_ts(""); assert_eq!(test_res, test_eqs) @@ -65,7 +65,7 @@ fn test_eq_3() { #[test] fn test_diseq_0() { - with_default_globals(|| { + with_default_session_globals(|| { let test_res = string_to_ts("::bar::baz"); let test_eqs = string_to_ts("bar::baz"); assert_eq!(test_res == test_eqs, false) @@ -74,7 +74,7 @@ fn test_diseq_0() { #[test] fn test_diseq_1() { - with_default_globals(|| { + with_default_session_globals(|| { let test_res = string_to_ts("(bar,baz)"); let test_eqs = string_to_ts("bar,baz"); assert_eq!(test_res == test_eqs, false) @@ -83,7 +83,7 @@ fn test_diseq_1() { #[test] fn test_is_empty() { - with_default_globals(|| { + with_default_session_globals(|| { let test0: TokenStream = Vec::::new().into_iter().collect(); let test1: TokenStream = TokenTree::token(token::Ident(Symbol::intern("a"), false), sp(0, 1)).into(); @@ -97,7 +97,7 @@ fn test_is_empty() { #[test] fn test_dotdotdot() { - with_default_globals(|| { + with_default_session_globals(|| { let mut builder = TokenStreamBuilder::new(); builder.push(TokenTree::token(token::Dot, sp(0, 1)).joint()); builder.push(TokenTree::token(token::Dot, sp(1, 2)).joint()); diff --git a/src/librustc_interface/interface.rs b/src/librustc_interface/interface.rs index 920cc6021e6..4059022e7a5 100644 --- a/src/librustc_interface/interface.rs +++ b/src/librustc_interface/interface.rs @@ -74,7 +74,7 @@ pub fn build_output_filenames( /// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`. pub fn parse_cfgspecs(cfgspecs: Vec) -> FxHashSet<(String, Option)> { - rustc_ast::with_default_globals(move || { + rustc_ast::with_default_session_globals(move || { let cfg = cfgspecs .into_iter() .map(|s| { diff --git a/src/librustc_interface/tests.rs b/src/librustc_interface/tests.rs index e35dbbc0c2f..651a77912c6 100644 --- a/src/librustc_interface/tests.rs +++ b/src/librustc_interface/tests.rs @@ -73,7 +73,7 @@ fn mk_map(entries: Vec<(K, V)>) -> BTreeMap { // When the user supplies --test we should implicitly supply --cfg test #[test] fn test_switch_implies_cfg_test() { - rustc_ast::with_default_globals(|| { + rustc_ast::with_default_session_globals(|| { let matches = optgroups().parse(&["--test".to_string()]).unwrap(); let (sess, cfg) = mk_session(matches); let cfg = build_configuration(&sess, to_crate_config(cfg)); @@ -84,7 +84,7 @@ fn test_switch_implies_cfg_test() { // When the user supplies --test and --cfg test, don't implicitly add another --cfg test #[test] fn test_switch_implies_cfg_test_unless_cfg_test() { - rustc_ast::with_default_globals(|| { + rustc_ast::with_default_session_globals(|| { let matches = optgroups().parse(&["--test".to_string(), "--cfg=test".to_string()]).unwrap(); let (sess, cfg) = mk_session(matches); let cfg = build_configuration(&sess, to_crate_config(cfg)); @@ -96,20 +96,20 @@ fn test_switch_implies_cfg_test_unless_cfg_test() { #[test] fn test_can_print_warnings() { - rustc_ast::with_default_globals(|| { + rustc_ast::with_default_session_globals(|| { let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap(); let (sess, _) = mk_session(matches); assert!(!sess.diagnostic().can_emit_warnings()); }); - rustc_ast::with_default_globals(|| { + rustc_ast::with_default_session_globals(|| { let matches = optgroups().parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()]).unwrap(); let (sess, _) = mk_session(matches); assert!(sess.diagnostic().can_emit_warnings()); }); - rustc_ast::with_default_globals(|| { + rustc_ast::with_default_session_globals(|| { let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap(); let (sess, _) = mk_session(matches); assert!(sess.diagnostic().can_emit_warnings()); diff --git a/src/librustc_interface/util.rs b/src/librustc_interface/util.rs index 924908e5724..fe091e92062 100644 --- a/src/librustc_interface/util.rs +++ b/src/librustc_interface/util.rs @@ -141,7 +141,7 @@ pub fn spawn_thread_pool R + Send, R: Send>( crate::callbacks::setup_callbacks(); scoped_thread(cfg, || { - rustc_ast::with_globals(edition, || { + rustc_ast::with_session_globals(edition, || { ty::tls::GCX_PTR.set(&Lock::new(0), || { if let Some(stderr) = stderr { io::set_panic(Some(box Sink(stderr.clone()))); @@ -177,16 +177,17 @@ pub fn spawn_thread_pool R + Send, R: Send>( let with_pool = move |pool: &ThreadPool| pool.install(move || f()); - rustc_ast::with_globals(edition, || { - rustc_ast::GLOBALS.with(|syntax_globals| { - rustc_span::GLOBALS.with(|rustc_span_globals| { - // The main handler runs for each Rayon worker thread and sets up - // the thread local rustc uses. syntax_globals and rustc_span_globals are - // captured and set on the new threads. ty::tls::with_thread_locals sets up - // thread local callbacks from librustc_ast + rustc_ast::with_session_globals(edition, || { + rustc_ast::SESSION_GLOBALS.with(|ast_session_globals| { + rustc_span::SESSION_GLOBALS.with(|span_session_globals| { + // The main handler runs for each Rayon worker thread and sets + // up the thread local rustc uses. ast_session_globals and + // span_session_globals are captured and set on the new + // threads. ty::tls::with_thread_locals sets up thread local + // callbacks from librustc_ast. let main_handler = move |thread: ThreadBuilder| { - rustc_ast::GLOBALS.set(syntax_globals, || { - rustc_span::GLOBALS.set(rustc_span_globals, || { + rustc_ast::SESSION_GLOBALS.set(ast_session_globals, || { + rustc_span::SESSION_GLOBALS.set(span_session_globals, || { if let Some(stderr) = stderr { io::set_panic(Some(box Sink(stderr.clone()))); } diff --git a/src/librustc_middle/ty/query/job.rs b/src/librustc_middle/ty/query/job.rs index 5f7a9e81158..60b93b3d44d 100644 --- a/src/librustc_middle/ty/query/job.rs +++ b/src/librustc_middle/ty/query/job.rs @@ -13,16 +13,15 @@ pub unsafe fn handle_deadlock() { let gcx_ptr = tls::GCX_PTR.with(|gcx_ptr| gcx_ptr as *const _); let gcx_ptr = &*gcx_ptr; - let rustc_span_globals = - rustc_span::GLOBALS.with(|rustc_span_globals| rustc_span_globals as *const _); - let rustc_span_globals = &*rustc_span_globals; - let syntax_globals = rustc_ast::attr::GLOBALS.with(|syntax_globals| syntax_globals as *const _); - let syntax_globals = &*syntax_globals; + let span_session_globals = rustc_span::SESSION_GLOBALS.with(|ssg| ssg as *const _); + let span_session_globals = &*span_session_globals; + let ast_session_globals = rustc_ast::attr::SESSION_GLOBALS.with(|asg| asg as *const _); + let ast_session_globals = &*ast_session_globals; thread::spawn(move || { tls::GCX_PTR.set(gcx_ptr, || { - rustc_ast::attr::GLOBALS.set(syntax_globals, || { - rustc_span::GLOBALS - .set(rustc_span_globals, || tls::with_global(|tcx| deadlock(tcx, ®istry))) + rustc_ast::attr::SESSION_GLOBALS.set(ast_session_globals, || { + rustc_span::SESSION_GLOBALS + .set(span_session_globals, || tls::with_global(|tcx| deadlock(tcx, ®istry))) }); }) }); diff --git a/src/librustc_parse_format/tests.rs b/src/librustc_parse_format/tests.rs index 9932c1df7a9..9fd0497fffe 100644 --- a/src/librustc_parse_format/tests.rs +++ b/src/librustc_parse_format/tests.rs @@ -144,8 +144,8 @@ fn format_align_fill() { } #[test] fn format_counts() { - use rustc_span::{edition, Globals, GLOBALS}; - GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || { + use rustc_span::{edition, SessionGlobals, SESSION_GLOBALS}; + SESSION_GLOBALS.set(&SessionGlobals::new(edition::DEFAULT_EDITION), || { same( "{:10x}", &[NextArgument(Argument { diff --git a/src/librustc_span/hygiene.rs b/src/librustc_span/hygiene.rs index fef1e34a23b..7249894ba28 100644 --- a/src/librustc_span/hygiene.rs +++ b/src/librustc_span/hygiene.rs @@ -27,7 +27,7 @@ use crate::def_id::{DefId, CRATE_DEF_INDEX}; use crate::edition::Edition; use crate::symbol::{kw, sym, Symbol}; -use crate::GLOBALS; +use crate::SESSION_GLOBALS; use crate::{Span, DUMMY_SP}; use rustc_data_structures::fx::FxHashMap; @@ -174,7 +174,7 @@ impl HygieneData { } fn with T>(f: F) -> T { - GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut())) + SESSION_GLOBALS.with(|session_globals| f(&mut *session_globals.hygiene_data.borrow_mut())) } fn fresh_expn(&mut self, expn_data: Option) -> ExpnId { diff --git a/src/librustc_span/lib.rs b/src/librustc_span/lib.rs index 046554067f4..699871f1c61 100644 --- a/src/librustc_span/lib.rs +++ b/src/librustc_span/lib.rs @@ -65,16 +65,20 @@ #[cfg(test)] mod tests; -pub struct Globals { +// Per-session global variables: this struct is stored in thread-local storage +// in such a way that it is accessible without any kind of handle to all +// threads within the compilation session, but is not accessible outside the +// session. +pub struct SessionGlobals { symbol_interner: Lock, span_interner: Lock, hygiene_data: Lock, source_map: Lock>>, } -impl Globals { - pub fn new(edition: Edition) -> Globals { - Globals { +impl SessionGlobals { + pub fn new(edition: Edition) -> SessionGlobals { + SessionGlobals { symbol_interner: Lock::new(symbol::Interner::fresh()), span_interner: Lock::new(span_encoding::SpanInterner::default()), hygiene_data: Lock::new(hygiene::HygieneData::new(edition)), @@ -83,7 +87,7 @@ pub fn new(edition: Edition) -> Globals { } } -scoped_tls::scoped_thread_local!(pub static GLOBALS: Globals); +scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: SessionGlobals); // FIXME: Perhaps this should not implement Rustc{Decodable, Encodable} // @@ -713,14 +717,14 @@ fn default_decode(d: &mut D) -> Result { /// the `SourceMap` provided to this function. If that is not available, /// we fall back to printing the raw `Span` field values pub fn with_source_map T>(source_map: Lrc, f: F) -> T { - GLOBALS.with(|globals| { - *globals.source_map.borrow_mut() = Some(source_map); + SESSION_GLOBALS.with(|session_globals| { + *session_globals.source_map.borrow_mut() = Some(source_map); }); struct ClearSourceMap; impl Drop for ClearSourceMap { fn drop(&mut self) { - GLOBALS.with(|globals| { - globals.source_map.borrow_mut().take(); + SESSION_GLOBALS.with(|session_globals| { + session_globals.source_map.borrow_mut().take(); }); } } @@ -738,8 +742,8 @@ pub fn debug_with_source_map( } pub fn default_span_debug(span: Span, f: &mut fmt::Formatter<'_>) -> fmt::Result { - GLOBALS.with(|globals| { - if let Some(source_map) = &*globals.source_map.borrow() { + SESSION_GLOBALS.with(|session_globals| { + if let Some(source_map) = &*session_globals.source_map.borrow() { debug_with_source_map(span, f, source_map) } else { f.debug_struct("Span") diff --git a/src/librustc_span/span_encoding.rs b/src/librustc_span/span_encoding.rs index d769cf83a03..6b672d344fa 100644 --- a/src/librustc_span/span_encoding.rs +++ b/src/librustc_span/span_encoding.rs @@ -5,7 +5,7 @@ // See https://internals.rust-lang.org/t/rfc-compiler-refactoring-spans/1357/28 use crate::hygiene::SyntaxContext; -use crate::GLOBALS; +use crate::SESSION_GLOBALS; use crate::{BytePos, SpanData}; use rustc_data_structures::fx::FxHashMap; @@ -136,5 +136,5 @@ fn get(&self, index: u32) -> &SpanData { // If an interner exists, return it. Otherwise, prepare a fresh one. #[inline] fn with_span_interner T>(f: F) -> T { - GLOBALS.with(|globals| f(&mut *globals.span_interner.lock())) + SESSION_GLOBALS.with(|session_globals| f(&mut *session_globals.span_interner.lock())) } diff --git a/src/librustc_span/symbol.rs b/src/librustc_span/symbol.rs index acc2a50abe8..0470ceb60b1 100644 --- a/src/librustc_span/symbol.rs +++ b/src/librustc_span/symbol.rs @@ -14,7 +14,7 @@ use std::hash::{Hash, Hasher}; use std::str; -use crate::{Span, DUMMY_SP, GLOBALS}; +use crate::{Span, DUMMY_SP, SESSION_GLOBALS}; #[cfg(test)] mod tests; @@ -1307,7 +1307,7 @@ pub fn is_raw_guess(self) -> bool { #[inline] fn with_interner T>(f: F) -> T { - GLOBALS.with(|globals| f(&mut *globals.symbol_interner.lock())) + SESSION_GLOBALS.with(|session_globals| f(&mut *session_globals.symbol_interner.lock())) } /// An alternative to `Symbol`, useful when the chars within the symbol need to diff --git a/src/librustc_span/symbol/tests.rs b/src/librustc_span/symbol/tests.rs index f74b9a0cd1d..47da03424b7 100644 --- a/src/librustc_span/symbol/tests.rs +++ b/src/librustc_span/symbol/tests.rs @@ -1,6 +1,6 @@ use super::*; -use crate::{edition, Globals}; +use crate::{edition, SessionGlobals}; #[test] fn interner_tests() { @@ -18,7 +18,7 @@ fn interner_tests() { #[test] fn without_first_quote_test() { - GLOBALS.set(&Globals::new(edition::DEFAULT_EDITION), || { + SESSION_GLOBALS.set(&SessionGlobals::new(edition::DEFAULT_EDITION), || { let i = Ident::from_str("'break"); assert_eq!(i.without_first_quote().name, kw::Break); }); diff --git a/src/librustdoc/clean/cfg/tests.rs b/src/librustdoc/clean/cfg/tests.rs index 716263393ba..8d1193e7f82 100644 --- a/src/librustdoc/clean/cfg/tests.rs +++ b/src/librustdoc/clean/cfg/tests.rs @@ -2,7 +2,7 @@ use rustc_ast::ast::*; use rustc_ast::attr; -use rustc_ast::with_default_globals; +use rustc_ast::with_default_session_globals; use rustc_span::symbol::{Ident, Symbol}; use rustc_span::DUMMY_SP; @@ -52,7 +52,7 @@ macro_rules! dummy_meta_item_list { #[test] fn test_cfg_not() { - with_default_globals(|| { + with_default_session_globals(|| { assert_eq!(!Cfg::False, Cfg::True); assert_eq!(!Cfg::True, Cfg::False); assert_eq!(!word_cfg("test"), Cfg::Not(Box::new(word_cfg("test")))); @@ -70,7 +70,7 @@ fn test_cfg_not() { #[test] fn test_cfg_and() { - with_default_globals(|| { + with_default_session_globals(|| { let mut x = Cfg::False; x &= Cfg::True; assert_eq!(x, Cfg::False); @@ -154,7 +154,7 @@ fn test_cfg_and() { #[test] fn test_cfg_or() { - with_default_globals(|| { + with_default_session_globals(|| { let mut x = Cfg::True; x |= Cfg::False; assert_eq!(x, Cfg::True); @@ -238,7 +238,7 @@ fn test_cfg_or() { #[test] fn test_parse_ok() { - with_default_globals(|| { + with_default_session_globals(|| { let mi = dummy_meta_item_word("all"); assert_eq!(Cfg::parse(&mi), Ok(word_cfg("all"))); @@ -271,7 +271,7 @@ fn test_parse_ok() { #[test] fn test_parse_err() { - with_default_globals(|| { + with_default_session_globals(|| { let mi = attr::mk_name_value_item(Ident::from_str("foo"), LitKind::Bool(false), DUMMY_SP); assert!(Cfg::parse(&mi).is_err()); @@ -303,7 +303,7 @@ fn test_parse_err() { #[test] fn test_render_short_html() { - with_default_globals(|| { + with_default_session_globals(|| { assert_eq!(word_cfg("unix").render_short_html(), "Unix"); assert_eq!(name_value_cfg("target_os", "macos").render_short_html(), "macOS"); assert_eq!(name_value_cfg("target_pointer_width", "16").render_short_html(), "16-bit"); @@ -358,7 +358,7 @@ fn test_render_short_html() { #[test] fn test_render_long_html() { - with_default_globals(|| { + with_default_session_globals(|| { assert_eq!( word_cfg("unix").render_long_html(), "This is supported on Unix only." diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index e9504aa3af1..71d74e77e33 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -1,5 +1,4 @@ use rustc_ast::ast; -use rustc_ast::with_globals; use rustc_data_structures::sync::Lrc; use rustc_errors::ErrorReported; use rustc_feature::UnstableFeatures; @@ -399,7 +398,7 @@ pub fn make_test( // Uses librustc_ast to parse the doctest and find if there's a main fn and the extern // crate already is included. let result = rustc_driver::catch_fatal_errors(|| { - with_globals(edition, || { + rustc_ast::with_session_globals(edition, || { use rustc_errors::emitter::EmitterWriter; use rustc_errors::Handler; use rustc_parse::maybe_new_parser_from_source_str; diff --git a/src/test/ui-fulldeps/mod_dir_path_canonicalized.rs b/src/test/ui-fulldeps/mod_dir_path_canonicalized.rs index ff7bbafe7c2..836cb07d5d1 100644 --- a/src/test/ui-fulldeps/mod_dir_path_canonicalized.rs +++ b/src/test/ui-fulldeps/mod_dir_path_canonicalized.rs @@ -19,7 +19,7 @@ mod gravy; pub fn main() { - rustc_ast::with_default_globals(|| parse()); + rustc_ast::with_default_session_globals(|| parse()); assert_eq!(gravy::foo(), 10); } diff --git a/src/test/ui-fulldeps/pprust-expr-roundtrip.rs b/src/test/ui-fulldeps/pprust-expr-roundtrip.rs index 6da26e6cfbe..8286b7fdb66 100644 --- a/src/test/ui-fulldeps/pprust-expr-roundtrip.rs +++ b/src/test/ui-fulldeps/pprust-expr-roundtrip.rs @@ -208,7 +208,7 @@ fn visit_expr(&mut self, e: &mut P) { } fn main() { - rustc_ast::with_default_globals(|| run()); + rustc_ast::with_default_session_globals(|| run()); } fn run() { diff --git a/src/tools/error_index_generator/main.rs b/src/tools/error_index_generator/main.rs index 097fb1f985a..9aea859999c 100644 --- a/src/tools/error_index_generator/main.rs +++ b/src/tools/error_index_generator/main.rs @@ -284,7 +284,7 @@ fn parse_args() -> (OutputFormat, PathBuf) { fn main() { env_logger::init(); let (format, dst) = parse_args(); - let result = rustc_ast::with_default_globals(move || main_with_result(format, &dst)); + let result = rustc_ast::with_default_session_globals(move || main_with_result(format, &dst)); if let Err(e) = result { panic!("{}", e.to_string()); }