Rename more Error and Result types.

This commit is contained in:
Dan Gohman
2018-06-12 04:43:02 -07:00
parent 43bd3cb2a3
commit 1b55a2d005
12 changed files with 157 additions and 150 deletions

View File

@@ -36,7 +36,7 @@ def gen_to_and_from_str(ty, values, fmt):
with fmt.indented('impl str::FromStr for {} {{'.format(ty), '}'):
fmt.line('type Err = ();')
with fmt.indented(
'fn from_str(s: &str) -> result::Result<Self, Self::Err> {',
'fn from_str(s: &str) -> Result<Self, Self::Err> {',
'}'):
with fmt.indented('match s {', '}'):
for v in values:

View File

@@ -58,7 +58,7 @@ use isa::enc_tables::Encodings;
use regalloc;
use result::CodegenResult;
use settings;
use settings::CallConv;
use settings::{CallConv, SetResult};
use std::boxed::Box;
use std::fmt;
use target_lexicon::{Architecture, Triple};
@@ -146,11 +146,11 @@ impl Builder {
}
impl settings::Configurable for Builder {
fn set(&mut self, name: &str, value: &str) -> settings::Result<()> {
fn set(&mut self, name: &str, value: &str) -> SetResult<()> {
self.setup.set(name, value)
}
fn enable(&mut self, name: &str) -> settings::Result<()> {
fn enable(&mut self, name: &str) -> SetResult<()> {
self.setup.enable(name)
}
}

View File

@@ -24,7 +24,6 @@ use constant_hash::{probe, simple_hash};
use isa::TargetIsa;
use std::boxed::Box;
use std::fmt;
use std::result;
use std::str;
/// A string-based configurator for settings groups.
@@ -35,12 +34,12 @@ pub trait Configurable {
/// Set the string value of any setting by name.
///
/// This can set any type of setting whether it is numeric, boolean, or enumerated.
fn set(&mut self, name: &str, value: &str) -> Result<()>;
fn set(&mut self, name: &str, value: &str) -> SetResult<()>;
/// Enable a boolean setting or apply a preset.
///
/// If the identified setting isn't a boolean or a preset, a `BadType` error is returned.
fn enable(&mut self, name: &str) -> Result<()>;
fn enable(&mut self, name: &str) -> SetResult<()>;
}
/// Collect settings values based on a template.
@@ -84,9 +83,9 @@ impl Builder {
}
/// Look up a descriptor by name.
fn lookup(&self, name: &str) -> Result<(usize, detail::Detail)> {
fn lookup(&self, name: &str) -> SetResult<(usize, detail::Detail)> {
match probe(self.template, name, simple_hash(name)) {
Err(_) => Err(Error::BadName),
Err(_) => Err(SetError::BadName),
Ok(entry) => {
let d = &self.template.descriptors[self.template.hash_table[entry] as usize];
Ok((d.offset as usize, d.detail))
@@ -95,23 +94,23 @@ impl Builder {
}
}
fn parse_bool_value(value: &str) -> Result<bool> {
fn parse_bool_value(value: &str) -> SetResult<bool> {
match value {
"true" | "on" | "yes" | "1" => Ok(true),
"false" | "off" | "no" | "0" => Ok(false),
_ => Err(Error::BadValue),
_ => Err(SetError::BadValue),
}
}
fn parse_enum_value(value: &str, choices: &[&str]) -> Result<u8> {
fn parse_enum_value(value: &str, choices: &[&str]) -> SetResult<u8> {
match choices.iter().position(|&tag| tag == value) {
Some(idx) => Ok(idx as u8),
None => Err(Error::BadValue),
None => Err(SetError::BadValue),
}
}
impl Configurable for Builder {
fn enable(&mut self, name: &str) -> Result<()> {
fn enable(&mut self, name: &str) -> SetResult<()> {
use self::detail::Detail;
let (offset, detail) = self.lookup(name)?;
match detail {
@@ -123,27 +122,27 @@ impl Configurable for Builder {
self.apply_preset(&self.template.presets[offset..]);
Ok(())
}
_ => Err(Error::BadType),
_ => Err(SetError::BadType),
}
}
fn set(&mut self, name: &str, value: &str) -> Result<()> {
fn set(&mut self, name: &str, value: &str) -> SetResult<()> {
use self::detail::Detail;
let (offset, detail) = self.lookup(name)?;
match detail {
Detail::Bool { bit } => {
// Cannot currently propagate Result<()> up on functions returning ()
// Cannot currently propagate SetResult<()> up on functions returning ()
// with the `?` operator
self.set_bit(offset, bit, parse_bool_value(value)?);
}
Detail::Num => {
self.bytes[offset] = value.parse().map_err(|_| Error::BadValue)?;
self.bytes[offset] = value.parse().map_err(|_| SetError::BadValue)?;
}
Detail::Enum { last, enumerators } => {
self.bytes[offset] =
parse_enum_value(value, self.template.enums(last, enumerators))?;
}
Detail::Preset => return Err(Error::BadName),
Detail::Preset => return Err(SetError::BadName),
}
Ok(())
}
@@ -151,7 +150,7 @@ impl Configurable for Builder {
/// An error produced when changing a setting.
#[derive(Debug, PartialEq, Eq)]
pub enum Error {
pub enum SetError {
/// No setting by this name exists.
BadName,
@@ -163,7 +162,7 @@ pub enum Error {
}
/// A result returned when changing a setting.
pub type Result<T> = result::Result<T, Error>;
pub type SetResult<T> = Result<T, SetError>;
/// A reference to just the boolean predicates of a settings object.
///
@@ -348,7 +347,7 @@ impl<'a> From<&'a TargetIsa> for FlagsOrIsa<'a> {
#[cfg(test)]
mod tests {
use super::Configurable;
use super::Error::*;
use super::SetError::*;
use super::{builder, Flags};
use std::string::ToString;

View File

@@ -6,13 +6,12 @@
use ir::{DataFlowGraph, Ebb, Function, Inst, SigRef, Type, Value, ValueDef};
use isa::{RegInfo, TargetIsa};
use packed_option::ReservedValue;
use std::fmt::{self, Error, Result, Write};
use std::result;
use std::fmt::{self, Write};
use std::string::String;
/// Write `func` to `w` as equivalent text.
/// Use `isa` to emit ISA-dependent annotations.
pub fn write_function(w: &mut Write, func: &Function, isa: Option<&TargetIsa>) -> Result {
pub fn write_function(w: &mut Write, func: &Function, isa: Option<&TargetIsa>) -> fmt::Result {
let regs = isa.map(TargetIsa::register_info);
let regs = regs.as_ref();
@@ -34,7 +33,7 @@ pub fn write_function(w: &mut Write, func: &Function, isa: Option<&TargetIsa>) -
//
// Function spec.
fn write_spec(w: &mut Write, func: &Function, regs: Option<&RegInfo>) -> Result {
fn write_spec(w: &mut Write, func: &Function, regs: Option<&RegInfo>) -> fmt::Result {
write!(w, "{}{}", func.name, func.signature.display(regs))
}
@@ -42,7 +41,7 @@ fn write_preamble(
w: &mut Write,
func: &Function,
regs: Option<&RegInfo>,
) -> result::Result<bool, Error> {
) -> Result<bool, fmt::Error> {
let mut any = false;
for (ss, slot) in func.stack_slots.iter() {
@@ -91,7 +90,12 @@ fn write_preamble(
//
// Basic blocks
pub fn write_arg(w: &mut Write, func: &Function, regs: Option<&RegInfo>, arg: Value) -> Result {
pub fn write_arg(
w: &mut Write,
func: &Function,
regs: Option<&RegInfo>,
arg: Value,
) -> fmt::Result {
write!(w, "{}: {}", arg, func.dfg.value_type(arg))?;
let loc = func.locations[arg];
if loc.is_assigned() {
@@ -107,7 +111,7 @@ pub fn write_ebb_header(
isa: Option<&TargetIsa>,
ebb: Ebb,
indent: usize,
) -> Result {
) -> fmt::Result {
// Write out the basic block header, outdented:
//
// ebb1:
@@ -137,7 +141,7 @@ pub fn write_ebb_header(
writeln!(w, "):")
}
pub fn write_ebb(w: &mut Write, func: &Function, isa: Option<&TargetIsa>, ebb: Ebb) -> Result {
pub fn write_ebb(w: &mut Write, func: &Function, isa: Option<&TargetIsa>, ebb: Ebb) -> fmt::Result {
// Indent all instructions if any encodings are present.
let indent = if func.encodings.is_empty() && func.srclocs.is_empty() {
4
@@ -191,7 +195,7 @@ fn type_suffix(func: &Function, inst: Inst) -> Option<Type> {
}
// Write out any value aliases appearing in `inst`.
fn write_value_aliases(w: &mut Write, func: &Function, inst: Inst, indent: usize) -> Result {
fn write_value_aliases(w: &mut Write, func: &Function, inst: Inst, indent: usize) -> fmt::Result {
for &arg in func.dfg.inst_args(inst) {
let resolved = func.dfg.resolve_aliases(arg);
if resolved != arg {
@@ -207,7 +211,7 @@ fn write_instruction(
isa: Option<&TargetIsa>,
inst: Inst,
indent: usize,
) -> Result {
) -> fmt::Result {
// Value aliases come out on lines before the instruction using them.
write_value_aliases(w, func, inst, indent)?;
@@ -272,7 +276,7 @@ pub fn write_operands(
dfg: &DataFlowGraph,
isa: Option<&TargetIsa>,
inst: Inst,
) -> Result {
) -> fmt::Result {
let pool = &dfg.value_lists;
use ir::instructions::InstructionData::*;
match dfg[inst] {
@@ -472,7 +476,7 @@ pub fn write_operands(
}
/// Write EBB args using optional parantheses.
fn write_ebb_args(w: &mut Write, args: &[Value]) -> Result {
fn write_ebb_args(w: &mut Write, args: &[Value]) -> fmt::Result {
if args.is_empty() {
Ok(())
} else {
@@ -484,7 +488,7 @@ fn write_ebb_args(w: &mut Write, args: &[Value]) -> Result {
struct DisplayValues<'a>(&'a [Value]);
impl<'a> fmt::Display for DisplayValues<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, val) in self.0.iter().enumerate() {
if i == 0 {
write!(f, "{}", val)?;
@@ -499,7 +503,7 @@ impl<'a> fmt::Display for DisplayValues<'a> {
struct DisplayValuesWithDelimiter<'a>(&'a [Value], char);
impl<'a> fmt::Display for DisplayValuesWithDelimiter<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> Result {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for (i, val) in self.0.iter().enumerate() {
if i == 0 {
write!(f, "{}", val)?;