From 9aacd144ca3ea97d56abbbfba1e2d119ac8ee0fa Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 22 Aug 2022 20:21:40 +0900 Subject: [PATCH] Integrate CompileError to compiler-core::BaseError --- compiler/codegen/src/error.rs | 2 +- compiler/core/src/error.rs | 28 +++++++++++++++++++++++----- compiler/core/src/lib.rs | 2 +- compiler/parser/src/error.rs | 8 ++++---- compiler/src/lib.rs | 31 ++++++++++++++++++------------- src/shell.rs | 14 +++++++++++--- 6 files changed, 58 insertions(+), 27 deletions(-) diff --git a/compiler/codegen/src/error.rs b/compiler/codegen/src/error.rs index a4ac60bd7..cfc4741cf 100644 --- a/compiler/codegen/src/error.rs +++ b/compiler/codegen/src/error.rs @@ -1,6 +1,6 @@ use std::{error::Error, fmt}; -pub type CodegenError = rustpython_compiler_core::Error; +pub type CodegenError = rustpython_compiler_core::BaseError; #[derive(Debug)] #[non_exhaustive] diff --git a/compiler/core/src/error.rs b/compiler/core/src/error.rs index d2497555e..40d97343a 100644 --- a/compiler/core/src/error.rs +++ b/compiler/core/src/error.rs @@ -3,13 +3,13 @@ use std::fmt::Display; use crate::Location; #[derive(Debug, PartialEq, Eq)] -pub struct Error { +pub struct BaseError { pub error: T, pub location: Location, pub source_path: String, } -impl std::ops::Deref for Error { +impl std::ops::Deref for BaseError { type Target = T; fn deref(&self) -> &Self::Target { @@ -17,7 +17,7 @@ impl std::ops::Deref for Error { } } -impl std::error::Error for Error +impl std::error::Error for BaseError where T: std::fmt::Display + std::fmt::Debug, { @@ -26,7 +26,7 @@ where } } -impl Display for Error +impl Display for BaseError where T: std::fmt::Display, { @@ -35,8 +35,26 @@ where } } -impl Error { +impl BaseError { pub fn error(self) -> T { self.error } + + pub fn from(obj: BaseError) -> Self + where + U: Into, + { + Self { + error: obj.error.into(), + location: obj.location, + source_path: obj.source_path, + } + } + + pub fn into(self) -> BaseError + where + T: Into, + { + BaseError::from(self) + } } diff --git a/compiler/core/src/lib.rs b/compiler/core/src/lib.rs index f38c6d0b3..7360cf77f 100644 --- a/compiler/core/src/lib.rs +++ b/compiler/core/src/lib.rs @@ -7,6 +7,6 @@ mod location; mod mode; pub use bytecode::*; -pub use error::Error; +pub use error::BaseError; pub use location::Location; pub use mode::Mode; diff --git a/compiler/parser/src/error.rs b/compiler/parser/src/error.rs index 257d4c69f..77f2f8f82 100644 --- a/compiler/parser/src/error.rs +++ b/compiler/parser/src/error.rs @@ -118,7 +118,7 @@ impl From for LalrpopError { /// Represents an error during parsing #[derive(Debug, PartialEq)] -pub struct ParseError(rustpython_compiler_core::Error); +pub struct ParseError(rustpython_compiler_core::BaseError); #[derive(Debug, PartialEq)] pub enum ParseErrorType { @@ -134,7 +134,7 @@ pub enum ParseErrorType { Lexical(LexicalErrorType), } -impl From for rustpython_compiler_core::Error { +impl From for rustpython_compiler_core::BaseError { fn from(err: ParseError) -> Self { err.0 } @@ -149,7 +149,7 @@ impl From for ParseErrorType { /// Convert `lalrpop_util::ParseError` to our internal type impl ParseError { fn new(error: ParseErrorType, location: Location, source_path: String) -> Self { - Self(rustpython_compiler_core::Error { + Self(rustpython_compiler_core::BaseError { error, location, source_path, @@ -239,7 +239,7 @@ impl ParseErrorType { } impl std::ops::Deref for ParseError { - type Target = rustpython_compiler_core::Error; + type Target = rustpython_compiler_core::BaseError; fn deref(&self) -> &Self::Target { &self.0 } diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index 933f0d028..afcb1adc7 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs @@ -1,5 +1,5 @@ use rustpython_codegen::{compile, symboltable}; -use rustpython_compiler_core::CodeObject; +use rustpython_compiler_core::{BaseError, CodeObject}; use rustpython_parser::{ ast::{fold::Fold, ConstantOptimizer, Location}, error::ParseErrorType, @@ -18,14 +18,21 @@ pub enum CompileErrorType { Parse(#[from] rustpython_parser::error::ParseErrorType), } +pub type CompileErrorBody = BaseError; + #[derive(Debug, thiserror::Error)] pub struct CompileError { - pub error: CompileErrorType, - pub source_path: String, - pub location: Location, + pub body: CompileErrorBody, pub statement: Option, } +impl std::ops::Deref for CompileError { + type Target = CompileErrorBody; + fn deref(&self) -> &Self::Target { + &self.body + } +} + impl fmt::Display for CompileError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let loc = self.location; @@ -41,20 +48,18 @@ impl fmt::Display for CompileError { impl CompileError { fn from_codegen(error: rustpython_codegen::error::CodegenError, source: &str) -> Self { + let statement = get_statement(source, error.location); Self { - error: error.error.into(), - location: error.location, - source_path: error.source_path, - statement: get_statement(source, error.location), + body: error.into(), + statement, } } fn from_parse(error: rustpython_parser::error::ParseError, source: &str) -> Self { - let error: rustpython_compiler_core::Error = error.into(); + let error: rustpython_compiler_core::BaseError = error.into(); + let statement = get_statement(source, error.location); Self { - error: error.error.into(), - location: error.location, - source_path: error.source_path, - statement: get_statement(source, error.location), + body: error.into(), + statement, } } fn from_symtable( diff --git a/src/shell.rs b/src/shell.rs index 859ced498..955dd33e9 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -4,7 +4,7 @@ use rustpython_parser::error::{LexicalErrorType, ParseErrorType}; use rustpython_vm::readline::{Readline, ReadlineResult}; use rustpython_vm::{ builtins::PyBaseExceptionRef, - compile::{self, CompileError, CompileErrorType}, + compile::{self, CompileError, CompileErrorBody, CompileErrorType}, scope::Scope, AsObject, PyResult, VirtualMachine, }; @@ -22,11 +22,19 @@ fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResul Err(err) => ShellExecResult::PyErr(err), }, Err(CompileError { - error: CompileErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::Eof)), + body: + CompileErrorBody { + error: CompileErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::Eof)), + .. + }, .. }) | Err(CompileError { - error: CompileErrorType::Parse(ParseErrorType::Eof), + body: + CompileErrorBody { + error: CompileErrorType::Parse(ParseErrorType::Eof), + .. + }, .. }) => ShellExecResult::Continue, Err(err) => ShellExecResult::PyErr(vm.new_syntax_error(&err)),