From f6680d63fe390c46c9955a2d66fe6cde3670bdbb Mon Sep 17 00:00:00 2001 From: jac3km4 Date: Sat, 13 Feb 2021 20:06:09 +0000 Subject: [PATCH] Remove reachable panics --- compiler/src/assembler.rs | 6 ++++-- compiler/src/parser.rs | 4 ++-- compiler/src/scope.rs | 2 +- core/src/ast.rs | 2 +- decompiler/src/lib.rs | 2 +- decompiler/src/print.rs | 2 +- 6 files changed, 10 insertions(+), 8 deletions(-) diff --git a/compiler/src/assembler.rs b/compiler/src/assembler.rs index 0b0878e1..ec85a020 100644 --- a/compiler/src/assembler.rs +++ b/compiler/src/assembler.rs @@ -60,7 +60,7 @@ impl Assembler { match scope.resolve_reference(name.clone(), *pos)? { Reference::Local(idx) => self.emit(Instr::Local(idx)), Reference::Parameter(idx) => self.emit(Instr::Param(idx)), - _ => panic!("Shouldn't get here"), + _ => return Err(Error::CompileError("Expected a value".to_owned(), *pos)), }; } Expr::Constant(cons, _) => match cons { @@ -345,7 +345,9 @@ impl Assembler { Expr::This(_) | Expr::Super(_) => { self.emit(Instr::This); } - Expr::Goto(_, _) | Expr::Break => panic!("Not supported yet"), + Expr::Goto(_, pos) | Expr::Break(pos) => { + return Err(Error::CompileError("Not supported yet".to_owned(), *pos)) + } }; Ok(()) } diff --git a/compiler/src/parser.rs b/compiler/src/parser.rs index 66d7ede6..f7dd39ea 100644 --- a/compiler/src/parser.rs +++ b/compiler/src/parser.rs @@ -252,7 +252,7 @@ peg::parser! { / if_: if_() { if_ } / switch: switch() { switch } / pos:position!() keyword("return") _ val:expr()? ";" { Expr::Return(val.map(Box::new), Pos::new(pos)) } - / keyword("break") _ ";" { Expr::Break } + / pos:position!() keyword("break") _ ";" { Expr::Break(Pos::new(pos)) } / let_:let() { let_ } / expr:expr() _ ";" { expr } @@ -406,7 +406,7 @@ mod tests { .unwrap(); assert_eq!( format!("{:?}", stmt), - r#"Switch(Ident(Ident("value"), Pos(7)), [SwitchCase(Constant(String(String, "0"), Pos(37)), Seq { exprs: [] }), SwitchCase(Constant(String(String, "1"), Pos(64)), Seq { exprs: [Call(Ident("Log"), [Constant(String(String, "0 or 1"), Pos(93))], Pos(89))] }), SwitchCase(Constant(String(String, "2"), Pos(126)), Seq { exprs: [Break] })], Some(Seq { exprs: [Call(Ident("Log"), [Constant(String(String, "default"), Pos(208))], Pos(204))] }))"# + r#"Switch(Ident(Ident("value"), Pos(7)), [SwitchCase(Constant(String(String, "0"), Pos(37)), Seq { exprs: [] }), SwitchCase(Constant(String(String, "1"), Pos(64)), Seq { exprs: [Call(Ident("Log"), [Constant(String(String, "0 or 1"), Pos(93))], Pos(89))] }), SwitchCase(Constant(String(String, "2"), Pos(126)), Seq { exprs: [Break(Pos(151))] })], Some(Seq { exprs: [Call(Ident("Log"), [Constant(String(String, "default"), Pos(208))], Pos(204))] }))"# ); } diff --git a/compiler/src/scope.rs b/compiler/src/scope.rs index 404bcf9a..54cebc30 100644 --- a/compiler/src/scope.rs +++ b/compiler/src/scope.rs @@ -571,7 +571,7 @@ impl Scope { Expr::While(_, _, _) => TypeId::Void, Expr::Goto(_, _) => TypeId::Void, Expr::If(_, _, _, _) => TypeId::Void, - Expr::Break => TypeId::Void, + Expr::Break(_) => TypeId::Void, Expr::Return(_, _) => TypeId::Void, Expr::Seq(_) => TypeId::Void, Expr::Switch(_, _, _) => TypeId::Void, diff --git a/core/src/ast.rs b/core/src/ast.rs index 4bc0edf0..d4193f38 100644 --- a/core/src/ast.rs +++ b/core/src/ast.rs @@ -27,7 +27,7 @@ pub enum Expr { UnOp(Box, UnOp, Pos), This(Pos), Super(Pos), - Break, + Break(Pos), Null, } diff --git a/decompiler/src/lib.rs b/decompiler/src/lib.rs index 9cb34446..de0c9ece 100644 --- a/decompiler/src/lib.rs +++ b/decompiler/src/lib.rs @@ -118,7 +118,7 @@ impl<'a> Decompiler<'a> { let mut body = self.consume_path(exit)?; if let Some(Expr::Goto(_, _)) = body.exprs.last() { body.exprs.pop(); - body.exprs.push(Expr::Break); + body.exprs.push(Expr::Break(Pos::ZERO)); } cases.push(SwitchCase(matched, body)); } diff --git a/decompiler/src/print.rs b/decompiler/src/print.rs index 1271e6ec..9995adc3 100644 --- a/decompiler/src/print.rs +++ b/decompiler/src/print.rs @@ -331,7 +331,7 @@ fn write_expr(out: &mut W, expr: &Expr, verbose: bool, depth: usize) - Expr::UnOp(val, op, _) => { write_unop(out, val, *op, verbose)?; } - Expr::Break => write!(out, "break")?, + Expr::Break(_) => write!(out, "break")?, Expr::Null => write!(out, "null")?, Expr::This(_) => write!(out, "this")?, Expr::Super(_) => write!(out, "super")?,