Skip to content

Commit f36ae45

Browse files
authored
Merge pull request #205 from dlrobertson/use_failure
Use the failure crate instead of error_chain
2 parents 013544c + 8f39aa6 commit f36ae45

File tree

18 files changed

+291
-256
lines changed

18 files changed

+291
-256
lines changed

Cargo.lock

Lines changed: 30 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ edition = "2018"
1212
[dependencies]
1313
diff = "0.1.11"
1414
docopt = "1.0.0"
15-
error-chain = "0.12.0"
15+
failure = "0.1"
1616
itertools = "0.7.8"
1717
lalrpop-intern = "0.15.1"
1818
petgraph = "0.4.13"

chalk-ir/src/fold/subst.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use super::*;
22
use crate::fold::shift::Shift;
3-
use crate::*;
43

54
pub struct Subst<'s> {
65
/// Values to substitute. A reference to a free variable with

chalk-parse/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ version = "0.16"
1818
version = "0.16"
1919

2020
[dependencies]
21-
error-chain = "0.12.0"
21+
failure = "0.1"
2222
lalrpop-intern = "0.15.1"
2323
regex = "1.0.5"

chalk-parse/src/ast.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ pub struct Identifier {
214214
pub span: Span,
215215
}
216216

217+
impl fmt::Display for Identifier {
218+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
219+
write!(f, "{}", self.str)
220+
}
221+
}
222+
217223
#[derive(Clone, PartialEq, Eq, Debug)]
218224
pub enum WhereClause {
219225
Implemented { trait_ref: TraitRef },

chalk-parse/src/errors.rs

Lines changed: 0 additions & 1 deletion
This file was deleted.

chalk-parse/src/lib.rs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
#![recursion_limit = "1024"]
22

33
#[macro_use]
4-
extern crate error_chain;
4+
extern crate failure;
55

66
#[macro_use]
77
extern crate lalrpop_util;
88
extern crate lalrpop_intern;
99

1010
pub mod ast;
11-
pub mod errors;
1211
#[rustfmt::skip]
1312
lalrpop_mod!(pub parser);
1413

15-
use crate::errors::Result;
14+
use failure::Fallible;
1615
use lalrpop_util::ParseError;
1716
use std::fmt::Write;
1817

19-
pub fn parse_program(text: &str) -> Result<ast::Program> {
18+
pub fn parse_program(text: &str) -> Fallible<ast::Program> {
2019
match parser::ProgramParser::new().parse(text) {
2120
Ok(v) => Ok(v),
22-
Err(e) => bail!("parse error: {:?}", e),
21+
Err(e) => Err(format_err!("parse error: {:?}", e)),
2322
}
2423
}
2524

26-
pub fn parse_ty(text: &str) -> Result<ast::Ty> {
25+
pub fn parse_ty(text: &str) -> Fallible<ast::Ty> {
2726
match parser::TyParser::new().parse(text) {
2827
Ok(v) => Ok(v),
29-
Err(e) => bail!("error parsing `{}`: {:?}", text, e),
28+
Err(e) => Err(format_err!("error parsing `{}`: {:?}", text, e)),
3029
}
3130
}
3231

33-
pub fn parse_goal(text: &str) -> Result<Box<ast::Goal>> {
32+
pub fn parse_goal(text: &str) -> Fallible<Box<ast::Goal>> {
3433
match parser::GoalParser::new().parse(text) {
3534
Ok(v) => Ok(v),
3635
Err(e) => {
@@ -44,20 +43,28 @@ pub fn parse_goal(text: &str) -> Result<Box<ast::Goal>> {
4443
output
4544
};
4645
match e {
47-
ParseError::InvalidToken { location } => bail!(
46+
ParseError::InvalidToken { location } => Err(format_err!(
4847
"parse error: {:?}\n{}",
4948
e,
5049
position_string(location, location + 1)
51-
),
50+
)),
5251
ParseError::UnrecognizedToken {
5352
token: Some((start, _, end)),
5453
..
55-
} => bail!("parse error: {:?}\n{}", e, position_string(start, end)),
54+
} => Err(format_err!(
55+
"parse error: {:?}\n{}",
56+
e,
57+
position_string(start, end)
58+
)),
5659
ParseError::ExtraToken {
5760
token: (start, _, end),
5861
..
59-
} => bail!("parse error: {:?}\n{}", e, position_string(start, end)),
60-
_ => bail!("parse error: {:?}", e),
62+
} => Err(format_err!(
63+
"parse error: {:?}\n{}",
64+
e,
65+
position_string(start, end)
66+
)),
67+
_ => Err(format_err!("parse error: {:?}", e)),
6168
}
6269
}
6370
}

src/bin/chalki.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern crate rustyline;
1010
extern crate serde_derive;
1111

1212
#[macro_use]
13-
extern crate error_chain;
13+
extern crate failure;
1414

1515
use std::fs::File;
1616
use std::io::Read;
@@ -25,6 +25,7 @@ use chalk_engine::fallible::NoSolution;
2525
use chalk_solve::ext::*;
2626
use chalk_solve::solve::SolverChoice;
2727
use docopt::Docopt;
28+
use failure::Fallible;
2829
use rustyline::error::ReadlineError;
2930

3031
const USAGE: &'static str = "
@@ -51,18 +52,6 @@ struct Args {
5152
flag_no_cache: bool,
5253
}
5354

54-
error_chain! {
55-
links {
56-
Parse(chalk_parse::errors::Error, chalk_parse::errors::ErrorKind);
57-
Chalk(chalk::errors::Error, chalk::errors::ErrorKind);
58-
}
59-
60-
foreign_links {
61-
Io(::std::io::Error);
62-
Rustyline(ReadlineError);
63-
}
64-
}
65-
6655
/// A loaded and parsed program.
6756
struct Program {
6857
text: String,
@@ -75,7 +64,7 @@ impl Program {
7564
/// a [`SolverChoice`].
7665
///
7766
/// [`SolverChoice`]: struct.solve.SolverChoice.html
78-
fn new(text: String, solver_choice: SolverChoice) -> Result<Program> {
67+
fn new(text: String, solver_choice: SolverChoice) -> Fallible<Program> {
7968
ChalkDatabase::with_program(Arc::new(text.clone()), solver_choice, |db| {
8069
let ir = db.checked_program().unwrap();
8170
let env = db.environment().unwrap();
@@ -84,9 +73,7 @@ impl Program {
8473
}
8574
}
8675

87-
quick_main!(run);
88-
89-
fn run() -> Result<()> {
76+
fn run() -> Fallible<()> {
9077
// Parse the command line arguments.
9178
let args: &Args = &Docopt::new(USAGE)
9279
.and_then(|d| d.deserialize())
@@ -121,12 +108,13 @@ fn run() -> Result<()> {
121108
// Check that a program was provided.
122109
// TODO: It's customary to print Usage info when an error like this
123110
// happens.
124-
let prog =
125-
prog.ok_or("error: cannot eval without a program; use `--program` to specify one.")?;
111+
let prog = prog.ok_or(format_err!(
112+
"error: cannot eval without a program; use `--program` to specify one."
113+
))?;
126114

127115
// Evaluate the goal(s). If any goal returns an error, print the error
128116
// and exit.
129-
chalk_ir::tls::set_current_program(&prog.ir, || -> Result<()> {
117+
chalk_ir::tls::set_current_program(&prog.ir, || -> Fallible<()> {
130118
for g in &args.flag_goal {
131119
if let Err(e) = goal(&args, g, &prog) {
132120
eprintln!("error: {}", e);
@@ -145,7 +133,7 @@ fn run() -> Result<()> {
145133
///
146134
/// The loop terminates (and the program ends) when EOF is reached or if an error
147135
/// occurs while reading the next line.
148-
fn readline_loop<F>(rl: &mut rustyline::Editor<()>, prompt: &str, mut f: F) -> Result<()>
136+
fn readline_loop<F>(rl: &mut rustyline::Editor<()>, prompt: &str, mut f: F) -> Fallible<()>
149137
where
150138
F: FnMut(&mut rustyline::Editor<()>, &str),
151139
{
@@ -179,7 +167,7 @@ fn process(
179167
command: &str,
180168
rl: &mut rustyline::Editor<()>,
181169
prog: &mut Option<Program>,
182-
) -> Result<()> {
170+
) -> Fallible<()> {
183171
if command == "help" || command == "h" {
184172
// Print out interpreter commands.
185173
// TODO: Implement "help <command>" for more specific help.
@@ -200,12 +188,12 @@ fn process(
200188
// The command is either "print", "lowered", or a goal.
201189

202190
// Check that a program has been loaded.
203-
let prog = prog
204-
.as_ref()
205-
.ok_or("no program currently loaded; type 'help' to see available commands")?;
191+
let prog = prog.as_ref().ok_or(format_err!(
192+
"no program currently loaded; type 'help' to see available commands"
193+
))?;
206194

207195
// Attempt to parse the program.
208-
chalk_ir::tls::set_current_program(&prog.ir, || -> Result<()> {
196+
chalk_ir::tls::set_current_program(&prog.ir, || -> Fallible<()> {
209197
match command {
210198
// Print out the loaded program.
211199
"print" => println!("{}", prog.text),
@@ -228,7 +216,7 @@ fn process(
228216
/// Load the file into a string, and parse it.
229217
// TODO: Could we pass in an Options struct or something? The Args struct
230218
// still has Strings where it should have Enums... (e.g. solver_choice)
231-
fn load_program(args: &Args, filename: &str) -> Result<Program> {
219+
fn load_program(args: &Args, filename: &str) -> Fallible<Program> {
232220
let mut text = String::new();
233221
File::open(filename)?.read_to_string(&mut text)?;
234222
Ok(Program::new(text, args.solver_choice())?)
@@ -248,8 +236,8 @@ fn help() {
248236
}
249237

250238
/// Read a program from the command-line. Stop reading when EOF is read. If
251-
/// an error occurs while reading, a Result::Err is returned.
252-
fn read_program(rl: &mut rustyline::Editor<()>) -> Result<String> {
239+
/// an error occurs while reading, a `Err` is returned.
240+
fn read_program(rl: &mut rustyline::Editor<()>) -> Fallible<String> {
253241
println!("Enter a program; press Ctrl-D when finished");
254242
let mut text = String::new();
255243
readline_loop(rl, "| ", |_, line| {
@@ -262,7 +250,7 @@ fn read_program(rl: &mut rustyline::Editor<()>) -> Result<String> {
262250
/// Parse a goal and attempt to solve it, using the specified solver.
263251
// TODO: Could we pass in an Options struct or something? The Args struct
264252
// still has Strings where it should have Enums... (e.g. solver_choice)
265-
fn goal(args: &Args, text: &str, prog: &Program) -> Result<()> {
253+
fn goal(args: &Args, text: &str, prog: &Program) -> Fallible<()> {
266254
let goal = chalk_parse::parse_goal(text)?.lower(&*prog.ir)?;
267255
let peeled_goal = goal.into_peeled_goal();
268256
match args
@@ -283,3 +271,15 @@ impl Args {
283271
}
284272
}
285273
}
274+
275+
fn main() {
276+
use ::std::io::Write;
277+
278+
::std::process::exit(match run() {
279+
Ok(_) => 0,
280+
Err(ref e) => {
281+
write!(&mut ::std::io::stderr(), "{}", e).expect("Error writing to stderr");
282+
1
283+
}
284+
});
285+
}

src/coherence.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
use petgraph::prelude::*;
22

3-
use crate::errors::Result;
43
use crate::rust_ir::Program;
54
use chalk_ir::ProgramEnvironment;
6-
use chalk_ir::{self, ItemId};
5+
use chalk_ir::{self, Identifier, ItemId};
76
use chalk_solve::solve::SolverChoice;
7+
use failure::Fallible;
88
use std::sync::Arc;
99

1010
crate mod orphan;
1111
mod solve;
1212
mod test;
1313

14+
#[derive(Fail, Debug)]
15+
pub enum CoherenceError {
16+
#[fail(display = "overlapping impls of trait {:?}", _0)]
17+
OverlappingImpls(Identifier),
18+
#[fail(display = "impl for trait {:?} violates the orphan rules", _0)]
19+
FailedOrphanCheck(Identifier),
20+
}
21+
1422
impl Program {
1523
crate fn record_specialization_priorities(
1624
&mut self,
1725
env: Arc<ProgramEnvironment>,
1826
solver_choice: SolverChoice,
19-
) -> Result<()> {
27+
) -> Fallible<()> {
2028
chalk_ir::tls::set_current_program(&Arc::new(self.clone()), || {
2129
let forest = self.build_specialization_forest(env, solver_choice)?;
2230

@@ -35,7 +43,7 @@ impl Program {
3543
&self,
3644
env: Arc<ProgramEnvironment>,
3745
solver_choice: SolverChoice,
38-
) -> Result<Graph<ItemId, ()>> {
46+
) -> Fallible<Graph<ItemId, ()>> {
3947
// The forest is returned as a graph but built as a GraphMap; this is
4048
// so that we never add multiple nodes with the same ItemId.
4149
let mut forest = DiGraphMap::new();

0 commit comments

Comments
 (0)