Skip to content

Commit 9aacd14

Browse files
committed
Integrate CompileError to compiler-core::BaseError
1 parent 31f95ee commit 9aacd14

File tree

6 files changed

+58
-27
lines changed

6 files changed

+58
-27
lines changed

compiler/codegen/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{error::Error, fmt};
22

3-
pub type CodegenError = rustpython_compiler_core::Error<CodegenErrorType>;
3+
pub type CodegenError = rustpython_compiler_core::BaseError<CodegenErrorType>;
44

55
#[derive(Debug)]
66
#[non_exhaustive]

compiler/core/src/error.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ use std::fmt::Display;
33
use crate::Location;
44

55
#[derive(Debug, PartialEq, Eq)]
6-
pub struct Error<T> {
6+
pub struct BaseError<T> {
77
pub error: T,
88
pub location: Location,
99
pub source_path: String,
1010
}
1111

12-
impl<T> std::ops::Deref for Error<T> {
12+
impl<T> std::ops::Deref for BaseError<T> {
1313
type Target = T;
1414

1515
fn deref(&self) -> &Self::Target {
1616
&self.error
1717
}
1818
}
1919

20-
impl<T> std::error::Error for Error<T>
20+
impl<T> std::error::Error for BaseError<T>
2121
where
2222
T: std::fmt::Display + std::fmt::Debug,
2323
{
@@ -26,7 +26,7 @@ where
2626
}
2727
}
2828

29-
impl<T> Display for Error<T>
29+
impl<T> Display for BaseError<T>
3030
where
3131
T: std::fmt::Display,
3232
{
@@ -35,8 +35,26 @@ where
3535
}
3636
}
3737

38-
impl<T> Error<T> {
38+
impl<T> BaseError<T> {
3939
pub fn error(self) -> T {
4040
self.error
4141
}
42+
43+
pub fn from<U>(obj: BaseError<U>) -> Self
44+
where
45+
U: Into<T>,
46+
{
47+
Self {
48+
error: obj.error.into(),
49+
location: obj.location,
50+
source_path: obj.source_path,
51+
}
52+
}
53+
54+
pub fn into<U>(self) -> BaseError<U>
55+
where
56+
T: Into<U>,
57+
{
58+
BaseError::from(self)
59+
}
4260
}

compiler/core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ mod location;
77
mod mode;
88

99
pub use bytecode::*;
10-
pub use error::Error;
10+
pub use error::BaseError;
1111
pub use location::Location;
1212
pub use mode::Mode;

compiler/parser/src/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl From<FStringError> for LalrpopError<Location, Tok, LexicalError> {
118118

119119
/// Represents an error during parsing
120120
#[derive(Debug, PartialEq)]
121-
pub struct ParseError(rustpython_compiler_core::Error<ParseErrorType>);
121+
pub struct ParseError(rustpython_compiler_core::BaseError<ParseErrorType>);
122122

123123
#[derive(Debug, PartialEq)]
124124
pub enum ParseErrorType {
@@ -134,7 +134,7 @@ pub enum ParseErrorType {
134134
Lexical(LexicalErrorType),
135135
}
136136

137-
impl From<ParseError> for rustpython_compiler_core::Error<ParseErrorType> {
137+
impl From<ParseError> for rustpython_compiler_core::BaseError<ParseErrorType> {
138138
fn from(err: ParseError) -> Self {
139139
err.0
140140
}
@@ -149,7 +149,7 @@ impl From<ParseError> for ParseErrorType {
149149
/// Convert `lalrpop_util::ParseError` to our internal type
150150
impl ParseError {
151151
fn new(error: ParseErrorType, location: Location, source_path: String) -> Self {
152-
Self(rustpython_compiler_core::Error {
152+
Self(rustpython_compiler_core::BaseError {
153153
error,
154154
location,
155155
source_path,
@@ -239,7 +239,7 @@ impl ParseErrorType {
239239
}
240240

241241
impl std::ops::Deref for ParseError {
242-
type Target = rustpython_compiler_core::Error<ParseErrorType>;
242+
type Target = rustpython_compiler_core::BaseError<ParseErrorType>;
243243
fn deref(&self) -> &Self::Target {
244244
&self.0
245245
}

compiler/src/lib.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustpython_codegen::{compile, symboltable};
2-
use rustpython_compiler_core::CodeObject;
2+
use rustpython_compiler_core::{BaseError, CodeObject};
33
use rustpython_parser::{
44
ast::{fold::Fold, ConstantOptimizer, Location},
55
error::ParseErrorType,
@@ -18,14 +18,21 @@ pub enum CompileErrorType {
1818
Parse(#[from] rustpython_parser::error::ParseErrorType),
1919
}
2020

21+
pub type CompileErrorBody = BaseError<CompileErrorType>;
22+
2123
#[derive(Debug, thiserror::Error)]
2224
pub struct CompileError {
23-
pub error: CompileErrorType,
24-
pub source_path: String,
25-
pub location: Location,
25+
pub body: CompileErrorBody,
2626
pub statement: Option<String>,
2727
}
2828

29+
impl std::ops::Deref for CompileError {
30+
type Target = CompileErrorBody;
31+
fn deref(&self) -> &Self::Target {
32+
&self.body
33+
}
34+
}
35+
2936
impl fmt::Display for CompileError {
3037
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3138
let loc = self.location;
@@ -41,20 +48,18 @@ impl fmt::Display for CompileError {
4148

4249
impl CompileError {
4350
fn from_codegen(error: rustpython_codegen::error::CodegenError, source: &str) -> Self {
51+
let statement = get_statement(source, error.location);
4452
Self {
45-
error: error.error.into(),
46-
location: error.location,
47-
source_path: error.source_path,
48-
statement: get_statement(source, error.location),
53+
body: error.into(),
54+
statement,
4955
}
5056
}
5157
fn from_parse(error: rustpython_parser::error::ParseError, source: &str) -> Self {
52-
let error: rustpython_compiler_core::Error<ParseErrorType> = error.into();
58+
let error: rustpython_compiler_core::BaseError<ParseErrorType> = error.into();
59+
let statement = get_statement(source, error.location);
5360
Self {
54-
error: error.error.into(),
55-
location: error.location,
56-
source_path: error.source_path,
57-
statement: get_statement(source, error.location),
61+
body: error.into(),
62+
statement,
5863
}
5964
}
6065
fn from_symtable(

src/shell.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustpython_parser::error::{LexicalErrorType, ParseErrorType};
44
use rustpython_vm::readline::{Readline, ReadlineResult};
55
use rustpython_vm::{
66
builtins::PyBaseExceptionRef,
7-
compile::{self, CompileError, CompileErrorType},
7+
compile::{self, CompileError, CompileErrorBody, CompileErrorType},
88
scope::Scope,
99
AsObject, PyResult, VirtualMachine,
1010
};
@@ -22,11 +22,19 @@ fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResul
2222
Err(err) => ShellExecResult::PyErr(err),
2323
},
2424
Err(CompileError {
25-
error: CompileErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::Eof)),
25+
body:
26+
CompileErrorBody {
27+
error: CompileErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::Eof)),
28+
..
29+
},
2630
..
2731
})
2832
| Err(CompileError {
29-
error: CompileErrorType::Parse(ParseErrorType::Eof),
33+
body:
34+
CompileErrorBody {
35+
error: CompileErrorType::Parse(ParseErrorType::Eof),
36+
..
37+
},
3038
..
3139
}) => ShellExecResult::Continue,
3240
Err(err) => ShellExecResult::PyErr(vm.new_syntax_error(&err)),

0 commit comments

Comments
 (0)