Skip to content

Commit

Permalink
Rollup merge of #84961 - GuillaumeGomez:rework-session-globals, r=oli…
Browse files Browse the repository at this point in the history
…-obk

Rework SESSION_GLOBALS API

Fixes #84954.

<s>Needs #84953 to be merged first (I cherry-picked its commits to have CI pass).</s> (done)

r? ``@Aaron1011``
  • Loading branch information
GuillaumeGomez authored Jul 8, 2021
2 parents 0deb536 + d891c8c commit d9297ae
Show file tree
Hide file tree
Showing 28 changed files with 152 additions and 104 deletions.
10 changes: 5 additions & 5 deletions compiler/rustc_ast/src/util/comments/tests.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::*;
use rustc_span::with_default_session_globals;
use rustc_span::create_default_session_globals_then;

#[test]
fn test_block_doc_comment_1() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let comment = "\n * Test \n ** Test\n * Test\n";
let stripped = beautify_doc_string(Symbol::intern(comment));
assert_eq!(stripped.as_str(), " Test \n* Test\n Test");
Expand All @@ -12,7 +12,7 @@ fn test_block_doc_comment_1() {

#[test]
fn test_block_doc_comment_2() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let comment = "\n * Test\n * Test\n";
let stripped = beautify_doc_string(Symbol::intern(comment));
assert_eq!(stripped.as_str(), " Test\n Test");
Expand All @@ -21,7 +21,7 @@ fn test_block_doc_comment_2() {

#[test]
fn test_block_doc_comment_3() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let comment = "\n let a: *i32;\n *a = 5;\n";
let stripped = beautify_doc_string(Symbol::intern(comment));
assert_eq!(stripped.as_str(), " let a: *i32;\n *a = 5;");
Expand All @@ -30,7 +30,7 @@ fn test_block_doc_comment_3() {

#[test]
fn test_line_doc_comment() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let stripped = beautify_doc_string(Symbol::intern(" test"));
assert_eq!(stripped.as_str(), " test");
let stripped = beautify_doc_string(Symbol::intern("! test"));
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_ast_pretty/src/pprust/tests.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::*;

use rustc_ast as ast;
use rustc_span::create_default_session_globals_then;
use rustc_span::symbol::Ident;
use rustc_span::with_default_session_globals;

fn fun_to_string(
decl: &ast::FnDecl,
Expand All @@ -24,7 +24,7 @@ fn variant_to_string(var: &ast::Variant) -> String {

#[test]
fn test_fun_to_string() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let abba_ident = Ident::from_str("abba");

let decl =
Expand All @@ -39,7 +39,7 @@ fn test_fun_to_string() {

#[test]
fn test_variant_to_string() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let ident = Ident::from_str("principal_skinner");

let var = ast::Variant {
Expand Down
7 changes: 1 addition & 6 deletions compiler/rustc_errors/src/json/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,11 @@ impl<T: Write> Write for Shared<T> {
}
}

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_session_globals(|| {
rustc_span::create_default_session_globals_then(|| {
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 compiler/rustc_expand/src/mut_visit/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use crate::tests::{matches_codepattern, string_to_crate};
use rustc_ast as ast;
use rustc_ast::mut_visit::MutVisitor;
use rustc_ast_pretty::pprust;
use rustc_span::create_default_session_globals_then;
use rustc_span::symbol::Ident;
use rustc_span::with_default_session_globals;

// This version doesn't care about getting comments or doc-strings in.
fn print_crate_items(krate: &ast::Crate) -> String {
Expand Down Expand Up @@ -38,7 +38,7 @@ macro_rules! assert_pred {
// Make sure idents get transformed everywhere.
#[test]
fn ident_transformation() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
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_session_globals(|| {
create_default_session_globals_then(|| {
let mut zz_visitor = ToZzIdentMutVisitor;
let mut krate = string_to_crate(
"macro_rules! a {(b $c:expr $(d $e:token)f+ => \
Expand Down
24 changes: 12 additions & 12 deletions compiler/rustc_expand/src/parse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use rustc_errors::PResult;
use rustc_parse::new_parser_from_source_str;
use rustc_parse::parser::ForceCollect;
use rustc_session::parse::ParseSess;
use rustc_span::create_default_session_globals_then;
use rustc_span::source_map::FilePathMapping;
use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::with_default_session_globals;
use rustc_span::{BytePos, FileName, Pos, Span};

use std::path::PathBuf;
Expand Down Expand Up @@ -51,15 +51,15 @@ fn string_to_item(source_str: String) -> Option<P<ast::Item>> {
#[should_panic]
#[test]
fn bad_path_expr_1() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
string_to_expr("::abc::def::return".to_string());
})
}

// Checks the token-tree-ization of macros.
#[test]
fn string_to_tts_macro() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let tts: Vec<_> =
string_to_stream("macro_rules! zip (($a)=>($a))".to_string()).trees().collect();
let tts: &[TokenTree] = &tts[..];
Expand Down Expand Up @@ -96,7 +96,7 @@ fn string_to_tts_macro() {

#[test]
fn string_to_tts_1() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let tts = string_to_stream("fn a (b : i32) { b; }".to_string());

let expected = TokenStream::new(vec![
Expand Down Expand Up @@ -131,7 +131,7 @@ fn string_to_tts_1() {

#[test]
fn parse_use() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let use_s = "use foo::bar::baz;";
let vitem = string_to_item(use_s.to_string()).unwrap();
let vitem_s = item_to_string(&vitem);
Expand All @@ -146,7 +146,7 @@ fn parse_use() {

#[test]
fn parse_extern_crate() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let ex_s = "extern crate foo;";
let vitem = string_to_item(ex_s.to_string()).unwrap();
let vitem_s = item_to_string(&vitem);
Expand Down Expand Up @@ -184,7 +184,7 @@ fn get_spans_of_pat_idents(src: &str) -> Vec<Span> {

#[test]
fn span_of_self_arg_pat_idents_are_correct() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let srcs = [
"impl z { fn a (&self, &myarg: i32) {} }",
"impl z { fn a (&mut self, &myarg: i32) {} }",
Expand All @@ -208,7 +208,7 @@ fn span_of_self_arg_pat_idents_are_correct() {

#[test]
fn parse_exprs() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
// just make sure that they parse....
string_to_expr("3 + 4".to_string());
string_to_expr("a::z.froob(b,&(987+3))".to_string());
Expand All @@ -217,7 +217,7 @@ fn parse_exprs() {

#[test]
fn attrs_fix_bug() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
string_to_item(
"pub fn mk_file_writer(path: &Path, flags: &[FileFlag])
-> Result<Box<Writer>, String> {
Expand All @@ -238,7 +238,7 @@ let mut fflags: c_int = wb();

#[test]
fn crlf_doc_comments() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let sess = sess();

let name_1 = FileName::Custom("crlf_source_1".to_string());
Expand Down Expand Up @@ -272,7 +272,7 @@ fn ttdelim_span() {
new_parser_from_source_str(sess, name, source).parse_expr()
}

with_default_session_globals(|| {
create_default_session_globals_then(|| {
let sess = sess();
let expr = parse_expr_from_source_str(
PathBuf::from("foo").into(),
Expand Down Expand Up @@ -300,7 +300,7 @@ fn ttdelim_span() {
// See `recurse_into_file_modules` in the parser.
#[test]
fn out_of_line_mod() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let item = parse_item_from_source_str(
PathBuf::from("foo").into(),
"mod foo { struct S; mod this_does_not_exist; }".to_owned(),
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use rustc_ast as ast;
use rustc_ast::tokenstream::TokenStream;
use rustc_parse::{new_parser_from_source_str, parser::Parser, source_file_to_stream};
use rustc_session::parse::ParseSess;
use rustc_span::create_default_session_if_not_set_then;
use rustc_span::source_map::{FilePathMapping, SourceMap};
use rustc_span::with_default_session_globals;
use rustc_span::{BytePos, MultiSpan, Span};

use rustc_data_structures::sync::Lrc;
Expand Down Expand Up @@ -124,7 +124,7 @@ impl<T: Write> Write for Shared<T> {
}

fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &str) {
with_default_session_globals(|| {
create_default_session_if_not_set_then(|_| {
let output = Arc::new(Mutex::new(Vec::new()));

let source_map = Lrc::new(SourceMap::new(FilePathMapping::empty()));
Expand Down
20 changes: 10 additions & 10 deletions compiler/rustc_expand/src/tokenstream/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::tests::string_to_stream;

use rustc_ast::token;
use rustc_ast::tokenstream::{Spacing, TokenStream, TokenStreamBuilder, TokenTree};
use rustc_span::with_default_session_globals;
use rustc_span::create_default_session_globals_then;
use rustc_span::{BytePos, Span, Symbol};
use smallvec::smallvec;

Expand All @@ -20,7 +20,7 @@ fn joint(tree: TokenTree) -> TokenStream {

#[test]
fn test_concat() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let test_res = string_to_ts("foo::bar::baz");
let test_fst = string_to_ts("foo::bar");
let test_snd = string_to_ts("::baz");
Expand All @@ -33,7 +33,7 @@ fn test_concat() {

#[test]
fn test_to_from_bijection() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let test_start = string_to_ts("foo::bar(baz)");
let test_end = test_start.trees().collect();
assert_eq!(test_start, test_end)
Expand All @@ -42,7 +42,7 @@ fn test_to_from_bijection() {

#[test]
fn test_eq_0() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let test_res = string_to_ts("foo");
let test_eqs = string_to_ts("foo");
assert_eq!(test_res, test_eqs)
Expand All @@ -51,7 +51,7 @@ fn test_eq_0() {

#[test]
fn test_eq_1() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let test_res = string_to_ts("::bar::baz");
let test_eqs = string_to_ts("::bar::baz");
assert_eq!(test_res, test_eqs)
Expand All @@ -60,7 +60,7 @@ fn test_eq_1() {

#[test]
fn test_eq_3() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let test_res = string_to_ts("");
let test_eqs = string_to_ts("");
assert_eq!(test_res, test_eqs)
Expand All @@ -69,7 +69,7 @@ fn test_eq_3() {

#[test]
fn test_diseq_0() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let test_res = string_to_ts("::bar::baz");
let test_eqs = string_to_ts("bar::baz");
assert_eq!(test_res == test_eqs, false)
Expand All @@ -78,7 +78,7 @@ fn test_diseq_0() {

#[test]
fn test_diseq_1() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let test_res = string_to_ts("(bar,baz)");
let test_eqs = string_to_ts("bar,baz");
assert_eq!(test_res == test_eqs, false)
Expand All @@ -87,7 +87,7 @@ fn test_diseq_1() {

#[test]
fn test_is_empty() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let test0: TokenStream = Vec::<TokenTree>::new().into_iter().collect();
let test1: TokenStream =
TokenTree::token(token::Ident(Symbol::intern("a"), false), sp(0, 1)).into();
Expand All @@ -101,7 +101,7 @@ fn test_is_empty() {

#[test]
fn test_dotdotdot() {
with_default_session_globals(|| {
create_default_session_globals_then(|| {
let mut builder = TokenStreamBuilder::new();
builder.push(joint(TokenTree::token(token::Dot, sp(0, 1))));
builder.push(joint(TokenTree::token(token::Dot, sp(1, 2))));
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl Compiler {

/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
rustc_span::with_default_session_globals(move || {
rustc_span::create_default_session_if_not_set_then(move |_| {
let cfg = cfgspecs
.into_iter()
.map(|s| {
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ fn assert_non_crate_hash_different(x: &Options, y: &Options) {
// When the user supplies --test we should implicitly supply --cfg test
#[test]
fn test_switch_implies_cfg_test() {
rustc_span::with_default_session_globals(|| {
rustc_span::create_default_session_globals_then(|| {
let matches = optgroups().parse(&["--test".to_string()]).unwrap();
let (sess, cfg) = mk_session(matches);
let cfg = build_configuration(&sess, to_crate_config(cfg));
Expand All @@ -118,7 +118,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_span::with_default_session_globals(|| {
rustc_span::create_default_session_globals_then(|| {
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));
Expand All @@ -130,20 +130,20 @@ fn test_switch_implies_cfg_test_unless_cfg_test() {

#[test]
fn test_can_print_warnings() {
rustc_span::with_default_session_globals(|| {
rustc_span::create_default_session_globals_then(|| {
let matches = optgroups().parse(&["-Awarnings".to_string()]).unwrap();
let (sess, _) = mk_session(matches);
assert!(!sess.diagnostic().can_emit_warnings());
});

rustc_span::with_default_session_globals(|| {
rustc_span::create_default_session_globals_then(|| {
let matches =
optgroups().parse(&["-Awarnings".to_string(), "-Dwarnings".to_string()]).unwrap();
let (sess, _) = mk_session(matches);
assert!(sess.diagnostic().can_emit_warnings());
});

rustc_span::with_default_session_globals(|| {
rustc_span::create_default_session_globals_then(|| {
let matches = optgroups().parse(&["-Adead_code".to_string()]).unwrap();
let (sess, _) = mk_session(matches);
assert!(sess.diagnostic().can_emit_warnings());
Expand Down
Loading

0 comments on commit d9297ae

Please sign in to comment.