Skip to content

Commit f786437

Browse files
committed
syntax: refactor (Span)Handler and ParseSess constructors to be methods.
1 parent 6a045b9 commit f786437

File tree

17 files changed

+62
-65
lines changed

17 files changed

+62
-65
lines changed

src/librustc/middle/astencode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,7 @@ impl FakeExtCtxt for parse::ParseSess {
18661866

18671867
#[cfg(test)]
18681868
fn mk_ctxt() -> parse::ParseSess {
1869-
parse::new_parse_sess()
1869+
parse::ParseSess::new()
18701870
}
18711871

18721872
#[cfg(test)]

src/librustc/session/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
852852
parse::parse_meta_from_source_str("cfgspec".to_string(),
853853
s.to_string(),
854854
Vec::new(),
855-
&parse::new_parse_sess())
855+
&parse::ParseSess::new())
856856
}).collect::<ast::CrateConfig>()
857857
}
858858

src/librustc/session/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,9 @@ pub fn build_session(sopts: config::Options,
368368

369369
let codemap = codemap::CodeMap::new();
370370
let diagnostic_handler =
371-
diagnostic::default_handler(sopts.color, Some(registry), can_print_warnings);
371+
diagnostic::Handler::new(sopts.color, Some(registry), can_print_warnings);
372372
let span_diagnostic_handler =
373-
diagnostic::mk_span_handler(diagnostic_handler, codemap);
373+
diagnostic::SpanHandler::new(diagnostic_handler, codemap);
374374

375375
build_session_(sopts, local_crate_source_file, span_diagnostic_handler)
376376
}
@@ -387,7 +387,7 @@ pub fn build_session_(sopts: config::Options,
387387
}
388388
};
389389
let target_cfg = config::build_target_config(&sopts, &span_diagnostic);
390-
let p_s = parse::new_parse_sess_special_handler(span_diagnostic);
390+
let p_s = parse::ParseSess::with_span_handler(span_diagnostic);
391391
let default_sysroot = match sopts.maybe_sysroot {
392392
Some(_) => None,
393393
None => Some(filesearch::get_or_default_sysroot())

src/librustc_back/target/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ impl Target {
218218
// this is 1. ugly, 2. error prone.
219219

220220

221-
let handler = diagnostic::default_handler(diagnostic::Auto, None, true);
221+
let handler = diagnostic::Handler::new(diagnostic::Auto, None, true);
222222

223223
let get_req_field = |name: &str| {
224224
match obj.find(name)

src/librustc_driver/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ fn test_env<F>(source_string: &str,
105105
let codemap =
106106
CodeMap::new();
107107
let diagnostic_handler =
108-
diagnostic::mk_handler(true, emitter);
108+
diagnostic::Handler::with_emitter(true, emitter);
109109
let span_diagnostic_handler =
110-
diagnostic::mk_span_handler(diagnostic_handler, codemap);
110+
diagnostic::SpanHandler::new(diagnostic_handler, codemap);
111111

112112
let sess = session::build_session_(options, None, span_diagnostic_handler);
113113
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));

src/librustc_trans/back/write.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use util::common::time;
2121
use util::common::path2cstr;
2222
use syntax::codemap;
2323
use syntax::diagnostic;
24-
use syntax::diagnostic::{Emitter, Handler, Level, mk_handler};
24+
use syntax::diagnostic::{Emitter, Handler, Level};
2525

2626
use std::ffi::{CStr, CString};
2727
use std::fs;
@@ -928,7 +928,7 @@ fn run_work_multithreaded(sess: &Session,
928928
futures.push(rx);
929929

930930
thread::Builder::new().name(format!("codegen-{}", i)).spawn(move || {
931-
let diag_handler = mk_handler(true, box diag_emitter);
931+
let diag_handler = Handler::with_emitter(true, box diag_emitter);
932932

933933
// Must construct cgcx inside the proc because it has non-Send
934934
// fields.

src/librustdoc/core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ pub fn run_core(search_paths: SearchPaths, cfgs: Vec<String>, externs: Externs,
110110
};
111111

112112
let codemap = codemap::CodeMap::new();
113-
let diagnostic_handler = diagnostic::default_handler(diagnostic::Auto, None, true);
113+
let diagnostic_handler = diagnostic::Handler::new(diagnostic::Auto, None, true);
114114
let span_diagnostic_handler =
115-
diagnostic::mk_span_handler(diagnostic_handler, codemap);
115+
diagnostic::SpanHandler::new(diagnostic_handler, codemap);
116116

117117
let sess = session::build_session_(sessopts, cpath,
118118
span_diagnostic_handler);

src/librustdoc/html/highlight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use syntax::parse;
2424
/// Highlights some source code, returning the HTML output.
2525
pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
2626
debug!("highlighting: ================\n{}\n==============", src);
27-
let sess = parse::new_parse_sess();
27+
let sess = parse::ParseSess::new();
2828
let fm = parse::string_to_filemap(&sess,
2929
src.to_string(),
3030
"<stdin>".to_string());

src/librustdoc/test.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ pub fn run(input: &str,
6565
};
6666

6767
let codemap = CodeMap::new();
68-
let diagnostic_handler = diagnostic::default_handler(diagnostic::Auto, None, true);
68+
let diagnostic_handler = diagnostic::Handler::new(diagnostic::Auto, None, true);
6969
let span_diagnostic_handler =
70-
diagnostic::mk_span_handler(diagnostic_handler, codemap);
70+
diagnostic::SpanHandler::new(diagnostic_handler, codemap);
7171

7272
let sess = session::build_session_(sessopts,
7373
Some(input_path.clone()),
@@ -184,7 +184,7 @@ fn runtest(test: &str, cratename: &str, libs: SearchPaths,
184184
// it with a sink that is also passed to rustc itself. When this function
185185
// returns the output of the sink is copied onto the output of our own thread.
186186
//
187-
// The basic idea is to not use a default_handler() for rustc, and then also
187+
// The basic idea is to not use a default Handler for rustc, and then also
188188
// not print things by default to the actual stderr.
189189
struct Sink(Arc<Mutex<Vec<u8>>>);
190190
impl Write for Sink {
@@ -206,9 +206,9 @@ fn runtest(test: &str, cratename: &str, libs: SearchPaths,
206206

207207
// Compile the code
208208
let codemap = CodeMap::new();
209-
let diagnostic_handler = diagnostic::mk_handler(true, box emitter);
209+
let diagnostic_handler = diagnostic::Handler::with_emitter(true, box emitter);
210210
let span_diagnostic_handler =
211-
diagnostic::mk_span_handler(diagnostic_handler, codemap);
211+
diagnostic::SpanHandler::new(diagnostic_handler, codemap);
212212

213213
let sess = session::build_session_(sessopts,
214214
None,

src/libsyntax/diagnostic.rs

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ pub struct SpanHandler {
122122
}
123123

124124
impl SpanHandler {
125+
pub fn new(handler: Handler, cm: codemap::CodeMap) -> SpanHandler {
126+
SpanHandler {
127+
handler: handler,
128+
cm: cm,
129+
}
130+
}
125131
pub fn span_fatal(&self, sp: Span, msg: &str) -> FatalError {
126132
self.handler.emit(Some((&self.cm, sp)), msg, Fatal);
127133
return FatalError;
@@ -187,6 +193,19 @@ pub struct Handler {
187193
}
188194

189195
impl Handler {
196+
pub fn new(color_config: ColorConfig,
197+
registry: Option<diagnostics::registry::Registry>,
198+
can_emit_warnings: bool) -> Handler {
199+
let emitter = Box::new(EmitterWriter::stderr(color_config, registry));
200+
Handler::with_emitter(can_emit_warnings, emitter)
201+
}
202+
pub fn with_emitter(can_emit_warnings: bool, e: Box<Emitter + Send>) -> Handler {
203+
Handler {
204+
err_count: Cell::new(0),
205+
emit: RefCell::new(e),
206+
can_emit_warnings: can_emit_warnings
207+
}
208+
}
190209
pub fn fatal(&self, msg: &str) -> ! {
191210
self.emit.borrow_mut().emit(None, msg, None, Fatal);
192211
panic!(FatalError);
@@ -254,27 +273,6 @@ impl Handler {
254273
}
255274
}
256275

257-
pub fn mk_span_handler(handler: Handler, cm: codemap::CodeMap) -> SpanHandler {
258-
SpanHandler {
259-
handler: handler,
260-
cm: cm,
261-
}
262-
}
263-
264-
pub fn default_handler(color_config: ColorConfig,
265-
registry: Option<diagnostics::registry::Registry>,
266-
can_emit_warnings: bool) -> Handler {
267-
mk_handler(can_emit_warnings, Box::new(EmitterWriter::stderr(color_config, registry)))
268-
}
269-
270-
pub fn mk_handler(can_emit_warnings: bool, e: Box<Emitter + Send>) -> Handler {
271-
Handler {
272-
err_count: Cell::new(0),
273-
emit: RefCell::new(e),
274-
can_emit_warnings: can_emit_warnings
275-
}
276-
}
277-
278276
#[derive(Copy, PartialEq, Clone, Debug)]
279277
pub enum Level {
280278
Bug,

src/libsyntax/ext/expand.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ mod tests {
16841684
#[test] fn macros_cant_escape_fns_test () {
16851685
let src = "fn bogus() {macro_rules! z (() => (3+4));}\
16861686
fn inty() -> i32 { z!() }".to_string();
1687-
let sess = parse::new_parse_sess();
1687+
let sess = parse::ParseSess::new();
16881688
let crate_ast = parse::parse_crate_from_source_str(
16891689
"<test>".to_string(),
16901690
src,
@@ -1698,7 +1698,7 @@ mod tests {
16981698
#[test] fn macros_cant_escape_mods_test () {
16991699
let src = "mod foo {macro_rules! z (() => (3+4));}\
17001700
fn inty() -> i32 { z!() }".to_string();
1701-
let sess = parse::new_parse_sess();
1701+
let sess = parse::ParseSess::new();
17021702
let crate_ast = parse::parse_crate_from_source_str(
17031703
"<test>".to_string(),
17041704
src,
@@ -1710,7 +1710,7 @@ mod tests {
17101710
#[test] fn macros_can_escape_flattened_mods_test () {
17111711
let src = "#[macro_use] mod foo {macro_rules! z (() => (3+4));}\
17121712
fn inty() -> i32 { z!() }".to_string();
1713-
let sess = parse::new_parse_sess();
1713+
let sess = parse::ParseSess::new();
17141714
let crate_ast = parse::parse_crate_from_source_str(
17151715
"<test>".to_string(),
17161716
src,
@@ -1719,7 +1719,7 @@ mod tests {
17191719
}
17201720

17211721
fn expand_crate_str(crate_str: String) -> ast::Crate {
1722-
let ps = parse::new_parse_sess();
1722+
let ps = parse::ParseSess::new();
17231723
let crate_ast = panictry!(string_to_parser(&ps, crate_str).parse_crate_mod());
17241724
// the cfg argument actually does matter, here...
17251725
expand_crate(&ps,test_ecfg(),vec!(),vec!(),crate_ast)

src/libsyntax/parse/lexer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,8 +1406,8 @@ mod tests {
14061406
fn mk_sh() -> diagnostic::SpanHandler {
14071407
// FIXME (#22405): Replace `Box::new` with `box` here when/if possible.
14081408
let emitter = diagnostic::EmitterWriter::new(Box::new(io::sink()), None);
1409-
let handler = diagnostic::mk_handler(true, Box::new(emitter));
1410-
diagnostic::mk_span_handler(handler, CodeMap::new())
1409+
let handler = diagnostic::Handler::with_emitter(true, Box::new(emitter));
1410+
diagnostic::SpanHandler::new(handler, CodeMap::new())
14111411
}
14121412

14131413
// open a string reader for the given string

src/libsyntax/parse/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
1313
use ast;
1414
use codemap::{Span, CodeMap, FileMap};
15-
use diagnostic::{SpanHandler, mk_span_handler, default_handler, Auto, FatalError};
15+
use diagnostic::{SpanHandler, Handler, Auto, FatalError};
1616
use parse::attr::ParserAttr;
1717
use parse::parser::Parser;
1818
use ptr::P;
@@ -46,17 +46,17 @@ pub struct ParseSess {
4646
included_mod_stack: RefCell<Vec<PathBuf>>,
4747
}
4848

49-
pub fn new_parse_sess() -> ParseSess {
50-
ParseSess {
51-
span_diagnostic: mk_span_handler(default_handler(Auto, None, true), CodeMap::new()),
52-
included_mod_stack: RefCell::new(Vec::new()),
49+
impl ParseSess {
50+
pub fn new() -> ParseSess {
51+
let handler = SpanHandler::new(Handler::new(Auto, None, true), CodeMap::new());
52+
ParseSess::with_span_handler(handler)
5353
}
54-
}
5554

56-
pub fn new_parse_sess_special_handler(sh: SpanHandler) -> ParseSess {
57-
ParseSess {
58-
span_diagnostic: sh,
59-
included_mod_stack: RefCell::new(Vec::new()),
55+
pub fn with_span_handler(sh: SpanHandler) -> ParseSess {
56+
ParseSess {
57+
span_diagnostic: sh,
58+
included_mod_stack: RefCell::new(vec![])
59+
}
6060
}
6161
}
6262

@@ -886,7 +886,7 @@ mod tests {
886886
}
887887

888888
#[test] fn parse_ident_pat () {
889-
let sess = new_parse_sess();
889+
let sess = ParseSess::new();
890890
let mut parser = string_to_parser(&sess, "b".to_string());
891891
assert!(panictry!(parser.parse_pat_nopanic())
892892
== P(ast::Pat{
@@ -1067,7 +1067,7 @@ mod tests {
10671067
}
10681068

10691069
#[test] fn crlf_doc_comments() {
1070-
let sess = new_parse_sess();
1070+
let sess = ParseSess::new();
10711071

10721072
let name = "<source>".to_string();
10731073
let source = "/// doc comment\r\nfn foo() {}".to_string();
@@ -1090,7 +1090,7 @@ mod tests {
10901090

10911091
#[test]
10921092
fn ttdelim_span() {
1093-
let sess = parse::new_parse_sess();
1093+
let sess = ParseSess::new();
10941094
let expr = parse::parse_expr_from_source_str("foo".to_string(),
10951095
"foo!( fn main() { body } )".to_string(), vec![], &sess);
10961096

src/libsyntax/util/parser_testing.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
// except according to those terms.
1010

1111
use ast;
12-
use parse::new_parse_sess;
1312
use parse::{ParseSess,string_to_filemap,filemap_to_tts};
1413
use parse::new_parser_from_source_str;
1514
use parse::parser::Parser;
@@ -19,7 +18,7 @@ use str::char_at;
1918

2019
/// Map a string to tts, using a made-up filename:
2120
pub fn string_to_tts(source_str: String) -> Vec<ast::TokenTree> {
22-
let ps = new_parse_sess();
21+
let ps = ParseSess::new();
2322
filemap_to_tts(&ps,
2423
string_to_filemap(&ps, source_str, "bogofile".to_string()))
2524
}
@@ -35,7 +34,7 @@ pub fn string_to_parser<'a>(ps: &'a ParseSess, source_str: String) -> Parser<'a>
3534
fn with_error_checking_parse<T, F>(s: String, f: F) -> T where
3635
F: FnOnce(&mut Parser) -> T,
3736
{
38-
let ps = new_parse_sess();
37+
let ps = ParseSess::new();
3938
let mut p = string_to_parser(&ps, s);
4039
let x = f(&mut p);
4140
p.abort_if_errors();
@@ -75,7 +74,7 @@ pub fn string_to_stmt(source_str : String) -> P<ast::Stmt> {
7574
pub fn string_to_pat(source_str: String) -> P<ast::Pat> {
7675
// Binding `sess` and `parser` works around dropck-injected
7776
// region-inference issues; see #25212, #22323, #22321.
78-
let sess = new_parse_sess();
77+
let sess = ParseSess::new();
7978
let mut parser = string_to_parser(&sess, source_str);
8079
parser.parse_pat()
8180
}

src/test/compile-fail-fulldeps/qquote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use syntax::parse;
2020
use syntax::print::pprust;
2121

2222
fn main() {
23-
let ps = syntax::parse::new_parse_sess();
23+
let ps = syntax::parse::ParseSess::new();
2424
let mut cx = syntax::ext::base::ExtCtxt::new(
2525
&ps, vec![],
2626
syntax::ext::expand::ExpansionConfig::default("qquote".to_string()));

src/test/run-fail-fulldeps/qquote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use syntax::parse;
2222
use syntax::print::pprust;
2323

2424
fn main() {
25-
let ps = syntax::parse::new_parse_sess();
25+
let ps = syntax::parse::ParseSess::new();
2626
let mut cx = syntax::ext::base::ExtCtxt::new(
2727
&ps, vec![],
2828
syntax::ext::expand::ExpansionConfig::default("qquote".to_string()));

src/test/run-pass-fulldeps/qquote.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use syntax::codemap::DUMMY_SP;
1818
use syntax::print::pprust::*;
1919

2020
fn main() {
21-
let ps = syntax::parse::new_parse_sess();
21+
let ps = syntax::parse::ParseSess::new();
2222
let mut cx = syntax::ext::base::ExtCtxt::new(
2323
&ps, vec![],
2424
syntax::ext::expand::ExpansionConfig::default("qquote".to_string()));

0 commit comments

Comments
 (0)