Skip to content

Commit bac1cdc

Browse files
committed
Remove compiler_core::CompileError
1 parent 91b9ed1 commit bac1cdc

File tree

17 files changed

+86
-68
lines changed

17 files changed

+86
-68
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ members = [
1717
]
1818

1919
[workspace.dependencies]
20-
rustpython-literal = { git = "https://github.com/RustPython/Parser.git", rev = "6b60f85cc4ca248af9b787697c41be2adb7a3ec8" }
21-
rustpython-compiler-core = { git = "https://github.com/RustPython/Parser.git", rev = "6b60f85cc4ca248af9b787697c41be2adb7a3ec8" }
22-
rustpython-parser = { git = "https://github.com/RustPython/Parser.git", rev = "6b60f85cc4ca248af9b787697c41be2adb7a3ec8" }
23-
rustpython-ast = { git = "https://github.com/RustPython/Parser.git", rev = "6b60f85cc4ca248af9b787697c41be2adb7a3ec8" }
20+
rustpython-literal = { git = "https://github.com/RustPython/Parser.git", rev = "48920a034e93ae7c737129ea427c068dc30e2da5" }
21+
rustpython-compiler-core = { git = "https://github.com/RustPython/Parser.git", rev = "48920a034e93ae7c737129ea427c068dc30e2da5" }
22+
rustpython-parser = { git = "https://github.com/RustPython/Parser.git", rev = "48920a034e93ae7c737129ea427c068dc30e2da5" }
23+
rustpython-ast = { git = "https://github.com/RustPython/Parser.git", rev = "48920a034e93ae7c737129ea427c068dc30e2da5" }
2424
# rustpython-literal = { path = "../RustPython-parser/literal" }
2525
# rustpython-compiler-core = { path = "../RustPython-parser/core" }
2626
# rustpython-parser = { path = "../RustPython-parser/parser" }

compiler/src/lib.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustpython_codegen::{compile, symboltable};
22
use rustpython_parser::ast::{fold::Fold, ConstantOptimizer};
33

44
pub use rustpython_codegen::compile::CompileOpts;
5-
pub use rustpython_compiler_core::{BaseError as CompileErrorBody, CodeObject, Mode};
5+
pub use rustpython_compiler_core::{CodeObject, Mode};
66

77
// these modules are out of repository. re-exporting them here for convenience.
88
pub use rustpython_codegen as codegen;
@@ -45,12 +45,7 @@ impl From<parser::ParseErrorType> for CompileErrorType {
4545
}
4646
}
4747

48-
pub type CompileError = rustpython_compiler_core::CompileError<CompileErrorType>;
49-
50-
fn error_from_parse(error: parser::ParseError, source: &str) -> CompileError {
51-
let error: CompileErrorBody<parser::ParseErrorType> = error.into();
52-
CompileError::from(error, source)
53-
}
48+
pub type CompileError = rustpython_compiler_core::BaseError<CompileErrorType>;
5449

