Skip to content

Commit 31f95ee

Browse files
committed
integrate CodegenError to compiler-core::Error
1 parent 9ecbff8 commit 31f95ee

File tree

7 files changed

+32
-30
lines changed

7 files changed

+32
-30
lines changed

compiler/codegen/src/error.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
use rustpython_compiler_core::Location;
21
use std::{error::Error, fmt};
32

4-
#[derive(Debug)]
5-
pub struct CodegenError {
6-
pub error: CodegenErrorType,
7-
pub location: Location,
8-
pub source_path: String,
9-
}
3+
pub type CodegenError = rustpython_compiler_core::Error<CodegenErrorType>;
104

115
#[derive(Debug)]
126
#[non_exhaustive]
@@ -87,15 +81,3 @@ impl fmt::Display for CodegenErrorType {
8781
}
8882

8983
impl Error for CodegenErrorType {}
90-
91-
impl fmt::Display for CodegenError {
92-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
93-
self.location.fmt_with(f, &self.error)
94-
}
95-
}
96-
97-
impl Error for CodegenError {
98-
fn source(&self) -> Option<&(dyn Error + 'static)> {
99-
None
100-
}
101-
}

compiler/core/src/error.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt::Display;
2+
13
use crate::Location;
24

35
#[derive(Debug, PartialEq, Eq)]
@@ -15,6 +17,24 @@ impl<T> std::ops::Deref for Error<T> {
1517
}
1618
}
1719

20+
impl<T> std::error::Error for Error<T>
21+
where
22+
T: std::fmt::Display + std::fmt::Debug,
23+
{
24+
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
25+
None
26+
}
27+
}
28+
29+
impl<T> Display for Error<T>
30+
where
31+
T: std::fmt::Display,
32+
{
33+
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
34+
self.location.fmt_with(f, &self.error)
35+
}
36+
}
37+
1838
impl<T> Error<T> {
1939
pub fn error(self) -> T {
2040
self.error

compiler/parser/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl ParseError {
193193

194194
impl fmt::Display for ParseError {
195195
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
196-
self.location.fmt_with(f, &self.error)
196+
self.0.fmt(f)
197197
}
198198
}
199199

compiler/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub use rustpython_codegen::compile::CompileOpts;
1111
pub use rustpython_compiler_core::Mode;
1212

1313
#[derive(Debug, thiserror::Error)]
14-
pub enum CodegenErrorType {
14+
pub enum CompileErrorType {
1515
#[error(transparent)]
1616
Compile(#[from] rustpython_codegen::error::CodegenErrorType),
1717
#[error(transparent)]
@@ -20,7 +20,7 @@ pub enum CodegenErrorType {
2020

2121
#[derive(Debug, thiserror::Error)]
2222
pub struct CompileError {
23-
pub error: CodegenErrorType,
23+
pub error: CompileErrorType,
2424
pub source_path: String,
2525
pub location: Location,
2626
pub statement: Option<String>,

src/shell.rs

Lines changed: 3 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, CodegenErrorType, CompileError},
7+
compile::{self, CompileError, CompileErrorType},
88
scope::Scope,
99
AsObject, PyResult, VirtualMachine,
1010
};
@@ -22,11 +22,11 @@ fn shell_exec(vm: &VirtualMachine, source: &str, scope: Scope) -> ShellExecResul
2222
Err(err) => ShellExecResult::PyErr(err),
2323
},
2424
Err(CompileError {
25-
error: CodegenErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::Eof)),
25+
error: CompileErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::Eof)),
2626
..
2727
})
2828
| Err(CompileError {
29-
error: CodegenErrorType::Parse(ParseErrorType::Eof),
29+
error: CompileErrorType::Parse(ParseErrorType::Eof),
3030
..
3131
}) => ShellExecResult::Continue,
3232
Err(err) => ShellExecResult::PyErr(vm.new_syntax_error(&err)),

vm/src/vm/vm_new.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[cfg(feature = "rustpython-compiler")]
2-
use crate::compile::{CodegenErrorType, CompileError};
2+
use crate::compile::{CompileError, CompileErrorType};
33
use crate::{
44
builtins::{
55
pystr::IntoPyStrRef,
@@ -212,10 +212,10 @@ impl VirtualMachine {
212212
#[cfg(feature = "rustpython-compiler")]
213213
pub fn new_syntax_error(&self, error: &CompileError) -> PyBaseExceptionRef {
214214
let syntax_error_type = match &error.error {
215-
CodegenErrorType::Parse(p) if p.is_indentation_error() => {
215+
CompileErrorType::Parse(p) if p.is_indentation_error() => {
216216
self.ctx.exceptions.indentation_error
217217
}
218-
CodegenErrorType::Parse(p) if p.is_tab_error() => self.ctx.exceptions.tab_error,
218+
CompileErrorType::Parse(p) if p.is_tab_error() => self.ctx.exceptions.tab_error,
219219
_ => self.ctx.exceptions.syntax_error,
220220
}
221221
.to_owned();

wasm/lib/src/convert.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use js_sys::{Array, ArrayBuffer, Object, Promise, Reflect, SyntaxError, Uint8Arr
44
use rustpython_parser::error::ParseErrorType;
55
use rustpython_vm::{
66
builtins::PyBaseExceptionRef,
7-
compile::{CodegenErrorType, CompileError},
7+
compile::{CompileError, CompileErrorType},
88
exceptions,
99
function::{ArgBytesLike, FuncArgs},
1010
py_serde, AsObject, PyObjectRef, PyPayload, PyResult, TryFromBorrowedObject, VirtualMachine,
@@ -253,7 +253,7 @@ pub fn syntax_err(err: CompileError) -> SyntaxError {
253253
&"col".into(),
254254
&(err.location.column() as u32).into(),
255255
);
256-
let can_continue = matches!(&err.error, CodegenErrorType::Parse(ParseErrorType::Eof));
256+
let can_continue = matches!(&err.error, CompileErrorType::Parse(ParseErrorType::Eof));
257257
let _ = Reflect::set(&js_err, &"canContinue".into(), &can_continue.into());
258258
js_err
259259
}

0 commit comments

Comments
 (0)