Skip to content

Commit 6309b0f

Browse files
committed
move error handling from libsyntax/diagnostics.rs to libsyntax/errors/*
Also split out emitters into their own module.
1 parent 073b0f9 commit 6309b0f

File tree

33 files changed

+207
-1109
lines changed

33 files changed

+207
-1109
lines changed

src/librustc/lint/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use rustc_front::hir;
4747
use rustc_front::util;
4848
use rustc_front::intravisit as hir_visit;
4949
use syntax::visit as ast_visit;
50-
use syntax::diagnostic;
50+
use syntax::errors;
5151

5252
/// Information about the registered lints.
5353
///
@@ -167,7 +167,7 @@ impl LintStore {
167167
match (sess, from_plugin) {
168168
// We load builtin lints first, so a duplicate is a compiler bug.
169169
// Use early_error when handling -W help with no crate.
170-
(None, _) => early_error(diagnostic::Auto, &msg[..]),
170+
(None, _) => early_error(errors::ColorConfig::Auto, &msg[..]),
171171
(Some(sess), false) => sess.bug(&msg[..]),
172172

173173
// A duplicate name from a plugin is a user error.
@@ -191,7 +191,7 @@ impl LintStore {
191191
match (sess, from_plugin) {
192192
// We load builtin lints first, so a duplicate is a compiler bug.
193193
// Use early_error when handling -W help with no crate.
194-
(None, _) => early_error(diagnostic::Auto, &msg[..]),
194+
(None, _) => early_error(errors::ColorConfig::Auto, &msg[..]),
195195
(Some(sess), false) => sess.bug(&msg[..]),
196196

197197
// A duplicate name from a plugin is a user error.

src/librustc/session/config.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use middle::cstore;
2727
use syntax::ast::{self, IntTy, UintTy};
2828
use syntax::attr;
2929
use syntax::attr::AttrMetaMethods;
30-
use syntax::diagnostic::{ColorConfig, Auto, Always, Never, SpanHandler};
30+
use syntax::errors::{ColorConfig, Handler};
3131
use syntax::parse;
3232
use syntax::parse::token::InternedString;
3333
use syntax::feature_gate::UnstableFeatures;
@@ -238,7 +238,7 @@ pub fn basic_options() -> Options {
238238
debugging_opts: basic_debugging_options(),
239239
prints: Vec::new(),
240240
cg: basic_codegen_options(),
241-
color: Auto,
241+
color: ColorConfig::Auto,
242242
show_span: None,
243243
externs: HashMap::new(),
244244
crate_name: None,
@@ -687,19 +687,19 @@ pub fn build_configuration(sess: &Session) -> ast::CrateConfig {
687687
v
688688
}
689689

690-
pub fn build_target_config(opts: &Options, sp: &SpanHandler) -> Config {
690+
pub fn build_target_config(opts: &Options, sp: &Handler) -> Config {
691691
let target = match Target::search(&opts.target_triple) {
692692
Ok(t) => t,
693693
Err(e) => {
694-
panic!(sp.handler().fatal(&format!("Error loading target specification: {}", e)));
694+
panic!(sp.fatal(&format!("Error loading target specification: {}", e)));
695695
}
696696
};
697697

698698
let (int_type, uint_type) = match &target.target_pointer_width[..] {
699699
"32" => (ast::TyI32, ast::TyU32),
700700
"64" => (ast::TyI64, ast::TyU64),
701-
w => panic!(sp.handler().fatal(&format!("target specification was invalid: \
702-
unrecognized target-pointer-width {}", w))),
701+
w => panic!(sp.fatal(&format!("target specification was invalid: \
702+
unrecognized target-pointer-width {}", w))),
703703
};
704704

705705
Config {
@@ -884,16 +884,16 @@ pub fn parse_cfgspecs(cfgspecs: Vec<String> ) -> ast::CrateConfig {
884884

885885
pub fn build_session_options(matches: &getopts::Matches) -> Options {
886886
let color = match matches.opt_str("color").as_ref().map(|s| &s[..]) {
887-
Some("auto") => Auto,
888-
Some("always") => Always,
889-
Some("never") => Never,
887+
Some("auto") => ColorConfig::Auto,
888+
Some("always") => ColorConfig::Always,
889+
Some("never") => ColorConfig::Never,
890890

891-
None => Auto,
891+
None => ColorConfig::Auto,
892892

893893
Some(arg) => {
894-
early_error(Auto, &format!("argument for --color must be auto, always \
895-
or never (instead was `{}`)",
896-
arg))
894+
early_error(ColorConfig::Auto, &format!("argument for --color must be auto, always \
895+
or never (instead was `{}`)",
896+
arg))
897897
}
898898
};
899899

src/librustc/session/mod.rs

+29-30
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ use util::nodemap::{NodeMap, FnvHashMap};
1616

1717
use syntax::ast::{NodeId, NodeIdAssigner, Name};
1818
use syntax::codemap::Span;
19-
use syntax::diagnostic::{self, Emitter};
19+
use syntax::errors;
20+
use syntax::errors::emitter::{Emitter, BasicEmitter};
2021
use syntax::diagnostics;
2122
use syntax::feature_gate;
2223
use syntax::parse;
@@ -99,7 +100,7 @@ impl Session {
99100
if self.opts.treat_err_as_bug {
100101
self.bug(msg);
101102
}
102-
panic!(self.diagnostic().handler().fatal(msg))
103+
panic!(self.diagnostic().fatal(msg))
103104
}
104105
pub fn span_err_or_warn(&self, is_warning: bool, sp: Span, msg: &str) {
105106
if is_warning {
@@ -137,16 +138,16 @@ impl Session {
137138
if self.opts.treat_err_as_bug {
138139
self.bug(msg);
139140
}
140-
self.diagnostic().handler().err(msg)
141+
self.diagnostic().err(msg)
141142
}
142143
pub fn err_count(&self) -> usize {
143-
self.diagnostic().handler().err_count()
144+
self.diagnostic().err_count()
144145
}
145146
pub fn has_errors(&self) -> bool {
146-
self.diagnostic().handler().has_errors()
147+
self.diagnostic().has_errors()
147148
}
148149
pub fn abort_if_errors(&self) {
149-
self.diagnostic().handler().abort_if_errors();
150+
self.diagnostic().abort_if_errors();
150151

151152
let delayed_bug = self.delayed_span_bug.borrow();
152153
match *delayed_bug {
@@ -177,7 +178,7 @@ impl Session {
177178
}
178179
pub fn warn(&self, msg: &str) {
179180
if self.can_print_warnings {
180-
self.diagnostic().handler().warn(msg)
181+
self.diagnostic().warn(msg)
181182
}
182183
}
183184
pub fn opt_span_warn(&self, opt_sp: Option<Span>, msg: &str) {
@@ -195,7 +196,7 @@ impl Session {
195196

196197
/// Prints out a message with a suggested edit of the code.
197198
///
198-
/// See `diagnostic::RenderSpan::Suggestion` for more information.
199+
/// See `errors::RenderSpan::Suggestion` for more information.
199200
pub fn span_suggestion(&self, sp: Span, msg: &str, suggestion: String) {
200201
self.diagnostic().span_suggestion(sp, msg, suggestion)
201202
}
@@ -209,10 +210,10 @@ impl Session {
209210
self.diagnostic().fileline_help(sp, msg)
210211
}
211212
pub fn note(&self, msg: &str) {
212-
self.diagnostic().handler().note(msg)
213+
self.diagnostic().note(msg)
213214
}
214215
pub fn help(&self, msg: &str) {
215-
self.diagnostic().handler().help(msg)
216+
self.diagnostic().help(msg)
216217
}
217218
pub fn opt_span_bug(&self, opt_sp: Option<Span>, msg: &str) -> ! {
218219
match opt_sp {
@@ -229,13 +230,13 @@ impl Session {
229230
self.diagnostic().span_bug(sp, msg)
230231
}
231232
pub fn bug(&self, msg: &str) -> ! {
232-
self.diagnostic().handler().bug(msg)
233+
self.diagnostic().bug(msg)
233234
}
234235
pub fn span_unimpl(&self, sp: Span, msg: &str) -> ! {
235236
self.diagnostic().span_unimpl(sp, msg)
236237
}
237238
pub fn unimpl(&self, msg: &str) -> ! {
238-
self.diagnostic().handler().unimpl(msg)
239+
self.diagnostic().unimpl(msg)
239240
}
240241
pub fn add_lint(&self,
241242
lint: &'static lint::Lint,
@@ -260,7 +261,7 @@ impl Session {
260261

261262
id
262263
}
263-
pub fn diagnostic<'a>(&'a self) -> &'a diagnostic::SpanHandler {
264+
pub fn diagnostic<'a>(&'a self) -> &'a errors::Handler {
264265
&self.parse_sess.span_diagnostic
265266
}
266267
pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap {
@@ -414,29 +415,27 @@ pub fn build_session(sopts: config::Options,
414415
.last()
415416
.unwrap_or(true);
416417

417-
let codemap = codemap::CodeMap::new();
418+
let codemap = Rc::new(codemap::CodeMap::new());
418419
let diagnostic_handler =
419-
diagnostic::Handler::new(sopts.color, Some(registry), can_print_warnings);
420-
let span_diagnostic_handler =
421-
diagnostic::SpanHandler::new(diagnostic_handler, codemap);
420+
errors::Handler::new(sopts.color, Some(registry), can_print_warnings, codemap.clone());
422421

423-
build_session_(sopts, local_crate_source_file, span_diagnostic_handler, cstore)
422+
build_session_(sopts, local_crate_source_file, diagnostic_handler, codemap, cstore)
424423
}
425424

426425
pub fn build_session_(sopts: config::Options,
427426
local_crate_source_file: Option<PathBuf>,
428-
span_diagnostic: diagnostic::SpanHandler,
427+
span_diagnostic: errors::Handler,
428+
codemap: Rc<codemap::CodeMap>,
429429
cstore: Rc<for<'a> CrateStore<'a>>)
430430
-> Session {
431431
let host = match Target::search(config::host_triple()) {
432432
Ok(t) => t,
433433
Err(e) => {
434-
panic!(span_diagnostic.handler()
435-
.fatal(&format!("Error loading host specification: {}", e)));
434+
panic!(span_diagnostic.fatal(&format!("Error loading host specification: {}", e)));
436435
}
437436
};
438437
let target_cfg = config::build_target_config(&sopts, &span_diagnostic);
439-
let p_s = parse::ParseSess::with_span_handler(span_diagnostic);
438+
let p_s = parse::ParseSess::with_span_handler(span_diagnostic, codemap);
440439
let default_sysroot = match sopts.maybe_sysroot {
441440
Some(_) => None,
442441
None => Some(filesearch::get_or_default_sysroot())
@@ -494,16 +493,16 @@ pub fn build_session_(sopts: config::Options,
494493
pub fn expect<T, M>(sess: &Session, opt: Option<T>, msg: M) -> T where
495494
M: FnOnce() -> String,
496495
{
497-
diagnostic::expect(sess.diagnostic(), opt, msg)
496+
errors::expect(sess.diagnostic(), opt, msg)
498497
}
499498

500-
pub fn early_error(color: diagnostic::ColorConfig, msg: &str) -> ! {
501-
let mut emitter = diagnostic::EmitterWriter::stderr(color, None);
502-
emitter.emit(None, msg, None, diagnostic::Fatal);
503-
panic!(diagnostic::FatalError);
499+
pub fn early_error(color: errors::ColorConfig, msg: &str) -> ! {
500+
let mut emitter = BasicEmitter::stderr(color);
501+
emitter.emit(None, msg, None, errors::Level::Fatal);
502+
panic!(errors::FatalError);
504503
}
505504

506-
pub fn early_warn(color: diagnostic::ColorConfig, msg: &str) {
507-
let mut emitter = diagnostic::EmitterWriter::stderr(color, None);
508-
emitter.emit(None, msg, None, diagnostic::Warning);
505+
pub fn early_warn(color: errors::ColorConfig, msg: &str) {
506+
let mut emitter = BasicEmitter::stderr(color);
507+
emitter.emit(None, msg, None, errors::Level::Warning);
509508
}

src/librustc/session/search_paths.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use std::slice;
1212
use std::path::{Path, PathBuf};
1313
use session::early_error;
14-
use syntax::diagnostic;
14+
use syntax::errors;
1515

1616
#[derive(Clone, Debug)]
1717
pub struct SearchPaths {
@@ -38,7 +38,7 @@ impl SearchPaths {
3838
SearchPaths { paths: Vec::new() }
3939
}
4040

41-
pub fn add_path(&mut self, path: &str, color: diagnostic::ColorConfig) {
41+
pub fn add_path(&mut self, path: &str, color: errors::ColorConfig) {
4242
let (kind, path) = if path.starts_with("native=") {
4343
(PathKind::Native, &path["native=".len()..])
4444
} else if path.starts_with("crate=") {

src/librustc_back/target/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
use serialize::json::Json;
4949
use std::default::Default;
5050
use std::io::prelude::*;
51-
use syntax::{diagnostic, abi};
51+
use syntax::abi;
5252

5353
mod android_base;
5454
mod apple_base;
@@ -263,17 +263,13 @@ impl Target {
263263
pub fn from_json(obj: Json) -> Target {
264264
// this is 1. ugly, 2. error prone.
265265

266-
267-
let handler = diagnostic::Handler::new(diagnostic::Auto, None, true);
268-
269266
let get_req_field = |name: &str| {
270267
match obj.find(name)
271268
.map(|s| s.as_string())
272269
.and_then(|os| os.map(|s| s.to_string())) {
273270
Some(val) => val,
274271
None => {
275-
panic!(handler.fatal(&format!("Field {} in target specification is required",
276-
name)))
272+
panic!("Field {} in target specification is required", name)
277273
}
278274
}
279275
};

src/librustc_driver/lib.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@ extern crate log;
5959
extern crate syntax;
6060
extern crate syntax_ext;
6161

62-
pub use syntax::diagnostic;
63-
6462
use driver::CompileController;
6563
use pretty::{PpMode, UserIdentifiedItem};
6664

@@ -91,7 +89,8 @@ use rustc::session::early_error;
9189

9290
use syntax::ast;
9391
use syntax::parse;
94-
use syntax::diagnostic::Emitter;
92+
use syntax::errors;
93+
use syntax::errors::emitter::Emitter;
9594
use syntax::diagnostics;
9695
use syntax::parse::token;
9796

@@ -239,7 +238,7 @@ pub trait CompilerCalls<'a> {
239238
fn early_callback(&mut self,
240239
_: &getopts::Matches,
241240
_: &diagnostics::registry::Registry,
242-
_: diagnostic::ColorConfig)
241+
_: errors::ColorConfig)
243242
-> Compilation {
244243
Compilation::Continue
245244
}
@@ -315,7 +314,7 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
315314
fn early_callback(&mut self,
316315
matches: &getopts::Matches,
317316
descriptions: &diagnostics::registry::Registry,
318-
color: diagnostic::ColorConfig)
317+
color: errors::ColorConfig)
319318
-> Compilation {
320319
match matches.opt_str("explain") {
321320
Some(ref code) => {
@@ -774,7 +773,7 @@ pub fn handle_options(mut args: Vec<String>) -> Option<getopts::Matches> {
774773
&opt.opt_group.short_name
775774
};
776775
if m.opt_present(opt_name) {
777-
early_error(diagnostic::Auto,
776+
early_error(errors::ColorConfig::Auto,
778777
&format!("use of unstable option '{}' requires -Z \
779778
unstable-options",
780779
opt_name));
@@ -783,7 +782,7 @@ pub fn handle_options(mut args: Vec<String>) -> Option<getopts::Matches> {
783782
}
784783
m
785784
}
786-
Err(f) => early_error(diagnostic::Auto, &f.to_string()),
785+
Err(f) => early_error(errors::ColorConfig::Auto, &f.to_string()),
787786
}
788787
}
789788

@@ -895,25 +894,25 @@ pub fn monitor<F: FnOnce() + Send + 'static>(f: F) {
895894
}
896895
Err(value) => {
897896
// Thread panicked without emitting a fatal diagnostic
898-
if !value.is::<diagnostic::FatalError>() {
899-
let mut emitter = diagnostic::EmitterWriter::stderr(diagnostic::Auto, None);
897+
if !value.is::<errors::FatalError>() {
898+
let mut emitter = errors::emitter::BasicEmitter::stderr(errors::ColorConfig::Auto);
900899

901900
// a .span_bug or .bug call has already printed what
902901
// it wants to print.
903-
if !value.is::<diagnostic::ExplicitBug>() {
904-
emitter.emit(None, "unexpected panic", None, diagnostic::Bug);
902+
if !value.is::<errors::ExplicitBug>() {
903+
emitter.emit(None, "unexpected panic", None, errors::Level::Bug);
905904
}
906905

907906
let xs = ["the compiler unexpectedly panicked. this is a bug.".to_string(),
908907
format!("we would appreciate a bug report: {}", BUG_REPORT_URL)];
909908
for note in &xs {
910-
emitter.emit(None, &note[..], None, diagnostic::Note)
909+
emitter.emit(None, &note[..], None, errors::Level::Note)
911910
}
912911
if let None = env::var_os("RUST_BACKTRACE") {
913912
emitter.emit(None,
914913
"run with `RUST_BACKTRACE=1` for a backtrace",
915914
None,
916-
diagnostic::Note);
915+
errors::Level::Note);
917916
}
918917

919918
println!("{}", str::from_utf8(&data.lock().unwrap()).unwrap());

0 commit comments

Comments
 (0)