From 6309b0f5bb558b844f45b2d313d2078fd7b7614c Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 14 Dec 2015 11:17:55 +1300 Subject: move error handling from libsyntax/diagnostics.rs to libsyntax/errors/* Also split out emitters into their own module. --- src/libsyntax/parse/lexer/comments.rs | 4 ++-- src/libsyntax/parse/lexer/mod.rs | 17 ++++++++--------- src/libsyntax/parse/mod.rs | 27 +++++++++++++++------------ src/libsyntax/parse/obsolete.rs | 3 +-- src/libsyntax/parse/parser.rs | 13 ++++++------- 5 files changed, 32 insertions(+), 32 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index e5e2c3a986d..d2156d7cb68 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -12,7 +12,7 @@ pub use self::CommentStyle::*; use ast; use codemap::{BytePos, CharPos, CodeMap, Pos}; -use diagnostic; +use errors; use parse::lexer::is_block_doc_comment; use parse::lexer::{StringReader, TokenAndSpan}; use parse::lexer::{is_whitespace, Reader}; @@ -334,7 +334,7 @@ pub struct Literal { // it appears this function is called only from pprust... that's // probably not a good thing. -pub fn gather_comments_and_literals(span_diagnostic: &diagnostic::SpanHandler, +pub fn gather_comments_and_literals(span_diagnostic: &errors::Handler, path: String, srdr: &mut Read) -> (Vec, Vec) { diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index cb2181a0831..570e0882a85 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -11,8 +11,7 @@ use ast; use codemap::{BytePos, CharPos, CodeMap, Pos, Span}; use codemap; -use diagnostic::FatalError; -use diagnostic::SpanHandler; +use errors::{FatalError, Handler}; use ext::tt::transcribe::tt_next_token; use parse::token::str_to_ident; use parse::token; @@ -58,7 +57,7 @@ pub struct TokenAndSpan { } pub struct StringReader<'a> { - pub span_diagnostic: &'a SpanHandler, + pub span_diagnostic: &'a Handler, /// The absolute offset within the codemap of the next character to read pub pos: BytePos, /// The absolute offset within the codemap of the last character read(curr) @@ -128,10 +127,10 @@ impl<'a> Reader for TtReader<'a> { impl<'a> StringReader<'a> { /// For comments.rs, which hackily pokes into pos and curr - pub fn new_raw<'b>(span_diagnostic: &'b SpanHandler, + pub fn new_raw<'b>(span_diagnostic: &'b Handler, filemap: Rc) -> StringReader<'b> { if filemap.src.is_none() { - span_diagnostic.handler.bug(&format!("Cannot lex filemap without source: {}", + span_diagnostic.bug(&format!("Cannot lex filemap without source: {}", filemap.name)[..]); } @@ -153,7 +152,7 @@ impl<'a> StringReader<'a> { sr } - pub fn new<'b>(span_diagnostic: &'b SpanHandler, + pub fn new<'b>(span_diagnostic: &'b Handler, filemap: Rc) -> StringReader<'b> { let mut sr = StringReader::new_raw(span_diagnostic, filemap); sr.advance_token(); @@ -1428,15 +1427,15 @@ mod tests { use parse::token::{str_to_ident}; use std::io; - fn mk_sh() -> diagnostic::SpanHandler { + fn mk_sh() -> diagnostic::Handler { // FIXME (#22405): Replace `Box::new` with `box` here when/if possible. let emitter = diagnostic::EmitterWriter::new(Box::new(io::sink()), None); let handler = diagnostic::Handler::with_emitter(true, Box::new(emitter)); - diagnostic::SpanHandler::new(handler, CodeMap::new()) + diagnostic::Handler::new(handler, CodeMap::new()) } // open a string reader for the given string - fn setup<'a>(span_handler: &'a diagnostic::SpanHandler, + fn setup<'a>(span_handler: &'a diagnostic::Handler, teststr: String) -> StringReader<'a> { let fm = span_handler.cm.new_filemap("zebra.rs".to_string(), teststr); StringReader::new(span_handler, fm) diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index e9c8173a4d9..f74e9023ed1 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -12,7 +12,7 @@ use ast; use codemap::{self, Span, CodeMap, FileMap}; -use diagnostic::{SpanHandler, Handler, Auto, FatalError}; +use errors::{Handler, ColorConfig, FatalError}; use parse::parser::Parser; use parse::token::InternedString; use ptr::P; @@ -40,26 +40,29 @@ pub mod obsolete; /// Info about a parsing session. pub struct ParseSess { - pub span_diagnostic: SpanHandler, // better be the same as the one in the reader! + pub span_diagnostic: Handler, // better be the same as the one in the reader! /// Used to determine and report recursive mod inclusions included_mod_stack: RefCell>, + code_map: Rc, } impl ParseSess { pub fn new() -> ParseSess { - let handler = SpanHandler::new(Handler::new(Auto, None, true), CodeMap::new()); - ParseSess::with_span_handler(handler) + let cm = Rc::new(CodeMap::new()); + let handler = Handler::new(ColorConfig::Auto, None, true, cm.clone()); + ParseSess::with_span_handler(handler, cm) } - pub fn with_span_handler(sh: SpanHandler) -> ParseSess { + pub fn with_span_handler(handler: Handler, code_map: Rc) -> ParseSess { ParseSess { - span_diagnostic: sh, - included_mod_stack: RefCell::new(vec![]) + span_diagnostic: handler, + included_mod_stack: RefCell::new(vec![]), + code_map: code_map } } pub fn codemap(&self) -> &CodeMap { - &self.span_diagnostic.cm + &self.code_map } } @@ -235,7 +238,7 @@ fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option) let msg = format!("couldn't read {:?}: {}", path.display(), e); match spanopt { Some(sp) => panic!(sess.span_diagnostic.span_fatal(sp, &msg)), - None => panic!(sess.span_diagnostic.handler().fatal(&msg)) + None => panic!(sess.span_diagnostic.fatal(&msg)) } } } @@ -438,7 +441,7 @@ fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool { } fn filtered_float_lit(data: token::InternedString, suffix: Option<&str>, - sd: &SpanHandler, sp: Span) -> ast::Lit_ { + sd: &Handler, sp: Span) -> ast::Lit_ { debug!("filtered_float_lit: {}, {:?}", data, suffix); match suffix.as_ref().map(|s| &**s) { Some("f32") => ast::LitFloat(data, ast::TyF32), @@ -459,7 +462,7 @@ fn filtered_float_lit(data: token::InternedString, suffix: Option<&str>, } } pub fn float_lit(s: &str, suffix: Option, - sd: &SpanHandler, sp: Span) -> ast::Lit_ { + sd: &Handler, sp: Span) -> ast::Lit_ { debug!("float_lit: {:?}, {:?}", s, suffix); // FIXME #2252: bounds checking float literals is deferred until trans let s = s.chars().filter(|&c| c != '_').collect::(); @@ -561,7 +564,7 @@ pub fn byte_str_lit(lit: &str) -> Rc> { pub fn integer_lit(s: &str, suffix: Option, - sd: &SpanHandler, + sd: &Handler, sp: Span) -> ast::Lit_ { // s can only be ascii, byte indexing is fine diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index bc355f70fb3..5dba1e189ab 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -66,10 +66,9 @@ impl<'a> ParserObsoleteMethods for parser::Parser<'a> { } if !self.obsolete_set.contains(&kind) && - (error || self.sess.span_diagnostic.handler().can_emit_warnings) { + (error || self.sess.span_diagnostic.can_emit_warnings) { self.sess .span_diagnostic - .handler() .note(&format!("{}", desc)); self.obsolete_set.insert(kind); } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9398f1a5733..04b0c0fa273 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -60,7 +60,7 @@ use attr::{ThinAttributes, ThinAttributesExt, AttributesExt}; use ast; use ast_util::{self, ident_to_path}; use codemap::{self, Span, BytePos, Spanned, spanned, mk_sp, CodeMap}; -use diagnostic; +use errors::{self, FatalError}; use ext::tt::macro_parser; use parse; use parse::classify; @@ -75,7 +75,6 @@ use print::pprust; use ptr::P; use owned_slice::OwnedSlice; use parse::PResult; -use diagnostic::FatalError; use std::collections::HashSet; use std::io::prelude::*; @@ -983,16 +982,16 @@ impl<'a> Parser<'a> { } f(&self.buffer[((self.buffer_start + dist - 1) & 3) as usize].tok) } - pub fn fatal(&self, m: &str) -> diagnostic::FatalError { + pub fn fatal(&self, m: &str) -> errors::FatalError { self.sess.span_diagnostic.span_fatal(self.span, m) } - pub fn span_fatal(&self, sp: Span, m: &str) -> diagnostic::FatalError { + pub fn span_fatal(&self, sp: Span, m: &str) -> errors::FatalError { self.sess.span_diagnostic.span_fatal(sp, m) } - pub fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> diagnostic::FatalError { + pub fn span_fatal_help(&self, sp: Span, m: &str, help: &str) -> errors::FatalError { self.span_err(sp, m); self.fileline_help(sp, help); - diagnostic::FatalError + errors::FatalError } pub fn span_note(&self, sp: Span, m: &str) { self.sess.span_diagnostic.span_note(sp, m) @@ -1022,7 +1021,7 @@ impl<'a> Parser<'a> { self.sess.span_diagnostic.span_bug(sp, m) } pub fn abort_if_errors(&self) { - self.sess.span_diagnostic.handler().abort_if_errors(); + self.sess.span_diagnostic.abort_if_errors(); } pub fn id_to_interned_str(&mut self, id: Ident) -> InternedString { -- cgit 1.4.1-3-g733a5 From a47881182237201f207a4b89166a6be6903a8228 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Mon, 14 Dec 2015 18:15:39 +1300 Subject: Move a bunch of stuff from Session to syntax::errors The intention here is that Session is a very thin wrapper over the error handling infra. --- src/librustc/session/mod.rs | 71 +++++--------------------------- src/librustc_trans/back/write.rs | 2 +- src/librustc_trans/trans/callee.rs | 6 +-- src/librustc_trans/trans/monomorphize.rs | 6 +-- src/libsyntax/parse/mod.rs | 2 +- 5 files changed, 19 insertions(+), 68 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 76d765c9626..7b96db4bf0a 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -64,14 +64,10 @@ pub struct Session { pub crate_metadata: RefCell>, pub features: RefCell, - pub delayed_span_bug: RefCell>, - /// The maximum recursion limit for potentially infinitely recursive /// operations such as auto-dereference and monomorphization. pub recursion_limit: Cell, - pub can_print_warnings: bool, - /// The metadata::creader module may inject an allocator dependency if it /// didn't already find one, and this tracks what was injected. pub injected_allocator: Cell>, @@ -85,21 +81,12 @@ pub struct Session { impl Session { pub fn span_fatal(&self, sp: Span, msg: &str) -> ! { - if self.opts.treat_err_as_bug { - self.span_bug(sp, msg); - } panic!(self.diagnostic().span_fatal(sp, msg)) } pub fn span_fatal_with_code(&self, sp: Span, msg: &str, code: &str) -> ! { - if self.opts.treat_err_as_bug { - self.span_bug(sp, msg); - } panic!(self.diagnostic().span_fatal_with_code(sp, msg, code)) } pub fn fatal(&self, msg: &str) -> ! { - if self.opts.treat_err_as_bug { - self.bug(msg); - } panic!(self.diagnostic().fatal(msg)) } pub fn span_err_or_warn(&self, is_warning: bool, sp: Span, msg: &str) { @@ -110,9 +97,6 @@ impl Session { } } pub fn span_err(&self, sp: Span, msg: &str) { - if self.opts.treat_err_as_bug { - self.span_bug(sp, msg); - } match split_msg_into_multilines(msg) { Some(msg) => self.diagnostic().span_err(sp, &msg[..]), None => self.diagnostic().span_err(sp, msg) @@ -126,18 +110,12 @@ impl Session { See RFC 1214 for details.")); } pub fn span_err_with_code(&self, sp: Span, msg: &str, code: &str) { - if self.opts.treat_err_as_bug { - self.span_bug(sp, msg); - } match split_msg_into_multilines(msg) { Some(msg) => self.diagnostic().span_err_with_code(sp, &msg[..], code), None => self.diagnostic().span_err_with_code(sp, msg, code) } } pub fn err(&self, msg: &str) { - if self.opts.treat_err_as_bug { - self.bug(msg); - } self.diagnostic().err(msg) } pub fn err_count(&self) -> usize { @@ -148,14 +126,6 @@ impl Session { } pub fn abort_if_errors(&self) { self.diagnostic().abort_if_errors(); - - let delayed_bug = self.delayed_span_bug.borrow(); - match *delayed_bug { - Some((span, ref errmsg)) => { - self.diagnostic().span_bug(span, errmsg); - }, - _ => {} - } } pub fn abort_if_new_errors(&self, mut f: F) where F: FnMut() @@ -167,19 +137,13 @@ impl Session { } } pub fn span_warn(&self, sp: Span, msg: &str) { - if self.can_print_warnings { - self.diagnostic().span_warn(sp, msg) - } + self.diagnostic().span_warn(sp, msg) } pub fn span_warn_with_code(&self, sp: Span, msg: &str, code: &str) { - if self.can_print_warnings { - self.diagnostic().span_warn_with_code(sp, msg, code) - } + self.diagnostic().span_warn_with_code(sp, msg, code) } pub fn warn(&self, msg: &str) { - if self.can_print_warnings { - self.diagnostic().warn(msg) - } + self.diagnostic().warn(msg) } pub fn opt_span_warn(&self, opt_sp: Option, msg: &str) { match opt_sp { @@ -223,8 +187,7 @@ impl Session { } /// Delay a span_bug() call until abort_if_errors() pub fn delay_span_bug(&self, sp: Span, msg: &str) { - let mut delayed = self.delayed_span_bug.borrow_mut(); - *delayed = Some((sp, msg.to_string())); + self.diagnostic().delay_span_bug(sp, msg) } pub fn span_bug(&self, sp: Span, msg: &str) -> ! { self.diagnostic().span_bug(sp, msg) @@ -270,8 +233,7 @@ impl Session { // This exists to help with refactoring to eliminate impossible // cases later on pub fn impossible_case(&self, sp: Span, msg: &str) -> ! { - self.span_bug(sp, - &format!("impossible case reached: {}", msg)); + self.span_bug(sp, &format!("impossible case reached: {}", msg)); } pub fn verbose(&self) -> bool { self.opts.debugging_opts.verbose } pub fn time_passes(&self) -> bool { self.opts.debugging_opts.time_passes } @@ -414,10 +376,15 @@ pub fn build_session(sopts: config::Options, .map(|&(_, ref level)| *level != lint::Allow) .last() .unwrap_or(true); + let treat_err_as_bug = sopts.treat_err_as_bug; let codemap = Rc::new(codemap::CodeMap::new()); let diagnostic_handler = - errors::Handler::new(sopts.color, Some(registry), can_print_warnings, codemap.clone()); + errors::Handler::new(sopts.color, + Some(registry), + can_print_warnings, + treat_err_as_bug, + codemap.clone()); build_session_(sopts, local_crate_source_file, diagnostic_handler, codemap, cstore) } @@ -450,13 +417,6 @@ pub fn build_session_(sopts: config::Options, } ); - let can_print_warnings = sopts.lint_opts - .iter() - .filter(|&&(ref key, _)| *key == "warnings") - .map(|&(_, ref level)| *level != lint::Allow) - .last() - .unwrap_or(true); - let sess = Session { target: target_cfg, host: host, @@ -477,10 +437,8 @@ pub fn build_session_(sopts: config::Options, crate_types: RefCell::new(Vec::new()), dependency_formats: RefCell::new(FnvHashMap()), crate_metadata: RefCell::new(Vec::new()), - delayed_span_bug: RefCell::new(None), features: RefCell::new(feature_gate::Features::new()), recursion_limit: Cell::new(64), - can_print_warnings: can_print_warnings, next_node_id: Cell::new(1), injected_allocator: Cell::new(None), available_macros: RefCell::new(HashSet::new()), @@ -489,13 +447,6 @@ pub fn build_session_(sopts: config::Options, sess } -// Seems out of place, but it uses session, so I'm putting it here -pub fn expect(sess: &Session, opt: Option, msg: M) -> T where - M: FnOnce() -> String, -{ - errors::expect(sess.diagnostic(), opt, msg) -} - pub fn early_error(color: errors::ColorConfig, msg: &str) -> ! { let mut emitter = BasicEmitter::stderr(color); emitter.emit(None, msg, None, errors::Level::Fatal); diff --git a/src/librustc_trans/back/write.rs b/src/librustc_trans/back/write.rs index 26813d89915..dc20b99f9a6 100644 --- a/src/librustc_trans/back/write.rs +++ b/src/librustc_trans/back/write.rs @@ -862,7 +862,7 @@ fn run_work_multithreaded(sess: &Session, futures.push(rx); thread::Builder::new().name(format!("codegen-{}", i)).spawn(move || { - let diag_handler = Handler::with_emitter(true, box diag_emitter); + let diag_handler = Handler::with_emitter(true, false, box diag_emitter); // Must construct cgcx inside the proc because it has non-Send // fields. diff --git a/src/librustc_trans/trans/callee.rs b/src/librustc_trans/trans/callee.rs index a22c12588e5..cf82a3b72d5 100644 --- a/src/librustc_trans/trans/callee.rs +++ b/src/librustc_trans/trans/callee.rs @@ -20,7 +20,6 @@ pub use self::CallArgs::*; use arena::TypedArena; use back::link; -use session; use llvm::{self, ValueRef, get_params}; use middle::cstore::LOCAL_CRATE; use middle::def; @@ -57,6 +56,7 @@ use rustc_front::hir; use syntax::abi as synabi; use syntax::ast; +use syntax::errors; use syntax::ptr::P; #[derive(Copy, Clone)] @@ -412,8 +412,8 @@ pub fn trans_fn_ref_with_substs<'a, 'tcx>( Some(n) => n, None => { return false; } }; - let map_node = session::expect( - &tcx.sess, + let map_node = errors::expect( + &tcx.sess.diagnostic(), tcx.map.find(node_id), || "local item should be in ast map".to_string()); diff --git a/src/librustc_trans/trans/monomorphize.rs b/src/librustc_trans/trans/monomorphize.rs index 9c1fcaff7c8..4b6a0d1a509 100644 --- a/src/librustc_trans/trans/monomorphize.rs +++ b/src/librustc_trans/trans/monomorphize.rs @@ -9,7 +9,6 @@ // except according to those terms. use back::link::exported_name; -use session; use llvm::ValueRef; use llvm; use middle::def_id::DefId; @@ -32,6 +31,7 @@ use rustc_front::hir; use syntax::abi; use syntax::ast; use syntax::attr; +use syntax::errors; use std::hash::{Hasher, Hash, SipHasher}; pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, @@ -83,8 +83,8 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, hash_id); - let map_node = session::expect( - ccx.sess(), + let map_node = errors::expect( + ccx.sess().diagnostic(), ccx.tcx().map.find(fn_node_id), || { format!("while monomorphizing {:?}, couldn't find it in \ diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index f74e9023ed1..ed87961e7f3 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -49,7 +49,7 @@ pub struct ParseSess { impl ParseSess { pub fn new() -> ParseSess { let cm = Rc::new(CodeMap::new()); - let handler = Handler::new(ColorConfig::Auto, None, true, cm.clone()); + let handler = Handler::new(ColorConfig::Auto, None, true, false, cm.clone()); ParseSess::with_span_handler(handler, cm) } -- cgit 1.4.1-3-g733a5 From ff0c74f7d47f5261ebda7cb3b9a637e0cfc69104 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Tue, 15 Dec 2015 16:51:13 +1300 Subject: test errors --- src/librustc/session/config.rs | 6 +- src/librustc_driver/test.rs | 30 +++------ src/librustdoc/test.rs | 4 +- src/libsyntax/errors/emitter.rs | 61 +++++++++++++++++- src/libsyntax/errors/mod.rs | 58 ------------------ src/libsyntax/parse/lexer/mod.rs | 78 ++++++++++++++++-------- src/libsyntax_ext/lib.rs | 2 +- src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs | 2 +- src/test/run-pass-fulldeps/compiler-calls.rs | 4 +- 9 files changed, 131 insertions(+), 114 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 75761dbc15a..e33fe9570c0 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -1224,7 +1224,7 @@ mod tests { let sessopts = build_session_options(&matches); let sess = build_session(sessopts, None, registry, Rc::new(DummyCrateStore)); - assert!(!sess.can_print_warnings); + assert!(!sess.diagnostic().can_emit_warnings); } { @@ -1236,7 +1236,7 @@ mod tests { let sessopts = build_session_options(&matches); let sess = build_session(sessopts, None, registry, Rc::new(DummyCrateStore)); - assert!(sess.can_print_warnings); + assert!(sess.diagnostic().can_emit_warnings); } { @@ -1247,7 +1247,7 @@ mod tests { let sessopts = build_session_options(&matches); let sess = build_session(sessopts, None, registry, Rc::new(DummyCrateStore)); - assert!(sess.can_print_warnings); + assert!(sess.diagnostic().can_emit_warnings); } } } diff --git a/src/librustc_driver/test.rs b/src/librustc_driver/test.rs index 2fb23c943c7..df9294a9d5b 100644 --- a/src/librustc_driver/test.rs +++ b/src/librustc_driver/test.rs @@ -10,8 +10,6 @@ //! # Standalone Tests for the Inference Module -use diagnostic; -use diagnostic::Emitter; use driver; use rustc_lint; use rustc_resolve as resolve; @@ -34,9 +32,10 @@ use rustc::front::map as hir_map; use rustc::session::{self, config}; use std::rc::Rc; use syntax::{abi, ast}; -use syntax::codemap; use syntax::codemap::{Span, CodeMap, DUMMY_SP}; -use syntax::diagnostic::{Level, RenderSpan, Bug, Fatal, Error, Warning, Note, Help}; +use syntax::errors; +use syntax::errors::emitter::Emitter; +use syntax::errors::{Level, RenderSpan}; use syntax::parse::token; use syntax::feature_gate::UnstableFeatures; @@ -60,8 +59,8 @@ struct ExpectErrorEmitter { fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) { match lvl { - Bug | Fatal | Error => {} - Warning | Note | Help => { + Level::Bug | Level::Fatal | Level::Error => {} + Level::Warning | Level::Note | Level::Help => { return; } } @@ -79,14 +78,14 @@ fn remove_message(e: &mut ExpectErrorEmitter, msg: &str, lvl: Level) { impl Emitter for ExpectErrorEmitter { fn emit(&mut self, - _cmsp: Option<(&codemap::CodeMap, Span)>, + _sp: Option, msg: &str, _: Option<&str>, lvl: Level) { remove_message(self, msg, lvl); } - fn custom_emit(&mut self, _cm: &codemap::CodeMap, _sp: RenderSpan, msg: &str, lvl: Level) { + fn custom_emit(&mut self, _sp: RenderSpan, msg: &str, lvl: Level) { remove_message(self, msg, lvl); } } @@ -105,13 +104,11 @@ fn test_env(source_string: &str, let mut options = config::basic_options(); options.debugging_opts.verbose = true; options.unstable_features = UnstableFeatures::Allow; - let codemap = CodeMap::new(); - let diagnostic_handler = diagnostic::Handler::with_emitter(true, emitter); - let span_diagnostic_handler = diagnostic::SpanHandler::new(diagnostic_handler, codemap); + let diagnostic_handler = errors::Handler::with_emitter(true, false, emitter); let cstore = Rc::new(CStore::new(token::get_ident_interner())); - let sess = session::build_session_(options, None, span_diagnostic_handler, - cstore.clone()); + let sess = session::build_session_(options, None, diagnostic_handler, + Rc::new(CodeMap::new()), cstore.clone()); rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess)); let krate_config = Vec::new(); let input = config::Input::Str(source_string.to_string()); @@ -366,13 +363,6 @@ impl<'a, 'tcx> Env<'a, 'tcx> { self.infcx.glb(true, trace) } - pub fn make_lub_ty(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> Ty<'tcx> { - match self.lub().relate(&t1, &t2) { - Ok(t) => t, - Err(ref e) => panic!("unexpected error computing LUB: {}", e), - } - } - /// Checks that `t1 <: t2` is true (this may register additional /// region checks). pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) { diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 4ac20ba001b..fde8299d2d2 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -225,7 +225,9 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec, libs: SearchPaths, } let data = Arc::new(Mutex::new(Vec::new())); let codemap = Rc::new(CodeMap::new()); - let emitter = errors::emitter::EmitterWriter::new(box Sink(data.clone()), None, codemap.clone()); + let emitter = errors::emitter::EmitterWriter::new(box Sink(data.clone()), + None, + codemap.clone()); let old = io::set_panic(box Sink(data.clone())); let _bomb = Bomb(data, old.unwrap_or(box io::stdout())); diff --git a/src/libsyntax/errors/emitter.rs b/src/libsyntax/errors/emitter.rs index e65eab58d9a..7fef85a833e 100644 --- a/src/libsyntax/errors/emitter.rs +++ b/src/libsyntax/errors/emitter.rs @@ -45,7 +45,7 @@ impl ColorConfig { ColorConfig::Always => true, ColorConfig::Never => false, ColorConfig::Auto => stderr_isatty(), - } + } } } @@ -619,3 +619,62 @@ impl Write for Destination { } } + +#[cfg(test)] +mod test { + use errors::Level; + use super::EmitterWriter; + use codemap::{mk_sp, CodeMap}; + use std::sync::{Arc, Mutex}; + use std::io::{self, Write}; + use std::str::from_utf8; + use std::rc::Rc; + + // Diagnostic doesn't align properly in span where line number increases by one digit + #[test] + fn test_hilight_suggestion_issue_11715() { + struct Sink(Arc>>); + impl Write for Sink { + fn write(&mut self, data: &[u8]) -> io::Result { + Write::write(&mut *self.0.lock().unwrap(), data) + } + fn flush(&mut self) -> io::Result<()> { Ok(()) } + } + let data = Arc::new(Mutex::new(Vec::new())); + let cm = Rc::new(CodeMap::new()); + let mut ew = EmitterWriter::new(Box::new(Sink(data.clone())), None, cm.clone()); + let content = "abcdefg + koksi + line3 + line4 + cinq + line6 + line7 + line8 + line9 + line10 + e-lä-vän + tolv + dreizehn + "; + let file = cm.new_filemap_and_lines("dummy.txt", content); + let start = file.lines.borrow()[7]; + let end = file.lines.borrow()[11]; + let sp = mk_sp(start, end); + let lvl = Level::Error; + println!("span_to_lines"); + let lines = cm.span_to_lines(sp); + println!("highlight_lines"); + ew.highlight_lines(sp, lvl, lines).unwrap(); + println!("done"); + let vec = data.lock().unwrap().clone(); + let vec: &[u8] = &vec; + let str = from_utf8(vec).unwrap(); + println!("{}", str); + assert_eq!(str, "dummy.txt: 8 line8\n\ + dummy.txt: 9 line9\n\ + dummy.txt:10 line10\n\ + dummy.txt:11 e-lä-vän\n\ + dummy.txt:12 tolv\n"); + } +} diff --git a/src/libsyntax/errors/mod.rs b/src/libsyntax/errors/mod.rs index 920fd2fdb00..f2e61090ba2 100644 --- a/src/libsyntax/errors/mod.rs +++ b/src/libsyntax/errors/mod.rs @@ -336,61 +336,3 @@ pub fn expect(diag: &Handler, opt: Option, msg: M) -> T where None => diag.bug(&msg()), } } - -#[cfg(test)] -mod test { - use super::Level; - use emitter::EmitterWriter; - use codemap::{mk_sp, CodeMap}; - use std::sync::{Arc, Mutex}; - use std::io::{self, Write}; - use std::str::from_utf8; - - // Diagnostic doesn't align properly in span where line number increases by one digit - #[test] - fn test_hilight_suggestion_issue_11715() { - struct Sink(Arc>>); - impl Write for Sink { - fn write(&mut self, data: &[u8]) -> io::Result { - Write::write(&mut *self.0.lock().unwrap(), data) - } - fn flush(&mut self) -> io::Result<()> { Ok(()) } - } - let data = Arc::new(Mutex::new(Vec::new())); - let mut ew = EmitterWriter::new(Box::new(Sink(data.clone())), None); - let cm = CodeMap::new(); - let content = "abcdefg - koksi - line3 - line4 - cinq - line6 - line7 - line8 - line9 - line10 - e-lä-vän - tolv - dreizehn - "; - let file = cm.new_filemap_and_lines("dummy.txt", content); - let start = file.lines.borrow()[7]; - let end = file.lines.borrow()[11]; - let sp = mk_sp(start, end); - let lvl = Level::Error; - println!("span_to_lines"); - let lines = cm.span_to_lines(sp); - println!("highlight_lines"); - ew.highlight_lines(&cm, sp, lvl, lines).unwrap(); - println!("done"); - let vec = data.lock().unwrap().clone(); - let vec: &[u8] = &vec; - let str = from_utf8(vec).unwrap(); - println!("{}", str); - assert_eq!(str, "dummy.txt: 8 line8\n\ - dummy.txt: 9 line9\n\ - dummy.txt:10 line10\n\ - dummy.txt:11 e-lä-vän\n\ - dummy.txt:12 tolv\n"); - } -} diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index 570e0882a85..4619410ada7 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -1422,28 +1422,30 @@ mod tests { use super::*; use codemap::{BytePos, CodeMap, Span, NO_EXPANSION}; - use diagnostic; + use errors; use parse::token; use parse::token::{str_to_ident}; use std::io; + use std::rc::Rc; - fn mk_sh() -> diagnostic::Handler { + fn mk_sh(cm: Rc) -> errors::Handler { // FIXME (#22405): Replace `Box::new` with `box` here when/if possible. - let emitter = diagnostic::EmitterWriter::new(Box::new(io::sink()), None); - let handler = diagnostic::Handler::with_emitter(true, Box::new(emitter)); - diagnostic::Handler::new(handler, CodeMap::new()) + let emitter = errors::emitter::EmitterWriter::new(Box::new(io::sink()), None, cm); + errors::Handler::with_emitter(true, false, Box::new(emitter)) } // open a string reader for the given string - fn setup<'a>(span_handler: &'a diagnostic::Handler, + fn setup<'a>(cm: &CodeMap, + span_handler: &'a errors::Handler, teststr: String) -> StringReader<'a> { - let fm = span_handler.cm.new_filemap("zebra.rs".to_string(), teststr); + let fm = cm.new_filemap("zebra.rs".to_string(), teststr); StringReader::new(span_handler, fm) } #[test] fn t1 () { - let span_handler = mk_sh(); - let mut string_reader = setup(&span_handler, + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + let mut string_reader = setup(&cm, &sh, "/* my source file */ \ fn main() { println!(\"zebra\"); }\n".to_string()); let id = str_to_ident("fn"); @@ -1481,21 +1483,27 @@ mod tests { } #[test] fn doublecolonparsing () { - check_tokenization(setup(&mk_sh(), "a b".to_string()), + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + check_tokenization(setup(&cm, &sh, "a b".to_string()), vec![mk_ident("a", token::Plain), token::Whitespace, mk_ident("b", token::Plain)]); } #[test] fn dcparsing_2 () { - check_tokenization(setup(&mk_sh(), "a::b".to_string()), + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + check_tokenization(setup(&cm, &sh, "a::b".to_string()), vec![mk_ident("a",token::ModName), token::ModSep, mk_ident("b", token::Plain)]); } #[test] fn dcparsing_3 () { - check_tokenization(setup(&mk_sh(), "a ::b".to_string()), + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + check_tokenization(setup(&cm, &sh, "a ::b".to_string()), vec![mk_ident("a", token::Plain), token::Whitespace, token::ModSep, @@ -1503,7 +1511,9 @@ mod tests { } #[test] fn dcparsing_4 () { - check_tokenization(setup(&mk_sh(), "a:: b".to_string()), + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + check_tokenization(setup(&cm, &sh, "a:: b".to_string()), vec![mk_ident("a",token::ModName), token::ModSep, token::Whitespace, @@ -1511,40 +1521,52 @@ mod tests { } #[test] fn character_a() { - assert_eq!(setup(&mk_sh(), "'a'".to_string()).next_token().tok, + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + assert_eq!(setup(&cm, &sh, "'a'".to_string()).next_token().tok, token::Literal(token::Char(token::intern("a")), None)); } #[test] fn character_space() { - assert_eq!(setup(&mk_sh(), "' '".to_string()).next_token().tok, + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + assert_eq!(setup(&cm, &sh, "' '".to_string()).next_token().tok, token::Literal(token::Char(token::intern(" ")), None)); } #[test] fn character_escaped() { - assert_eq!(setup(&mk_sh(), "'\\n'".to_string()).next_token().tok, + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + assert_eq!(setup(&cm, &sh, "'\\n'".to_string()).next_token().tok, token::Literal(token::Char(token::intern("\\n")), None)); } #[test] fn lifetime_name() { - assert_eq!(setup(&mk_sh(), "'abc".to_string()).next_token().tok, + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + assert_eq!(setup(&cm, &sh, "'abc".to_string()).next_token().tok, token::Lifetime(token::str_to_ident("'abc"))); } #[test] fn raw_string() { - assert_eq!(setup(&mk_sh(), + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + assert_eq!(setup(&cm, &sh, "r###\"\"#a\\b\x00c\"\"###".to_string()).next_token() .tok, token::Literal(token::StrRaw(token::intern("\"#a\\b\x00c\""), 3), None)); } #[test] fn literal_suffixes() { + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); macro_rules! test { ($input: expr, $tok_type: ident, $tok_contents: expr) => {{ - assert_eq!(setup(&mk_sh(), format!("{}suffix", $input)).next_token().tok, + assert_eq!(setup(&cm, &sh, format!("{}suffix", $input)).next_token().tok, token::Literal(token::$tok_type(token::intern($tok_contents)), Some(token::intern("suffix")))); // with a whitespace separator: - assert_eq!(setup(&mk_sh(), format!("{} suffix", $input)).next_token().tok, + assert_eq!(setup(&cm, &sh, format!("{} suffix", $input)).next_token().tok, token::Literal(token::$tok_type(token::intern($tok_contents)), None)); }} @@ -1560,13 +1582,13 @@ mod tests { test!("1.0", Float, "1.0"); test!("1.0e10", Float, "1.0e10"); - assert_eq!(setup(&mk_sh(), "2us".to_string()).next_token().tok, + assert_eq!(setup(&cm, &sh, "2us".to_string()).next_token().tok, token::Literal(token::Integer(token::intern("2")), Some(token::intern("us")))); - assert_eq!(setup(&mk_sh(), "r###\"raw\"###suffix".to_string()).next_token().tok, + assert_eq!(setup(&cm, &sh, "r###\"raw\"###suffix".to_string()).next_token().tok, token::Literal(token::StrRaw(token::intern("raw"), 3), Some(token::intern("suffix")))); - assert_eq!(setup(&mk_sh(), "br###\"raw\"###suffix".to_string()).next_token().tok, + assert_eq!(setup(&cm, &sh, "br###\"raw\"###suffix".to_string()).next_token().tok, token::Literal(token::ByteStrRaw(token::intern("raw"), 3), Some(token::intern("suffix")))); } @@ -1578,8 +1600,9 @@ mod tests { } #[test] fn nested_block_comments() { - let sh = mk_sh(); - let mut lexer = setup(&sh, "/* /* */ */'a'".to_string()); + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + let mut lexer = setup(&cm, &sh, "/* /* */ */'a'".to_string()); match lexer.next_token().tok { token::Comment => { }, _ => panic!("expected a comment!") @@ -1588,8 +1611,9 @@ mod tests { } #[test] fn crlf_comments() { - let sh = mk_sh(); - let mut lexer = setup(&sh, "// test\r\n/// test\r\n".to_string()); + let cm = Rc::new(CodeMap::new()); + let sh = mk_sh(cm.clone()); + let mut lexer = setup(&cm, &sh, "// test\r\n/// test\r\n".to_string()); let comment = lexer.next_token(); assert_eq!(comment.tok, token::Comment); assert_eq!(comment.sp, ::codemap::mk_sp(BytePos(0), BytePos(7))); diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index f4dd621c97e..01dc9662588 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -39,7 +39,7 @@ use syntax::parse::token::intern; macro_rules! panictry { ($e:expr) => ({ use std::result::Result::{Ok, Err}; - use syntax::diagnostic::FatalError; + use syntax::errors::FatalError; match $e { Ok(e) => e, Err(FatalError) => panic!(FatalError) diff --git a/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs b/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs index e40abe05023..7c1a45d020b 100644 --- a/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs +++ b/src/test/run-pass-fulldeps/ast_stmt_expr_attr.rs @@ -44,7 +44,7 @@ fn with_error_checking_parse(s: String, f: F) -> PResult where let mut p = string_to_parser(&ps, s); let x = f(&mut p); - if ps.span_diagnostic.handler().has_errors() || p.token != token::Eof { + if ps.span_diagnostic.has_errors() || p.token != token::Eof { return Err(p.fatal("parse error")); } diff --git a/src/test/run-pass-fulldeps/compiler-calls.rs b/src/test/run-pass-fulldeps/compiler-calls.rs index 8850f6e6d2a..e3eeeb86356 100644 --- a/src/test/run-pass-fulldeps/compiler-calls.rs +++ b/src/test/run-pass-fulldeps/compiler-calls.rs @@ -23,7 +23,7 @@ extern crate syntax; use rustc::session::Session; use rustc::session::config::{self, Input}; use rustc_driver::{driver, CompilerCalls, Compilation}; -use syntax::{diagnostics, diagnostic}; +use syntax::{diagnostics, errors}; use std::path::PathBuf; @@ -35,7 +35,7 @@ impl<'a> CompilerCalls<'a> for TestCalls { fn early_callback(&mut self, _: &getopts::Matches, _: &diagnostics::registry::Registry, - _: diagnostic::ColorConfig) + _: errors::emitter::ColorConfig) -> Compilation { self.count *= 2; Compilation::Continue -- cgit 1.4.1-3-g733a5