Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate confusing "globals" terminology. #74079

Merged
merged 1 commit into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions src/librustc_ast/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,61 @@ use log::debug;
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<GrowableBitSet<AttrId>>,
known_attrs: Lock<GrowableBitSet<AttrId>>,
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<R>(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<R>(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<R>(f: impl FnOnce() -> R) -> R {
with_globals(DEFAULT_EDITION, f)
pub fn with_default_session_globals<R>(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 {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_ast/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_ast/util/lev_distance/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_ast_pretty/pprust/tests.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 =
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/librustc_errors/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ impl<T: Write> Write for Shared<T> {
}
}

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());

Expand Down
6 changes: 3 additions & 3 deletions src/librustc_expand/mut_visit/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::tests::{matches_codepattern, string_to_crate};

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;

Expand Down Expand Up @@ -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());
Expand All @@ -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+ => \
Expand Down
28 changes: 14 additions & 14 deletions src/librustc_expand/parse/lexer/tests.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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),);
Expand All @@ -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),);
Expand All @@ -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!(
Expand All @@ -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!(
Expand All @@ -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!(
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
Expand All @@ -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());
Expand Down
Loading