5550
/// Compile a given source code into a bytecode object.
5651
pub fn compile(
@@ -61,31 +56,30 @@ pub fn compile(
6156
) -> Result<CodeObject, CompileError> {
6257
let mut ast = match parser::parse(source, mode.into(), &source_path) {
6358
Ok(x) => x,
64-
Err(e) => return Err(error_from_parse(e, source)),
59+
Err(e) => return Err(e.into()),
6560
};
6661
if opts.optimize > 0 {
6762
ast = ConstantOptimizer::new()
6863
.fold_mod(ast)
6964
.unwrap_or_else(|e| match e {});
7065
}
71-
compile::compile_top(&ast, source_path, mode, opts).map_err(|e| CompileError::from(e, source))
66+
compile::compile_top(&ast, source_path, mode, opts).map_err(|e| e.into())
7267
}
7368

7469
pub fn compile_symtable(
7570
source: &str,
7671
mode: compile::Mode,
7772
source_path: &str,
7873
) -> Result<symboltable::SymbolTable, CompileError> {
79-
let parse_err = |e| error_from_parse(e, source);
8074
let res = match mode {
8175
compile::Mode::Exec | compile::Mode::Single | compile::Mode::BlockExpr => {
82-
let ast = parser::parse_program(source, source_path).map_err(parse_err)?;
76+
let ast = parser::parse_program(source, source_path).map_err(|e| e.into())?;
8377
symboltable::SymbolTable::scan_program(&ast)
8478
}
8579
compile::Mode::Eval => {
86-
let expr = parser::parse_expression(source, source_path).map_err(parse_err)?;
80+
let expr = parser::parse_expression(source, source_path).map_err(|e| e.into())?;
8781
symboltable::SymbolTable::scan_expr(&expr)
8882
}
8983
};
90-
res.map_err(|e| CompileError::from(e.into_codegen_error(source_path.to_owned()), source))
84+
res.map_err(|e| e.into_codegen_error(source_path.to_owned()).into())
9185
}

examples/hello_embed.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,10 @@ use rustpython_vm as vm;
33
fn main() -> vm::PyResult<()> {
44
vm::Interpreter::without_stdlib(Default::default()).enter(|vm| {
55
let scope = vm.new_scope_with_builtins();
6-
6+
let source = r#"print("Hello World!")"#;
77
let code_obj = vm
8-
.compile(
9-
r#"print("Hello World!")"#,
10-
vm::compiler::Mode::Exec,
11-
"<embedded>".to_owned(),
12-
)
13-
.map_err(|err| vm.new_syntax_error(&err))?;
8+
.compile(source, vm::compiler::Mode::Exec, "<embedded>".to_owned())
9+
.map_err(|err| vm.new_syntax_error(&err, Some(source)))?;
1410

1511
vm.run_code_obj(code_obj, scope)?;
1612

examples/mini_repl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def fib(n):
6565
// (note that this is only the case when compiler::Mode::Single is passed to vm.compile)
6666
match vm
6767
.compile(&input, vm::compiler::Mode::Single, "<embedded>".to_owned())
68-
.map_err(|err| vm.new_syntax_error(&err))
68+
.map_err(|err| vm.new_syntax_error(&err, Some(&input)))
6969
.and_then(|code_obj| vm.run_code_obj(code_obj, scope.clone()))
7070
{
7171
Ok(output) => {

src/shell.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ mod helper;
33
use rustpython_parser::{lexer::LexicalErrorType, ParseErrorType, Tok};
44
use rustpython_vm::{
55
builtins::PyBaseExceptionRef,
6-
compiler::{self, CompileError, CompileErrorBody, CompileErrorType},
6+
compiler::{self, CompileError, CompileErrorType},
77
readline::{Readline, ReadlineResult},
88
scope::Scope,
99
AsObject, PyResult, VirtualMachine,
@@ -36,19 +36,11 @@ fn shell_exec(
3636
}
3737
}
3838
Err(CompileError {
39-
body:
40-
CompileErrorBody {
41-
error: CompileErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::Eof)),
42-
..
43-
},
39+
error: CompileErrorType::Parse(ParseErrorType::Lexical(LexicalErrorType::Eof)),
4440
..
4541
})
4642
| Err(CompileError {
47-
body:
48-
CompileErrorBody {
49-
error: CompileErrorType::Parse(ParseErrorType::Eof),
50-
..
51-
},
43+
error: CompileErrorType::Parse(ParseErrorType::Eof),
5244
..
5345
}) => ShellExecResult::Continue,
5446
Err(err) => {
@@ -57,13 +49,13 @@ fn shell_exec(
5749
// since indentations errors on columns other than 0 should be ignored.
5850
// if its an unrecognized token for dedent, set to false
5951

60-
let bad_error = match err.body.error {
52+
let bad_error = match err.error {
6153
CompileErrorType::Parse(ref p) => {
6254
if matches!(
6355
p,
6456
ParseErrorType::Lexical(LexicalErrorType::IndentationError)
6557
) {
66-
continuing && err.body.location.column() != 0
58+
continuing && err.location.column() != 0
6759
} else {
6860
!matches!(p, ParseErrorType::UnrecognizedToken(Tok::Dedent, _))
6961
}
@@ -73,7 +65,7 @@ fn shell_exec(
7365

7466
// If we are handling an error on an empty line or an error worthy of throwing
7567
if empty_line_given || bad_error {
76-
ShellExecResult::PyErr(vm.new_syntax_error(&err))
68+
ShellExecResult::PyErr(vm.new_syntax_error(&err, Some(source)))
7769
} else {
7870
ShellExecResult::Continue
7971
}

stdlib/src/dis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ mod decl {
1616
} else if let Ok(co_str) = PyStrRef::try_from_object(vm, obj.clone()) {
1717
// String:
1818
vm.compile(co_str.as_str(), compiler::Mode::Exec, "<dis>".to_owned())
19-
.map_err(|err| vm.new_syntax_error(&err))?
19+
.map_err(|err| vm.new_syntax_error(&err, Some(co_str.as_str())))?
2020
} else {
2121
PyRef::try_from_object(vm, obj)?
2222
};

vm/src/compiler.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ mod error {
2626
#[cfg(not(feature = "rustpython-compiler"))]
2727
pub use error::{CompileError, CompileErrorType};
2828

29-
impl ToPyException for CompileError {
29+
impl ToPyException for (CompileError, Option<&str>) {
3030
fn to_pyexception(&self, vm: &VirtualMachine) -> PyBaseExceptionRef {
31-
vm.new_syntax_error(self)
31+
vm.new_syntax_error(&self.0, self.1)
3232
}
3333
}

vm/src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub fn eval(vm: &VirtualMachine, source: &str, scope: Scope, source_path: &str)
66
debug!("Code object: {:?}", bytecode);
77
vm.run_code_obj(bytecode, scope)
88
}
9-
Err(err) => Err(vm.new_syntax_error(&err)),
9+
Err(err) => Err(vm.new_syntax_error(&err, Some(source))),
1010
}
1111
}
1212

vm/src/import.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub fn import_file(
118118
file_path,
119119
vm.compile_opts(),
120120
)
121-
.map_err(|err| vm.new_syntax_error(&err))?;
121+
.map_err(|err| vm.new_syntax_error(&err, Some(&content)))?;
122122
import_codeobj(vm, module_name, code, true)
123123
}
124124

0 commit comments

Comments
 (0)