Converts all try! macros to ? syntax.

Fixes #46
This commit is contained in:
rep-nop
2017-02-25 22:12:33 -05:00
committed by Jakob Stoklund Olesen
parent cf5701b137
commit b23f1fb347
22 changed files with 270 additions and 270 deletions

View File

@@ -15,14 +15,14 @@ pub fn run(files: Vec<String>) -> CommandResult {
if i != 0 {
println!("");
}
try!(cat_one(f))
cat_one(f)?
}
Ok(())
}
fn cat_one(filename: String) -> CommandResult {
let buffer = try!(read_to_string(&filename).map_err(|e| format!("{}: {}", filename, e)));
let items = try!(parse_functions(&buffer).map_err(|e| format!("{}: {}", filename, e)));
let buffer = read_to_string(&filename).map_err(|e| format!("{}: {}", filename, e))?;
let items = parse_functions(&buffer).map_err(|e| format!("{}: {}", filename, e))?;
for (idx, func) in items.into_iter().enumerate() {
if idx != 0 {

View File

@@ -39,7 +39,7 @@ impl SubTest for TestLegalizer {
legalize_function(&mut func, isa);
let mut text = String::new();
try!(write_function(&mut text, &func, Some(isa)).map_err(|e| e.to_string()));
write_function(&mut text, &func, Some(isa)).map_err(|e| e.to_string())?;
run_filecheck(&text, context)
}
}

View File

@@ -49,7 +49,7 @@ impl SubTest for TestRegalloc {
comp_ctx.regalloc(isa);
let mut text = String::new();
try!(write_function(&mut text, &comp_ctx.func, Some(isa)).map_err(|e| e.to_string()));
write_function(&mut text, &comp_ctx.func, Some(isa)).map_err(|e| e.to_string())?;
run_filecheck(&text, context)
}
}

View File

@@ -18,14 +18,14 @@ use filetest::subtest::{SubTest, Context, Result};
/// If running this test causes a panic, it will propagate as normal.
pub fn run(path: &Path) -> TestResult {
let started = time::Instant::now();
let buffer = try!(read_to_string(path).map_err(|e| e.to_string()));
let testfile = try!(parse_test(&buffer).map_err(|e| e.to_string()));
let buffer = read_to_string(path).map_err(|e| e.to_string())?;
let testfile = parse_test(&buffer).map_err(|e| e.to_string())?;
if testfile.functions.is_empty() {
return Err("no functions found".to_string());
}
// Parse the test commands.
let mut tests = try!(testfile.commands.iter().map(new_subtest).collect::<Result<Vec<_>>>());
let mut tests = testfile.commands.iter().map(new_subtest).collect::<Result<Vec<_>>>()?;
// Flags to use for those tests that don't need an ISA.
// This is the cumulative effect of all the `set` commands in the file.
@@ -39,7 +39,7 @@ pub fn run(path: &Path) -> TestResult {
tests.sort_by_key(|st| (st.is_mutating(), st.needs_verifier()));
// Expand the tests into (test, flags, isa) tuples.
let mut tuples = try!(test_tuples(&tests, &testfile.isa_spec, flags));
let mut tuples = test_tuples(&tests, &testfile.isa_spec, flags)?;
// Isolate the last test in the hope that this is the only mutating test.
// If so, we can completely avoid cloning functions.
@@ -58,11 +58,11 @@ pub fn run(path: &Path) -> TestResult {
};
for tuple in &tuples {
try!(run_one_test(*tuple, Cow::Borrowed(&func), &mut context));
run_one_test(*tuple, Cow::Borrowed(&func), &mut context)?;
}
// Run the last test with an owned function which means it won't need to clone it before
// mutating.
try!(run_one_test(last_tuple, Cow::Owned(func), &mut context));
run_one_test(last_tuple, Cow::Owned(func), &mut context)?;
}
@@ -108,7 +108,7 @@ fn run_one_test<'a>(tuple: (&'a SubTest, &'a Flags, Option<&'a TargetIsa>),
// Should we run the verifier before this test?
if !context.verified && test.needs_verifier() {
try!(verify_function(&func).map_err(|e| e.to_string()));
verify_function(&func).map_err(|e| e.to_string())?;
context.verified = true;
}

View File

@@ -72,13 +72,13 @@ impl<'a> filecheck::VariableMap for Context<'a> {
/// Run filecheck on `text`, using directives extracted from `context`.
pub fn run_filecheck(text: &str, context: &Context) -> Result<()> {
let checker = try!(build_filechecker(context));
if try!(checker.check(&text, context).map_err(|e| format!("filecheck: {}", e))) {
let checker = build_filechecker(context)?;
if checker.check(&text, context).map_err(|e| format!("filecheck: {}", e))? {
Ok(())
} else {
// Filecheck mismatch. Emit an explanation as output.
let (_, explain) = try!(checker.explain(&text, context)
.map_err(|e| format!("explain: {}", e)));
let (_, explain) = checker.explain(&text, context)
.map_err(|e| format!("explain: {}", e))?;
Err(format!("filecheck failed:\n{}{}", checker, explain))
}
}
@@ -88,10 +88,10 @@ pub fn build_filechecker(context: &Context) -> Result<Checker> {
let mut builder = CheckerBuilder::new();
// Preamble comments apply to all functions.
for comment in context.preamble_comments {
try!(builder.directive(comment.text).map_err(|e| format!("filecheck: {}", e)));
builder.directive(comment.text).map_err(|e| format!("filecheck: {}", e))?;
}
for comment in &context.details.comments {
try!(builder.directive(comment.text).map_err(|e| format!("filecheck: {}", e)));
builder.directive(comment.text).map_err(|e| format!("filecheck: {}", e))?;
}
let checker = builder.finish();
if checker.is_empty() {

View File

@@ -19,7 +19,7 @@ pub fn run(files: Vec<String>) -> CommandResult {
if i != 0 {
println!("");
}
try!(print_cfg(f))
print_cfg(f)?
}
Ok(())
}
@@ -39,37 +39,37 @@ impl<'a> CFGPrinter<'a> {
/// Write the CFG for this function to `w`.
pub fn write(&self, w: &mut Write) -> Result {
try!(self.header(w));
try!(self.ebb_nodes(w));
try!(self.cfg_connections(w));
self.header(w)?;
self.ebb_nodes(w)?;
self.cfg_connections(w)?;
writeln!(w, "}}")
}
fn header(&self, w: &mut Write) -> Result {
try!(writeln!(w, "digraph {} {{", self.func.name));
writeln!(w, "digraph {} {{", self.func.name)?;
if let Some(entry) = self.func.layout.entry_block() {
try!(writeln!(w, " {{rank=min; {}}}", entry));
writeln!(w, " {{rank=min; {}}}", entry)?;
}
Ok(())
}
fn ebb_nodes(&self, w: &mut Write) -> Result {
for ebb in &self.func.layout {
try!(write!(w, " {} [shape=record, label=\"{{{}", ebb, ebb));
write!(w, " {} [shape=record, label=\"{{{}", ebb, ebb)?;
// Add all outgoing branch instructions to the label.
for inst in self.func.layout.ebb_insts(ebb) {
let idata = &self.func.dfg[inst];
match idata.analyze_branch() {
BranchInfo::SingleDest(dest, _) => {
try!(write!(w, " | <{}>{} {}", inst, idata.opcode(), dest))
write!(w, " | <{}>{} {}", inst, idata.opcode(), dest)?
}
BranchInfo::Table(table) => {
try!(write!(w, " | <{}>{} {}", inst, idata.opcode(), table))
write!(w, " | <{}>{} {}", inst, idata.opcode(), table)?
}
BranchInfo::NotABranch => {}
}
}
try!(writeln!(w, "}}\"]"))
writeln!(w, "}}\"]")?
}
Ok(())
}
@@ -77,7 +77,7 @@ impl<'a> CFGPrinter<'a> {
fn cfg_connections(&self, w: &mut Write) -> Result {
for ebb in &self.func.layout {
for &(parent, inst) in self.cfg.get_predecessors(ebb) {
try!(writeln!(w, " {}:{} -> {}", parent, inst, ebb));
writeln!(w, " {}:{} -> {}", parent, inst, ebb)?;
}
}
Ok(())
@@ -91,8 +91,8 @@ impl<'a> Display for CFGPrinter<'a> {
}
fn print_cfg(filename: String) -> CommandResult {
let buffer = try!(read_to_string(&filename).map_err(|e| format!("{}: {}", filename, e)));
let items = try!(parse_functions(&buffer).map_err(|e| format!("{}: {}", filename, e)));
let buffer = read_to_string(&filename).map_err(|e| format!("{}: {}", filename, e))?;
let items = parse_functions(&buffer).map_err(|e| format!("{}: {}", filename, e))?;
for (idx, func) in items.into_iter().enumerate() {
if idx != 0 {

View File

@@ -7,7 +7,7 @@ pub fn run(files: Vec<String>, verbose: bool) -> CommandResult {
if files.is_empty() {
return Err("No check files".to_string());
}
let checker = try!(read_checkfile(&files[0]));
let checker = read_checkfile(&files[0])?;
if checker.is_empty() {
return Err(format!("{}: no filecheck directives found", files[0]));
}
@@ -18,11 +18,11 @@ pub fn run(files: Vec<String>, verbose: bool) -> CommandResult {
}
let mut buffer = String::new();
try!(io::stdin().read_to_string(&mut buffer).map_err(|e| format!("stdin: {}", e)));
io::stdin().read_to_string(&mut buffer).map_err(|e| format!("stdin: {}", e))?;
if verbose {
let (success, explain) = try!(checker.explain(&buffer, NO_VARIABLES)
.map_err(|e| e.to_string()));
let (success, explain) = checker.explain(&buffer, NO_VARIABLES)
.map_err(|e| e.to_string())?;
print!("{}", explain);
if success {
println!("OK");
@@ -30,18 +30,18 @@ pub fn run(files: Vec<String>, verbose: bool) -> CommandResult {
} else {
Err("Check failed".to_string())
}
} else if try!(checker.check(&buffer, NO_VARIABLES).map_err(|e| e.to_string())) {
} else if checker.check(&buffer, NO_VARIABLES).map_err(|e| e.to_string())? {
Ok(())
} else {
let (_, explain) = try!(checker.explain(&buffer, NO_VARIABLES).map_err(|e| e.to_string()));
let (_, explain) = checker.explain(&buffer, NO_VARIABLES).map_err(|e| e.to_string())?;
print!("{}", explain);
Err("Check failed".to_string())
}
}
fn read_checkfile(filename: &str) -> Result<Checker, String> {
let buffer = try!(read_to_string(&filename).map_err(|e| format!("{}: {}", filename, e)));
let buffer = read_to_string(&filename).map_err(|e| format!("{}: {}", filename, e))?;
let mut builder = CheckerBuilder::new();
try!(builder.text(&buffer).map_err(|e| format!("{}: {}", filename, e)));
builder.text(&buffer).map_err(|e| format!("{}: {}", filename, e))?;
Ok(builder.finish())
}

View File

@@ -6,9 +6,9 @@ use std::io::{Result, Read};
/// Read an entire file into a string.
pub fn read_to_string<P: AsRef<Path>>(path: P) -> Result<String> {
let mut file = try!(File::open(path));
let mut file = File::open(path)?;
let mut buffer = String::new();
try!(file.read_to_string(&mut buffer));
file.read_to_string(&mut buffer)?;
Ok(buffer)
}