test errors

This commit is contained in:
Nick Cameron 2015-12-15 16:51:13 +13:00
parent e2371518c4
commit ff0c74f7d4
9 changed files with 131 additions and 114 deletions

View File

@ -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);
}
}
}

View File

@ -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<Span>,
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<F>(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>) {

View File

@ -225,7 +225,9 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, 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()));

View File

@ -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<Mutex<Vec<u8>>>);
impl Write for Sink {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
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--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--vän\n\
dummy.txt:12 tolv\n");
}
}

View File

@ -336,61 +336,3 @@ pub fn expect<T, M>(diag: &Handler, opt: Option<T>, 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<Mutex<Vec<u8>>>);
impl Write for Sink {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
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--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--vän\n\
dummy.txt:12 tolv\n");
}
}

View File

@ -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<CodeMap>) -> 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)));

View File

@ -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)

View File

@ -44,7 +44,7 @@ fn with_error_checking_parse<T, F>(s: String, f: F) -> PResult<T> 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"));
}

View File

@ -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